From 8cffee569a4fb0a8310a1dac45855f82f8d2370e Mon Sep 17 00:00:00 2001 From: Sakamoto Shiina <68018796+ShiinaSakamoto@users.noreply.github.com> Date: Tue, 24 Oct 2023 16:07:29 +0900 Subject: [PATCH] =?UTF-8?q?[Refactor]=20Textbox=E3=81=AEUi=20Size=E3=82=92?= =?UTF-8?q?=E7=8B=AC=E7=AB=8B=E3=81=95=E3=81=9B=E3=82=8B=E3=81=9F=E3=82=81?= =?UTF-8?q?=E3=81=AE=E6=BA=96=E5=82=99=E3=81=A7=E3=83=AA=E3=83=95=E3=82=A1?= =?UTF-8?q?=E3=82=AF=E3=82=BF=E3=83=AA=E3=83=B3=E3=82=B0=E3=80=82=E3=82=AF?= =?UTF-8?q?=E3=83=A9=E3=82=B9=E3=81=AA=E3=81=A9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- view.py | 6 +- vrct_gui/_printToTextbox.py | 150 ++++++++++++++++------- vrct_gui/ui_managers/UiScalingManager.py | 8 +- vrct_gui/ui_utils/ui_utils.py | 8 ++ vrct_gui/vrct_gui.py | 18 +-- 5 files changed, 132 insertions(+), 58 deletions(-) diff --git a/view.py b/view.py index 6a5ff860..51215f7a 100644 --- a/view.py +++ b/view.py @@ -517,7 +517,7 @@ class View(): # Insert sample conversation for testing. - # self._insertSampleConversationToTextbox() + self._insertSampleConversationToTextbox() @@ -1054,11 +1054,11 @@ class View(): @staticmethod - def _printToTextbox_Info(info_message): + def _printToTextbox_Info(info_message, **kwargs): vrct_gui._printToTextbox( target_type="SYSTEM", original_message=info_message, - # translated_message="", + **kwargs, ) diff --git a/vrct_gui/_printToTextbox.py b/vrct_gui/_printToTextbox.py index 943b047d..ddc4b411 100644 --- a/vrct_gui/_printToTextbox.py +++ b/vrct_gui/_printToTextbox.py @@ -1,66 +1,116 @@ from datetime import datetime from customtkinter import CTkFont +from .ui_utils import calculateUiSize -def _printToTextbox(vrct_gui, - settings, - target_type, - original_message=None, - translated_message=None, - tags=None, - disable_print_to_textbox_all:bool=False, - ): +class _PrintToTextbox(): + def __init__( + self, + vrct_gui, + settings, + ): - now_raw_data = datetime.now() - # now = now_raw_data.strftime("%H:%M:%S") - now_hm = now_raw_data.strftime("%H:%M") - # set target textbox widget + self.vrct_gui = vrct_gui + self.settings = settings - is_only_one_message = True if original_message is None or translated_message is None or translated_message == "" else False - - match (target_type): - case "SYSTEM": - target_textbox = vrct_gui.textbox_system - case "SENT": - target_textbox = vrct_gui.textbox_sent - case "RECEIVED": - target_textbox = vrct_gui.textbox_received - case (_): - raise ValueError(f"No matching case for target_type: {target_type}") + self._DEFAULT_TEXTBOX_FIRST_INSERT_SPACING = self.settings.uism.TEXTBOX_FIRST_INSERT_SPACING + self._DEFAULT_TEXTBOX_FONT_SIZE__LABEL = self.settings.uism.TEXTBOX_FONT_SIZE__LABEL + self._DEFAULT_TEXTBOX_FONT_SIZE__TIMESTAMP = self.settings.uism.TEXTBOX_FONT_SIZE__TIMESTAMP + self._DEFAULT_TEXTBOX_FONT_SIZE__SYSTEM_TEXT_FONT = self.settings.uism.TEXTBOX_FONT_SIZE__SYSTEM_TEXT_FONT + self._DEFAULT_TEXTBOX_FONT_SIZE__SECONDARY_TEXT_FONT = self.settings.uism.TEXTBOX_FONT_SIZE__SECONDARY_TEXT_FONT + self._DEFAULT_TEXTBOX_FONT_SIZE__MAIN_TEXT_FONT = self.settings.uism.TEXTBOX_FONT_SIZE__MAIN_TEXT_FONT - def printEachTextbox(target_textbox): + + self.textbox_first_insert_spacing = None + self.textbox_font_size__label = None + self.textbox_font_size__timestamp = None + self.textbox_font_size__system_text_font = None + self.textbox_font_size__secondary_text_font = None + self.textbox_font_size__main_text_font = None + + + self.all_textbox_widgets = [self.vrct_gui.textbox_all, self.vrct_gui.textbox_system, self.vrct_gui.textbox_sent, self.vrct_gui.textbox_received] + + + self.setTagsSettings() + + + def printToTextbox(self, target_type, original_message=None, translated_message=None, to_print_to_textbox_all:bool=True): + self._printEachTextbox( + target_textbox=self._getTargetTextboxWidget(target_type), + print_type=target_type, + original_message=original_message, + translated_message=translated_message, + ) + + # To automatically print the same log to the textbox_all widget as well. + if to_print_to_textbox_all is True: + self._printEachTextbox( + target_textbox=self._getTargetTextboxWidget("ALL"), + print_type=target_type, + original_message=original_message, + translated_message=translated_message, + ) + + def setTagsSettings(self, custom_font_size_scale:float=1.0): + # Calculate Textbox's ui size by default size * textbox_ui_scale + self.textbox_first_insert_spacing = calculateUiSize(self._DEFAULT_TEXTBOX_FIRST_INSERT_SPACING, custom_font_size_scale) + self.textbox_font_size__label = calculateUiSize(self._DEFAULT_TEXTBOX_FONT_SIZE__LABEL, custom_font_size_scale) + self.textbox_font_size__timestamp = calculateUiSize(self._DEFAULT_TEXTBOX_FONT_SIZE__TIMESTAMP, custom_font_size_scale) + self.textbox_font_size__system_text_font = calculateUiSize(self._DEFAULT_TEXTBOX_FONT_SIZE__SYSTEM_TEXT_FONT, custom_font_size_scale) + self.textbox_font_size__secondary_text_font = calculateUiSize(self._DEFAULT_TEXTBOX_FONT_SIZE__SECONDARY_TEXT_FONT, custom_font_size_scale) + self.textbox_font_size__main_text_font = calculateUiSize(self._DEFAULT_TEXTBOX_FONT_SIZE__MAIN_TEXT_FONT, custom_font_size_scale) + + for each_textbox_widget in self.all_textbox_widgets: + self._setTagsSettings(target_textbox=each_textbox_widget) + + + def _setTagsSettings(self, target_textbox): target_textbox.tag_config("JUSTIFY_CENTER", justify="center") target_textbox.tag_config("JUSTIFY_RIGHT", justify="right") target_textbox.tag_config("JUSTIFY_LEFT", justify="left") # common tag settings # target_textbox._textbox.tag_configure("START", spacing1=16) - target_textbox._textbox.tag_configure("LABEL", font=CTkFont(family=settings.FONT_FAMILY, size=settings.uism.TEXTBOX_FONT_SIZE__LABEL, weight="normal")) - target_textbox._textbox.tag_configure("TIMESTAMP", font=CTkFont(family=settings.FONT_FAMILY, size=settings.uism.TEXTBOX_FONT_SIZE__TIMESTAMP, weight="normal"), foreground=settings.ctm.TEXTBOX_TIMESTAMP_TEXT_COLOR) - target_textbox._textbox.tag_configure("SECONDARY_TEXT_FONT", font=CTkFont(family=settings.FONT_FAMILY, size=settings.uism.TEXTBOX_FONT_SIZE__SECONDARY_TEXT_FONT, weight="normal")) - target_textbox._textbox.tag_configure("MAIN_TEXT_FONT", font=CTkFont(family=settings.FONT_FAMILY, size=settings.uism.TEXTBOX_FONT_SIZE__MAIN_TEXT_FONT, weight="normal")) + target_textbox.tag_config("FIRST_INSERT_SPACING", spacing1=self.textbox_first_insert_spacing) + target_textbox._textbox.tag_configure("LABEL", font=CTkFont(family=self.settings.FONT_FAMILY, size=self.textbox_font_size__label, weight="normal")) + target_textbox._textbox.tag_configure("TIMESTAMP", font=CTkFont(family=self.settings.FONT_FAMILY, size=self.textbox_font_size__timestamp, weight="normal"), foreground=self.settings.ctm.TEXTBOX_TIMESTAMP_TEXT_COLOR) + target_textbox._textbox.tag_configure("SECONDARY_TEXT_FONT", font=CTkFont(family=self.settings.FONT_FAMILY, size=self.textbox_font_size__secondary_text_font, weight="normal")) + target_textbox._textbox.tag_configure("MAIN_TEXT_FONT", font=CTkFont(family=self.settings.FONT_FAMILY, size=self.textbox_font_size__main_text_font, weight="normal")) # System Tag Settings - target_textbox.tag_config("FIRST_INSERT_SPACING", spacing1=settings.uism.TEXTBOX_FIRST_INSERT_SPACING) - target_textbox.tag_config("SYSTEM_TAG", foreground=settings.ctm.TEXTBOX_SYSTEM_TAG_TEXT_COLOR) - target_textbox.tag_config("SYSTEM_TEXT", foreground=settings.ctm.TEXTBOX_TEXT_SUB_COLOR) - target_textbox._textbox.tag_configure("SYSTEM_TEXT_FONT", font=CTkFont(family=settings.FONT_FAMILY, size=settings.uism.TEXTBOX_FONT_SIZE__SYSTEM_TEXT_FONT, weight="normal")) + target_textbox.tag_config("SYSTEM_TAG", foreground=self.settings.ctm.TEXTBOX_SYSTEM_TAG_TEXT_COLOR) + target_textbox.tag_config("SYSTEM_TEXT", foreground=self.settings.ctm.TEXTBOX_TEXT_SUB_COLOR) + target_textbox._textbox.tag_configure("SYSTEM_TEXT_FONT", font=CTkFont(family=self.settings.FONT_FAMILY, size=self.textbox_font_size__system_text_font, weight="normal")) # Sent Tag Settings - target_textbox.tag_config("SENT_TAG", foreground=settings.ctm.TEXTBOX_SENT_TAG_TEXT_COLOR) - target_textbox.tag_config("SENT_TEXT", foreground=settings.ctm.TEXTBOX_TEXT_COLOR) - target_textbox.tag_config("SENT_SUB_TEXT", foreground=settings.ctm.TEXTBOX_TEXT_SUB_COLOR) + target_textbox.tag_config("SENT_TAG", foreground=self.settings.ctm.TEXTBOX_SENT_TAG_TEXT_COLOR) + target_textbox.tag_config("SENT_TEXT", foreground=self.settings.ctm.TEXTBOX_TEXT_COLOR) + target_textbox.tag_config("SENT_SUB_TEXT", foreground=self.settings.ctm.TEXTBOX_TEXT_SUB_COLOR) # Received Tag Settings - target_textbox.tag_config("RECEIVED_TAG", foreground=settings.ctm.TEXTBOX_RECEIVED_TAG_TEXT_COLOR) - target_textbox.tag_config("RECEIVED_TEXT", foreground=settings.ctm.TEXTBOX_TEXT_COLOR) - target_textbox.tag_config("RECEIVED_SUB_TEXT", foreground=settings.ctm.TEXTBOX_TEXT_SUB_COLOR) + target_textbox.tag_config("RECEIVED_TAG", foreground=self.settings.ctm.TEXTBOX_RECEIVED_TAG_TEXT_COLOR) + target_textbox.tag_config("RECEIVED_TEXT", foreground=self.settings.ctm.TEXTBOX_TEXT_COLOR) + target_textbox.tag_config("RECEIVED_SUB_TEXT", foreground=self.settings.ctm.TEXTBOX_TEXT_SUB_COLOR) + + + def _printEachTextbox( + self, + target_textbox, + print_type, + original_message, + translated_message, + ): + now_raw_data = datetime.now() + now_hm = now_raw_data.strftime("%H:%M") + + is_only_one_message = True if original_message is None or translated_message is None or translated_message == "" else False FAKE_MARGIN = " " # insert target_textbox.configure(state="normal") target_textbox.insert("end", "\n") - match (target_type): + match (print_type): case "SYSTEM": target_textbox.insert("end", "System", ("SYSTEM_TAG", "FIRST_INSERT_SPACING", "JUSTIFY_CENTER", "LABEL")) target_textbox.insert("end", FAKE_MARGIN+original_message+FAKE_MARGIN, ("SYSTEM_TEXT", "SYSTEM_TEXT_FONT", "JUSTIFY_CENTER")) @@ -92,7 +142,23 @@ def _printToTextbox(vrct_gui, target_textbox.configure(state="disabled") target_textbox.see("end") - printEachTextbox(target_textbox) - # To automatically print the same log to the textbox_all widget as well. - if disable_print_to_textbox_all is not True: printEachTextbox(vrct_gui.textbox_all) \ No newline at end of file + + + + + + def _getTargetTextboxWidget(self, target_type): + match (target_type): + case "ALL": + target_textbox = self.vrct_gui.textbox_all + case "SYSTEM": + target_textbox = self.vrct_gui.textbox_system + case "SENT": + target_textbox = self.vrct_gui.textbox_sent + case "RECEIVED": + target_textbox = self.vrct_gui.textbox_received + case (_): + raise ValueError(f"No matching case for target_type: {target_type}") + + return target_textbox \ No newline at end of file diff --git a/vrct_gui/ui_managers/UiScalingManager.py b/vrct_gui/ui_managers/UiScalingManager.py index dc385b34..8f5a9c09 100644 --- a/vrct_gui/ui_managers/UiScalingManager.py +++ b/vrct_gui/ui_managers/UiScalingManager.py @@ -1,5 +1,7 @@ from types import SimpleNamespace +from ..ui_utils import calculateUiSize + class UiScalingManager(): def __init__(self, scaling_percentage): scaling_float = int(scaling_percentage.replace("%", "")) / 100 @@ -274,11 +276,7 @@ class UiScalingManager(): def _calculateUiSize(self, default_size, is_allowed_odd:bool=False, is_zero_allowed:bool=False): - size = int(default_size * self.SCALING_FLOAT) - size += 1 if not is_allowed_odd and size % 2 != 0 else 0 - if size <= 0: - size = 0 if is_zero_allowed else 1 - + size = calculateUiSize(default_size, self.SCALING_FLOAT, is_allowed_odd, is_zero_allowed) return size @staticmethod diff --git a/vrct_gui/ui_utils/ui_utils.py b/vrct_gui/ui_utils/ui_utils.py index bf64a985..5138fb5c 100644 --- a/vrct_gui/ui_utils/ui_utils.py +++ b/vrct_gui/ui_utils/ui_utils.py @@ -42,6 +42,14 @@ def getLongestText(text_list:list): longest_text = text return longest_text +def calculateUiSize(default_size, scaling_float, is_allowed_odd:bool=False, is_zero_allowed:bool=False): + size = int(default_size * scaling_float) + size += 1 if not is_allowed_odd and size % 2 != 0 else 0 + if size <= 0: + size = 0 if is_zero_allowed else 1 + + return size + def bindEnterAndLeaveColor(target_widgets, enter_color, leave_color): for target_widget in target_widgets: target_widget.bind("", lambda e, widgets=target_widgets: [w.configure(fg_color=enter_color) for w in widgets], "+") diff --git a/vrct_gui/vrct_gui.py b/vrct_gui/vrct_gui.py index 06f5f4f5..b423cb1d 100644 --- a/vrct_gui/vrct_gui.py +++ b/vrct_gui/vrct_gui.py @@ -8,7 +8,7 @@ from ._CreateDropdownMenuWindow import _CreateDropdownMenuWindow from ._changeMainWindowWidgetsStatus import _changeMainWindowWidgetsStatus from ._changeConfigWindowWidgetsStatus import _changeConfigWindowWidgetsStatus from ._CreateConfirmationModal import _CreateConfirmationModal -from ._printToTextbox import _printToTextbox +from ._PrintToTextbox import _PrintToTextbox from .main_window import createMainWindowWidgets from .config_window import ConfigWindow @@ -133,6 +133,12 @@ class VRCT_GUI(CTk): modal_type="information" ) + self.print_to_textbox = _PrintToTextbox( + vrct_gui=self, + settings=self.settings.main, + ) + + def _startMainLoop(self): @@ -229,14 +235,10 @@ class VRCT_GUI(CTk): target_names=target_names, ) - def _printToTextbox(self, target_type, original_message=None, translated_message=None): - _printToTextbox( - vrct_gui=self, - settings=self.settings.main, + def _printToTextbox(self, target_type, **kwargs): + self.print_to_textbox.printToTextbox( target_type=target_type, - original_message=original_message, - translated_message=translated_message, - tags=target_type, + **kwargs ) def _setDefaultActiveLanguagePresetTab(self, tab_no:str):