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
@@ -104,6 +104,7 @@ import { uiFontPickerMenuItem } from "../font-picker";
|
||||
import { NotificationFilterLayer } from "../notification-filter";
|
||||
import { compatibleModelDiscoveryMenuItem } from "../openai-compatible-model-picker";
|
||||
import { keyboardSettingsMenuItem } from "../keyboard-settings";
|
||||
import { keybindingsMenuItem } from "../keybinding-settings";
|
||||
import { isVeniceApiBaseUrl } from "../../native/openai-chat";
|
||||
|
||||
/** The Settings section the Notifications app's "Settings" menu item opens. */
|
||||
@@ -248,7 +249,7 @@ function settingsSections(): SettingsSection[] {
|
||||
// out: the Settings window can be open across a change of glasses.
|
||||
{
|
||||
label: "Keyboard",
|
||||
items: [keyboardSettingsMenuItem()],
|
||||
items: [keyboardSettingsMenuItem(), keybindingsMenuItem()],
|
||||
hidden: () => glassesTypeSetting.get() !== "g2",
|
||||
},
|
||||
{
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
import { getDefaultSmallFont } from "../graphics/ui-fonts";
|
||||
import {
|
||||
DEFAULT_KEYS_HELP,
|
||||
KEY_ACTIONS,
|
||||
chordId,
|
||||
describeChord,
|
||||
keyActionLabel,
|
||||
type KeyChord,
|
||||
} from "../g2/keyboard/bindings";
|
||||
import { keyboardService } from "../g2/keyboard/service";
|
||||
import { TextViewerLayer } from "../apps/files/text-viewer";
|
||||
import type { LayerContext } from "./layers";
|
||||
import { drawRightValueMenuItem, MenuLayer, openModalMenu, submenuItem, TextPageLayer, type MenuItem } from "./menu";
|
||||
|
||||
/**
|
||||
* Settings > Keyboard > Keybindings: the custom keybindings (a key, with
|
||||
* any modifiers held, to a gesture), which come before the built-in
|
||||
* meanings in app/g2/keyboard/bindings.ts. Adding one asks for the key on
|
||||
* the keyboard itself, so it works for a remote whose buttons send who
|
||||
* knows what.
|
||||
*/
|
||||
|
||||
/** Waits for the key to bind. Double tap backs out, as from any page. */
|
||||
class KeyCaptureLayer extends TextPageLayer {
|
||||
private cancel: (() => void) | null = null;
|
||||
|
||||
constructor(private readonly onChord: (chord: KeyChord, ctx: LayerContext) => void) {
|
||||
super(
|
||||
"Add keybinding",
|
||||
"Press the key to bind, on the keyboard. Hold Shift, Ctrl or Alt with it to bind the combination.",
|
||||
);
|
||||
}
|
||||
|
||||
open(ctx: LayerContext): void {
|
||||
ctx.stack.push(this);
|
||||
this.cancel = keyboardService.captureNextKey((chord) => {
|
||||
this.cancel = null;
|
||||
ctx.stack.popThrough(this);
|
||||
this.onChord(chord, ctx);
|
||||
ctx.actions.requestRender();
|
||||
});
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.cancel?.();
|
||||
this.cancel = null;
|
||||
}
|
||||
}
|
||||
|
||||
class KeybindingsLayer extends MenuLayer {
|
||||
constructor(viewport: { width: number; height: number }) {
|
||||
super("Keybindings", [], {
|
||||
x: "center",
|
||||
y: 8,
|
||||
width: Math.min(320, viewport.width - 40),
|
||||
minHeight: Math.min(160, viewport.height - 40),
|
||||
maxHeight: Math.min(240, viewport.height - 40),
|
||||
depth: 4,
|
||||
});
|
||||
}
|
||||
|
||||
open(ctx: LayerContext): void {
|
||||
this.updateRows();
|
||||
ctx.stack.push(this);
|
||||
}
|
||||
|
||||
/** Rebuild the rows, with the highlight on `selectId`'s keybinding when given. */
|
||||
private updateRows(selectId?: string): void {
|
||||
const bindings = keyboardService.keyBindings();
|
||||
const rows: MenuItem[] = bindings.map((binding) => {
|
||||
const label = describeChord(binding.chord);
|
||||
return {
|
||||
label,
|
||||
onSelect: (ctx: LayerContext) => this.pickAction(ctx, binding.chord),
|
||||
render: ({ image, x, y, width }) => {
|
||||
drawRightValueMenuItem(image, getDefaultSmallFont(), x, y, width, label, keyActionLabel(binding.action));
|
||||
},
|
||||
};
|
||||
});
|
||||
rows.push(
|
||||
{ label: "Add keybinding", onSelect: (ctx) => this.add(ctx) },
|
||||
// Scrolls, where a page would cut the text off in a short window.
|
||||
submenuItem("Default keys", (ctx) => ctx.stack.push(new TextViewerLayer(DEFAULT_KEYS_HELP, "Default keys"))),
|
||||
);
|
||||
const selected = selectId === undefined ? -1 : bindings.findIndex((binding) => chordId(binding.chord) === selectId);
|
||||
if (selected >= 0) this.setItems(rows, selected);
|
||||
else this.setItems(rows);
|
||||
}
|
||||
|
||||
private add(ctx: LayerContext): void {
|
||||
if (!keyboardService.bridge.isConnected()) {
|
||||
ctx.stack.push(new TextPageLayer(
|
||||
"Add keybinding",
|
||||
"No keyboard is connected. Pair or reconnect one under Bluetooth keyboard, then come back: the key to bind is read from the keyboard.",
|
||||
));
|
||||
return;
|
||||
}
|
||||
new KeyCaptureLayer((chord, captureCtx) => this.pickAction(captureCtx, chord)).open(ctx);
|
||||
}
|
||||
|
||||
/** Choose what `chord` does: for a new keybinding, or to change or remove one. */
|
||||
private pickAction(ctx: LayerContext, chord: KeyChord): void {
|
||||
const id = chordId(chord);
|
||||
const current = keyboardService.keyBindings().find((binding) => chordId(binding.chord) === id)?.action;
|
||||
const done = (inner: LayerContext): void => {
|
||||
inner.stack.pop();
|
||||
this.updateRows(id);
|
||||
inner.actions.requestRender();
|
||||
};
|
||||
const rows: MenuItem[] = KEY_ACTIONS.map((entry) => ({
|
||||
label: entry.action === current ? `${entry.label} *` : entry.label,
|
||||
onSelect: (inner: LayerContext) => {
|
||||
if (!keyboardService.setKeyBinding(chord, entry.action)) {
|
||||
inner.stack.pop();
|
||||
inner.stack.push(new TextPageLayer("Keybindings", "There is no room for another keybinding. Remove one first."));
|
||||
return;
|
||||
}
|
||||
done(inner);
|
||||
},
|
||||
}));
|
||||
if (current !== undefined) {
|
||||
rows.push({
|
||||
label: "Remove keybinding",
|
||||
onSelect: (inner) => {
|
||||
keyboardService.removeKeyBinding(chord);
|
||||
done(inner);
|
||||
},
|
||||
});
|
||||
}
|
||||
const selected = Math.max(0, KEY_ACTIONS.findIndex((entry) => entry.action === current));
|
||||
openModalMenu(ctx, describeChord(chord), rows, selected);
|
||||
}
|
||||
}
|
||||
|
||||
/** The Settings row that opens the keybindings menu. */
|
||||
export function keybindingsMenuItem(): MenuItem {
|
||||
return {
|
||||
label: "Keybindings",
|
||||
description:
|
||||
"Choose what keys do: arrows, Enter and Esc navigate by default, and any key (or a remote's button) can be bound to select, back, a swipe, a scroll or a menu.",
|
||||
onSelect: (ctx) => new KeybindingsLayer(ctx.stack.getBaseSize()).open(ctx),
|
||||
render: ({ image, x, y, width }) => {
|
||||
const count = keyboardService.keyBindings().length;
|
||||
drawRightValueMenuItem(image, getDefaultSmallFont(), x, y, width, "Keybindings", count ? `${count} custom` : "Default");
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -196,9 +196,10 @@ export type ShellWindow = {
|
||||
/**
|
||||
* Take a key from a hardware keyboard (app/g2/keyboard) as a key. Optional:
|
||||
* only windows that give keys a meaning of their own (the terminal)
|
||||
* implement it, and they get every key while they have input focus,
|
||||
* presses and releases alike. For the rest the controller turns the few
|
||||
* navigation keys into ring gestures (navigationGestureForKey).
|
||||
* implement it. They get the keys no keybinding claimed while they have
|
||||
* input focus, presses and releases alike; the rest of the time, and for
|
||||
* every other window, the navigation keys act as gestures (see
|
||||
* app/g2/keyboard/bindings.ts).
|
||||
*/
|
||||
handleKeyEvent?: (event: KeyEvent) => void;
|
||||
/** Foreground state changed: this window's surface is (not) the visible one. */
|
||||
|
||||
@@ -58,6 +58,16 @@ export type WorkerAppReply =
|
||||
type: "worker-ready";
|
||||
}
|
||||
| { type: "yield-focus"; windowId: string }
|
||||
| {
|
||||
/**
|
||||
* A "key" message the window had no use for as a key (a terminal
|
||||
* showing its session list rather than a session). The host gives it
|
||||
* the keyboard's navigation meaning instead.
|
||||
*/
|
||||
type: "key-unhandled";
|
||||
windowId: string;
|
||||
event: unknown;
|
||||
}
|
||||
| {
|
||||
/** Foreground and focus one of the app's existing windows. */
|
||||
type: "focus-window";
|
||||
@@ -202,9 +212,9 @@ export type WorkerWindowSpec = {
|
||||
acceptsDirectional?: boolean;
|
||||
/**
|
||||
* Deliver hardware-keyboard keys to the worker as "key" messages while the
|
||||
* window has input focus, instead of the navigation-gesture fallback. The
|
||||
* worker then owns what every key means in every window state, navigation
|
||||
* included (see ShellWindow.handleKeyEvent).
|
||||
* window has input focus (see ShellWindow.handleKeyEvent). The worker
|
||||
* answers "key-unhandled" for a key it doesn't want as a key just then,
|
||||
* which gets the keyboard's navigation meaning instead.
|
||||
*/
|
||||
acceptsKeys?: boolean;
|
||||
};
|
||||
@@ -234,6 +244,8 @@ export type WorkerAppHostOptions = {
|
||||
startTextSettingEdit: (settingId: string) => void;
|
||||
/** Close the phone text editor opened by startTextSettingEdit. */
|
||||
endTextSettingEdit: () => void;
|
||||
/** A key a window was sent and handed back (the "key-unhandled" reply); a KeyEvent. */
|
||||
unhandledKey?: (event: unknown) => void;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -323,6 +335,10 @@ export class WorkerAppHost {
|
||||
shell.yieldFocusToSidebar();
|
||||
}
|
||||
break;
|
||||
case "key-unhandled":
|
||||
// Stale once focus has moved on: the key was meant for that window.
|
||||
if (shell.isWindowFocused(message.windowId)) this.options.unhandledKey?.(message.event);
|
||||
break;
|
||||
case "focus-window":
|
||||
if (this.openWindows.has(message.windowId)) {
|
||||
shell.focusWindow(message.windowId);
|
||||
|
||||
Reference in new issue
Block a user