Skip to content
This repository was archived by the owner on Jul 19, 2022. It is now read-only.

Commit 71ff9ff

Browse files
committed
Finder: arrowkeys, clickable areas, and sequences
* Fix an issue where arrow keys used for moving up and down the Finder results would move the cursor in the input (implemented this with a more compositional keyboard event API) * Ensure the names in the results of the Finder have the clickable cursor * Update the keyboard sequence delay to 3 seconds to make it more ergonomic to perform sequence shortcuts
1 parent 8f08e8a commit 71ff9ff

File tree

4 files changed

+69
-23
lines changed

4 files changed

+69
-23
lines changed

src/Finder.elm

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,10 +513,16 @@ view model =
513513
, results
514514
]
515515
)
516+
517+
keyboardEvent =
518+
KeyboardEvent.on KeyboardEvent.Keydown Keydown
519+
|> KeyboardEvent.stopPropagation
520+
|> KeyboardEvent.preventDefaultWhen (\evt -> evt.key == ArrowUp || evt.key == ArrowDown)
521+
|> KeyboardEvent.attach
516522
in
517523
Modal.modal "finder" Close content
518524
-- We stopPropagation such that movement shortcuts, like J or K, for the
519525
-- workspace aren't triggered when in the modal when the use is trying to
520526
-- type those letters into the search field
521-
|> Modal.withAttributes [ KeyboardEvent.stopPropagationOn KeyboardEvent.Keydown Keydown ]
527+
|> Modal.withAttributes [ keyboardEvent ]
522528
|> Modal.view

src/KeyboardShortcut.elm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ collect model key =
115115

116116
decay : Key -> Cmd Msg
117117
decay key =
118-
Process.sleep 2000 |> Task.perform (always (Decay key))
118+
Process.sleep 3000 |> Task.perform (always (Decay key))
119119

120120

121121

src/KeyboardShortcut/KeyboardEvent.elm

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@
4040
module KeyboardShortcut.KeyboardEvent exposing
4141
( KeyboardEvent
4242
, KeyboardEventType(..)
43+
, attach
4344
, decode
4445
, decodeToMsg
4546
, isHoldingModifier
4647
, modifiersHeld
4748
, on
48-
, stopPropagationOn
49+
, preventDefault
50+
, preventDefaultWhen
51+
, stopPropagation
52+
, stopPropagationWhen
4953
, subscribe
5054
)
5155

@@ -116,26 +120,60 @@ modifiersHeld { altKey, ctrlKey, metaKey, shiftKey } =
116120
-- EVENTS
117121

118122

119-
stopPropagationOn : KeyboardEventType -> (KeyboardEvent -> msg) -> Html.Attribute msg
120-
stopPropagationOn keyboardEventType toMsg =
121-
Html.Events.custom (keyboardEventTypeToEventString keyboardEventType)
122-
(decodeToMsg toMsg
123-
|> Decode.andThen
124-
(\msg ->
125-
Decode.succeed
126-
{ message = msg
127-
, stopPropagation = True
128-
, preventDefault = False
129-
}
130-
)
131-
)
132-
133-
134-
on : KeyboardEventType -> (KeyboardEvent -> msg) -> Html.Attribute msg
135-
on eventType toMsg =
136-
Html.Events.on
137-
(keyboardEventTypeToEventString eventType)
138-
(Decode.map toMsg decode)
123+
type alias KeyboardListener msg =
124+
{ keyboardEventType : KeyboardEventType
125+
, toMsg : KeyboardEvent -> msg
126+
, stopPropagation : KeyboardEvent -> Bool
127+
, preventDefault : KeyboardEvent -> Bool
128+
}
129+
130+
131+
on : KeyboardEventType -> (KeyboardEvent -> msg) -> KeyboardListener msg
132+
on keyboardEventType toMsg =
133+
{ keyboardEventType = keyboardEventType
134+
, toMsg = toMsg
135+
, stopPropagation = always False
136+
, preventDefault = always False
137+
}
138+
139+
140+
stopPropagation : KeyboardListener msg -> KeyboardListener msg
141+
stopPropagation listener =
142+
{ listener | stopPropagation = always True }
143+
144+
145+
stopPropagationWhen : (KeyboardEvent -> Bool) -> KeyboardListener msg -> KeyboardListener msg
146+
stopPropagationWhen when listener =
147+
{ listener | stopPropagation = when }
148+
149+
150+
preventDefault : KeyboardListener msg -> KeyboardListener msg
151+
preventDefault listener =
152+
{ listener | preventDefault = always True }
153+
154+
155+
preventDefaultWhen : (KeyboardEvent -> Bool) -> KeyboardListener msg -> KeyboardListener msg
156+
preventDefaultWhen when listener =
157+
{ listener | preventDefault = when }
158+
159+
160+
attach : KeyboardListener msg -> Html.Attribute msg
161+
attach listener =
162+
let
163+
toEventConfig event =
164+
{ message = listener.toMsg event
165+
, stopPropagation = listener.stopPropagation event
166+
, preventDefault = listener.preventDefault event
167+
}
168+
in
169+
Html.Events.custom (keyboardEventTypeToEventString listener.keyboardEventType)
170+
(Decode.map toEventConfig decode)
171+
172+
173+
174+
{--| Note that there's a limitation to Browser.Event in that it does not support
175+
stopPropagation and preventDefault: https://github.com/elm/browser/issues/77,
176+
https://github.com/elm/browser/issues/89 --}
139177

140178

141179
subscribe : KeyboardEventType -> (KeyboardEvent -> msg) -> Sub msg

src/css/main.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,7 @@ button.danger:hover {
17311731
text-overflow: ellipsis;
17321732
max-width: 14rem;
17331733
overflow: hidden;
1734+
cursor: pointer;
17341735
}
17351736

17361737
#finder .definition-match .namespace {
@@ -1739,6 +1740,7 @@ button.danger:hover {
17391740
text-overflow: ellipsis;
17401741
max-width: 14rem;
17411742
overflow: hidden;
1743+
cursor: pointer;
17421744
}
17431745

17441746
#finder .definition-match .namespace .in {

0 commit comments

Comments
 (0)