Add support for a BLE mouse (ie, the touchpad/mouse buttons in a handheld mouse/keyboard combo like the Rii K28)

This commit is contained in:
James Babcock committed 2026-11-15 23:57:33 -08:00
1 parent e09f8eae96
commit ab692741f7
18 files changed
+901 -57

No files matched your search

+57
View File
@@ -243,3 +243,60 @@ test('router: capturing a key for a new keybinding takes the chord and nothing e
r.handleKey(key('x', { code: 'KeyX' }))
assert.deepEqual(captured.at(-1), chord('KeyX'))
})
test('a mouse by default: left taps, right double taps, middle holds, the wheel is the ring\'s scroll', () => {
const action = (k, overrides) => defaultKeyAction(key(k, overrides))
// Nothing types a mouse button, so these hold in a terminal session too.
assert.deepEqual(action('MouseButton1'), { action: 'click', overridesTyping: true })
assert.deepEqual(action('MouseButton2'), { action: 'double-click', overridesTyping: true })
assert.deepEqual(action('MouseButton3'), { action: 'long-press', overridesTyping: true })
assert.deepEqual(action('WheelUp'), { action: 'scroll-up', overridesTyping: true })
assert.deepEqual(action('WheelDown'), { action: 'scroll-down', overridesTyping: true })
// Whatever is held on the keyboard.
assert.deepEqual(action('MouseButton1', { modifiers: { ctrl: true, shift: true } }), { action: 'click', overridesTyping: true })
// Extra buttons and the horizontal wheel mean nothing until bound.
for (const k of ['MouseButton4', 'MouseButton5', 'WheelLeft', 'WheelRight']) assert.equal(action(k), null)
const said = code => describeChord(chord(code))
assert.equal(said('MouseButton1'), 'Left button'); assert.equal(said('MouseButton2'), 'Right button')
assert.equal(said('MouseButton3'), 'Middle button'); assert.equal(said('MouseButton4'), 'Mouse button 4')
assert.equal(said('WheelUp'), 'Wheel up'); assert.equal(describeChord(chord('WheelDown', { ctrl: true })), 'Ctrl+Wheel down')
assert.deepEqual(parseChordId('Shift+MouseButton2'), chord('MouseButton2', { shift: true }))
})
test('router: the mouse works where keys are being typed, its wheel is paced, and its buttons can be rebound', async () => {
const { r, state } = router({ bindings: [{ chord: chord('MouseButton4'), action: 'short-then-long-press' }, { chord: chord('MouseButton2'), action: 'none' }] })
const wheel = k => { r.handleKey(key(k, { burst: true })); r.handleKey(up(k, { burst: true })) }
state.windowTakesKeys = true
r.handleKey(key('MouseButton1')); r.handleKey(up('MouseButton1'))
r.handleKey(key('MouseButton3')); r.handleKey(up('MouseButton3'))
await flush(); state.now += 500
wheel('WheelDown')
// The wheel is the ring's scroll, and what follows it is the ring's too.
await flush(); state.now += 500
r.handleKey(key('MouseButton1')); r.handleKey(up('MouseButton1'))
assert.deepEqual(state.gestures, ['click/watch', 'long-press/watch', 'scroll-down/ring', 'click/ring'])
assert.deepEqual(state.offered, [], 'never typed')
// A custom keybinding for an extra button; "Nothing" on the right button leaves it inert.
state.gestures.length = 0
r.handleKey(key('MouseButton4')); r.handleKey(up('MouseButton4')); r.handleKey(key('MouseButton2')); r.handleKey(up('MouseButton2'))
assert.deepEqual(state.gestures, ['short-then-long-press/ring'])
// A two-finger scroll reports far faster than the display can follow: every 100 ms at most.
state.gestures.length = 0; state.offered.length = 0
for (let i = 0; i < 40; i++) { await flush(); state.now += 25; wheel('WheelUp') }
assert.equal(state.gestures.length, 10); assert.ok(state.gestures.every(g => g === 'scroll-up/ring'))
// On a dark display a click wakes it and does nothing else.
state.screenOn = false; state.gestures.length = 0
r.handleKey(key('MouseButton1')); r.handleKey(up('MouseButton1'))
assert.equal(state.wakes, 1); assert.deepEqual(state.gestures, [])
})
test('router: a mouse button or a turn of the wheel can be captured for a keybinding', () => {
const { r, state } = router()
const captured = []
r.captureNextKey(c => captured.push(c))
r.handleKey(key('MouseButton2')); r.handleKey(up('MouseButton2'))
r.captureNextKey(c => captured.push(c))
r.handleKey(key('WheelUp', { burst: true, modifiers: { shift: true } })); r.handleKey(up('WheelUp', { burst: true }))
assert.deepEqual(captured, [chord('MouseButton2'), chord('WheelUp', { shift: true })])
assert.deepEqual(state.gestures, [])
})
+82 -8
View File
@@ -1,6 +1,6 @@
const test = require('node:test')
const assert = require('node:assert/strict')
const { HidKeys, parseReportMap, reportKeys } = require('../.test-build/app/g2/keyboard/hid.js')
const { HidKeys, parseReportMap, reportHasKeys, reportHasPointer, reportKeys, reportPointer } = require('../.test-build/app/g2/keyboard/hid.js')
const {
KeyboardRecords, keyboardControl, le16, u16, parseKeyboardAdvert, mergeKeyboardAdverts,
parseKeyboardAddress, formatKeyboardAddress,
@@ -69,6 +69,41 @@ test('descriptor resource bounds and malformed items', () => {
}
})
// A wheel mouse as most are described: three buttons, 8-bit X/Y and wheel.
const mouseMap = Uint8Array.from([0x05,1,0x09,2,0xa1,1,0x09,1,0xa1,0,0x05,9,0x19,1,0x29,3,0x15,0,0x25,1,0x75,1,0x95,3,0x81,2,
0x75,5,0x95,1,0x81,1,0x05,1,0x09,0x30,0x09,0x31,0x09,0x38,0x15,0x81,0x25,0x7f,0x75,8,0x95,3,0x81,6,0xc0,0xc0])
test('a mouse report: buttons, movement and wheel; not keys', () => {
const r = parseReportMap(mouseMap).get(0)
assert.equal(reportHasPointer(r), true); assert.equal(reportHasKeys(r), false)
assert.deepEqual(reportPointer(r, Uint8Array.of(0, 0, 0, 0)), { buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 })
assert.deepEqual(reportPointer(r, Uint8Array.of(0b101, 10, 0xf6, 1)), { buttons: 5, dx: 10, dy: -10, wheel: 1, pan: 0 })
assert.deepEqual(reportPointer(r, Uint8Array.of(0b010, 0, 0, 0xff)), { buttons: 2, dx: 0, dy: 0, wheel: -1, pan: 0 })
// Some devices leave trailing bytes off; what is missing is zero. Extra bytes are ignored.
assert.deepEqual(reportPointer(r, Uint8Array.of(1, 3)), { buttons: 1, dx: 3, dy: 0, wheel: 0, pan: 0 })
assert.deepEqual(reportPointer(r, Uint8Array.of(0, 1, 1, 1, 9, 9)), { buttons: 0, dx: 1, dy: 1, wheel: 1, pan: 0 })
// -128 is outside the declared range: no reading, rather than a jump.
assert.deepEqual(reportPointer(r, Uint8Array.of(0, 0x80, 0, 0)), { buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 })
assert.deepEqual(reportKeys(r, Uint8Array.of(7, 1, 1, 1)), [])
// A keyboard's report is not a pointer's.
assert.equal(reportHasPointer(parseReportMap(boot).get(0)), false)
})
test('a touchpad with 12-bit axes and a horizontal wheel; an absolute digitizer is not a mouse', () => {
// Five buttons, 3 bits of padding, X/Y packed 12 bits each, wheel, then AC Pan from the consumer page.
const map = Uint8Array.from([0x05,1,0x09,2,0xa1,1,0x05,9,0x19,1,0x29,5,0x15,0,0x25,1,0x75,1,0x95,5,0x81,2,
0x75,3,0x95,1,0x81,1,0x05,1,0x09,0x30,0x09,0x31,0x16,0x01,0xf8,0x26,0xff,0x07,0x75,12,0x95,2,0x81,6,
0x09,0x38,0x15,0x81,0x25,0x7f,0x75,8,0x95,1,0x81,6,0x05,0x0c,0x0a,0x38,0x02,0x81,6,0xc0])
const r = parseReportMap(map).get(0)
assert.equal(reportHasPointer(r), true)
// X = 0x123 (291), Y = 0xfff (-1): packed little endian, 12 bits each.
assert.deepEqual(reportPointer(r, Uint8Array.of(0b10000, 0x23, 0xf1, 0xff, 2, 0xfe)), { buttons: 16, dx: 291, dy: -1, wheel: 2, pan: -2 })
// The pan field is on the consumer page, but it is a movement, not a key going down.
assert.equal(reportHasKeys(r), false); assert.deepEqual(reportKeys(r, Uint8Array.of(0, 0, 0, 0, 0, 5)), [])
// Absolute X/Y (a touchscreen, a pen) is something else, and is left alone.
const absolute = parseReportMap(Uint8Array.of(0x05,1,0x09,0x30,0x09,0x31,0x15,0,0x26,0xff,0x0f,0x75,16,0x95,2,0x81,2)).get(0)
assert.equal(reportHasPointer(absolute), false)
assert.deepEqual(reportPointer(absolute, Uint8Array.of(1, 2, 3, 4)), { buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 })
})
test('control records, UUIDs and addresses', () => {
assert.deepEqual([...keyboardControl(4,0x1234,0x5678,[1,2,3,4,5,6,7])], [75,66,1,4,0x34,0x12,0x78,0x56,1,2,3,4,5,6,7])
assert.throws(() => keyboardControl(13,1,1))
@@ -238,23 +273,39 @@ function makeCombo(g) {
g.attrs.clear(); for (const [handle, attr] of sorted) g.attrs.set(handle, attr)
}
for (const declarations of ['short', 'refused']) {
test(`client: a keyboard with a trackpad whose stack answers declaration reads ${declarations} still types`, async () => {
const events = [], logs = []
test(`client: a keyboard with a trackpad whose stack answers declaration reads ${declarations} types and points`, async () => {
const events = [], pointer = [], logs = []
let client
const g = fakeGlasses(payload => client.accept(payload))
makeCombo(g); g.declarations = declarations
client = new KeyboardClient(g.send, { keys: e => events.push(...e), log: line => logs.push(line) })
client = new KeyboardClient(g.send, { keys: e => events.push(...e), pointer: p => pointer.push(p), log: line => logs.push(line) })
await client.enable(); await client.connect(1, new Uint8Array(6)); const config = await client.configure()
// Keys and media keys are subscribed; the pointer and the battery are not.
assert.deepEqual(config.watched, [27,31]); assert.deepEqual(g.watches, [27,31,3]); assert.deepEqual(g.writes, [25,4,28,32])
// Keys, media keys and the trackpad are subscribed; the battery level is not.
assert.deepEqual(config.watched, [27,31,35]); assert.equal(config.pointer, true)
assert.deepEqual(g.watches, [27,31,35,3]); assert.deepEqual(g.writes, [25,4,28,32,36])
assert.deepEqual(config.reports.map(r => [r.handle, r.id]), [[27,1],[31,2],[35,4]])
assert.deepEqual(config.reports.map(r => r.properties), declarations === 'short' ? [0x12,0x12,0x12] : [undefined,undefined,undefined])
g.input(27, [0,0,4,0,0,0,0,0])
assert.deepEqual(events.map(e => [e.usage,e.down]), [[4,true]])
// The trackpad: movement and buttons, and never a key.
g.input(35, [0, 5, 0xfd]); g.input(35, [1, 0, 0]); g.input(35, [5, 0x81, 0x7f])
assert.deepEqual(pointer, [
{ buttons: 0, dx: 5, dy: -3, wheel: 0, pan: 0 },
{ buttons: 1, dx: 0, dy: 0, wheel: 0, pan: 0 },
{ buttons: 5, dx: -127, dy: 127, wheel: 0, pan: 0 },
])
assert.equal(events.length, 1)
// A record lost on the way: the held buttons are let go along with the held keys.
g.seq++; g.status()
assert.deepEqual(pointer.at(-1), { buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 })
assert.deepEqual([events.at(-1).usage, events.at(-1).down], [4, false])
// With nothing held, another gap has nothing to say.
const count = pointer.length; g.seq++; g.status()
assert.equal(pointer.length, count)
// What was skipped, and why, is in the log; so is the table it came from.
assert.ok(logs.some(line => /^gatt 1=2800 2=2803 /.test(line)), logs.join('\n'))
assert.ok(logs.some(line => /report 42 skipped: no Report Reference/.test(line)), logs.join('\n'))
assert.ok(logs.some(line => /report 35 \(id 4\) carries no keys/.test(line)), logs.join('\n'))
assert.ok(logs.some(line => /report 35 \(id 4\) is a pointer's/.test(line)), logs.join('\n'))
assert.ok(logs.some(line => line.startsWith('report map 22: 0501')))
client.close()
})
@@ -313,7 +364,7 @@ test('client: firmware errors carry their code, and a dead link rejects at once'
})
function bridgeSetup(saved = null) {
const host = { keys: [], gone: 0, changes: 0, logs: [], prompts: [], saved, reconnect: true }
const host = { keys: [], pointer: [], gone: 0, changes: 0, logs: [], prompts: [], saved, reconnect: true }
let bridge
const g = fakeGlasses(payload => bridge.accept(payload))
bridge = new KeyboardBridge({
@@ -321,6 +372,7 @@ function bridgeSetup(saved = null) {
loadSaved: () => host.saved,
storeSaved: value => { host.saved = value },
onKeys: events => host.keys.push(...events),
onPointer: sample => host.pointer.push(sample),
onKeyboardGone: () => { host.gone++ },
onChanged: () => {
host.changes++
@@ -479,6 +531,28 @@ test('bridge: a scan started by hand takes over from an attempt in flight, and n
assert.deepEqual(g.ops, [])
bridge.linkLost()
})
test('bridge: a keyboard\'s touchpad is passed on while it is in use, and the snapshot says it has one', async () => {
const plain = bridgeSetup()
plain.bridge.linkReady(); await plain.bridge.connect(K380)
assert.equal(plain.bridge.snapshot().pointer, false)
plain.bridge.linkLost()
const { bridge, g, host } = bridgeSetup()
makeCombo(g)
bridge.linkReady()
assert.equal(bridge.snapshot().pointer, false)
await bridge.connect(K380)
assert.equal(bridge.snapshot().pointer, true)
assert.ok(host.logs.some(line => /keyboard ready: K380, 3 of 3 input reports subscribed, with a pointer/.test(line)), host.logs.join('\n'))
g.input(35, [2, 1, 1])
assert.deepEqual(host.pointer, [{ buttons: 2, dx: 1, dy: 1, wheel: 0, pan: 0 }])
// The keyboard drops with a button down: the button is let go, as held keys
// are, before the host hears the keyboard is gone; and the snapshot follows.
g.close(); await settle()
assert.deepEqual(host.pointer.at(-1), { buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 }); assert.equal(host.pointer.length, 2)
assert.equal(host.gone, 1); assert.equal(bridge.snapshot().pointer, false)
bridge.linkLost()
})
test('bridge: leaving on purpose tells the glasses to let the keyboard go', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] })
const { bridge, g } = bridgeSetup(K380)
+21
View File
@@ -142,3 +142,24 @@ test('terminal: Shift+PageUp and Shift+PageDown page the scrollback instead', ()
assert.equal(page('PageUp', {}), null); assert.equal(page('PageUp', { shift: true, ctrl: true }), null)
assert.equal(page('PageUp', { shift: true }, 'up'), null); assert.equal(page('ArrowUp', { shift: true }), null)
})
test('mouse buttons are keys that neither repeat nor take the repeat from a held key; a wheel detent is a press and release', () => {
const { t, events, advance } = translator()
t.handle([down(A)])
t.handle([{ page: 9, usage: 1, down: true }])
assert.deepEqual([events.at(-1).key, events.at(-1).code, events.at(-1).text], ['MouseButton1', 'MouseButton1', undefined])
// The held key goes on repeating; the held button never does.
advance(500)
assert.ok(events.filter(e => e.repeat).every(e => e.key === 'a')); assert.ok(events.some(e => e.repeat))
t.handle([up(A), { page: 9, usage: 1, down: false }])
assert.deepEqual([events.at(-1).phase, events.at(-1).key], ['up', 'MouseButton1'])
assert.equal(translateUsage(9, 3, false, false).code, 'MouseButton3')
// The wheel: down and up at once, marked as droppable, with the keyboard's modifiers.
events.length = 0
t.handle([down(SHIFT)]); t.pulse('WheelUp')
assert.deepEqual(events.slice(1).map(e => [e.phase, e.key, e.code, e.burst, e.repeat, e.modifiers.shift]),
[['down', 'WheelUp', 'WheelUp', true, false, true], ['up', 'WheelUp', 'WheelUp', true, false, true]])
const count = events.length; advance(2000)
assert.equal(events.length, count)
})
+58
View File
@@ -0,0 +1,58 @@
const test = require('node:test')
const assert = require('node:assert/strict')
const { PointerInput } = require('../.test-build/app/g2/keyboard/pointer.js')
const sample = (buttons, extra = {}) => ({ buttons, dx: 0, dy: 0, wheel: 0, pan: 0, ...extra })
function pointer() {
const out = []
const input = new PointerInput({
buttons: events => out.push(...events.map(e => `${e.down ? 'down' : 'up'}:${e.page}:${e.usage}`)),
wheel: key => out.push(key),
})
return { input, out }
}
test('buttons become key transitions; the wheel a key per report; movement nothing', () => {
const { input, out } = pointer()
input.handle(sample(0, { dx: 40, dy: -3 }))
assert.deepEqual(out, [])
input.handle(sample(0b001)); input.handle(sample(0b001, { dx: 2 })); input.handle(sample(0b110)); input.handle(sample(0))
assert.deepEqual(out, ['down:9:1', 'up:9:1', 'down:9:2', 'down:9:3', 'up:9:2', 'up:9:3'])
out.length = 0
// However many detents a report claims, it is one step; a two-finger scroll sends many.
input.handle(sample(0, { wheel: 1 })); input.handle(sample(0, { wheel: -3 })); input.handle(sample(0, { pan: 2 })); input.handle(sample(0, { pan: -1 }))
assert.deepEqual(out, ['WheelUp', 'WheelDown', 'WheelRight', 'WheelLeft'])
})
test('taking the pointer: samples go to the taker, held buttons are let go first, and its clicks are nobody else\'s', () => {
const { input, out } = pointer()
const taken = []
input.handle(sample(0b001))
const release = input.take(s => taken.push(s))
assert.deepEqual(out, ['down:9:1', 'up:9:1'])
out.length = 0
input.handle(sample(0b001, { dx: 5 })); input.handle(sample(0b011, { wheel: 1 })); input.handle(sample(0b010))
assert.deepEqual(taken, [sample(0b001, { dx: 5 }), sample(0b011, { wheel: 1 }), sample(0b010)])
assert.deepEqual(out, [])
// Given back with the right button still down: its release is not a key-up for anyone.
release()
input.handle(sample(0)); input.handle(sample(0b100)); input.handle(sample(0))
assert.deepEqual(out, ['down:9:3', 'up:9:3'])
// A stale release function can't take the pointer away from a later taker.
const second = []
const releaseSecond = input.take(s => second.push(s))
release()
input.handle(sample(0, { dx: 1 }))
assert.equal(second.length, 1)
releaseSecond()
})
test('the keyboard going away forgets held buttons without reporting releases', () => {
const { input, out } = pointer()
input.handle(sample(0b001))
input.reset()
assert.deepEqual(out, ['down:9:1'])
// The next keyboard starts clean: a press is a press.
input.handle(sample(0b001))
assert.deepEqual(out, ['down:9:1', 'down:9:1'])
})
+136
View File
@@ -0,0 +1,136 @@
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, globals = {}) {
const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
const context = { exports: {}, console, ...globals, 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 { bordered } = require('./helpers/viewport.cjs');
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'));
/** The page over a fake keyboard service, shell and clock. */
function fixture({ state = 'connected', pointer = true } = {}) {
let now = 10_000, visible = true, renders = 0, nextTimer = 1;
const timers = new Map();
const service = {
listener: null, takes: 0, releases: 0,
snapshot: () => ({ state, pointer }),
takePointer(listener) {
this.takes++; this.listener = listener;
return () => { if (this.listener === listener) this.listener = null; this.releases++; };
},
};
const globals = {
Date: { now: () => now },
setInterval: (fn, ms) => { timers.set(nextTimer, { fn, ms, at: now + ms, repeat: true }); return nextTimer++; },
setTimeout: (fn, ms) => { timers.set(nextTimer, { fn, ms, at: now + ms, repeat: false }); return nextTimer++; },
clearInterval: id => timers.delete(id), clearTimeout: id => timers.delete(id),
};
const { MouseTestLayer } = load('app/apps/developer/mouse-test.ts', {
'../../graphics/ui-fonts': { getDefaultSmallFont: () => font }, '../../graphics/image': graphics,
'../../g2/keyboard/service': { keyboardService: service },
'../../ui/shell/shell': { shell: { isWindowVisible: () => visible } },
'../../util/numeric-util': { clamp: (n, lo, hi) => Math.max(lo, Math.min(hi, n)) },
'../../ui/gestures': {}, '../../ui/layers': {},
}, globals);
const layer = new MouseTestLayer('developer', () => { renders++; });
const viewport = bordered(576, 260);
const popped = [];
const ctx = { stack: { getBaseSize: () => viewport, pop: () => popped.push(1) } };
function advance(ms) {
const end = now + ms;
for (;;) {
const due = [...timers].filter(([, t]) => t.at <= end).sort((a, b) => a[1].at - b[1].at)[0];
if (!due) break;
const [id, timer] = due;
now = timer.at;
if (timer.repeat) timer.at += timer.ms; else timers.delete(id);
timer.fn();
}
now = end;
}
const sample = (buttons, extra = {}) => service.listener({ buttons, dx: 0, dy: 0, wheel: 0, pan: 0, ...extra });
return { layer, service, ctx, viewport, timers, popped, advance, sample, paint: () => layer.paint(ctx),
renders: () => renders, setVisible: value => { visible = value; } };
}
/** Whether the arrow's tip (its top-left pixel, full white) is at (x, y) and nowhere it shouldn't be. */
const tipAt = (image, x, y) => image.getPixel(x, y) === 255 && image.getPixel(x, y + 1) === 255 && image.getPixel(x + 1, y + 2) === 70;
test('the arrow follows the touchpad, stays on the page, and repaints at a rate the link can carry', () => {
const f = fixture();
assert.equal(f.service.takes, 1, 'takes the pointer while the page shows');
f.paint();
const { width, height } = f.viewport;
// The first movement starts from the middle of the page.
f.sample(0, { dx: 10, dy: -20 });
assert.ok(tipAt(f.paint(), width / 2 + 10, height / 2 - 20));
// A hundred reports in a second are one repaint per 33 ms, not a hundred.
const before = f.renders();
for (let i = 0; i < 100; i++) { f.sample(0, { dx: 1 }); f.advance(10); }
assert.ok(f.renders() - before >= 20 && f.renders() - before <= 31, String(f.renders() - before));
assert.ok(tipAt(f.paint(), width / 2 + 110, height / 2 - 20));
// Pushed past an edge it stops there.
f.sample(0, { dx: 5000, dy: 5000 });
let image = f.paint();
assert.equal(image.getPixel(width - 1, height - 1), 255);
f.sample(0, { dx: -9000, dy: -9000 });
assert.ok(tipAt(f.paint(), 0, 0));
});
test('buttons and wheel show on the page; a left click leaves a mark and a right click clears them', () => {
const f = fixture();
f.paint();
f.sample(0, { dx: 100, dy: 40 });
const { width, height } = f.viewport;
const at = { x: width / 2 + 100, y: height / 2 + 40 };
f.sample(1);
f.sample(0, { dx: -60, wheel: 2 });
let image = f.paint();
// The mark: a small cross where the click was, after the arrow has moved off it.
assert.equal(image.getPixel(at.x - 4, at.y), 140); assert.equal(image.getPixel(at.x, at.y + 4), 140);
assert.ok(tipAt(image, at.x - 60, at.y));
f.sample(2);
image = f.paint();
assert.equal(image.getPixel(at.x - 4, at.y), 0);
f.sample(0);
assert.doesNotThrow(() => f.paint());
});
test('the pointer is only held while the page can be seen, and given back when the page goes', () => {
const f = fixture();
f.paint();
f.sample(1);
// Another window comes to the front: the buttons go back to being keys.
f.setVisible(false); f.advance(400);
assert.equal(f.service.releases, 1); assert.equal(f.service.listener, null);
f.setVisible(true); f.advance(400);
assert.equal(f.service.takes, 2);
// Double tap (or Esc, which is one) leaves; leaving gives the pointer back and stops the timers.
f.layer.handleInput({ type: 'double-click' }, f.ctx);
assert.equal(f.popped.length, 1);
f.layer.onRemoved();
assert.equal(f.service.releases, 2); assert.equal(f.timers.size, 0);
const renders = f.renders();
f.advance(5000);
assert.equal(f.renders(), renders);
});
test('without a keyboard, or with one that has no pointer, the page says so', () => {
for (const options of [{ state: 'idle' }, { state: 'connected', pointer: false }]) {
const f = fixture(options);
const image = f.paint();
assert.ok(image instanceof graphics.GrayImage);
f.layer.onRemoved();
}
});
+1
View File
@@ -23,6 +23,7 @@
"../app/g2/keyboard/keys.ts",
"../app/g2/keyboard/bindings.ts",
"../app/g2/keyboard/key-router.ts",
"../app/g2/keyboard/pointer.ts",
"../app/apps/terminal/terminal-keys.ts",
"../app/phone-ui/ble-bandwidth-meter.ts",
"../app/apps/terminal/hub-header.ts",