From 72877ab98b43bc9b191eedb2df6a893725823849 Mon Sep 17 00:00:00 2001 From: Sakamoto Shiina <68018796+ShiinaSakamoto@users.noreply.github.com> Date: Sun, 5 Nov 2023 11:30:34 +0900 Subject: [PATCH] =?UTF-8?q?[Update]=20Message=20Format:=20[message]?= =?UTF-8?q?=E3=81=A8[translation]=E3=81=AF=E4=B8=80=E6=84=8F=E3=81=8B?= =?UTF-8?q?=E3=81=A4=E3=81=9D=E3=82=8C=E3=81=9E=E3=82=8C=E4=B8=80=E3=81=A4?= =?UTF-8?q?=E3=81=A0=E3=81=91=E3=81=AE=E4=BD=BF=E7=94=A8=E3=81=AB=E3=80=82?= =?UTF-8?q?=E3=81=9D=E3=82=8C=E3=81=AB=E4=BC=B4=E3=81=84=E3=82=A8=E3=83=A9?= =?UTF-8?q?=E3=83=BC=E8=A1=A8=E7=A4=BA=E3=82=84=E5=88=9D=E6=9C=9F=E5=80=A4?= =?UTF-8?q?=E3=81=AE=E6=8C=BF=E5=85=A5=E3=81=AA=E3=81=A9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 4 ++-- controller.py | 12 +++++++++--- locales/en.yml | 1 + locales/ja.yml | 1 + utils.py | 18 +++++++++++++++++- view.py | 15 ++++++++++++++- .../_SettingBoxGenerator.py | 8 ++++---- .../createSettingBox_Others.py | 1 + 8 files changed, 49 insertions(+), 11 deletions(-) diff --git a/config.py b/config.py index eeda8c1e..7dc8f759 100644 --- a/config.py +++ b/config.py @@ -8,7 +8,7 @@ from tkinter import font from languages import selectable_languages from models.translation.translation_languages import translatorEngine from models.transcription.transcription_utils import getInputDevices, getDefaultInputDevice -from utils import generatePercentageStringsList +from utils import generatePercentageStringsList, isUniqueStrings json_serializable_vars = {} def json_serializable(var_name): @@ -447,7 +447,7 @@ class Config: @MESSAGE_FORMAT.setter def MESSAGE_FORMAT(self, value): if type(value) is str: - if "[message]" not in value or "[translation]" not in value: + if isUniqueStrings(["[message]", "[translation]"], value) is False: value = "[message]([translation])" self._MESSAGE_FORMAT = value saveJson(self.PATH_CONFIG, inspect.currentframe().f_code.co_name, value) diff --git a/controller.py b/controller.py index 4842196b..183dafbf 100644 --- a/controller.py +++ b/controller.py @@ -3,7 +3,7 @@ from threading import Thread from config import config from model import model from view import view -from utils import get_key_by_value +from utils import get_key_by_value, isUniqueStrings from languages import selectable_languages # Common @@ -627,8 +627,14 @@ def callbackSetEnableAutoExportMessageLogs(value): def callbackSetMessageFormat(value): print("callbackSetMessageFormat", value) if len(value) > 0: - config.MESSAGE_FORMAT = value - view.setMessageFormatEntryWidgets(config.MESSAGE_FORMAT) + if isUniqueStrings(["[message]", "[translation]"], value) is True: + config.MESSAGE_FORMAT = value + view.clearErrorMessage() + view.setMessageFormatEntryWidgets(config.MESSAGE_FORMAT) + else: + view.showErrorMessage_MessageFormat() + view.setMessageFormatEntryWidgets(config.MESSAGE_FORMAT) + def callbackSetEnableSendMessageToVrc(value): print("callbackSetEnableSendMessageToVrc", value) diff --git a/locales/en.yml b/locales/en.yml index 0a77d70d..23fd1380 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -182,6 +182,7 @@ config_window: label: Message Format desc: "You can change the decoration of the message you want to send.\n[message] will be replaced with the message, and [translation] will be replaced with the translated message.\nIt will be used in Notification XSOverlay too." example_text: This is an example sentence. Fonts, line breaks, etc. may differ from the actual display. + error_message: "The characters '[message]' and '[translation]' cannot be used." send_message_to_vrc: label: Send Message To VRChat diff --git a/locales/ja.yml b/locales/ja.yml index a73b4e8e..9b0c8707 100644 --- a/locales/ja.yml +++ b/locales/ja.yml @@ -181,6 +181,7 @@ config_window: label: 送信するメッセージのフォーマット desc: "VRChatで相手に実際に見えるフォーマットを変更できます。\n[message]がメッセージに置換され、[translation]が翻訳されたメッセージに置換されます。\n※XSOverlayでの通知受け取り機能でも使われます。" example_text: これは例文です。フォントや改行箇所など、実際の表示とは異なる場合があります。 + error_message: "[message]と[translation]という文字は使えません。" send_message_to_vrc: label: VRChatにメッセージを送信する diff --git a/utils.py b/utils.py index 242faf15..9cb0dc94 100644 --- a/utils.py +++ b/utils.py @@ -1,3 +1,4 @@ +from typing import Union from os import path as os_path from PIL.Image import open as Image_open @@ -29,4 +30,19 @@ def generatePercentageStringsList(start=40, end=200, step=10): return strings def intToPercentageStringsFormatter(value:int): - return f"{value}%" \ No newline at end of file + return f"{value}%" + +def isUniqueStrings(unique_strings:Union[str, list], input_string:str, require=False): + import re + if isinstance(unique_strings, str): + unique_strings = [unique_strings] + patterns = [re.escape(s) for s in unique_strings] + + counts = [len(re.findall(pattern, input_string)) for pattern in patterns] + + if require is True: + # If require is True, unique_strings must appear once + return all(count == 1 for count in counts) and counts.count(1) == 2 + else: + # If require is False, check if unique strings are used exactly once + return all(count == 1 for count in counts) \ No newline at end of file diff --git a/view.py b/view.py index 09267802..7c8c005f 100644 --- a/view.py +++ b/view.py @@ -355,6 +355,7 @@ class View(): VAR_ENTRY_2_MESSAGE_FORMAT=StringVar(value=""), VAR_TEXT_REQUIRED_0_MESSAGE_FORMAT=StringVar(value="[message]"), VAR_TEXT_REQUIRED_1_MESSAGE_FORMAT=StringVar(value="[translation]"), + CALLBACK_FOCUS_OUT_MESSAGE_FORMAT=self.callbackBindFocusOut_MessageFormat, VAR_LABEL_ENABLE_SEND_MESSAGE_TO_VRC=StringVar(value=i18n.t("config_window.send_message_to_vrc.label")), @@ -1121,6 +1122,9 @@ class View(): case "SpeakerMaxPhrases": self.setGuiVariable_SpeakerMaxPhrases(config.INPUT_SPEAKER_MAX_PHRASES) + case "MessageFormat": + self.setMessageFormatEntryWidgets(config.MESSAGE_FORMAT) + case _: raise ValueError(f"No matching case for target_name: {target_name}") @@ -1270,7 +1274,9 @@ class View(): self.clearErrorMessage() - + def callbackBindFocusOut_MessageFormat(self, _e=None): + self.setLatestConfigVariable("MessageFormat") + self.clearErrorMessage() @@ -1352,6 +1358,13 @@ class View(): self._makeInvalidValueErrorMessage(i18n.t("config_window.speaker_dynamic_energy_threshold.no_device_error_message")) ) + + def showErrorMessage_MessageFormat(self): + self._showErrorMessage( + vrct_gui.config_window.sb__entry_message_format_2, + self._makeInvalidValueErrorMessage(i18n.t("config_window.message_format.error_message")) + ) + @staticmethod def _makeInvalidValueErrorMessage(error_message): return i18n.t("config_window.common_error_message.invalid_value") + "\n" + error_message diff --git a/vrct_gui/config_window/widgets/createSideMenuAndSettingsBoxContainers/setting_box_containers/_SettingBoxGenerator.py b/vrct_gui/config_window/widgets/createSideMenuAndSettingsBoxContainers/setting_box_containers/_SettingBoxGenerator.py index 82693501..d9c4cebd 100644 --- a/vrct_gui/config_window/widgets/createSideMenuAndSettingsBoxContainers/setting_box_containers/_SettingBoxGenerator.py +++ b/vrct_gui/config_window/widgets/createSideMenuAndSettingsBoxContainers/setting_box_containers/_SettingBoxGenerator.py @@ -547,7 +547,7 @@ class _SettingBoxGenerator(): justify="center", font=CTkFont(family=self.settings.FONT_FAMILY, size=self.settings.uism.SB__ENTRY_FONT_SIZE, weight="normal"), ) - setattr(self.config_window, base_entry_attr_name + str(i), entry_widget) + setattr(self.config_window, base_entry_attr_name + "_" + str(i), entry_widget) @@ -588,9 +588,9 @@ class _SettingBoxGenerator(): entries_wrapper.grid_columnconfigure((0,2,4), weight=1) entries_wrapper.grid_columnconfigure((1,3), weight=0, uniform="message_format_fixed_labels") - entry_widget_0 = getattr(self.config_window, base_entry_attr_name+"0") - entry_widget_1 = getattr(self.config_window, base_entry_attr_name+"1") - entry_widget_2 = getattr(self.config_window, base_entry_attr_name+"2") + entry_widget_0 = getattr(self.config_window, base_entry_attr_name+"_0") + entry_widget_1 = getattr(self.config_window, base_entry_attr_name+"_1") + entry_widget_2 = getattr(self.config_window, base_entry_attr_name+"_2") entry_widget_0.grid(row=0, column=0, sticky="ew") entry_widget_1.grid(row=0, column=2, sticky="ew") entry_widget_2.grid(row=0, column=4, sticky="ew") diff --git a/vrct_gui/config_window/widgets/createSideMenuAndSettingsBoxContainers/setting_box_containers/setting_box_others/createSettingBox_Others.py b/vrct_gui/config_window/widgets/createSideMenuAndSettingsBoxContainers/setting_box_containers/setting_box_others/createSettingBox_Others.py index 241d33c6..368d3edd 100644 --- a/vrct_gui/config_window/widgets/createSideMenuAndSettingsBoxContainers/setting_box_containers/setting_box_others/createSettingBox_Others.py +++ b/vrct_gui/config_window/widgets/createSideMenuAndSettingsBoxContainers/setting_box_containers/setting_box_others/createSettingBox_Others.py @@ -82,6 +82,7 @@ def createSettingBox_Others(setting_box_wrapper, config_window, settings, view_v textvariable_1=view_variable.VAR_TEXT_REQUIRED_1_MESSAGE_FORMAT, entry_bind__Any_KeyRelease=lambda value: entry_message_format_callback(value), # entry_textvariable=view_variable.VAR_MESSAGE_FORMAT, + entry_bind__FocusOut=view_variable.CALLBACK_FOCUS_OUT_MESSAGE_FORMAT, ) config_window.sb__message_format.grid(row=row) row+=1