-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from evan-liu/feat/leader-mode
✨ Add layer().leaderMode()
- Loading branch information
Showing
5 changed files
with
262 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
title: layer().leaderMode() | ||
--- | ||
|
||
# Leader Mode | ||
|
||
`layer()` (or `hyperLayer()` / `modifierLayer()`) has a "leader mode", which works | ||
similar to Vim leader keys: The layer stays activated even after the layer key is | ||
released, until one of the action or escape keys is pressed. | ||
|
||
```typescript | ||
hyperLayer('o') | ||
.description('Open App') | ||
.leaderMode() | ||
.notification() // Notification is highly recommanded when use leader mode | ||
.manipulators({ | ||
f: toApp('Finder'), | ||
}) | ||
``` | ||
|
||
## Sticky | ||
|
||
To keep the layer activated after the first action, and only deactivate the layer | ||
when one of the escape keys is pressed, set the `sticky` option. | ||
|
||
|
||
```typescript | ||
hyperLayer('o') | ||
.leaderMode({ sticky: true }) | ||
``` | ||
|
||
## Escape | ||
|
||
By default `escape` and `caps_lock` keys are used to deactivate the leader mode layer. | ||
To use other keys, set the `escape` option. | ||
|
||
```typescript | ||
hyperLayer('o') | ||
.leaderMode({ escape: ['spacebar', 'return_or_enter'] }) | ||
``` |
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { ConditionBuilder } from '../config/condition.ts' | ||
import { FromKeyParam, map } from '../config/from.ts' | ||
import { | ||
FromEvent, | ||
Manipulator, | ||
ToEvent, | ||
} from '../karabiner/karabiner-config.ts' | ||
|
||
export type LeaderModeOptions = { | ||
/** Default ['escape', 'caps_lock']. */ | ||
escape?: FromKeyParam | FromEvent | Array<FromKeyParam | FromEvent> | ||
/** Keep layer on until one of the `escape` keys pressed. */ | ||
sticky?: boolean | ||
} | ||
|
||
export const defaultLeaderModeOptions: LeaderModeOptions = { | ||
escape: ['escape', 'caps_lock'], | ||
} | ||
|
||
export function leaderModeEscape( | ||
keys: LeaderModeOptions['escape'], | ||
ifOn: ConditionBuilder, | ||
toOff: ToEvent[], | ||
): Manipulator[] { | ||
const result: Manipulator[] = [] | ||
if (!keys) return result | ||
|
||
for (const key of Array.isArray(keys) ? keys : [keys]) { | ||
const builder = typeof key === 'object' ? map(key) : map(key) // For TS fn overloads | ||
result.push(...builder.condition(ifOn).to(toOff).build()) | ||
} | ||
|
||
return result | ||
} |