Skip to content

Commit

Permalink
Added an around-next-timeout button
Browse files Browse the repository at this point in the history
  • Loading branch information
david-janssen committed Sep 29, 2021
1 parent 7b5142b commit 0eacad7
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 15 deletions.
13 changes: 10 additions & 3 deletions keymap/tutorial.kbd
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,12 @@
Shift pressed' or 'being inside some layer' or, less usefully, 'having d
pressed'. It is a more general and powerful version of `layer-next`.

There is also an `around-next-timeout` button that does the same thing as
`around-next`, except that if some other button press is not detected within
some timeout, some other button is tapped. This can be used to create a
leader-key that simply times out (by passing a non-button), or a key that can
still function as a normal key, but also as a leader key when used slowly.

I think expansion of this button-style is probably the future of leader-key,
hydra-style functionality support in KMonad.

Expand All @@ -817,16 +823,17 @@
(defalias
ns (around-next sft) ;; Shift the next press
nnm (around-next @num) ;; Perform next press in numbers layer
nd (around-next d) ;; Silly, but possible
ntm (around-next-timeout 500 sft XX)


)

(deflayer around-next-test
_ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _
@nd _ _ _ _ _ _ _ _ _ _ _ _
@ns _ _ _ _ _ _ _ _ _ _ _ _
@nnm _ _ _ _ _ _ _ _ _ _ _
@ns _ _ _ _ _ _ _
@ntm _ _ _ _ _ _ _
)

#| --------------------------------------------------------------------------
Expand Down
15 changes: 5 additions & 10 deletions keymap/user/david-janssen/testing.kbd
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@
fallthrough true
)

(defsrc
a
b
lsft
enter
)
(defsrc a s d f)

(deflayer layer1
a
(tap-hold-next-release 2000 b lctl)
(around-next lsft)
enter
(around-next-timeout 500 lsft q)
x
y
z
)
2 changes: 2 additions & 0 deletions src/KMonad/App/Parser/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ data DefButton
| KTapHoldNextRelease Int DefButton DefButton
-- ^ Like KTapNextRelease but with a timeout
| KAroundNext DefButton -- ^ Surround a future button
| KAroundNextTimeout Int DefButton DefButton
-- ^ Surround a future button, with some timeout
| KAroundNextSingle DefButton -- ^ Surround a future button
| KMultiTap [(Int, DefButton)] DefButton -- ^ Do things depending on tap-count
| KAround DefButton DefButton -- ^ Wrap 1 button around another
Expand Down
1 change: 1 addition & 0 deletions src/KMonad/Args/Joiner.hs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ joinButton ns als =
-> jst $ tapHoldNextRelease (fi ms) <$> go t <*> go h
KAroundNext b -> jst $ aroundNext <$> go b
KAroundNextSingle b -> jst $ aroundNextSingle <$> go b
KAroundNextTimeout ms b t -> jst $ aroundNextTimeout (fi ms) <$> go b <*> go t
KPause ms -> jst . pure $ onPress (pause ms)
KMultiTap bs d -> jst $ multiTap <$> go d <*> mapM f bs
where f (ms, b) = (fi ms,) <$> go b
Expand Down
1 change: 1 addition & 0 deletions src/KMonad/Args/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ keywordButtons =
, ("layer-delay" , KLayerDelay <$> lexeme numP <*> lexeme word)
, ("layer-next" , KLayerNext <$> lexeme word)
, ("around-next" , KAroundNext <$> buttonP)
, ("around-next-timeout", KAroundNextTimeout <$> lexeme numP <*> buttonP <*> buttonP)
, ("tap-macro"
, KTapMacro <$> lexeme (some buttonP) <*> optional (keywordP "delay" numP))
, ("tap-macro-release"
Expand Down
4 changes: 4 additions & 0 deletions src/KMonad/Args/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ instance Exception PErrors
--
-- Tokens representing different types of buttons

-- FIXME: This is really broken: why are there 2 lists of 'DefButton's? There is
-- one here, and one in Parser/Types.hs

-- | Button ADT
data DefButton
= KRef Text -- ^ Reference a named button
Expand All @@ -91,6 +94,7 @@ data DefButton
| KAroundNextSingle DefButton -- ^ Surround a future button
| KMultiTap [(Int, DefButton)] DefButton -- ^ Do things depending on tap-count
| KAround DefButton DefButton -- ^ Wrap 1 button around another
| KAroundNextTimeout Int DefButton DefButton
| KTapMacro [DefButton] (Maybe Int)
-- ^ Sequence of buttons to tap, possible delay between each press
| KTapMacroRelease [DefButton] (Maybe Int)
Expand Down
19 changes: 18 additions & 1 deletion src/KMonad/Model/Button.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module KMonad.Model.Button
Button
, HasButton(..)
, onPress
, onRelease
, mkButton
, around
, tapOn
Expand All @@ -37,6 +38,7 @@ module KMonad.Model.Button
-- * Button combinators
-- $combinators
, aroundNext
, aroundNextTimeout
, aroundNextSingle
, layerDelay
, layerNext
Expand Down Expand Up @@ -88,7 +90,7 @@ onPress :: AnyK () -> Button
onPress p = mkButton p $ pure ()

onRelease :: AnyK () -> Button
onRelease p = mkButton (pure ()) p
onRelease = mkButton (pure ())

--------------------------------------------------------------------------------
-- $running
Expand Down Expand Up @@ -190,6 +192,21 @@ aroundNext b = onPress $ await isPress $ \e -> do
pure NoCatch
pure NoCatch

-- | A 'Button' that, once pressed, will surround the next button within some timeout with another.
--
-- If some other key is not pressed within an interval another button will be triggered as a tap.
aroundNextTimeout ::
Milliseconds -- ^ How long before we tap
-> Button -- ^ The 'Button' to use to surround next
-> Button -- ^ The 'Button' to tap on timeout
-> Button -- ^ The resulting button
aroundNextTimeout d b t = onPress $ within d (pure isPress) (tap t) $ \trig -> do
runAction $ b^.pressAction
await (isReleaseOf $ trig^.event.keycode) $ \_ -> do
runAction $ b^.releaseAction
pure NoCatch
pure NoCatch

-- | A 'Button' that, once pressed, will surround the next button with another.
--
-- Think of this as, essentially, a tappable mod. For example, an 'aroundNext
Expand Down
2 changes: 1 addition & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resolver: lts-18.9
resolver: lts-18.6
packages:
- .
extra-deps:
Expand Down

0 comments on commit 0eacad7

Please sign in to comment.