Skip to content

Commit

Permalink
Add a key for replacing the top layer
Browse files Browse the repository at this point in the history
  • Loading branch information
xs5871 committed Mar 7, 2024
1 parent b0f20e7 commit 68914d1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/en/layers.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ keyboard.modules.append(Layers())

|Key |Description |
|-----------------|--------------------------------------------------------------------------|
|`KC.FD(layer)` |Replaces the top layer |
|`KC.DF(layer)` |Switches the default layer until the next time the keyboard powers off |
|`KC.MO(layer)` |Momentarily activates layer, switches off when you let go |
|`KC.LM(layer, mod)` |As `MO(layer)` but with `mod` active |
Expand Down
19 changes: 14 additions & 5 deletions kmk/modules/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def __init__(
on_press=self._mo_pressed,
on_release=self._mo_released,
)
make_argumented_key(
validator=layer_key_validator, names=('FD',), on_press=self._fd_pressed
)
make_argumented_key(
validator=layer_key_validator, names=('DF',), on_press=self._df_pressed
)
Expand Down Expand Up @@ -79,11 +82,17 @@ def __init__(
on_release=self.ht_released,
)

def _fd_pressed(self, key, keyboard, *args, **kwargs):
'''
Switches the top layer
'''
self.activate_layer(keyboard, key.meta.layer, idx=0)

def _df_pressed(self, key, keyboard, *args, **kwargs):
'''
Switches the default layer
'''
self.activate_layer(keyboard, key.meta.layer, as_default=True)
self.activate_layer(keyboard, key.meta.layer, idx=-1)

def _mo_pressed(self, key, keyboard, *args, **kwargs):
'''
Expand Down Expand Up @@ -132,11 +141,11 @@ def _print_debug(self, keyboard):
if debug.enabled:
debug(f'active_layers={keyboard.active_layers}')

def activate_layer(self, keyboard, layer, as_default=False):
if as_default:
keyboard.active_layers[-1] = layer
else:
def activate_layer(self, keyboard, layer, idx=None):
if idx is None:
keyboard.active_layers.insert(0, layer)
else:
keyboard.active_layers[idx] = layer

if self.combo_layers:
self._activate_combo_layer(keyboard)
Expand Down
11 changes: 9 additions & 2 deletions tests/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,19 @@ def setUp(self):
KC.TG(1),
KC.TO(2),
],
[KC.N1, KC.DF(0), None, KC.TRNS, KC.TO(1)],
[KC.N2, KC.DF(0)],
[KC.N1, KC.DF(0), KC.FD(2), KC.TRNS, KC.TO(1)],
[KC.N2, KC.DF(0), KC.TO(0)],
],
debug_enabled=False,
)

def test_fd_layer(self):
self.kb.test('', [(3, True), (3, False)], [{}])
self.assertEqual(self.kb.keyboard.active_layers, [1, 0])
self.kb.test('', [(2, True), (2, False)], [{}])
self.assertEqual(self.kb.keyboard.active_layers, [2, 0])
self.kb.test('', [(2, True), (2, False)], [{}])

def test_df_layer(self):
self.kb.test(
'',
Expand Down

0 comments on commit 68914d1

Please sign in to comment.