From 527ca13f8774d2d80c08b645e12c6ca4cb9a4bc2 Mon Sep 17 00:00:00 2001 From: choykarl <253440030@qq.com> Date: Mon, 29 Jan 2024 23:33:15 +0800 Subject: [PATCH] pref: optimize the use of MenuBarIconType --- Easydict/App/Localizable.xcstrings | 4 +-- .../Configuration+Defaults.swift | 2 +- Easydict/NewApp/EasydictApp.swift | 25 +++---------------- .../View/SettingView/Tabs/GeneralTab.swift | 12 ++++----- 4 files changed, 13 insertions(+), 30 deletions(-) diff --git a/Easydict/App/Localizable.xcstrings b/Easydict/App/Localizable.xcstrings index 0acfa746a..40a247e60 100644 --- a/Easydict/App/Localizable.xcstrings +++ b/Easydict/App/Localizable.xcstrings @@ -1805,7 +1805,7 @@ } } }, - "modify_menu_bar_icon" : { + "modify_menubar_icon" : { "extractionState" : "manual", "localizations" : { "en" : { @@ -1817,7 +1817,7 @@ "zh-Hans" : { "stringUnit" : { "state" : "translated", - "value" : "修改菜单栏图标" + "value" : "菜单栏图标" } } } diff --git a/Easydict/NewApp/Configuration/Configuration+Defaults.swift b/Easydict/NewApp/Configuration/Configuration+Defaults.swift index cc51f76d5..534bfc821 100644 --- a/Easydict/NewApp/Configuration/Configuration+Defaults.swift +++ b/Easydict/NewApp/Configuration/Configuration+Defaults.swift @@ -52,7 +52,7 @@ extension Defaults.Keys { static let appearanceType = Key("EZConfiguration_kApperanceKey", default: .followSystem) static let fontSizeOptionIndex = Key("EZConfiguration_kTranslationControllerFontKey", default: 0) - static let selectedMenuBarIcon = Key("EZConfiguration_kSelectedMenuBarIconKey", default: 0) + static let selectedMenuBarIcon = Key("EZConfiguration_kSelectedMenuBarIconKey", default: .classical) } extension Defaults.Keys { diff --git a/Easydict/NewApp/EasydictApp.swift b/Easydict/NewApp/EasydictApp.swift index 8522e0765..00463a91b 100644 --- a/Easydict/NewApp/EasydictApp.swift +++ b/Easydict/NewApp/EasydictApp.swift @@ -34,14 +34,6 @@ struct EasydictApp: App { @Default(.selectedMenuBarIcon) private var menuBarIcon - private var menuBarImage: String { - #if DEBUG - MenuBarIconType.debug.imageName - #else - menuBarIcon == MenuBarIconType.classical.rawValue ? MenuBarIconType.classical.imageName : MenuBarIconType.debug.imageName - #endif - } - var body: some Scene { if #available(macOS 13, *) { MenuBarExtra(isInserted: $hideMenuBar.toggledValue) { @@ -50,7 +42,7 @@ struct EasydictApp: App { Label { Text("Easydict") } icon: { - Image(menuBarImage) + Image(menuBarIcon.rawValue) .resizable() .renderingMode(.template) .scaledToFit() @@ -71,18 +63,9 @@ extension Bool { } } -enum MenuBarIconType: UInt, CaseIterable, Identifiable { +enum MenuBarIconType: String, CaseIterable, Defaults.Serializable, Identifiable { var id: Self { self } - - case classical = 0 - case debug = 1 - var imageName: String { - switch self { - case .classical: - return "status_icon" - case .debug: - return "status_icon_debug" - } - } + case classical = "status_icon" + case debug = "status_icon_debug" } diff --git a/Easydict/NewApp/View/SettingView/Tabs/GeneralTab.swift b/Easydict/NewApp/View/SettingView/Tabs/GeneralTab.swift index 84e25ce79..738ea73d3 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/GeneralTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/GeneralTab.swift @@ -137,12 +137,12 @@ struct GeneralTab: View { Text("hide_menu_bar_icon") } Picker( - "modify_menu_bar_icon", - selection: $selectedMenuBarIcon) { - ForEach(MenuBarIconType.allCases, id: \.rawValue) { option in - Image(option.imageName) - .tag(option) - } + "modify_menubar_icon", + selection: $selectedMenuBarIcon + ) { + ForEach(MenuBarIconType.allCases) { option in + Image(option.rawValue) + } } } header: { Text("setting.general.other.header")