|
| 1 | +local eventtap = require('hs.eventtap') |
| 2 | +local events = eventtap.event.types |
| 3 | + |
| 4 | +local modal={} |
| 5 | + |
| 6 | +-- Return an object whose behavior is inspired by hs.hotkey.modal. In this case, |
| 7 | +-- the modal state is entered when the specified modifier key is tapped (i.e., |
| 8 | +-- pressed and then released in quick succession). |
| 9 | +modal.new = function(modifier) |
| 10 | + local instance = { |
| 11 | + modifier = modifier, |
| 12 | + |
| 13 | + modalStateTimeoutInSeconds = 1.0, |
| 14 | + |
| 15 | + modalKeybindings = {}, |
| 16 | + |
| 17 | + inModalState = false, |
| 18 | + |
| 19 | + reset = function(self) |
| 20 | + -- Keep track of the three most recent events. |
| 21 | + self.eventHistory = { |
| 22 | + -- Serialize the event and push it into the history |
| 23 | + push = function(self, event) |
| 24 | + self[3] = self[2] |
| 25 | + self[2] = self[1] |
| 26 | + self[1] = event:asData() |
| 27 | + end, |
| 28 | + |
| 29 | + -- Fetch the event (if any) at the given index |
| 30 | + fetch = function(self, index) |
| 31 | + if self[index] then |
| 32 | + return eventtap.event.newEventFromData(self[index]) |
| 33 | + end |
| 34 | + end |
| 35 | + } |
| 36 | + |
| 37 | + return self |
| 38 | + end, |
| 39 | + |
| 40 | + -- Enable the modal |
| 41 | + -- |
| 42 | + -- Mimics hs.modal:enable() |
| 43 | + enable = function(self) |
| 44 | + self.watcher:start() |
| 45 | + end, |
| 46 | + |
| 47 | + -- Disable the modal |
| 48 | + -- |
| 49 | + -- Mimics hs.modal:disable() |
| 50 | + disable = function(self) |
| 51 | + self.watcher:stop() |
| 52 | + self.watcher:reset() |
| 53 | + end, |
| 54 | + |
| 55 | + -- Temporarily enter the modal state in which the modal's hotkeys are |
| 56 | + -- active. The modal state will terminate after `modalStateTimeoutInSeconds` |
| 57 | + -- or after the first keydown event, whichever comes first. |
| 58 | + -- |
| 59 | + -- Mimics hs.modal.modal:enter() |
| 60 | + enter = function(self) |
| 61 | + self.inModalState = true |
| 62 | + self:entered() |
| 63 | + self.autoExitTimer:setNextTrigger(self.modalStateTimeoutInSeconds) |
| 64 | + end, |
| 65 | + |
| 66 | + -- Exit the modal state in which the modal's hotkey are active |
| 67 | + -- |
| 68 | + -- Mimics hs.modal.modal:exit() |
| 69 | + exit = function(self) |
| 70 | + if not self.inModalState then return end |
| 71 | + |
| 72 | + self.autoExitTimer:stop() |
| 73 | + self.inModalState = false |
| 74 | + self:reset() |
| 75 | + self:exited() |
| 76 | + end, |
| 77 | + |
| 78 | + -- Optional callback for when modal state is entered |
| 79 | + -- |
| 80 | + -- Mimics hs.modal.modal:entered() |
| 81 | + entered = function(self) end, |
| 82 | + |
| 83 | + -- Optional callback for when modal state is exited |
| 84 | + -- |
| 85 | + -- Mimics hs.modal.modal:exited() |
| 86 | + exited = function(self) end, |
| 87 | + |
| 88 | + -- Bind hotkey that will be enabled/disabled as modal state is |
| 89 | + -- entered/exited |
| 90 | + bind = function(self, key, fn) |
| 91 | + self.modalKeybindings[key] = fn |
| 92 | + end, |
| 93 | + } |
| 94 | + |
| 95 | + isNoModifiers = function(flags) |
| 96 | + local isFalsey = function(value) |
| 97 | + return not value |
| 98 | + end |
| 99 | + |
| 100 | + return hs.fnutils.every(flags, isFalsey) |
| 101 | + end |
| 102 | + |
| 103 | + isOnlyModifier = function(flags) |
| 104 | + isPrimaryModiferDown = flags[modifier] |
| 105 | + areOtherModifiersDown = hs.fnutils.some(flags, function(isDown, modifierName) |
| 106 | + local isPrimaryModifier = modifierName == modifier |
| 107 | + return isDown and not isPrimaryModifier |
| 108 | + end) |
| 109 | + |
| 110 | + return isPrimaryModiferDown and not areOtherModifiersDown |
| 111 | + end |
| 112 | + |
| 113 | + isFlagsChangedEvent = function(event) |
| 114 | + return event and event:getType() == events.flagsChanged |
| 115 | + end |
| 116 | + |
| 117 | + isFlagsChangedEventWithNoModifiers = function(event) |
| 118 | + return isFlagsChangedEvent(event) and isNoModifiers(event:getFlags()) |
| 119 | + end |
| 120 | + |
| 121 | + isFlagsChangedEventWithOnlyModifier = function(event) |
| 122 | + return isFlagsChangedEvent(event) and isOnlyModifier(event:getFlags()) |
| 123 | + end |
| 124 | + |
| 125 | + instance.autoExitTimer = hs.timer.new(0, function() instance:exit() end) |
| 126 | + |
| 127 | + instance.watcher = eventtap.new({events.flagsChanged, events.keyDown}, |
| 128 | + function(event) |
| 129 | + -- If we're in the modal state, and we got a keydown event, then trigger |
| 130 | + -- the function associated with the key. |
| 131 | + if (event:getType() == events.keyDown and instance.inModalState) then |
| 132 | + local fn = instance.modalKeybindings[event:getCharacters():lower()] |
| 133 | + |
| 134 | + -- Some actions may take a while to perform (e.g., opening Slack when |
| 135 | + -- it's not yet running). We don't want to keep the modal state active |
| 136 | + -- while we wait for a long-running action to complete. So, we schedule |
| 137 | + -- the action to run in the background so that we can exit the modal |
| 138 | + -- state and let the user go on about their business. |
| 139 | + local delayInSeconds = 0.001 -- 1 millisecond |
| 140 | + hs.timer.doAfter(delayInSeconds, function() |
| 141 | + if fn then fn() end |
| 142 | + end) |
| 143 | + |
| 144 | + instance:exit() |
| 145 | + |
| 146 | + -- Delete the event so that we're the sole consumer of it |
| 147 | + return true |
| 148 | + end |
| 149 | + |
| 150 | + -- Otherwise, determine if this event should cause us to enter the modal |
| 151 | + -- state. |
| 152 | + |
| 153 | + local currentEvent = event |
| 154 | + local lastEvent = instance.eventHistory:fetch(1) |
| 155 | + local secondToLastEvent = instance.eventHistory:fetch(2) |
| 156 | + |
| 157 | + instance.eventHistory:push(currentEvent) |
| 158 | + |
| 159 | + -- If we've observed the following sequence of events, then enter the |
| 160 | + -- modal state: |
| 161 | + -- |
| 162 | + -- 1. No modifiers are down |
| 163 | + -- 2. Modifiers changed, and now only the primary modifier is down |
| 164 | + -- 3. Modifiers changed, and now no modifiers are down |
| 165 | + if (secondToLastEvent == nil or isNoModifiers(secondToLastEvent:getFlags())) and |
| 166 | + isFlagsChangedEventWithOnlyModifier(lastEvent) and |
| 167 | + isFlagsChangedEventWithNoModifiers(currentEvent) then |
| 168 | + |
| 169 | + instance:enter() |
| 170 | + end |
| 171 | + |
| 172 | + -- Let the event propagate |
| 173 | + return false |
| 174 | + end |
| 175 | + ) |
| 176 | + |
| 177 | + return instance:reset() |
| 178 | +end |
| 179 | + |
| 180 | +return modal |
0 commit comments