[Refactor] TextboxのUi Sizeを独立させるための準備でリファクタリング。クラスなど。

This commit is contained in:
Sakamoto Shiina
2023-10-24 16:07:29 +09:00
parent 64ac7d2c61
commit 8cffee569a
5 changed files with 132 additions and 58 deletions

View File

@@ -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,
)

View File

@@ -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)
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

View File

@@ -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

View File

@@ -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("<Enter>", lambda e, widgets=target_widgets: [w.configure(fg_color=enter_color) for w in widgets], "+")

View File

@@ -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):