Skip to content

Commit

Permalink
chore: try add shortcut in menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
AkaShark committed Jan 20, 2024
1 parent 7a802e6 commit 4afb37f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
60 changes: 60 additions & 0 deletions Easydict/NewApp/Feature/Shortcut/Shortcut.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ extension Shortcut {
if let screenshotOCRShortcutKeyCombo = restoreScreenshotOCRShortcutKey() {
bindingShortCut(keyCombo: screenshotOCRShortcutKeyCombo, type: .silentScreenshotOcr)
}
if let showMiniWindowShortcutKeyCombo = restoreShowMiniWindow() {
bindingShortCut(keyCombo: showMiniWindowShortcutKeyCombo, type: .showMiniWindow)
}
}

private func restoreInputTranslate() -> KeyCombo? {
Expand All @@ -85,6 +88,12 @@ extension Shortcut {
guard let keyCombo = try? JSONDecoder().decode(KeyCombo.self, from: data) else { return nil }
return keyCombo
}

private func restoreShowMiniWindow() -> KeyCombo? {
let data = Defaults[.showMiniWindowShortcutKey]
guard let keyCombo = try? JSONDecoder().decode(KeyCombo.self, from: data) else { return nil }
return keyCombo
}
}

// binding shortcut
Expand Down Expand Up @@ -144,3 +153,54 @@ extension Shortcut {
EZWindowManager.shared().screenshotOCR()
}
}

// fetch shortcut string
extension Shortcut {
// can't using keyEquivalent and EventModifiers in SwiftUI MenuItemView direct, because item
// keyboardShortcut not support double modifier key

// func fetchShortcutKeyEquivalent(_ type: ShortcutType) -> String {
// guard let keyCombo = fetchShortcutKeyCombo(type) else { return "" }
// return keyCombo.keyEquivalent
// }
//
// func fetchShortcutKeyEventModifiers(_ type: ShortcutType) -> EventModifiers {
// guard let keyCombo = fetchShortcutKeyCombo(type) else { return [] }
// return [.command, .command]
// }

func fetchShortcutKeyStr(_: ShortcutType) -> String {
guard let keyCombo = restoreInputTranslate() else { return "" }
return shortcutKeyStr(keyCombo)
}

private func shortcutKeyCombo(_ type: ShortcutType) -> KeyCombo? {
switch type {
case .inputTranslate:
guard let keyCombo = restoreInputTranslate() else { return nil }
return keyCombo
case .snipTranslate:
guard let keyCombo = restoreSnipShortcutKey() else { return nil }
return keyCombo
case .selectTranslate:
guard let keyCombo = restoreSelectionShortcutKey() else { return nil }
return keyCombo
case .silentScreenshotOcr:
guard let keyCombo = restoreScreenshotOCRShortcutKey() else { return nil }
return keyCombo
case .showMiniWindow:
guard let keyCombo = restoreShowMiniWindow() else { return nil }
return keyCombo
}
}

private func shortcutKeyStr(_ keyCombo: KeyCombo) -> String {
var shortcut = ""
if keyCombo.doubledModifiers {
shortcut = keyCombo.keyEquivalentModifierMaskString + keyCombo.keyEquivalentModifierMaskString
} else {
shortcut = keyCombo.keyEquivalentModifierMaskString + keyCombo.keyEquivalent
}
return shortcut
}
}
2 changes: 1 addition & 1 deletion Easydict/NewApp/View/MenuItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct MenuItemView: View {
Group {
versionItem
Divider()
inputItem
inputItem.keyboardShortcut(/*@START_MENU_TOKEN@*/ .defaultAction/*@END_MENU_TOKEN@*/)
screenshotItem
selectWordItem
miniWindowItem
Expand Down

0 comments on commit 4afb37f

Please sign in to comment.