Last active
March 28, 2023 18:43
-
-
Save Clarko/a3b8d1484d4f466c6f4f0581164bee9a to your computer and use it in GitHub Desktop.
Revisions
-
Clarko revised this gist
Feb 3, 2021 . 1 changed file with 17 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,11 @@ import SwiftUI extension View { func keyboardShortcut(_ key: KeyEquivalent, disabled: Bool) -> some View { self.modifier(RemovableKeyboardShortcut(key: key, disabled: disabled)) } func keyboardShortcut(_ shortcut: KeyboardShortcut, disabled: Bool) -> some View { self.modifier(RemovableKeyboardShortcut(shortcut: shortcut, disabled: disabled)) } func keyboardShortcut(_ key: KeyEquivalent, modifiers: EventModifiers, disabled: Bool) -> some View { @@ -19,16 +23,21 @@ extension View { } struct RemovableKeyboardShortcut: ViewModifier { var shortcut: KeyboardShortcut? = nil var key: KeyEquivalent? = nil var modifiers: EventModifiers? = nil var disabled: Bool func body(content: Content) -> some View { switch (shortcut, key, modifiers, disabled) { case (let .some(shortcut), nil, nil, false): content.keyboardShortcut(shortcut) case (nil, let .some(key), nil, false): content.keyboardShortcut(key) case (nil, let .some(key), let .some(modifiers), false): content.keyboardShortcut(key, modifiers: modifiers) default: content } } } -
Clarko created this gist
Feb 3, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ // // RemovableKeyboardShortcut.swift // // Created by Clarko on 2/2/21. // // Adds an argument to dynamically remove a keyboard shortcut // import SwiftUI extension View { func keyboardShortcut(_ key: KeyEquivalent, disabled: Bool) -> some View { self.modifier(RemovableKeyboardShortcut(key: key, modifiers: nil, disabled: disabled)) } func keyboardShortcut(_ key: KeyEquivalent, modifiers: EventModifiers, disabled: Bool) -> some View { self.modifier(RemovableKeyboardShortcut(key: key, modifiers: modifiers, disabled: disabled)) } } struct RemovableKeyboardShortcut: ViewModifier { let key: KeyEquivalent let modifiers: EventModifiers? let disabled: Bool func body(content: Content) -> some View { if disabled { content } else { content.keyboardShortcut(key, modifiers: modifiers ?? .command) } } }