From 51a7a0fdc71bab037d8fabd105352f5b8ca29e4e Mon Sep 17 00:00:00 2001 From: Sharker <1548742234@qq.com> Date: Wed, 7 Feb 2024 00:49:14 +0800 Subject: [PATCH] fix: optimize code --- .../NewApp/Feature/Shortcut/Shortcut.swift | 47 +++++++++++++++++++ .../MenuView/MainMenuShortcutCommand.swift | 33 ++++++------- .../MainMenuShortcutCommandItem.swift | 4 ++ .../View/Shortcut/AppShortcutSetting.swift | 30 ++++++------ .../View/Shortcut/GlobalShortcutSetting.swift | 10 ++-- 5 files changed, 86 insertions(+), 38 deletions(-) diff --git a/Easydict/NewApp/Feature/Shortcut/Shortcut.swift b/Easydict/NewApp/Feature/Shortcut/Shortcut.swift index 1dc9ab8e3..c08c2dc2a 100644 --- a/Easydict/NewApp/Feature/Shortcut/Shortcut.swift +++ b/Easydict/NewApp/Feature/Shortcut/Shortcut.swift @@ -37,6 +37,53 @@ public enum ShortcutType: String { case appleDic = "EZAppleDicShortcutKey" } +extension ShortcutType { + func localizedStringKey() -> String { + switch self { + case .inputTranslate: + "input_translate" + case .snipTranslate: + "snip_translate" + case .selectTranslate: + "select_translate" + case .silentScreenshotOcr: + "silent_screenshot_ocr" + case .showMiniWindow: + "show_mini_window" + case .clearInput: + "shortcut_clear_input" + case .clearAll: + "shortcut_clear_all" + case .copy: + "shortcut_copy" + case .copyFirstResult: + "shortcut_copy_first_translated_text" + case .focus: + "shortcut_focus" + case .play: + "shortcut_play" + case .retry: + "retry" + case .toggle: + "toggle_languages" + case .pin: + "pin" + case .hide: + "hide" + case .increaseFontSize: + "shortcut_increase_font" + case .decreaseFontSize: + "shortcut_decrease_font" + case .google: + "open_in_google" + case .eudic: + "open_in_eudic" + case .appleDic: + "open_in_apple_dictionary" + } + } +} + // Confict Message public struct ShortcutConfictAlertMessage: Identifiable { public var id: String { message } diff --git a/Easydict/NewApp/View/MenuView/MainMenuShortcutCommand.swift b/Easydict/NewApp/View/MenuView/MainMenuShortcutCommand.swift index e8d4d79ca..180cb45f0 100644 --- a/Easydict/NewApp/View/MenuView/MainMenuShortcutCommand.swift +++ b/Easydict/NewApp/View/MenuView/MainMenuShortcutCommand.swift @@ -11,21 +11,21 @@ import SwiftUI extension EasyDictMainMenu { struct MainMenuShortcutCommand: Commands { @State private var appShortcutCommandList = [ - MainMenuShortcutCommandDataItem(title: "shortcut_clear_input", type: .clearInput), - MainMenuShortcutCommandDataItem(title: "shortcut_clear_all", type: .clearAll), - MainMenuShortcutCommandDataItem(title: "shortcut_copy", type: .copy), - MainMenuShortcutCommandDataItem(title: "shortcut_copy_first_translated_text", type: .copyFirstResult), - MainMenuShortcutCommandDataItem(title: "shortcut_focus", type: .focus), - MainMenuShortcutCommandDataItem(title: "shortcut_play", type: .play), - MainMenuShortcutCommandDataItem(title: "retry", type: .retry), - MainMenuShortcutCommandDataItem(title: "toggle_languages", type: .toggle), - MainMenuShortcutCommandDataItem(title: "pin", type: .pin), - MainMenuShortcutCommandDataItem(title: "hide", type: .hide), - MainMenuShortcutCommandDataItem(title: "shortcut_increase_font", type: .increaseFontSize), - MainMenuShortcutCommandDataItem(title: "shortcut_decrease_font", type: .decreaseFontSize), - MainMenuShortcutCommandDataItem(title: "open_in_google", type: .google), - MainMenuShortcutCommandDataItem(title: "open_in_eudic", type: .eudic), - MainMenuShortcutCommandDataItem(title: "open_in_apple_dictionary", type: .appleDic), + MainMenuShortcutCommandDataItem(title: ShortcutType.clearInput.localizedStringKey(), type: .clearInput), + MainMenuShortcutCommandDataItem(title: ShortcutType.clearAll.localizedStringKey(), type: .clearAll), + MainMenuShortcutCommandDataItem(title: ShortcutType.copy.localizedStringKey(), type: .copy), + MainMenuShortcutCommandDataItem(title: ShortcutType.copyFirstResult.localizedStringKey(), type: .copyFirstResult), + MainMenuShortcutCommandDataItem(title: ShortcutType.focus.localizedStringKey(), type: .focus), + MainMenuShortcutCommandDataItem(title: ShortcutType.play.localizedStringKey(), type: .play), + MainMenuShortcutCommandDataItem(title: ShortcutType.retry.localizedStringKey(), type: .retry), + MainMenuShortcutCommandDataItem(title: ShortcutType.toggle.localizedStringKey(), type: .toggle), + MainMenuShortcutCommandDataItem(title: ShortcutType.pin.localizedStringKey(), type: .pin), + MainMenuShortcutCommandDataItem(title: ShortcutType.hide.localizedStringKey(), type: .hide), + MainMenuShortcutCommandDataItem(title: ShortcutType.increaseFontSize.localizedStringKey(), type: .increaseFontSize), + MainMenuShortcutCommandDataItem(title: ShortcutType.decreaseFontSize.localizedStringKey(), type: .decreaseFontSize), + MainMenuShortcutCommandDataItem(title: ShortcutType.google.localizedStringKey(), type: .google), + MainMenuShortcutCommandDataItem(title: ShortcutType.eudic.localizedStringKey(), type: .eudic), + MainMenuShortcutCommandDataItem(title: ShortcutType.appleDic.localizedStringKey(), type: .appleDic), ] var body: some Commands { @@ -33,9 +33,6 @@ extension EasyDictMainMenu { CommandMenu("shortcut") { ForEach(appShortcutCommandList) { item in MainMenuShortcutCommandItem(dataItem: item) - if item.title == "toggle_languages" || item.title == "shortcut_decrease_font" { - Divider() - } } } } diff --git a/Easydict/NewApp/View/MenuView/MainMenuShortcutCommandItem.swift b/Easydict/NewApp/View/MenuView/MainMenuShortcutCommandItem.swift index 5fe5876e0..f0d0af319 100644 --- a/Easydict/NewApp/View/MenuView/MainMenuShortcutCommandItem.swift +++ b/Easydict/NewApp/View/MenuView/MainMenuShortcutCommandItem.swift @@ -54,5 +54,9 @@ struct MainMenuShortcutCommandItem: View { } } .keyboardShortcut(dataItem.type) + + if dataItem.type == .toggle || dataItem.type == .decreaseFontSize { + Divider() + } } } diff --git a/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/AppShortcutSetting.swift b/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/AppShortcutSetting.swift index fdaf5c31f..a499b54e5 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/AppShortcutSetting.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/AppShortcutSetting.swift @@ -12,21 +12,21 @@ import SwiftUI extension ShortcutTab { struct AppShortcutSettingView: View { @State private var shortcutDataList = [ - KeyHolderDataItem(title: "shortcut_clear_input", type: .clearInput), - KeyHolderDataItem(title: "shortcut_clear_all", type: .clearAll), - KeyHolderDataItem(title: "shortcut_copy", type: .copy), - KeyHolderDataItem(title: "shortcut_copy_first_translated_text", type: .copyFirstResult), - KeyHolderDataItem(title: "shortcut_focus", type: .focus), - KeyHolderDataItem(title: "shortcut_play", type: .play), - KeyHolderDataItem(title: "retry", type: .retry), - KeyHolderDataItem(title: "toggle_languages", type: .toggle), - KeyHolderDataItem(title: "pin", type: .pin), - KeyHolderDataItem(title: "hide", type: .hide), - KeyHolderDataItem(title: "shortcut_increase_font", type: .increaseFontSize), - KeyHolderDataItem(title: "shortcut_decrease_font", type: .decreaseFontSize), - KeyHolderDataItem(title: "open_in_google", type: .google), - KeyHolderDataItem(title: "open_in_eudic", type: .eudic), - KeyHolderDataItem(title: "open_in_apple_dictionary", type: .appleDic), + KeyHolderDataItem(title: ShortcutType.clearInput.localizedStringKey(), type: .clearInput), + KeyHolderDataItem(title: ShortcutType.clearAll.localizedStringKey(), type: .clearAll), + KeyHolderDataItem(title: ShortcutType.copy.localizedStringKey(), type: .copy), + KeyHolderDataItem(title: ShortcutType.copyFirstResult.localizedStringKey(), type: .copyFirstResult), + KeyHolderDataItem(title: ShortcutType.focus.localizedStringKey(), type: .focus), + KeyHolderDataItem(title: ShortcutType.play.localizedStringKey(), type: .play), + KeyHolderDataItem(title: ShortcutType.retry.localizedStringKey(), type: .retry), + KeyHolderDataItem(title: ShortcutType.toggle.localizedStringKey(), type: .toggle), + KeyHolderDataItem(title: ShortcutType.pin.localizedStringKey(), type: .pin), + KeyHolderDataItem(title: ShortcutType.hide.localizedStringKey(), type: .hide), + KeyHolderDataItem(title: ShortcutType.increaseFontSize.localizedStringKey(), type: .increaseFontSize), + KeyHolderDataItem(title: ShortcutType.decreaseFontSize.localizedStringKey(), type: .decreaseFontSize), + KeyHolderDataItem(title: ShortcutType.google.localizedStringKey(), type: .google), + KeyHolderDataItem(title: ShortcutType.eudic.localizedStringKey(), type: .eudic), + KeyHolderDataItem(title: ShortcutType.appleDic.localizedStringKey(), type: .appleDic), ] @State var confictAlterMessage: ShortcutConfictAlertMessage = .init(title: "", message: "") var body: some View { diff --git a/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/GlobalShortcutSetting.swift b/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/GlobalShortcutSetting.swift index f6263bc7d..c911e5c1a 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/GlobalShortcutSetting.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/GlobalShortcutSetting.swift @@ -13,11 +13,11 @@ extension ShortcutTab { struct GlobalShortcutSettingView: View { @State var confictAlterMessage: ShortcutConfictAlertMessage = .init(title: "", message: "") @State private var shortcutDataList = [ - KeyHolderDataItem(title: "input_translate", type: .inputTranslate), - KeyHolderDataItem(title: "snip_translate", type: .snipTranslate), - KeyHolderDataItem(title: "select_translate", type: .selectTranslate), - KeyHolderDataItem(title: "show_mini_window", type: .showMiniWindow), - KeyHolderDataItem(title: "silent_screenshot_ocr", type: .silentScreenshotOcr), + KeyHolderDataItem(title: ShortcutType.inputTranslate.localizedStringKey(), type: .inputTranslate), + KeyHolderDataItem(title: ShortcutType.snipTranslate.localizedStringKey(), type: .snipTranslate), + KeyHolderDataItem(title: ShortcutType.selectTranslate.localizedStringKey(), type: .selectTranslate), + KeyHolderDataItem(title: ShortcutType.showMiniWindow.localizedStringKey(), type: .showMiniWindow), + KeyHolderDataItem(title: ShortcutType.silentScreenshotOcr.localizedStringKey(), type: .silentScreenshotOcr), ] var body: some View { let showAlter = Binding(