Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add ? + modifier alias for setting optional modifiers #74

Merged
merged 2 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config/double-tap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { BuildContext } from '../utils/build-context'
import { FromModifierParam } from './modifier'
import {
FromModifierOverloadParam,
FromOptionalModifierParam,
parseFromModifierOverload,
} from '../utils/from-modifier-overload'
import { FromOptionalModifierParam } from '../utils/optional-modifiers'

export const defaultDoubleTapParameters = {
'double_tap.delay_milliseconds': 200,
Expand Down
17 changes: 13 additions & 4 deletions src/config/from.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ test('map()', () => {
modifiers: { optional: ['command'] },
})

expect(map(1, 'optionalAny').to(2).build()[0].from).toEqual({
key_code: '1',
modifiers: { optional: ['any'] },
})
for (const optionalAny of ['optionalAny', '?any', '??'] as const) {
expect(map(1, optionalAny).to(2).build()[0].from).toEqual({
key_code: '1',
modifiers: { optional: ['any'] },
})
}

expect(map(1, { optional: 'any' }).to(2).build()[0].from).toEqual({
key_code: '1',
Expand All @@ -28,6 +30,13 @@ test('map()', () => {
key_code: '1',
modifiers: { optional: ['command', 'option'] },
})

expect(map(1, '?⌘⌥').to(2).build()[0].from).toEqual({
key_code: '1',
modifiers: { optional: ['command', 'option'] },
})

expect(() => map(1, '?⌘⌥x' as any)).toThrow()
})

test('mapConsumerKey()', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/config/from.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { FromConsumerKeyCode } from '../karabiner/consumer-key-code'
import { PointingButton } from '../karabiner/pointing-button'
import {
FromModifierOverloadParam,
FromOptionalModifierParam,
parseFromModifierOverload,
} from '../utils/from-modifier-overload'
import { FromOptionalModifierParam } from '../utils/optional-modifiers'

export type FromKeyParam = FromKeyCode | KeyAlias | NumberKeyValue

Expand Down
2 changes: 1 addition & 1 deletion src/config/simlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import { LayerKeyCode, LayerKeyParam, layerToggleManipulator } from './layer'
import { BuildContext } from '../utils/build-context'
import {
FromModifierOverloadParam,
FromOptionalModifierParam,
parseFromModifierOverload,
} from '../utils/from-modifier-overload'
import { FromModifierParam } from './modifier'
import { FromOptionalModifierParam } from '../utils/optional-modifiers'

export const defaultSimlayerParameters = {
'simlayer.threshold_milliseconds': 200,
Expand Down
2 changes: 1 addition & 1 deletion src/config/simultaneous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { FromKeyCode } from '../karabiner/key-code'
import { FromKeyParam } from './from'
import {
FromModifierOverloadParam,
FromOptionalModifierParam,
parseFromModifierOverload,
} from '../utils/from-modifier-overload'
import { FromModifierParam } from './modifier'
import { FromOptionalModifierParam } from '../utils/optional-modifiers'

/** Start a manipulator with from.simultaneous */
export function mapSimultaneous(
Expand Down
39 changes: 30 additions & 9 deletions src/utils/from-modifier-overload.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { FromModifierParam, parseFromModifierParams } from '../config/modifier'

export type FromOptionalModifierParam =
| 'optionalAny'
| { optional: FromModifierParam }
import {
FromModifierParam,
ModifierParam,
parseFromModifierParams,
parseModifierParam,
} from '../config/modifier'
import {
FromOptionalModifierParam,
isOptionalAnyAlias,
isOptionalModifierAlias,
} from './optional-modifiers'

export type FromModifierOverloadParam =
| FromModifierParam
Expand All @@ -16,14 +22,29 @@ export function parseFromModifierOverload(
) {
if (!mandatoryModifiers) {
return parseFromModifierParams(mandatoryModifiers, optionalModifiers)
} else if (mandatoryModifiers === 'optionalAny') {
}

if (isOptionalAnyAlias(mandatoryModifiers)) {
return parseFromModifierParams('', 'any')
} else if (
}

if (typeof mandatoryModifiers === 'string') {
if (isOptionalModifierAlias(mandatoryModifiers)) {
return parseFromModifierParams(
'',
parseModifierParam(mandatoryModifiers.slice(1) as ModifierParam),
)
} else if (mandatoryModifiers.startsWith('?')) {
throw new Error(`${mandatoryModifiers} is not valid optional alias`)
}
}

if (
typeof mandatoryModifiers === 'object' &&
'optional' in mandatoryModifiers
) {
return parseFromModifierParams('', mandatoryModifiers.optional)
} else {
return parseFromModifierParams(mandatoryModifiers, optionalModifiers)
}

return parseFromModifierParams(mandatoryModifiers, optionalModifiers)
}
29 changes: 29 additions & 0 deletions src/utils/optional-modifiers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { FromModifierParam } from '../config/modifier'
import { ModifierKeyAlias, modifierKeyAliases } from './key-alias'
import { MultiModifierAlias, multiModifierAliases } from './multi-modifier'

const optionalAnyAliases = ['optionalAny', '?any', '??'] as const
export type OptionalAnyAlias = (typeof optionalAnyAliases)[number]

export type OptionalModifierAlias = `?${ModifierKeyAlias | MultiModifierAlias}`

export function isOptionalAnyAlias(src: any): src is OptionalAnyAlias {
if (!src || typeof src !== 'string') return false
return optionalAnyAliases.includes(src as OptionalAnyAlias)
}

export type FromOptionalModifierParam =
| OptionalAnyAlias
| { optional: FromModifierParam }
| OptionalModifierAlias

const optionalModifierAliases = [
...Object.keys(modifierKeyAliases),
...Object.keys(multiModifierAliases),
].map((v) => `?${v}`)

export function isOptionalModifierAlias(
src: string,
): src is OptionalModifierAlias {
return optionalModifierAliases.includes(src)
}
6 changes: 2 additions & 4 deletions src/utils/with-modifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import { Manipulator, Modifier } from '../karabiner/karabiner-config'
import { FromModifierParam } from '../config/modifier'
import { buildManipulators, ManipulatorBuilder } from '../config/manipulator'
import { BuildContext } from './build-context'
import {
FromOptionalModifierParam,
parseFromModifierOverload,
} from './from-modifier-overload'
import { parseFromModifierOverload } from './from-modifier-overload'
import { FromOptionalModifierParam } from './optional-modifiers'

/**
* A high-order function to add modifiers to a group of manipulators
Expand Down