KeyShortcutHandler detect conflict with cancelKey

This commit is contained in:
2022-09-14 20:11:02 +00:00
parent 8785c00b12
commit 473e998160
2 changed files with 4 additions and 3 deletions

View File

@@ -12,6 +12,7 @@ typedef BadKeyHandler<T> = (String, PrefixMap<T>) -> Void;
typedef BadShortcutHandler<T> = (String, ShortcutKey<T>) -> Void; typedef BadShortcutHandler<T> = (String, ShortcutKey<T>) -> Void;
enum ShortcutKey<T> { enum ShortcutKey<T> {
Cancel(key:String);
Final(item:T); Final(item:T);
Prefix(keys:PrefixMap<T>); Prefix(keys:PrefixMap<T>);
} }

View File

@@ -75,12 +75,12 @@
(unless prefixMap (set prefixMap rootMap)) (unless prefixMap (set prefixMap rootMap))
(let [firstKey (keys.shift)] (let [firstKey (keys.shift)]
(cond (cond
((prefixMap.exists firstKey) ((or (prefixMap.exists firstKey) (= firstKey cancelKey))
(let [existingKey (dictGet prefixMap firstKey) (let [existingKey (if (= firstKey cancelKey) (Cancel cancelKey) (dictGet prefixMap firstKey))
conflictMessage "Keyboard shortcut for $description conflicts with $existingKey"] conflictMessage "Keyboard shortcut for $description conflicts with $existingKey"]
(if keys (if keys
(case existingKey (case existingKey
((Final _) ((or (Final _) (Cancel _))
(tryCallOrThrow onBadShortcut conflictMessage false description existingKey)) (tryCallOrThrow onBadShortcut conflictMessage false description existingKey))
((Prefix innerPrefixMap) ((Prefix innerPrefixMap)
(registerShortcut keys description item innerPrefixMap)) (registerShortcut keys description item innerPrefixMap))