diff --git a/src/config/layer.test.ts b/src/config/layer.test.ts index 6aa1225..90d1dc7 100644 --- a/src/config/layer.test.ts +++ b/src/config/layer.test.ts @@ -32,10 +32,19 @@ test('layer()', () => { expect(manipulators[0]).toEqual({ type: 'basic', from: { key_code: 'a' }, - to: [{ set_variable: { name: 'b-mode', value: 2 } }], - to_after_key_up: [{ set_variable: { name: 'b-mode', value: -1 } }], + to: [ + { set_variable: { name: 'b-mode', value: 2 } }, + { set_variable: { name: '__layer', value: 1 } }, + ], + to_after_key_up: [ + { set_variable: { name: 'b-mode', value: -1 } }, + { set_variable: { name: '__layer', value: 0 } }, + ], to_if_alone: [{ key_code: 'a' }], - conditions: [{ type: 'variable_unless', name: 'b-mode', value: 2 }], + conditions: [ + { type: 'variable_unless', name: 'b-mode', value: 2 }, + { type: 'variable_unless', name: '__layer', value: 1 }, + ], } as BasicManipulator) // Add variable condition to manipulators expect(manipulators[1].conditions).toEqual([ @@ -85,6 +94,7 @@ test('layer() conditions', () => { const manipulators = rule.manipulators as BasicManipulator[] expect(manipulators[0].conditions).toEqual([ { type: 'variable_unless', name: 'b', value: 1 }, + { type: 'variable_unless', name: '__layer', value: 1 }, { type: 'variable_if', name: 'c', value: 1 }, ]) }) @@ -115,10 +125,12 @@ test('multiple layer() by same key ', () => { const manipulator = rules[0].manipulators[0] as BasicManipulator expect(manipulator.to).toEqual([ { set_variable: { name: 'v1', value: 1 } }, + { set_variable: { name: '__layer', value: 1 } }, { set_variable: { name: 'v2', value: 1 } }, ]) expect(manipulator.to_after_key_up).toEqual([ { set_variable: { name: 'v1', value: 0 } }, + { set_variable: { name: '__layer', value: 0 } }, { set_variable: { name: 'v2', value: 0 } }, ]) @@ -137,15 +149,25 @@ test('layer().configKey()', () => { expect(manipulators[0]).toEqual({ type: 'basic', from: { key_code: 'a' }, - to: [{ set_variable: { name: 'v1', value: 1 } }, { key_code: 'b' }], - to_after_key_up: [{ set_variable: { name: 'v1', value: 0 } }], + to: [ + { set_variable: { name: 'v1', value: 1 } }, + { set_variable: { name: '__layer', value: 1 } }, + { key_code: 'b' }, + ], + to_after_key_up: [ + { set_variable: { name: 'v1', value: 0 } }, + { set_variable: { name: '__layer', value: 0 } }, + ], to_if_alone: [{ key_code: 'a' }], to_if_held_down: [{ key_code: 'c' }], to_delayed_action: { to_if_invoked: [{ key_code: 'x' }], to_if_canceled: [{ key_code: 'y' }], }, - conditions: [{ type: 'variable_unless', name: 'v1', value: 1 }], + conditions: [ + { type: 'variable_unless', name: 'v1', value: 1 }, + { type: 'variable_unless', name: '__layer', value: 1 }, + ], }) }) @@ -158,10 +180,19 @@ test('layer().configKey() replaceToIfAlone', () => { expect(manipulators[0]).toEqual({ type: 'basic', from: { key_code: 'caps_lock' }, - to: [{ set_variable: { name: 'v1', value: 1 } }], - to_after_key_up: [{ set_variable: { name: 'v1', value: 0 } }], + to: [ + { set_variable: { name: 'v1', value: 1 } }, + { set_variable: { name: '__layer', value: 1 } }, + ], + to_after_key_up: [ + { set_variable: { name: 'v1', value: 0 } }, + { set_variable: { name: '__layer', value: 0 } }, + ], to_if_alone: [{ key_code: 'b', modifiers: ['command'] }], - conditions: [{ type: 'variable_unless', name: 'v1', value: 1 }], + conditions: [ + { type: 'variable_unless', name: 'v1', value: 1 }, + { type: 'variable_unless', name: '__layer', value: 1 }, + ], }) }) @@ -297,19 +328,19 @@ test('layer() notification', () => { const rule = layer('a').notification().build() const manipulators = rule.manipulators as BasicManipulator[] expect(manipulators.length).toBe(1) - expect(manipulators[0].to?.[1]).toEqual({ + expect(manipulators[0].to?.[2]).toEqual({ set_notification_message: { id: 'layer-layer-a', text: 'Layer - layer-a', }, }) - expect(manipulators[0].to_after_key_up?.[1]).toEqual({ + expect(manipulators[0].to_after_key_up?.[2]).toEqual({ set_notification_message: { id: 'layer-layer-a', text: '' }, }) const ruleB = layer('a').notification('test-b').build() const manipulatorB = ruleB.manipulators[0] as BasicManipulator - expect(manipulatorB.to?.[1]).toEqual({ + expect(manipulatorB.to?.[2]).toEqual({ set_notification_message: { id: 'layer-layer-a', text: 'test-b', @@ -374,7 +405,7 @@ describe('layer() leader mode', () => { // layer toggle expect(manipulators[0].to_after_key_up).toBeUndefined() - expect(manipulators[0].to?.[1]).toEqual( + expect(manipulators[0].to?.[2]).toEqual( toNotificationMessage('layer-v', 'Layer - v'), ) @@ -387,7 +418,7 @@ describe('layer() leader mode', () => { const rule2 = layer('b').notification('Test B').build() const manipulators2 = rule2.manipulators as BasicManipulator[] - expect(manipulators2[0].to?.[1]).toEqual( + expect(manipulators2[0].to?.[2]).toEqual( toNotificationMessage('layer-layer-b', 'Test B'), ) }) diff --git a/src/config/layer.ts b/src/config/layer.ts index 95fdeb7..a65e6b6 100644 --- a/src/config/layer.ts +++ b/src/config/layer.ts @@ -319,14 +319,18 @@ export function layerToggleManipulator( return to } + const layerVarName = '__layer' // Shared by all layers, one layer at a time const manipulator = map({ key_code, modifiers }) .toVar(varName, onValue) - .condition(ifVar(varName, onValue).unless()) + .toVar(layerVarName) + .condition(ifVar(varName, onValue).unless(), ifVar(layerVarName).unless()) if (!modifiers?.mandatory?.length && !leaderMode) { manipulator.toIfAlone({ key_code }) } if (!leaderMode) { - manipulator.toAfterKeyUp(toSetVar(varName, offValue)) + manipulator + .toAfterKeyUp(toSetVar(varName, offValue)) + .toAfterKeyUp(toSetVar(layerVarName, 0)) } if (conditions?.length) { manipulator.condition(...conditions) diff --git a/src/config/simlayer.test.ts b/src/config/simlayer.test.ts index 25f5c97..d8faec5 100644 --- a/src/config/simlayer.test.ts +++ b/src/config/simlayer.test.ts @@ -127,11 +127,18 @@ test('simlayer().enableLayer()', () => { expect(manipulators[0]).toEqual({ type: 'basic', from: { key_code: 'c', modifiers: { optional: ['any'] } }, - to: [{ set_variable: { name: 'b', value: 1 } }], - to_after_key_up: [{ set_variable: { name: 'b', value: 0 } }], + to: [ + { set_variable: { name: 'b', value: 1 } }, + { set_variable: { name: '__layer', value: 1 } }, + ], + to_after_key_up: [ + { set_variable: { name: 'b', value: 0 } }, + { set_variable: { name: '__layer', value: 0 } }, + ], to_if_alone: [{ key_code: 'c' }], conditions: [ { type: 'variable_unless', name: 'b', value: 1 }, + { type: 'variable_unless', name: '__layer', value: 1 }, { type: 'variable_if', name: 'd', value: 1 }, ], })