From 000292cf5789fca766a2d1c99b0a97f7528a94e8 Mon Sep 17 00:00:00 2001 From: Kyle Date: Fri, 29 Dec 2023 01:45:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E9=BD=90=E6=B7=BB=E5=8A=A0=E6=9B=B4?= =?UTF-8?q?=E5=A4=9A=E8=8F=9C=E5=8D=95=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Easydict/App/Localizable.xcstrings | 29 +++++++++- Easydict/NewApp/View/MenuItemView.swift | 71 +++++++++++++++++++++++-- 2 files changed, 95 insertions(+), 5 deletions(-) diff --git a/Easydict/App/Localizable.xcstrings b/Easydict/App/Localizable.xcstrings index b85497d7d..3cad0f617 100644 --- a/Easydict/App/Localizable.xcstrings +++ b/Easydict/App/Localizable.xcstrings @@ -474,6 +474,16 @@ } } }, + "check_updates" : { + "localizations" : { + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "检查更新" + } + } + } + }, "chinese_phonetic" : { "localizations" : { "en" : { @@ -1945,6 +1955,16 @@ } } }, + "quit" : { + "localizations" : { + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "退出" + } + } + } + }, "replace_text" : { "localizations" : { "en" : { @@ -2059,7 +2079,14 @@ } }, "Settings..." : { - + "localizations" : { + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "设置..." + } + } + } }, "shortcut_select_translate_window_type" : { "localizations" : { diff --git a/Easydict/NewApp/View/MenuItemView.swift b/Easydict/NewApp/View/MenuItemView.swift index 40470e70d..806abc807 100644 --- a/Easydict/NewApp/View/MenuItemView.swift +++ b/Easydict/NewApp/View/MenuItemView.swift @@ -6,11 +6,58 @@ // Copyright © 2023 izual. All rights reserved. // +import Sparkle import SwiftUI +@available(macOS 13, *) struct MenuItemView: View { var body: some View { - settingItem + Group { + versionItem + Divider() + settingItem + .keyboardShortcut(.init(",")) + checkUpdateItem + Divider() + quitItem + .keyboardShortcut(.init("q")) + } + .task { + let version = await EZMenuItemManager.shared().fetchRepoLatestVersion(EZGithubRepoEasydict) + await MainActor.run { + latestVersion = version + } + } + } + + @State + private var currentVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "" + + @State + private var latestVersion: String? + + @Environment(\.openURL) + private var openURL + + @ViewBuilder + private var versionItem: some View { + Button(versionString) { + guard let versionURL = URL(string: "\(EZGithubRepoEasydictURL)/releases") else { + return + } + openURL(versionURL) + } + } + + private var versionString: String { + let defaultLabel = "Easydict \(currentVersion)" + if let latestVersion, + currentVersion.compare(latestVersion, options: .numeric) == .orderedAscending + { + return defaultLabel + " (✨ \(latestVersion)" + } else { + return defaultLabel + } } @ViewBuilder @@ -18,15 +65,31 @@ struct MenuItemView: View { if #available(macOS 14.0, *) { SettingsLink() } else { - Button { + Button("Settings...") { + NSLog("打开设置") NSApplication.shared.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil) - } label: { - Text("Settings...") } } } + + @ViewBuilder + private var checkUpdateItem: some View { + Button("check_updates") { + NSLog("检查更新") + SPUStandardUpdaterController(updaterDelegate: nil, userDriverDelegate: nil).checkForUpdates(nil) + } + } + + @ViewBuilder + private var quitItem: some View { + Button("quit") { + NSLog("退出应用") + NSApplication.shared.terminate(nil) + } + } } +@available(macOS 13, *) #Preview { MenuItemView() }