Files
herdrMario/spikes/ovis-keytest/main.js
T
2026-11-19 11:21:37 +01:00

93 lines
3.1 KiB
JavaScript

// Throwaway diagnostic: which layer delivers up/down (and other chords) to a
// mod whose Textarea sits inside a custom key_context. Not part of the product.
import { View, div } from "gpui-kit";
import { v_flex, TextareaState, Textarea } from "gpui-base";
import { theme, nextCommand } from "herdr";
const CTX = "ovis-keytest";
const ACTIONS = ["up", "down", "down2", "down3", "down4", "down5"];
export default class KeyTest extends View {
init(_props, cx) {
this.lines = [];
this.n = 0;
this.lastCaret = null;
this.handle = cx.focus_handle();
this.textarea = TextareaState.new({ value: "", placeholder: "type here, press the keys", rows: 1 });
this.textarea.set_auto_grow(1, 6);
this.textarea.set_soft_wrap(true);
this.textarea.on("change", (e, cx) => this.log("G change", cx));
this.textarea.on("submit", (e, cx) => this.log("G submit", cx));
this.textarea.on("focus", (e, cx) => this.log("G focus", cx));
this.textarea.on("blur", (e, cx) => this.log("G blur", cx));
const bind = (keystroke, action, context) => (context === undefined ? { keystroke, action } : { keystroke, action, context });
const count = cx.bind_keys([
bind("up", "ovis-keytest.up", `${CTX} > Input`),
bind("down", "ovis-keytest.down", `${CTX} > Input`),
bind("ctrl-alt-j", "ovis-keytest.down2", `${CTX} > Input`),
bind("ctrl-alt-k", "ovis-keytest.down3", "Input"),
bind("ctrl-alt-l", "ovis-keytest.down4"),
bind("ctrl-alt-u", "ovis-keytest.down5", CTX),
]);
this.log(`bind_keys installed ${count}`, cx);
this.textarea.focus();
// Caret movement shows whether the Textarea itself handled an arrow.
cx.timer.every(200, (cx) => {
let r = null;
try {
const range = this.textarea.selected_range();
r = `${range.start}-${range.end}`;
} catch {}
if (r !== this.lastCaret) {
this.lastCaret = r;
this.log(`G caret ${r}`, cx);
}
});
cx.spawn(async (cx) => {
for (;;) {
const command = await nextCommand();
this.log(`NEXTCOMMAND ${command}`, cx);
cx.notify();
}
});
}
log(text, cx) {
this.n += 1;
this.lines.push(`${this.n}. ${text}`);
if (this.lines.length > 12) this.lines.shift();
cx?.notify();
}
render(cx) {
const t = theme();
let root = v_flex()
.id("ovis-keytest")
.track_focus(this.handle)
.key_context(CTX)
.on_key_down((event, cx) => {
const m = event.modifiers ?? {};
const mods = [m.control ? "ctrl" : "", m.alt ? "alt" : "", m.shift ? "shift" : ""].filter(Boolean).join("+");
this.log(`F on_key_down ${mods ? mods + "+" : ""}${event.key}`, cx);
});
for (const name of ACTIONS) {
root = root.on_action(`ovis-keytest.${name}`, (e, cx) => this.log(`ON_ACTION ${name}`, cx));
}
return root
.size_full()
.p(10)
.gap(8)
.bg(t.canvas)
.text_color(t.foreground)
.child(
Textarea.new(this.textarea).w_full().px(10).py(8).rounded(8).bg(t.background).text_color(t.foreground).text_size(13),
)
.children(this.lines.map((l) => div().text_size(12).child(l)));
}
}