forked from Blendman974/faceclaw
Configurable custom keybindings for bluetooth keyboards paired with the glasses
This commit is contained in:
1 parent
fd3c26333f
commit
344e140e92
17 files changed
+1203
-100
No files matched your search
@@ -75,6 +75,7 @@ function settingsEnv(initialUrl = '', initialModel = 'openai-compatible', glasse
|
||||
'../font-picker': { uiFontPickerMenuItem: () => ({ label: 'Font' }) },
|
||||
'../notification-filter': { NotificationFilterLayer: class {} },
|
||||
'../keyboard-settings': { keyboardSettingsMenuItem: () => ({ label: 'Bluetooth keyboard' }) },
|
||||
'../keybinding-settings': { keybindingsMenuItem: () => ({ label: 'Keybindings' }) },
|
||||
'../openai-compatible-model-picker': {
|
||||
compatibleModelDiscoveryMenuItem: () => ({ label: 'Discover models' }),
|
||||
},
|
||||
@@ -160,7 +161,7 @@ test('the Keyboard section, with its pairing row, follows the glasses type while
|
||||
// options; whether it shows is asked again on every paint.
|
||||
assert.equal(labels.indexOf('Keyboard'), labels.indexOf('Watch') + 1);
|
||||
const keyboard = sections.find(section => section.label === 'Keyboard');
|
||||
assert.equal(keyboard.items.map(item => item.label).join(), 'Bluetooth keyboard');
|
||||
assert.equal(keyboard.items.map(item => item.label).join(), 'Bluetooth keyboard,Keybindings');
|
||||
assert.equal(keyboard.hidden(), true);
|
||||
glassesType = 'rayneo-io';
|
||||
assert.equal(keyboard.hidden(), true);
|
||||
|
||||
@@ -30,6 +30,9 @@ function fixture() {
|
||||
let now = 1000, nextTask = 0, screenOn = true, shellOptions;
|
||||
const battery = {};
|
||||
const tasks = new Map(), sent = [], previews = [], received = [], errors = [];
|
||||
// The keyboard bridge's service, as the controller sees it: what it was told, and the host it was given.
|
||||
const keyboard = { host: null, calls: [], setHost(host) { this.host = host; }, linkReady() { this.calls.push('ready'); },
|
||||
linkLost(release) { this.calls.push(release ? 'lost+release' : 'lost'); }, retryNow() { this.calls.push('retry'); }, handleUnhandledKey() {} };
|
||||
const boardStats = { starts: 0, stops: 0, paints: 0 };
|
||||
const registered = [], inProcessApps = [];
|
||||
const settings = { lock: true, enabled: true, tap: true, hold: true, tilt: true, duration: 3000, depth: 0 };
|
||||
@@ -165,6 +168,7 @@ function fixture() {
|
||||
'./device-addresses': { loadDeviceAddresses: () => ({ right: 'AA', left: 'BB', ring: '' }) }, './ios-peripheral-identity': { deviceAddressError: () => null },
|
||||
'./wear-remote': { WearRemote: class { schedulePublish() {} } },
|
||||
'./firmware-compat': load('app/g2/firmware-compat.ts', {}), './reconnect-policy': load('app/g2/reconnect-policy.ts', {}),
|
||||
'./keyboard/service': { keyboardService: keyboard },
|
||||
'../apps/launcher': { launcherEntries: () => [] },
|
||||
'../apps/evenhub/installed-apps': {}, '../apps/evenhub/manager': { onPhoneShownChanged: () => () => {}, phoneUiButton: () => null },
|
||||
'../apps/evenhub/updates': {}, '../apps/evenhub': {},
|
||||
@@ -223,7 +227,7 @@ function fixture() {
|
||||
}
|
||||
async function phone(type, origin = 'ring') { controller.gesture(type, origin); await render(); }
|
||||
async function connect() { await controller.connect(); await render(); }
|
||||
return { relay, controller, shell, battery, remoteHost, keyboardChanged: session => shellOptions.onKeyboardInputChanged(session), settings, phoneState, observers,
|
||||
return { relay, controller, shell, battery, remoteHost, keyboard, keyboardChanged: session => shellOptions.onKeyboardInputChanged(session), settings, phoneState, observers,
|
||||
get shellOptions() { return shellOptions; }, registered, inProcessApps,
|
||||
get bridge() { return bridge; }, connect, wear: wearing => bridge.emitWear(wearing), settingsChanged: () => settingsChanged(), sent, previews, received, boardStats, hardware, phone, advance, render };
|
||||
}
|
||||
@@ -348,3 +352,53 @@ test('the switcher can open the Notifications window in the background, once, wi
|
||||
assert.equal(f.shellOptions.openNotificationsWindow().opened, true);
|
||||
assert.equal(f.inProcessApps.length, 2);
|
||||
});
|
||||
|
||||
test('a paired keyboard: the bridge follows the G2 link, and its keys\' gestures go in with physical input', async () => {
|
||||
const f = fixture();
|
||||
await f.connect();
|
||||
// Connected is not enough: the firmware has to be one with the bridge.
|
||||
f.bridge.emitState('connected');
|
||||
assert.ok(!f.keyboard.calls.includes('ready'));
|
||||
f.bridge.listeners.firmware({ leftVersion: '2.3.0.24', rightVersion: '2.3.0.24', extension: 'Faceclaw/38' });
|
||||
assert.ok(!f.keyboard.calls.includes('ready'));
|
||||
f.bridge.listeners.firmware({ leftVersion: '2.3.0.24', rightVersion: '2.3.0.24', extension: 'Faceclaw/39' });
|
||||
assert.equal(f.keyboard.calls.at(-1), 'ready');
|
||||
assert.equal(f.keyboard.host.ready(), true);
|
||||
// The link dropping on its own ends it without a word to the firmware; coming back restores it.
|
||||
f.bridge.emitState('retrying', 'Reconnecting');
|
||||
assert.equal(f.keyboard.calls.at(-1), 'lost'); assert.equal(f.keyboard.host.ready(), false);
|
||||
f.bridge.emitState('connected');
|
||||
assert.equal(f.keyboard.calls.at(-1), 'ready');
|
||||
|
||||
// A key's gesture is ordinary input from there on, tagged with the device it stands in for.
|
||||
f.received.length = 0;
|
||||
await f.keyboard.host.gesture('swipe-left', 'watch'); await f.keyboard.host.gesture('click', 'ring');
|
||||
await f.keyboard.host.gesture('long-press', 'watch');
|
||||
assert.deepEqual(f.received.map(input => `${input.type}/${input.source}`),
|
||||
['swipe-left/watch', 'click/ring', 'long-press/watch', 'long-press-release/watch']);
|
||||
// A dark display: the bridge stops looking for an absent keyboard, a key wakes it, and it looks again.
|
||||
f.shell.sleep();
|
||||
assert.equal(f.keyboard.host.shouldReconnect(), false);
|
||||
f.keyboard.calls.length = 0; f.received.length = 0;
|
||||
f.keyboard.host.wake(); await f.render();
|
||||
assert.deepEqual(f.received.map(input => input.type), ['display-wake']);
|
||||
assert.equal(f.shell.isScreenOn(), true); assert.ok(f.keyboard.calls.includes('retry'));
|
||||
assert.equal(f.keyboard.host.shouldReconnect(), true);
|
||||
|
||||
// Disconnecting on purpose lets the keyboard go while the link is still up.
|
||||
f.keyboard.calls.length = 0;
|
||||
await f.controller.disconnect();
|
||||
assert.equal(f.keyboard.calls[0], 'lost+release');
|
||||
assert.equal(f.keyboard.host.ready(), false);
|
||||
});
|
||||
|
||||
test('a keyboard needs a G2: other glasses never bring the bridge up', async () => {
|
||||
glassesType = 'g1';
|
||||
try {
|
||||
const f = fixture();
|
||||
await f.connect();
|
||||
f.bridge.emitState('connected');
|
||||
f.bridge.listeners.firmware({ leftVersion: 'g1v0 0.5.66', rightVersion: 'g1v0 0.5.66', extension: 'Faceclaw/39' });
|
||||
assert.ok(!f.keyboard.calls.includes('ready'), f.keyboard.calls.join());
|
||||
} finally { glassesType = 'g2'; }
|
||||
});
|
||||
@@ -0,0 +1,160 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const vm = require('node:vm');
|
||||
const ts = require('typescript');
|
||||
|
||||
function load(file, modules) {
|
||||
const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
|
||||
const context = { exports: {}, console, require(name) { assert.ok(name in modules, name); return modules[name]; } };
|
||||
vm.runInNewContext(ts.transpileModule(source, {
|
||||
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
|
||||
}).outputText, context);
|
||||
return context.exports;
|
||||
}
|
||||
|
||||
const graphics = require('../.test-build/app/graphics/image.js');
|
||||
const metrics = require('../.test-build/app/ui/metrics.js');
|
||||
const menuCore = require('../.test-build/app/ui/menu-core.js');
|
||||
const bindings = require('../.test-build/app/g2/keyboard/bindings.js');
|
||||
const { BdfFont } = load('app/graphics/bdffont.ts', { '@nativescript/core': {} });
|
||||
const font = BdfFont.parse(fs.readFileSync(path.join(__dirname, '../app/fonts/terminus/ter-u12n.bdf'), 'utf8'));
|
||||
const textwrap = load('app/graphics/textwrap.ts', {});
|
||||
const fonts = { getDefaultSmallFont: () => font };
|
||||
const menu = load('app/ui/menu.ts', {
|
||||
'./menu-animation-pref': require('../.test-build/app/ui/menu-animation-pref.js'), './animation-speed': require('../.test-build/app/ui/animation-speed.js'), '../native/settings-store': { getStringSetting: (_key, fallback) => fallback }, '../graphics/screen-size': { screenWidth: () => 640, screenHeight: () => 480 },
|
||||
'../graphics/image': graphics, '../graphics/textwrap': textwrap, '../graphics/ui-fonts': fonts,
|
||||
'../util/numeric-util': { clamp: (n, lo, hi) => Math.max(lo, Math.min(hi, n)) }, './gestures': {},
|
||||
'./metrics': metrics, './menu-core': menuCore,
|
||||
});
|
||||
|
||||
/** The settings menu over a fake keyboard service and layer stack. */
|
||||
function fixture({ connected = true, stored = [], room = true } = {}) {
|
||||
const service = {
|
||||
list: [...stored], capture: null, cancelled: 0,
|
||||
bridge: { isConnected: () => connected },
|
||||
keyBindings() { return this.list; },
|
||||
setKeyBinding(chord, action) {
|
||||
if (!room) return false;
|
||||
const id = bindings.chordId(chord);
|
||||
const index = this.list.findIndex(b => bindings.chordId(b.chord) === id);
|
||||
const next = [...this.list];
|
||||
if (index < 0) next.push({ chord, action }); else next[index] = { chord, action };
|
||||
this.list = next;
|
||||
return true;
|
||||
},
|
||||
removeKeyBinding(chord) { this.list = this.list.filter(b => bindings.chordId(b.chord) !== bindings.chordId(chord)); },
|
||||
captureNextKey(onChord) { this.capture = onChord; return () => { if (this.capture === onChord) { this.capture = null; this.cancelled++; } }; },
|
||||
};
|
||||
class TextViewerLayer { constructor(text, title) { this.text = text; this.title = title; } handleInput(event, ctx) { if (event.type === 'double-click') ctx.stack.pop(); } }
|
||||
const { keybindingsMenuItem } = load('app/ui/keybinding-settings.ts', {
|
||||
'../graphics/ui-fonts': fonts, '../g2/keyboard/bindings': bindings, '../g2/keyboard/service': { keyboardService: service },
|
||||
'../apps/files/text-viewer': { TextViewerLayer }, './menu': menu,
|
||||
});
|
||||
const layers = [{ base: true }];
|
||||
let renders = 0;
|
||||
const removed = layer => layer.onRemoved?.();
|
||||
const ctx = {
|
||||
actions: { requestRender: () => { renders++; } },
|
||||
stack: {
|
||||
getBaseSize: () => ({ width: 576, height: 260, padding: { top: 0, right: 0, bottom: 0, left: 0 } }), isFocused: () => true,
|
||||
push: layer => { layers.push(layer); },
|
||||
pop: () => { if (layers.length > 1) removed(layers.pop()); },
|
||||
popThrough: layer => {
|
||||
const index = layers.indexOf(layer);
|
||||
if (index <= 0) return false;
|
||||
for (const gone of layers.splice(index).reverse()) removed(gone);
|
||||
return true;
|
||||
},
|
||||
},
|
||||
};
|
||||
const top = () => layers[layers.length - 1];
|
||||
const input = type => top().handleInput({ type, timestampMs: 0 }, ctx);
|
||||
const labels = () => top().items.map(item => item.label);
|
||||
/** Move the highlight of the menu on top to the row with this label, and select it. */
|
||||
async function choose(label) {
|
||||
const layer = top();
|
||||
for (let i = 0; i <= layer.items.length && layer.selectedItem?.label !== label; i++) await input('scroll-down');
|
||||
assert.equal(layer.selectedItem?.label, label, `${label} in ${labels().join(' | ')}`);
|
||||
await input('click');
|
||||
}
|
||||
const item = keybindingsMenuItem();
|
||||
return { service, item, ctx, layers, top, input, labels, choose, renders: () => renders,
|
||||
open: () => item.onSelect(ctx), chord: (code, mods = {}) => ({ code, shift: false, ctrl: false, alt: false, meta: false, ...mods }) };
|
||||
}
|
||||
|
||||
test('adding a keybinding: the key is read from the keyboard, then an action is picked', async () => {
|
||||
const f = fixture();
|
||||
f.open();
|
||||
assert.deepEqual(f.labels(), ['Add keybinding', 'Default keys']);
|
||||
await f.choose('Add keybinding');
|
||||
assert.ok(f.service.capture, 'waiting for a key');
|
||||
assert.match(f.top().body ?? f.top().constructor.name, /Press the key|KeyCaptureLayer/);
|
||||
f.service.capture(f.chord('AudioVolumeUp'));
|
||||
// The waiting page is gone and the picker for that key is up, with every action in it.
|
||||
assert.equal(f.layers.length, 3); assert.equal(f.top().title, 'Volume Up');
|
||||
assert.deepEqual(f.labels(), ['Select', 'Back', 'Swipe up', 'Swipe down', 'Swipe left', 'Swipe right',
|
||||
'Scroll up', 'Scroll down', 'System menu', 'App menu', 'Nothing']);
|
||||
await f.choose('Scroll up');
|
||||
assert.deepEqual(f.service.list, [{ chord: f.chord('AudioVolumeUp'), action: 'scroll-up' }]);
|
||||
// Back on the list, on the new row.
|
||||
assert.equal(f.layers.length, 2);
|
||||
assert.deepEqual(f.labels(), ['Volume Up', 'Add keybinding', 'Default keys']);
|
||||
assert.equal(f.top().selectedItem.label, 'Volume Up');
|
||||
assert.ok(f.renders() > 0);
|
||||
});
|
||||
|
||||
test('a keybinding can be changed or removed, and a chord is one keybinding however often it is added', async () => {
|
||||
const f = fixture();
|
||||
f.service.list = [{ chord: f.chord('Escape', { shift: true }), action: 'none' }, { chord: f.chord('KeyQ', { ctrl: true }), action: 'long-press' }];
|
||||
f.open();
|
||||
assert.deepEqual(f.labels(), ['Shift+Esc', 'Ctrl+Q', 'Add keybinding', 'Default keys']);
|
||||
await f.choose('Ctrl+Q');
|
||||
// The picker opens on the current action, marks it, and offers removal.
|
||||
assert.equal(f.top().selectedItem.label, 'System menu *'); assert.equal(f.labels().at(-1), 'Remove keybinding');
|
||||
await f.choose('App menu');
|
||||
assert.equal(f.service.list[1].action, 'short-then-long-press'); assert.equal(f.service.list.length, 2);
|
||||
// Adding the same chord again edits it rather than making a second one.
|
||||
await f.choose('Add keybinding');
|
||||
f.service.capture(f.chord('KeyQ', { ctrl: true }));
|
||||
assert.equal(f.top().selectedItem.label, 'App menu *');
|
||||
await f.choose('Back');
|
||||
assert.deepEqual(f.service.list.map(b => b.action), ['none', 'double-click']);
|
||||
await f.choose('Shift+Esc');
|
||||
await f.choose('Remove keybinding');
|
||||
assert.deepEqual(f.labels(), ['Ctrl+Q', 'Add keybinding', 'Default keys']);
|
||||
});
|
||||
|
||||
test('backing out of the wait calls the capture off; with no keyboard there is nothing to wait for', async () => {
|
||||
const f = fixture();
|
||||
f.open();
|
||||
await f.choose('Add keybinding');
|
||||
await f.input('double-click');
|
||||
assert.equal(f.service.capture, null); assert.equal(f.service.cancelled, 1); assert.equal(f.layers.length, 2);
|
||||
// A key pressed after that is not taken for a keybinding.
|
||||
assert.deepEqual(f.service.list, []);
|
||||
|
||||
const away = fixture({ connected: false });
|
||||
away.open();
|
||||
await away.choose('Add keybinding');
|
||||
assert.equal(away.service.capture, null);
|
||||
assert.match(away.top().body, /No keyboard is connected/);
|
||||
await away.input('double-click');
|
||||
assert.equal(away.layers.length, 2);
|
||||
});
|
||||
|
||||
test('the defaults are a page of their own, and a full list says so', async () => {
|
||||
const f = fixture();
|
||||
f.open();
|
||||
await f.choose('Default keys');
|
||||
assert.equal(f.top().title, 'Default keys');
|
||||
for (const phrase of ['Arrow keys', 'Enter', 'Shift+Esc', 'terminal']) assert.ok(f.top().text.includes(phrase), phrase);
|
||||
|
||||
const full = fixture({ room: false });
|
||||
full.open();
|
||||
await full.choose('Add keybinding');
|
||||
full.service.capture(full.chord('F5'));
|
||||
await full.choose('Select');
|
||||
assert.match(full.top().body, /no room/); assert.deepEqual(full.service.list, []);
|
||||
});
|
||||
@@ -0,0 +1,245 @@
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert/strict')
|
||||
const {
|
||||
KEY_ACTIONS, MAX_KEY_BINDINGS, GestureScheme, chordId, chordOf, defaultKeyAction, describeChord, gestureRepeats,
|
||||
isModifierCode, keyActionLabel, keyBindingMap, parseChordId, parseKeyBindings, resolveKey, serializeKeyBindings,
|
||||
} = require('../.test-build/app/g2/keyboard/bindings.js')
|
||||
const { KeyRouter } = require('../.test-build/app/g2/keyboard/key-router.js')
|
||||
|
||||
const key = (k, overrides = {}) => ({
|
||||
phase: 'down', key: k, code: overrides.code ?? k, repeat: false, timestampMs: 0,
|
||||
...overrides, modifiers: { shift: false, ctrl: false, alt: false, meta: false, ...overrides.modifiers },
|
||||
})
|
||||
const chord = (code, mods = {}) => ({ code, shift: false, ctrl: false, alt: false, meta: false, ...mods })
|
||||
const none = new Map()
|
||||
|
||||
test('built-in keys: arrows are the watch\'s swipes, Enter selects, Esc backs out', () => {
|
||||
const action = (k, overrides) => defaultKeyAction(key(k, overrides))
|
||||
assert.deepEqual(action('ArrowLeft'), { action: 'swipe-left', overridesTyping: false })
|
||||
assert.deepEqual(action('ArrowRight'), { action: 'swipe-right', overridesTyping: false })
|
||||
assert.deepEqual(action('ArrowUp'), { action: 'swipe-up', overridesTyping: false })
|
||||
assert.deepEqual(action('ArrowDown'), { action: 'swipe-down', overridesTyping: false })
|
||||
assert.deepEqual(action('Enter'), { action: 'click', overridesTyping: false })
|
||||
assert.deepEqual(action('Enter', { code: 'NumpadEnter' }), { action: 'click', overridesTyping: false })
|
||||
assert.deepEqual(action('Escape'), { action: 'double-click', overridesTyping: false })
|
||||
// With a modifier, Escape means "back" even where keys are being typed.
|
||||
for (const modifier of ['shift', 'ctrl', 'alt']) {
|
||||
assert.deepEqual(action('Escape', { modifiers: { [modifier]: true } }), { action: 'double-click', overridesTyping: true })
|
||||
}
|
||||
assert.equal(action('Escape', { modifiers: { meta: true } }), null)
|
||||
assert.equal(action('ArrowLeft', { modifiers: { ctrl: true } }), null)
|
||||
assert.equal(action('Enter', { modifiers: { alt: true } }), null)
|
||||
for (const k of ['a', ' ', 'Backspace', 'Tab', 'F1', 'AudioVolumeUp']) assert.equal(action(k), null)
|
||||
})
|
||||
|
||||
test('chords: canonical ids round-trip, and read naturally', () => {
|
||||
const c = chord('Escape', { shift: true, ctrl: true })
|
||||
assert.equal(chordId(c), 'Ctrl+Shift+Escape'); assert.deepEqual(parseChordId('Ctrl+Shift+Escape'), c)
|
||||
assert.deepEqual(parseChordId('ArrowLeft'), chord('ArrowLeft'))
|
||||
assert.deepEqual(chordOf(key('A', { code: 'KeyA', modifiers: { shift: true } })), chord('KeyA', { shift: true }))
|
||||
for (const bad of ['', 'Shift+', 'Shift+Shift+KeyA', 'Hyper+KeyA', 'ShiftLeft', 'Ctrl+ControlRight', 'Key A', '+'.repeat(3)]) {
|
||||
assert.equal(parseChordId(bad), null, bad)
|
||||
}
|
||||
assert.equal(isModifierCode('AltRight'), true); assert.equal(isModifierCode('CapsLock'), false)
|
||||
const said = (code, mods) => describeChord(chord(code, mods))
|
||||
assert.equal(said('Escape', { shift: true }), 'Shift+Esc'); assert.equal(said('ArrowLeft'), 'Left')
|
||||
assert.equal(said('KeyA', { ctrl: true, alt: true }), 'Ctrl+Alt+A'); assert.equal(said('Digit7'), '7')
|
||||
assert.equal(said('AudioVolumeUp'), 'Volume Up'); assert.equal(said('MediaPlayPause'), 'Play/Pause')
|
||||
assert.equal(said('NumpadEnter'), 'Numpad Enter'); assert.equal(said('F5'), 'F5'); assert.equal(said('Slash'), '/')
|
||||
// A remote's unnamed buttons keep the only name they have.
|
||||
assert.equal(said('Consumer1cb'), 'Media key 0x1cb'); assert.equal(said('Usagea5'), 'Key 0xa5')
|
||||
})
|
||||
|
||||
test('custom keybindings survive storage; junk in storage is dropped', () => {
|
||||
const bindings = [
|
||||
{ chord: chord('AudioVolumeUp'), action: 'scroll-up' },
|
||||
{ chord: chord('KeyQ', { ctrl: true }), action: 'long-press' },
|
||||
{ chord: chord('ArrowLeft'), action: 'none' },
|
||||
]
|
||||
assert.deepEqual(parseKeyBindings(serializeKeyBindings(bindings)), bindings)
|
||||
assert.deepEqual([...keyBindingMap(bindings)], [['AudioVolumeUp', 'scroll-up'], ['Ctrl+KeyQ', 'long-press'], ['ArrowLeft', 'none']])
|
||||
assert.deepEqual(parseKeyBindings(''), []); assert.deepEqual(parseKeyBindings('{'), []); assert.deepEqual(parseKeyBindings('{"a":1}'), [])
|
||||
assert.deepEqual(parseKeyBindings(JSON.stringify([
|
||||
null, 7, { key: 'KeyA' }, { key: 'KeyA', action: 'explode' }, { key: 'Shift+ShiftLeft', action: 'click' },
|
||||
{ key: 'KeyB', action: 'click' }, { key: 'KeyB', action: 'double-click' },
|
||||
])), [{ chord: chord('KeyB'), action: 'double-click' }])
|
||||
const many = Array.from({ length: 200 }, (_, i) => ({ key: `F${i + 1}`, action: 'click' }))
|
||||
assert.equal(parseKeyBindings(JSON.stringify(many)).length, MAX_KEY_BINDINGS)
|
||||
// Every action has a label for the picker, and the list has no duplicates.
|
||||
assert.equal(new Set(KEY_ACTIONS.map(a => a.action)).size, KEY_ACTIONS.length)
|
||||
assert.equal(keyActionLabel('double-click'), 'Back'); assert.equal(keyActionLabel('none'), 'Nothing')
|
||||
})
|
||||
|
||||
test('resolution: custom first and everywhere, then the built-ins; "Nothing" only removes the built-in', () => {
|
||||
const custom = keyBindingMap([
|
||||
{ chord: chord('AudioVolumeUp'), action: 'scroll-up' },
|
||||
{ chord: chord('ArrowLeft'), action: 'double-click' },
|
||||
{ chord: chord('ArrowRight'), action: 'none' },
|
||||
{ chord: chord('Escape', { shift: true }), action: 'none' },
|
||||
])
|
||||
// Unbound keys: typed where something takes keys, otherwise the built-in.
|
||||
assert.deepEqual(resolveKey(key('a', { code: 'KeyA' }), custom), { consume: false, gesture: null })
|
||||
assert.deepEqual(resolveKey(key('ArrowUp'), custom), { consume: false, gesture: 'swipe-up' })
|
||||
assert.deepEqual(resolveKey(key('Escape'), custom), { consume: false, gesture: 'double-click' })
|
||||
assert.deepEqual(resolveKey(key('Escape', { modifiers: { ctrl: true } }), custom), { consume: true, gesture: 'double-click' })
|
||||
// Custom: spoken for even in a terminal.
|
||||
assert.deepEqual(resolveKey(key('AudioVolumeUp'), custom), { consume: true, gesture: 'scroll-up' })
|
||||
assert.deepEqual(resolveKey(key('ArrowLeft'), custom), { consume: true, gesture: 'double-click' })
|
||||
// The chord is exact: Shift+Left is not the Left keybinding.
|
||||
assert.deepEqual(resolveKey(key('ArrowLeft', { modifiers: { shift: true } }), custom), { consume: false, gesture: 'swipe-left' })
|
||||
// "Nothing": no navigation, still typed; and it can take a built-in override away.
|
||||
assert.deepEqual(resolveKey(key('ArrowRight'), custom), { consume: false, gesture: null })
|
||||
assert.deepEqual(resolveKey(key('Escape', { modifiers: { shift: true } }), custom), { consume: false, gesture: null })
|
||||
// Held keys: moving repeats, the rest acts once but stays spoken for.
|
||||
assert.deepEqual(resolveKey(key('AudioVolumeUp', { repeat: true }), custom), { consume: true, gesture: 'scroll-up' })
|
||||
assert.deepEqual(resolveKey(key('ArrowLeft', { repeat: true }), custom), { consume: true, gesture: null })
|
||||
assert.deepEqual(resolveKey(key('ArrowDown', { repeat: true }), none), { consume: false, gesture: 'swipe-down' })
|
||||
assert.deepEqual(resolveKey(key('ArrowRight', { repeat: true }), none), { consume: false, gesture: null })
|
||||
assert.deepEqual(resolveKey(key('Enter', { repeat: true }), none), { consume: false, gesture: null })
|
||||
assert.deepEqual(['swipe-left', 'swipe-right', 'click', 'double-click', 'long-press'].filter(gestureRepeats), [])
|
||||
})
|
||||
|
||||
test('taps go with whichever scheme the keyboard last moved by', () => {
|
||||
const scheme = new GestureScheme()
|
||||
assert.equal(scheme.originFor('click'), 'watch')
|
||||
assert.equal(scheme.originFor('scroll-down'), 'ring'); assert.equal(scheme.originFor('click'), 'ring')
|
||||
assert.equal(scheme.originFor('double-click'), 'ring'); assert.equal(scheme.originFor('long-press'), 'ring')
|
||||
assert.equal(scheme.originFor('swipe-left'), 'watch'); assert.equal(scheme.originFor('click'), 'watch')
|
||||
assert.equal(scheme.originFor('short-then-long-press'), 'watch')
|
||||
})
|
||||
|
||||
function router(options = {}) {
|
||||
const state = {
|
||||
ready: true, locked: false, screenOn: true, windowTakesKeys: false, now: 1000,
|
||||
bindings: keyBindingMap(options.bindings ?? []), gestures: [], offered: [], wakes: 0, activity: 0, pending: [],
|
||||
hold: false,
|
||||
}
|
||||
const r = new KeyRouter({
|
||||
ready: () => state.ready, locked: () => state.locked, screenOn: () => state.screenOn,
|
||||
wake: () => { state.wakes++ },
|
||||
offerToWindow: event => { state.offered.push(`${event.phase}:${event.key}`); return state.windowTakesKeys },
|
||||
gesture: (gesture, origin) => {
|
||||
state.gestures.push(`${gesture}/${origin}`)
|
||||
return state.hold ? new Promise(resolve => state.pending.push(resolve)) : Promise.resolve()
|
||||
},
|
||||
bindings: () => state.bindings, noteActivity: () => { state.activity++ }, log: () => {}, now: () => state.now,
|
||||
})
|
||||
return { r, state }
|
||||
}
|
||||
const up = (k, overrides) => key(k, { ...overrides, phase: 'up' })
|
||||
const flush = () => new Promise(resolve => setImmediate(resolve))
|
||||
|
||||
test('router: navigation where nothing takes keys, typing where something does, Shift+Esc out of either', () => {
|
||||
const { r, state } = router()
|
||||
r.handleKey(key('ArrowLeft')); r.handleKey(up('ArrowLeft')); r.handleKey(key('Enter')); r.handleKey(key('Escape'))
|
||||
assert.deepEqual(state.gestures, ['swipe-left/watch', 'click/watch', 'double-click/watch'])
|
||||
// A terminal session: the same keys are its own, Escape included.
|
||||
state.windowTakesKeys = true; state.gestures.length = 0; state.offered.length = 0
|
||||
for (const k of ['ArrowLeft', 'Enter', 'Escape', 'a']) { r.handleKey(key(k)); r.handleKey(up(k)) }
|
||||
assert.deepEqual(state.gestures, [])
|
||||
assert.deepEqual(state.offered, ['down:ArrowLeft', 'up:ArrowLeft', 'down:Enter', 'up:Enter', 'down:Escape', 'up:Escape', 'down:a', 'up:a'])
|
||||
// Shift+Esc (or Ctrl, or Alt) backs out, and neither its press nor its release is typed.
|
||||
state.offered.length = 0
|
||||
for (const modifier of ['shift', 'ctrl', 'alt']) {
|
||||
r.handleKey(key('Escape', { modifiers: { [modifier]: true } })); r.handleKey(up('Escape'))
|
||||
}
|
||||
assert.deepEqual(state.gestures, ['double-click/watch', 'double-click/watch', 'double-click/watch'])
|
||||
assert.deepEqual(state.offered, [])
|
||||
})
|
||||
|
||||
test('router: a key the window hands back gets its navigation meaning; one it kept does not', () => {
|
||||
const { r, state } = router()
|
||||
state.windowTakesKeys = true
|
||||
r.handleKey(key('ArrowDown'))
|
||||
assert.deepEqual(state.gestures, [])
|
||||
// The terminal was showing its session list: the key comes back.
|
||||
r.handleUnhandledKey(key('ArrowDown'))
|
||||
assert.deepEqual(state.gestures, ['swipe-down/watch'])
|
||||
r.handleUnhandledKey(key('a')); r.handleUnhandledKey(up('ArrowDown'))
|
||||
assert.equal(state.gestures.length, 1)
|
||||
// Stale by the time it comes back: the display went dark, or locked.
|
||||
state.screenOn = false; r.handleUnhandledKey(key('ArrowDown'))
|
||||
state.screenOn = true; state.locked = true; r.handleUnhandledKey(key('ArrowDown'))
|
||||
assert.equal(state.gestures.length, 1); assert.equal(state.wakes, 0)
|
||||
})
|
||||
|
||||
test('router: custom keybindings work in a terminal too, and "Nothing" leaves the key to be typed', () => {
|
||||
const { r, state } = router({ bindings: [
|
||||
{ chord: chord('AudioVolumeUp'), action: 'scroll-up' },
|
||||
{ chord: chord('AudioVolumeDown'), action: 'scroll-down' },
|
||||
{ chord: chord('MediaPlayPause'), action: 'click' },
|
||||
{ chord: chord('ArrowLeft'), action: 'none' },
|
||||
] })
|
||||
state.windowTakesKeys = true
|
||||
r.handleKey(key('AudioVolumeDown')); r.handleKey(up('AudioVolumeDown')); r.handleKey(key('MediaPlayPause')); r.handleKey(up('MediaPlayPause'))
|
||||
// A remote bound to scroll is a ring: its select is a ring's tap.
|
||||
assert.deepEqual(state.gestures, ['scroll-down/ring', 'click/ring']); assert.deepEqual(state.offered, [])
|
||||
r.handleKey(key('ArrowLeft'))
|
||||
assert.deepEqual(state.offered, ['down:ArrowLeft'])
|
||||
// And where nothing takes keys, "Nothing" is nothing.
|
||||
state.windowTakesKeys = false; state.gestures.length = 0
|
||||
r.handleKey(key('ArrowLeft')); r.handleUnhandledKey(key('ArrowLeft'))
|
||||
assert.deepEqual(state.gestures, [])
|
||||
})
|
||||
|
||||
test('router: a dark display only wakes, a locked one stays locked, and nothing happens off the glasses', () => {
|
||||
const { r, state } = router()
|
||||
state.screenOn = false
|
||||
r.handleKey(key('a')); r.handleKey(key('a', { repeat: true })); r.handleKey(up('a'))
|
||||
assert.equal(state.wakes, 1); assert.deepEqual(state.gestures, []); assert.deepEqual(state.offered, [])
|
||||
state.locked = true
|
||||
r.handleKey(key('Enter'))
|
||||
assert.equal(state.wakes, 2)
|
||||
state.screenOn = true
|
||||
r.handleKey(key('Enter')); r.handleKey(key('Escape', { modifiers: { shift: true } }))
|
||||
assert.equal(state.wakes, 2); assert.deepEqual(state.gestures, [])
|
||||
state.locked = false; state.ready = false
|
||||
r.handleKey(key('Enter'))
|
||||
assert.deepEqual(state.gestures, [])
|
||||
})
|
||||
|
||||
test('router: a held key repeats movement at a rate the display can follow, and never queues behind itself', async () => {
|
||||
const { r, state } = router()
|
||||
r.handleKey(key('ArrowDown'))
|
||||
for (let i = 1; i <= 20; i++) { await flush(); state.now = 1400 + i * 50; r.handleKey(key('ArrowDown', { repeat: true })) }
|
||||
// Twenty repeats a second arrive; every other one is acted on.
|
||||
assert.equal(state.gestures.length, 1 + 10)
|
||||
// While a gesture is still being handled, repeats are dropped rather than queued.
|
||||
state.gestures.length = 0; state.hold = true; state.now += 1000
|
||||
r.handleKey(key('ArrowUp'))
|
||||
for (let i = 1; i <= 5; i++) { state.now += 200; r.handleKey(key('ArrowUp', { repeat: true })) }
|
||||
assert.equal(state.gestures.length, 1)
|
||||
state.pending.splice(0).forEach(resolve => resolve()); await flush()
|
||||
state.now += 200; r.handleKey(key('ArrowUp', { repeat: true }))
|
||||
assert.equal(state.gestures.length, 2)
|
||||
// A fresh press is never dropped, even on the heels of another gesture.
|
||||
r.handleKey(up('ArrowUp')); r.handleKey(key('Enter'))
|
||||
assert.equal(state.gestures.at(-1), 'click/watch')
|
||||
})
|
||||
|
||||
test('router: capturing a key for a new keybinding takes the chord and nothing else happens', () => {
|
||||
const { r, state } = router()
|
||||
const captured = []
|
||||
r.captureNextKey(c => captured.push(c))
|
||||
// Modifiers alone are waited through; the key pressed with them is the chord.
|
||||
r.handleKey(key('Shift', { code: 'ShiftLeft', modifiers: { shift: true } }))
|
||||
assert.deepEqual(captured, [])
|
||||
r.handleKey(key('Escape', { modifiers: { shift: true } }))
|
||||
assert.deepEqual(captured, [chord('Escape', { shift: true })])
|
||||
// Its release, and the modifier's, do nothing; the next key is ordinary again.
|
||||
r.handleKey(up('Escape')); r.handleKey(up('Shift', { code: 'ShiftLeft' }))
|
||||
assert.deepEqual(state.gestures, []); assert.ok(state.activity >= 2)
|
||||
r.handleKey(key('Enter'))
|
||||
assert.deepEqual(state.gestures, ['click/watch'])
|
||||
// Called off: keys go back to meaning what they mean.
|
||||
const cancel = r.captureNextKey(c => captured.push(c))
|
||||
cancel()
|
||||
r.handleKey(key('ArrowUp'))
|
||||
assert.equal(captured.length, 1); assert.equal(state.gestures.at(-1), 'swipe-up/watch')
|
||||
// A second capture replaces the first, and the first one's cancel can't undo it.
|
||||
const first = r.captureNextKey(() => captured.push('first'))
|
||||
r.captureNextKey(c => captured.push(c))
|
||||
first()
|
||||
r.handleKey(key('x', { code: 'KeyX' }))
|
||||
assert.deepEqual(captured.at(-1), chord('KeyX'))
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert/strict')
|
||||
const { KeyTranslator, translateUsage, navigationGestureForKey } = require('../.test-build/app/g2/keyboard/keys.js')
|
||||
const { KeyTranslator, translateUsage } = require('../.test-build/app/g2/keyboard/keys.js')
|
||||
const { encodeTerminalKey, terminalScrollbackKey } = require('../.test-build/app/apps/terminal/terminal-keys.js')
|
||||
|
||||
const K = 7
|
||||
@@ -98,19 +98,6 @@ test('reset forgets held keys and modifiers without emitting, and stops the repe
|
||||
assert.deepEqual([events.at(-1).key, events.at(-1).modifiers.shift], ['a', false])
|
||||
})
|
||||
|
||||
test('navigation keys stand in for ring gestures; everything else does nothing', () => {
|
||||
const gesture = (k, extra = {}) => navigationGestureForKey(key({ key: k, text: undefined, ...extra }))
|
||||
assert.equal(gesture('ArrowUp'), 'scroll-up'); assert.equal(gesture('ArrowDown'), 'scroll-down')
|
||||
assert.equal(gesture('Enter'), 'click'); assert.equal(gesture('ArrowRight'), 'click')
|
||||
assert.equal(gesture('Escape'), 'double-click'); assert.equal(gesture('ArrowLeft'), 'double-click')
|
||||
// Holding an arrow keeps scrolling; holding Enter or Escape acts once.
|
||||
assert.equal(gesture('ArrowDown', { repeat: true }), 'scroll-down')
|
||||
assert.equal(gesture('Enter', { repeat: true }), null); assert.equal(gesture('Escape', { repeat: true }), null)
|
||||
assert.equal(gesture('Enter', { phase: 'up' }), null)
|
||||
assert.equal(gesture('Enter', { modifiers: { ctrl: true } }), null)
|
||||
assert.equal(gesture('a'), null); assert.equal(gesture(' '), null); assert.equal(gesture('Backspace'), null)
|
||||
})
|
||||
|
||||
test('terminal: printable keys, Enter, Tab, Backspace, Escape', () => {
|
||||
const enc = (overrides, appCursor = false) => encodeTerminalKey(key(overrides), appCursor)
|
||||
assert.equal(enc({}), 'a')
|
||||
|
||||
@@ -21,6 +21,8 @@
|
||||
"../app/g2/keyboard/client.ts",
|
||||
"../app/g2/keyboard/bridge.ts",
|
||||
"../app/g2/keyboard/keys.ts",
|
||||
"../app/g2/keyboard/bindings.ts",
|
||||
"../app/g2/keyboard/key-router.ts",
|
||||
"../app/apps/terminal/terminal-keys.ts",
|
||||
"../app/phone-ui/ble-bandwidth-meter.ts",
|
||||
"../app/apps/terminal/hub-header.ts",
|
||||
|
||||
Reference in new issue
Block a user