diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 00000000..0a9489f1 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,13 @@ +{ + "name": "opencode", + "image": "oven/bun:1.3-debian", + "postCreateCommand": "apt-get update && apt-get install -y python3 make g++ && bun install", + "customizations": { + "vscode": { + "extensions": [ + "dbaeumer.vscode-eslint", + "esbenp.prettier-vscode" + ] + } + } +} \ No newline at end of file diff --git a/packages/tui/src/app.tsx b/packages/tui/src/app.tsx index 57f372ef..f5766340 100644 --- a/packages/tui/src/app.tsx +++ b/packages/tui/src/app.tsx @@ -46,6 +46,7 @@ import { DialogStatus } from "./component/dialog-status" import { DialogDebug } from "./component/dialog-debug" import { DialogThemeList } from "./component/dialog-theme-list" import { DialogHelp } from "./ui/dialog-help" +import { DialogScotty } from "./ui/dialog-scotty" import { DialogAgent } from "./component/dialog-agent" import { DialogSessionList } from "./component/dialog-session-list" import { DialogWorkspaceList } from "./component/dialog-workspace-list" @@ -841,6 +842,15 @@ function App(props: { onSnapshot?: () => Promise; pluginHost: TuiPlugi dialog.clear() }, }, + { + name: "scotty.show", + title: "Show Scotty the Scottish Terrier", + slashName: "scotty", + run: () => { + dialog.replace(() => ) + }, + category: "System", + }, { name: "app.console", title: "Toggle console", diff --git a/packages/tui/src/ui/dialog-scotty.tsx b/packages/tui/src/ui/dialog-scotty.tsx new file mode 100644 index 00000000..6d2fa24e --- /dev/null +++ b/packages/tui/src/ui/dialog-scotty.tsx @@ -0,0 +1,57 @@ +import { TextAttributes } from "@opentui/core" +import { For } from "solid-js" +import { useTheme } from "../context/theme" +import { useDialog } from "./dialog" +import { useBindings } from "../keymap" + +const SCOTTY = [ + " __", + " \\______/ \\", + " / o o \\", + " ( ^ )", + " \\ wwwww /", + " /| |\\", + " (_| \\__/ |_)", + " || ||", + " (_| |_)", +] + +export function DialogScotty() { + const dialog = useDialog() + const { theme } = useTheme() + + useBindings(() => ({ + bindings: [ + { key: "return", desc: "Close Scotty", group: "Dialog", cmd: () => dialog.clear() }, + { key: "escape", desc: "Close Scotty", group: "Dialog", cmd: () => dialog.clear() }, + ], + })) + + return ( + + + + Scotty + + dialog.clear()}> + esc/enter + + + + + {(line) => ( + + {line} + + )} + + + Let's go Tartans! + + dialog.clear()}> + ok + + + + ) +} diff --git a/packages/ui/src/v2/components/tooltip-v2-behavior.ts b/packages/ui/src/v2/components/tooltip-v2-behavior.ts new file mode 100644 index 00000000..6d064d78 --- /dev/null +++ b/packages/ui/src/v2/components/tooltip-v2-behavior.ts @@ -0,0 +1,19 @@ +export function isFocusInside(container: { contains(node: Node): boolean } | undefined, active: Node | null) { + return !!container && !!active && container.contains(active) +} + +export function shouldDropBlock(expand: boolean, hovered: boolean, focusInside: boolean) { + return !expand && !hovered && !focusInside +} + +export function shouldArmFromKey(key: string) { + return key === "Enter" || key === " " +} + +export function openChangePlan(input: { forceOpen: boolean; block: boolean; open: boolean; skipClick: boolean }) { + const blocked = input.forceOpen || (input.block && input.open) + return { + resetSkipClick: !blocked && input.skipClick, + applyOpen: !blocked && !input.skipClick, + } +} diff --git a/packages/ui/src/v2/components/tooltip-v2.test.ts b/packages/ui/src/v2/components/tooltip-v2.test.ts new file mode 100644 index 00000000..11abaada --- /dev/null +++ b/packages/ui/src/v2/components/tooltip-v2.test.ts @@ -0,0 +1,89 @@ +import { describe, expect, test } from "bun:test" +import { isFocusInside, openChangePlan, shouldArmFromKey, shouldDropBlock } from "./tooltip-v2-behavior" + +describe("isFocusInside", () => { + const child = {} as Node + const other = {} as Node + const container = { contains: (node: Node) => node === child } + + test("is false when the trigger is missing", () => { + expect(isFocusInside(undefined, child)).toBe(false) + }) + + test("is false when nothing is focused", () => { + expect(isFocusInside(container, null)).toBe(false) + }) + + test("is true only when focus is inside the trigger", () => { + expect(isFocusInside(container, child)).toBe(true) + expect(isFocusInside(container, other)).toBe(false) + }) +}) + +describe("shouldDropBlock", () => { + test("unblocks only when not expanded, not hovered, and focus is outside", () => { + expect(shouldDropBlock(false, false, false)).toBe(true) + }) + + test("keeps the block while a nested control is expanded", () => { + expect(shouldDropBlock(true, false, false)).toBe(false) + }) + + test("keeps the block while the trigger is hovered", () => { + expect(shouldDropBlock(false, true, false)).toBe(false) + }) + + test("keeps the block while focus is inside the trigger", () => { + expect(shouldDropBlock(false, false, true)).toBe(false) + }) +}) + +describe("shouldArmFromKey", () => { + test("arms on Enter and Space", () => { + expect(shouldArmFromKey("Enter")).toBe(true) + expect(shouldArmFromKey(" ")).toBe(true) + }) + + test("ignores other keys", () => { + expect(shouldArmFromKey("Tab")).toBe(false) + expect(shouldArmFromKey("Escape")).toBe(false) + expect(shouldArmFromKey("a")).toBe(false) + }) +}) + +describe("openChangePlan", () => { + test("applies the requested open state on a normal change", () => { + expect(openChangePlan({ forceOpen: false, block: false, open: true, skipClick: false })).toEqual({ + resetSkipClick: false, + applyOpen: true, + }) + }) + + test("ignores Kobalte while forceOpen is set, without consuming a trigger click", () => { + expect(openChangePlan({ forceOpen: true, block: false, open: false, skipClick: true })).toEqual({ + resetSkipClick: false, + applyOpen: false, + }) + }) + + test("does not open while blocked, without consuming a trigger click", () => { + expect(openChangePlan({ forceOpen: false, block: true, open: true, skipClick: true })).toEqual({ + resetSkipClick: false, + applyOpen: false, + }) + }) + + test("still allows a close while blocked", () => { + expect(openChangePlan({ forceOpen: false, block: true, open: false, skipClick: false })).toEqual({ + resetSkipClick: false, + applyOpen: true, + }) + }) + + test("consumes a click on the trigger and skips the open change", () => { + expect(openChangePlan({ forceOpen: false, block: false, open: false, skipClick: true })).toEqual({ + resetSkipClick: true, + applyOpen: false, + }) + }) +}) diff --git a/packages/ui/src/v2/components/tooltip-v2.tsx b/packages/ui/src/v2/components/tooltip-v2.tsx index 911307b6..6d47c613 100644 --- a/packages/ui/src/v2/components/tooltip-v2.tsx +++ b/packages/ui/src/v2/components/tooltip-v2.tsx @@ -2,6 +2,7 @@ import { Tooltip as KobalteTooltip } from "@kobalte/core/tooltip" import { createEffect, Match, onCleanup, splitProps, Switch, type JSX } from "solid-js" import type { ComponentProps } from "solid-js" import { createStore } from "solid-js/store" +import { isFocusInside, openChangePlan, shouldArmFromKey, shouldDropBlock } from "./tooltip-v2-behavior" import "./tooltip-v2.css" export interface TooltipV2Props extends ComponentProps { @@ -33,17 +34,12 @@ export function TooltipV2(props: TooltipV2Props) { const close = () => setState("open", false) - const inside = () => { - const active = document.activeElement - if (!ref || !active) return false - return ref.contains(active) - } + const inside = () => isFocusInside(ref, document.activeElement) const drop = (expand = state.expand) => { - if (expand) return - if (ref?.matches(":hover")) return - if (inside()) return - setState("block", false) + if (shouldDropBlock(expand, !!ref?.matches(":hover"), inside())) { + setState("block", false) + } } const sync = () => { @@ -52,7 +48,6 @@ export function TooltipV2(props: TooltipV2Props) { if (expand) { setState("block", true) close() - return } drop(expand) } @@ -68,16 +63,17 @@ export function TooltipV2(props: TooltipV2Props) { } createEffect(() => { - if (!ref) return - sync() - const obs = new MutationObserver(sync) - obs.observe(ref, { - subtree: true, - childList: true, - attributes: true, - attributeFilter: ["aria-expanded", "data-expanded"], - }) - onCleanup(() => obs.disconnect()) + if (ref) { + sync() + const obs = new MutationObserver(sync) + obs.observe(ref, { + subtree: true, + childList: true, + attributes: true, + attributeFilter: ["aria-expanded", "data-expanded"], + }) + onCleanup(() => obs.disconnect()) + } }) let justClickedTrigger = false @@ -95,13 +91,14 @@ export function TooltipV2(props: TooltipV2Props) { ignoreSafeArea={local.ignoreSafeArea ?? true} open={local.forceOpen || state.open} onOpenChange={(open) => { - if (local.forceOpen) return - if (state.block && open) return - if (justClickedTrigger) { - justClickedTrigger = false - return - } - setState("open", open) + const plan = openChangePlan({ + forceOpen: !!local.forceOpen, + block: state.block, + open, + skipClick: justClickedTrigger, + }) + if (plan.resetSkipClick) justClickedTrigger = false + if (plan.applyOpen) setState("open", open) }} > { - if (event.key !== "Enter" && event.key !== " ") return - arm() + if (shouldArmFromKey(event.key)) arm() }} onPointerLeave={leave} onFocusOut={() => requestAnimationFrame(() => drop())}