Repository: evan-liu/karabiner.ts
Pull request #71 (2023-05-08 07:52:10)

✨ Add simlayer().options()


Patch:
diff --git a/src/config/simlayer.ts b/src/config/simlayer.ts
index a0bbd3b..18ad38e 100644
--- a/src/config/simlayer.ts
+++ b/src/config/simlayer.ts
@@ -1,6 +1,11 @@
 import { BasicRuleBuilder } from './rule'
 import { buildCondition, ifVar } from './condition'
-import { FromEvent, Rule, ToVariable } from '../karabiner/karabiner-config'
+import {
+  FromEvent,
+  Rule,
+  SimultaneousOptions,
+  ToVariable,
+} from '../karabiner/karabiner-config'
 import { toArray } from '../utils/to-array'
 import { getKeyWithAlias } from '../utils/key-alias'
 import { toSetVar } from './to'
@@ -33,6 +38,12 @@ export class SimlayerRuleBuilder extends BasicRuleBuilder {
   protected readonly keys: LayerKeyCode[]
   protected readonly layerCondition = ifVar(this.varName, this.onValue)
   protected readonly sharedLayerKeys: LayerKeyCode[] = []
+  protected readonly simultaneousOptions: SimultaneousOptions = {
+    detect_key_down_uninterruptedly: true,
+    key_down_order: 'strict',
+    key_up_order: 'strict_inverse',
+    key_up_when: 'any',
+  }
   protected layerModifiers: FromEvent['modifiers'] = { optional: ['any'] }
 
   constructor(
@@ -65,6 +76,12 @@ export class SimlayerRuleBuilder extends BasicRuleBuilder {
     return this
   }
 
+  /** Set simultaneous_options on the simlayer toggle manipulator */
+  public options(v: Partial<SimultaneousOptions>): this {
+    Object.assign(this.simultaneousOptions, v)
+    return this
+  }
+
   /** Enable layer with the same variable and manipulators with this simlayer */
   public enableLayer(...key: LayerKeyParam[]): this {
     key
@@ -131,11 +148,11 @@ export class SimlayerRuleBuilder extends BasicRuleBuilder {
           from: {
             simultaneous: [{ key_code: layerKey }, { key_code: fromKey }],
             simultaneous_options: {
-              detect_key_down_uninterruptedly: true,
-              key_down_order: 'strict',
-              key_up_order: 'strict_inverse',
-              key_up_when: 'any',
-              to_after_key_up: [setVarOff],
+              ...this.simultaneousOptions,
+              to_after_key_up: [
+                ...(this.simultaneousOptions.to_after_key_up || []),
+                setVarOff,
+              ],
             },
             modifiers: this.layerModifiers,
           },


Test patch:
diff --git a/src/config/simlayer.test.ts b/src/config/simlayer.test.ts
index 612d009..a9d7b49 100644
--- a/src/config/simlayer.test.ts
+++ b/src/config/simlayer.test.ts
@@ -1,7 +1,10 @@
 import { expect, test } from 'vitest'
 import { simlayer } from './simlayer'
 import { map } from './from'
-import { BasicManipulator } from '../karabiner/karabiner-config'
+import {
+  BasicManipulator,
+  FromSimultaneousEvent,
+} from '../karabiner/karabiner-config'
 import { toKey, toSetVar } from './to'
 import { ifVar } from './condition'
 
@@ -129,3 +132,14 @@ test('simlayer().modifiers()', () => {
       .build().manipulators[1].from?.modifiers,
   ).toBeUndefined()
 })
+
+test('simlayer().options()', () => {
+  expect(
+    (
+      simlayer('a', 'b')
+        .options({ to_after_key_up: [toKey('x')] })
+        .manipulators([map(1).to(2)])
+        .build().manipulators[1].from as FromSimultaneousEvent
+    ).simultaneous_options?.to_after_key_up?.[0],
+  ).toEqual({ key_code: 'x' })
+})