From 213f092e96dd2c959a8f7c0889f542c39bdeb642 Mon Sep 17 00:00:00 2001 From: Sharker <1548742234@qq.com> Date: Fri, 9 Feb 2024 09:29:54 +0800 Subject: [PATCH] fix: shortcut bug (#394) * fix: shortcut bug https://github.com/tisfeng/Easydict/issues/391 * pref: optimize code * fix: upate menu crash * fix: fix review issues and remvoe unused localizable --- Easydict.xcodeproj/project.pbxproj | 4 ++++ Easydict/App/Localizable.xcstrings | 20 ------------------- .../Feature/Shortcut/Shortcut+Menu.swift | 20 +++++++++++++++++++ .../View/MenuView/MainMenuCommand.swift | 2 -- .../Tabs/View/Shortcut/KeyHolderWrapper.swift | 18 +++++++++-------- 5 files changed, 34 insertions(+), 30 deletions(-) create mode 100644 Easydict/NewApp/Feature/Shortcut/Shortcut+Menu.swift diff --git a/Easydict.xcodeproj/project.pbxproj b/Easydict.xcodeproj/project.pbxproj index 4f0d47908..c120c7813 100644 --- a/Easydict.xcodeproj/project.pbxproj +++ b/Easydict.xcodeproj/project.pbxproj @@ -283,6 +283,7 @@ 9643D9462B71D103000FBEA6 /* KeyHolderRowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9643D9452B71D103000FBEA6 /* KeyHolderRowView.swift */; }; 9643D94A2B71EABE000FBEA6 /* KeyHolderAlterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9643D9492B71EABE000FBEA6 /* KeyHolderAlterView.swift */; }; 9643D94C2B71F74D000FBEA6 /* MainMenuShortcutCommandItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9643D94B2B71F74D000FBEA6 /* MainMenuShortcutCommandItem.swift */; }; + 9643D9562B73B3CD000FBEA6 /* Shortcut+Menu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9643D9552B73B3CD000FBEA6 /* Shortcut+Menu.swift */; }; 9672D7D22B4008B40023B8FB /* MASShortcutBinder+EZMASShortcutBinder.m in Sources */ = {isa = PBXBuildFile; fileRef = 9672D7D12B4008B40023B8FB /* MASShortcutBinder+EZMASShortcutBinder.m */; }; 967712EA2B5B913600105E0F /* KeyHolder in Frameworks */ = {isa = PBXBuildFile; productRef = 967712E92B5B913600105E0F /* KeyHolder */; }; 967712EE2B5B943400105E0F /* Shortcut.swift in Sources */ = {isa = PBXBuildFile; fileRef = 967712ED2B5B943400105E0F /* Shortcut.swift */; }; @@ -806,6 +807,7 @@ 9643D9452B71D103000FBEA6 /* KeyHolderRowView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyHolderRowView.swift; sourceTree = ""; }; 9643D9492B71EABE000FBEA6 /* KeyHolderAlterView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyHolderAlterView.swift; sourceTree = ""; }; 9643D94B2B71F74D000FBEA6 /* MainMenuShortcutCommandItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainMenuShortcutCommandItem.swift; sourceTree = ""; }; + 9643D9552B73B3CD000FBEA6 /* Shortcut+Menu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Shortcut+Menu.swift"; sourceTree = ""; }; 9672D7D02B4008B40023B8FB /* MASShortcutBinder+EZMASShortcutBinder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "MASShortcutBinder+EZMASShortcutBinder.h"; sourceTree = ""; }; 9672D7D12B4008B40023B8FB /* MASShortcutBinder+EZMASShortcutBinder.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "MASShortcutBinder+EZMASShortcutBinder.m"; sourceTree = ""; }; 967712ED2B5B943400105E0F /* Shortcut.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Shortcut.swift; sourceTree = ""; }; @@ -2273,6 +2275,7 @@ 9643D9432B6FEF5F000FBEA6 /* Shortcut+Default.swift */, 9643D9412B6FE4AF000FBEA6 /* Shortcut+Bind.swift */, 9608354F2B6791F200C6A931 /* Shortcut+Validator.swift */, + 9643D9552B73B3CD000FBEA6 /* Shortcut+Menu.swift */, ); path = Shortcut; sourceTree = ""; @@ -3031,6 +3034,7 @@ 03F639952AA6CFBB009B9914 /* EZBingConfig.m in Sources */, 0AC8A83F2B689E68006DA5CC /* ServiceSecretConfigreValidatable.swift in Sources */, 03D2A3E329F4C6F50035CED4 /* EZNetworkManager.m in Sources */, + 9643D9562B73B3CD000FBEA6 /* Shortcut+Menu.swift in Sources */, 0309E1ED292B439A00AFB76A /* EZTextView.m in Sources */, 03B0232B29231FA6001C7E63 /* NSMutableAttributedString+MM.m in Sources */, 03B022E829231FA6001C7E63 /* entry.m in Sources */, diff --git a/Easydict/App/Localizable.xcstrings b/Easydict/App/Localizable.xcstrings index fe1356419..db79188c4 100644 --- a/Easydict/App/Localizable.xcstrings +++ b/Easydict/App/Localizable.xcstrings @@ -3648,16 +3648,6 @@ } } }, - "shortcut_confict_message (Shortcut.shared.confictMenuItem?.title ?? \"\")" : { - "localizations" : { - "zh-Hans" : { - "stringUnit" : { - "state" : "translated", - "value" : "" - } - } - } - }, "shortcut_confict_message %@" : { "extractionState" : "manual", "localizations" : { @@ -3675,16 +3665,6 @@ } } }, - "shortcut_confict_title (keyCombo!.keyEquivalentModifierMaskString + keyCombo!.characters)" : { - "localizations" : { - "zh-Hans" : { - "stringUnit" : { - "state" : "translated", - "value" : "" - } - } - } - }, "shortcut_confict_title %@" : { "extractionState" : "manual", "localizations" : { diff --git a/Easydict/NewApp/Feature/Shortcut/Shortcut+Menu.swift b/Easydict/NewApp/Feature/Shortcut/Shortcut+Menu.swift new file mode 100644 index 000000000..04f109aa6 --- /dev/null +++ b/Easydict/NewApp/Feature/Shortcut/Shortcut+Menu.swift @@ -0,0 +1,20 @@ +// +// Shortcut+Menu.swift +// Easydict +// +// Created by Sharker on 2024/2/7. +// Copyright © 2024 izual. All rights reserved. +// + +import SwiftUI + +extension Shortcut { + @available(macOS 13, *) + func updateMenu(_ type: ShortcutType) { // update shortcut menu + let shortcutTitle = String(localized: LocalizedStringResource(stringLiteral: type.localizedStringKey())) + let menuTitle = String(localized: LocalizedStringResource(stringLiteral: "shortcut")) + let shortcutMenu = NSApp.mainMenu?.items.first(where: { $0.title == menuTitle }) + let clearInput = shortcutMenu?.submenu?.items.first(where: { $0.title == shortcutTitle }) + clearInput?.keyEquivalent = "" + } +} diff --git a/Easydict/NewApp/View/MenuView/MainMenuCommand.swift b/Easydict/NewApp/View/MenuView/MainMenuCommand.swift index 84231b7bf..388caacd3 100644 --- a/Easydict/NewApp/View/MenuView/MainMenuCommand.swift +++ b/Easydict/NewApp/View/MenuView/MainMenuCommand.swift @@ -13,6 +13,4 @@ struct EasyDictMainMenu: Commands { // shortcut MainMenuShortcutCommand() } - - func showAPIWebSite() {} } diff --git a/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/KeyHolderWrapper.swift b/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/KeyHolderWrapper.swift index 80ef418be..b366864cc 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/KeyHolderWrapper.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/KeyHolderWrapper.swift @@ -17,6 +17,7 @@ public struct KeyHolderDataItem: Identifiable { var type: ShortcutType } +@available(macOS 13, *) struct KeyHolderWrapper: NSViewRepresentable { func makeCoordinator() -> Coordinator { .init(shortcutType: type, confictAlterMessage: $confictAlterMessage) @@ -44,6 +45,7 @@ struct KeyHolderWrapper: NSViewRepresentable { func updateNSView(_: NSViewType, context _: Context) {} } +@available(macOS 13, *) extension KeyHolderWrapper { class Coordinator: NSObject, RecordViewDelegate { private var type: ShortcutType @@ -67,17 +69,17 @@ extension KeyHolderWrapper { if let key = keyCombo { // shortcut validate confict if Shortcut.validateShortcut(key) { - if #available(macOS 12, *) { - confictAlterMessage = ShortcutConfictAlertMessage(title: String(localized: "shortcut_confict_title \(keyCombo!.keyEquivalentModifierMaskString + keyCombo!.characters)"), message: String(localized: "shortcut_confict_message \(Shortcut.shared.confictMenuItem?.title ?? "")")) - } else { - // Fallback on earlier versions - let title = NSLocalizedString("shortcut_confict_title \(keyCombo!.keyEquivalentModifierMaskString + keyCombo!.characters)", comment: "") - let msg = NSLocalizedString("shortcut_confict_message \(Shortcut.shared.confictMenuItem?.title ?? "")", comment: "") - confictAlterMessage = ShortcutConfictAlertMessage(title: title, message: msg) - } + let title = String(localized: "shortcut_confict_title \(key.keyEquivalentModifierMaskString + key.characters)") + let message = String(localized: "shortcut_confict_message \(Shortcut.shared.confictMenuItem?.title ?? "")") + confictAlterMessage = ShortcutConfictAlertMessage( + title: title, + message: message + ) recordView.clear() return } + } else { // clear shortcut + Shortcut.shared.updateMenu(type) } storeKeyCombo(with: keyCombo) Shortcut.shared.bindingShortcut(keyCombo: keyCombo, type: type)