Enhance auto-focus feature with settings configuration

• Added configurable auto-focus option in settings window

• Auto-focus now defaults to enabled but can be toggled off

• Added configuration management for auto_focus_enabled setting

• Enhanced FeedbackWindow to respect auto-focus preference

• Added UI controls in SettingsTab with modern switch component

• Implemented proper configuration save/load with debug logging

• Added i18n support for auto-focus setting in all languages:

  - English: 'Auto-focus input box when window opens'

  - 繁體中文: '窗口打開時自動聚焦到輸入框'

  - 简体中文: '窗口打开时自动聚焦到输入框'

This enhancement makes the auto-focus feature more user-friendly

by allowing users to choose their preferred input behavior.
This commit is contained in:
penn 2025-06-07 17:10:57 +08:00
parent 747270b43a
commit 75cc0dbe8a
6 changed files with 1069 additions and 1036 deletions

View File

@ -117,7 +117,8 @@
}, },
"window": { "window": {
"title": "Window Positioning", "title": "Window Positioning",
"alwaysCenter": "Always show window at primary screen center" "alwaysCenter": "Always show window at primary screen center",
"autoFocus": "Auto-focus input box when window opens"
}, },
"reset": { "reset": {
"title": "Reset Settings", "title": "Reset Settings",

View File

@ -97,7 +97,8 @@
}, },
"window": { "window": {
"title": "窗口定位", "title": "窗口定位",
"alwaysCenter": "总是在主屏幕中心显示窗口" "alwaysCenter": "总是在主屏幕中心显示窗口",
"autoFocus": "窗口打开时自动聚焦到输入框"
}, },
"reset": { "reset": {
"title": "重置设置", "title": "重置设置",

View File

@ -129,7 +129,8 @@
}, },
"window": { "window": {
"title": "視窗定位", "title": "視窗定位",
"alwaysCenter": "總是在主螢幕中心顯示視窗" "alwaysCenter": "總是在主螢幕中心顯示視窗",
"autoFocus": "窗口打開時自動聚焦到輸入框"
}, },
"reset": { "reset": {
"title": "重置設定", "title": "重置設定",

View File

@ -312,6 +312,12 @@ class SettingsTab(QWidget):
self.always_center_switch.toggled.connect(self._on_always_center_changed) self.always_center_switch.toggled.connect(self._on_always_center_changed)
options_layout.addWidget(self.always_center_switch) options_layout.addWidget(self.always_center_switch)
# 自動聚焦開關
self.auto_focus_switch = SwitchWithLabel(t('settings.window.autoFocus'))
self.auto_focus_switch.setChecked(self.config_manager.get_auto_focus_enabled())
self.auto_focus_switch.toggled.connect(self._on_auto_focus_changed)
options_layout.addWidget(self.auto_focus_switch)
layout.addLayout(options_layout) layout.addLayout(options_layout)
def _create_timeout_section(self, layout: QVBoxLayout) -> None: def _create_timeout_section(self, layout: QVBoxLayout) -> None:
@ -485,6 +491,12 @@ class SettingsTab(QWidget):
self.config_manager.set_always_center_window(checked) self.config_manager.set_always_center_window(checked)
debug_log(f"視窗定位設置已保存: {checked}") # 調試輸出 debug_log(f"視窗定位設置已保存: {checked}") # 調試輸出
def _on_auto_focus_changed(self, checked: bool) -> None:
"""自動聚焦選項變更事件處理"""
# 立即保存設定
self.config_manager.set_auto_focus_enabled(checked)
debug_log(f"自動聚焦設置已保存: {checked}") # 調試輸出
def _on_timeout_enabled_changed(self, enabled: bool) -> None: def _on_timeout_enabled_changed(self, enabled: bool) -> None:
"""超時啟用狀態變更事件處理""" """超時啟用狀態變更事件處理"""
# 立即保存設定 # 立即保存設定
@ -550,6 +562,8 @@ class SettingsTab(QWidget):
# 更新切換開關文字 # 更新切換開關文字
if hasattr(self, 'always_center_switch'): if hasattr(self, 'always_center_switch'):
self.always_center_switch.setText(t('settings.window.alwaysCenter')) self.always_center_switch.setText(t('settings.window.alwaysCenter'))
if hasattr(self, 'auto_focus_switch'):
self.auto_focus_switch.setText(t('settings.window.autoFocus'))
if hasattr(self, 'timeout_enabled_switch'): if hasattr(self, 'timeout_enabled_switch'):
self.timeout_enabled_switch.setText(t('timeout.enable')) self.timeout_enabled_switch.setText(t('timeout.enable'))
@ -585,6 +599,10 @@ class SettingsTab(QWidget):
always_center = self.config_manager.get_always_center_window() always_center = self.config_manager.get_always_center_window()
self.always_center_switch.setChecked(always_center) self.always_center_switch.setChecked(always_center)
debug_log(f"重新載入視窗定位設置: {always_center}") # 調試輸出 debug_log(f"重新載入視窗定位設置: {always_center}") # 調試輸出
if hasattr(self, 'auto_focus_switch'):
auto_focus = self.config_manager.get_auto_focus_enabled()
self.auto_focus_switch.setChecked(auto_focus)
debug_log(f"重新載入自動聚焦設置: {auto_focus}") # 調試輸出
# 重新載入超時設定 # 重新載入超時設定
if hasattr(self, 'timeout_enabled_switch'): if hasattr(self, 'timeout_enabled_switch'):

View File

@ -199,6 +199,15 @@ class ConfigManager:
}) })
debug_log(f"超時設置: {'啟用' if enabled else '停用'}, {seconds}") debug_log(f"超時設置: {'啟用' if enabled else '停用'}, {seconds}")
def get_auto_focus_enabled(self) -> bool:
"""獲取是否啟用自動聚焦到輸入框"""
return self.get('auto_focus_enabled', True) # 預設啟用
def set_auto_focus_enabled(self, enabled: bool) -> None:
"""設置是否啟用自動聚焦到輸入框"""
self.update_partial_config({'auto_focus_enabled': enabled})
debug_log(f"自動聚焦設置: {'啟用' if enabled else '停用'}")
def reset_settings(self) -> None: def reset_settings(self) -> None:
"""重置所有設定到預設值""" """重置所有設定到預設值"""
try: try:

View File

@ -61,11 +61,14 @@ class FeedbackWindow(QMainWindow):
# 如果啟用了超時,自動開始倒數計時 # 如果啟用了超時,自動開始倒數計時
self.start_timeout_if_enabled() self.start_timeout_if_enabled()
# 設置定時器在窗口顯示後自動聚焦到輸入框 # 設置定時器在窗口顯示後自動聚焦到輸入框(如果啟用)
if self.config_manager.get_auto_focus_enabled():
self._focus_timer = QTimer() self._focus_timer = QTimer()
self._focus_timer.setSingleShot(True) self._focus_timer.setSingleShot(True)
self._focus_timer.timeout.connect(self._auto_focus_input) self._focus_timer.timeout.connect(self._auto_focus_input)
self._focus_timer.start(300) # 延遲300ms確保窗口和UI元素完全加載 self._focus_timer.start(300) # 延遲300ms確保窗口和UI元素完全加載
else:
debug_log("自動聚焦已停用")
def _setup_ui(self) -> None: def _setup_ui(self) -> None:
"""設置用戶介面""" """設置用戶介面"""