Skip to content

Commit

Permalink
feat: add shortcut into menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
AkaShark committed Jan 21, 2024
1 parent c9a4cd5 commit 17e264b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 36 deletions.
64 changes: 38 additions & 26 deletions Easydict/NewApp/Feature/Shortcut/Shortcut.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import Defaults
import Foundation
import KeyHolder
import Magnet
import SwiftUI

/// Shortcut Service
enum ShortcutType: String {
public enum ShortcutType: String {
case inputTranslate = "EZInputShortcutKey"
case snipTranslate = "EZSnipShortcutKey"
case selectTranslate = "EZSelectionShortcutKey"
Expand Down Expand Up @@ -154,27 +155,9 @@ extension Shortcut {
}
}

// fetch shortcut string
// fetch shortcut KeyCombo
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? {
public func shortcutKeyCombo(_ type: ShortcutType) -> KeyCombo? {
switch type {
case .inputTranslate:
guard let keyCombo = restoreInputTranslate() else { return nil }
Expand All @@ -193,14 +176,43 @@ extension Shortcut {
return keyCombo
}
}
}

/// can't using keyEquivalent and EventModifiers in SwiftUI MenuItemView direct, because item
/// keyboardShortcut not support double modifier key but can use ⌥ as character
extension View {
public func keyboardShortcut(_ type: ShortcutType) -> some View {
guard let keyCombo = Shortcut.shared.shortcutKeyCombo(type) else { return AnyView(self) }
return AnyView(keyboardShortcut(fetchShortcutKeyEquivalent(keyCombo), modifiers: fetchShortcutKeyEventModifiers(keyCombo)))
}

private func shortcutKeyStr(_ keyCombo: KeyCombo) -> String {
var shortcut = ""
private func fetchShortcutKeyEquivalent(_ keyCombo: KeyCombo) -> KeyEquivalent {
if keyCombo.doubledModifiers {
shortcut = keyCombo.keyEquivalentModifierMaskString + keyCombo.keyEquivalentModifierMaskString
return KeyEquivalent(Character(keyCombo.keyEquivalentModifierMaskString))
} else {
shortcut = keyCombo.keyEquivalentModifierMaskString + keyCombo.keyEquivalent
return KeyEquivalent(Character(keyCombo.keyEquivalent))
}
return shortcut
}

private func fetchShortcutKeyEventModifiers(_ keyCombo: KeyCombo) -> EventModifiers {
var modifiers: EventModifiers = []

if keyCombo.keyEquivalentModifierMask.contains(NSEvent.ModifierFlags.command) {
modifiers.update(with: EventModifiers.command)
}

if keyCombo.keyEquivalentModifierMask.contains(NSEvent.ModifierFlags.control) {
modifiers.update(with: EventModifiers.control)
}

if keyCombo.keyEquivalentModifierMask.contains(NSEvent.ModifierFlags.option) {
modifiers.update(with: EventModifiers.option)
}

if keyCombo.keyEquivalentModifierMask.contains(NSEvent.ModifierFlags.shift) {
modifiers.update(with: EventModifiers.shift)
}

return modifiers
}
}
22 changes: 12 additions & 10 deletions Easydict/NewApp/View/MenuItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ struct MenuItemView: View {
versionItem
Divider()
inputItem
.keyboardShortcut(.inputTranslate)
screenshotItem
.keyboardShortcut(.snipTranslate)
selectWordItem
.keyboardShortcut(.selectTranslate)
miniWindowItem
.keyboardShortcut(.showMiniWindow)
Divider()
ocrItem
.keyboardShortcut(.silentScreenshotOcr)
Divider()
settingItem
.keyboardShortcut(.init(","))
Expand Down Expand Up @@ -104,17 +109,14 @@ struct MenuItemView: View {

@ViewBuilder
private var inputItem: some View {
HStack {
Button {
NSLog("输入翻译")
EZWindowManager.shared().inputTranslate()
} label: {
HStack {
Image(systemName: "keyboard")
Text("menu_input_translate")
}
Button {
NSLog("输入翻译")
EZWindowManager.shared().inputTranslate()
} label: {
HStack {
Image(systemName: "keyboard")
Text("menu_input_translate")
}
Text(Shortcut.shared.fetchShortcutKeyStr(.inputTranslate))
}
}

Expand Down

0 comments on commit 17e264b

Please sign in to comment.