// SPDX-License-Identifier: MIT // Copyright (C) 2026 Pooya Parsa // The launcher: the overlay the Dock's first tile opens (#launcher, Launchpad; // Dock.tsx #dock-launcher), in the manner of the iOS home screen: the page // blurred behind a search field and a grid of the apps (../apps/), the page's // own apps first (Terminal, Browser, Settings, System: the inline group), then // one folder per group. A folder's face is a fan of its first three apps' // real icons, the first in front at 80% of an app icon's size and the next // two tilted out behind it, left and right; it spreads a little under the // pointer and while selected. An open folder's title carries its group's own // icon (apps//index.ts). A tile runs its app (launcher.ts launch()). // // - A folder opens in place: it grows from its tile into a panel with its // title and its apps' tiles, over the top level blurred once more; a click // outside the panel, or Esc, closes it back into its tile (no motion with // prefers-reduced-motion). // - Typing searches every app, whatever the folder (title, id, description // and its group's title), in one flat grid; clearing it shows the folders. // - Keys: the arrows move the selection in whatever grid shows (the top // level, the open folder, the search results): left and right through the // list, up and down to the nearest tile in the next row; the selected tile // is scrolled into view. They do so with text in the field too (a short // query has no caret to steer; with Shift, Alt, Ctrl or ⌘ they edit the // text as usual, and Home and End move the caret). A new query selects its // first result. Enter opens a folder or runs an app, Esc closes the // folder, else clears the search, else closes the launcher; Tab goes // nowhere. The focus stays in the search field (tiles // take none), so display.ts never routes these keys to a program holding // the guest's keyboard. On touch devices the field is not focused (it would // raise the on-screen keyboard): the overlay itself takes the focus, away // from a terminal, and display.ts leaves keys in #launcher alone too. // - Under the grid, with a keyboard (not on touch devices): the keys, and // the shortcut prefix as key caps with "shortcuts" (keys.ts PREFIX); a // click on it closes the launcher and arms the prefix, so the shortcuts // HUD shows (KeysHud.tsx). It takes no focus and is no tab stop. // - A click on the backdrop closes it; so do its shortcuts (⌘K on Apple // devices, Ctrl-Shift-O, Ctrl-A o: keys.ts) and the Dock's tile. // - An app's tile and a folder have a menu (Menu.tsx, at the pointer; // #launcher-menu): a right click, a long press on touch (LONG_PRESS without // moving more than 8 px; the tap that ends it does nothing else), or the // ContextMenu key or Shift-F10 for the selected tile. Open, then Keep in // Dock or Remove from Dock (pins.ts: the app, or the folder's group, whose // Dock tile opens its apps). The launcher stays open while it is used and // closes only with an app's Open; a press outside the menu only closes the // menu (that press and its click do nothing else); its keys stay in it, // and Esc closes it first, before the folder and the launcher. // - An app being installed (installer.ts) has a progress bar over its icon, // and picking it shows its installer. // - A running app (a terminal named after it, or a page app's open window) // has a dot under its tile, and picking it shows that window (launcher.ts: // one run at a time); a folder has the dot while one of its apps runs. // - An app with `hidden` set is left out. One the page cannot run // (launcher.ts unavailable()) is dimmed, with why in its tooltip and under // the grid while it is selected (on touch, once it is tapped); a folder // whose apps all are is dimmed too, and still opens. import { useEffect, useLayoutEffect, useMemo, useRef, useState, type CSSProperties, type KeyboardEvent as ReactKeyboardEvent, type MouseEvent as ReactMouseEvent, type ReactNode, type RefObject, } from "react"; import { GROUPS } from "../apps/index.ts"; import { isPageApp, type App, type AppIcon } from "../apps/types.ts"; import { reducedMotion, useLongPress } from "../hooks.ts"; import { useInstalls, type Install } from "../installer.ts"; import { KW } from "../kw.ts"; import { appPackages, launch, setLauncher, unavailable } from "../launcher.ts"; import { PREFIX } from "../keys.ts"; import { canPin, canPinGroup, isPinned, pin, unpin, usePins, type PinTarget } from "../pins.ts"; import { isStopped, useStore } from "../store.ts"; import { useTermTabs } from "../termtabs.ts"; import { useWm } from "../wm.ts"; import { AppGlyph, SearchIcon } from "./icons.tsx"; import { TileProgress } from "./Installer.tsx"; import { Menu, type MenuClose, type MenuEntry } from "./Menu.tsx"; /** An app's tile. */ interface Tile { app: App; id: string; title: string; desc: string; icon: string | AppIcon; colors: [string, string]; /** Why it cannot run now, or null. */ why: string | null; /** Where it runs ("running in terminal 3", "open"), or null. */ running: string | null; tip: string; /** Its group's title, for search. */ group: string; /** Its install, while it is being installed. */ install?: Install; } /** A group's folder. */ interface Folder { id: string; title: string; desc: string; icon: string | AppIcon; colors: [string, string]; tiles: Tile[]; running: boolean; /** Every app in it is unavailable. */ off: boolean; } type Entry = { tile: Tile; folder?: undefined } | { folder: Folder; tile?: undefined }; const GRAY: [string, string] = ["#52525b", "#18181b"]; const GROUP_TITLE = new Map(GROUPS.map((g) => [g.id, g.title])); const matches = (t: Tile, q: string) => `${t.title} ${t.id} ${t.desc} ${t.group}`.toLowerCase().includes(q); const gradient = (c: [string, string]): CSSProperties => ({ backgroundImage: `linear-gradient(135deg, ${c[0]}, ${c[1]})` }); const optId = (e: Entry) => (e.folder ? `launcher-folder-${e.folder.id}` : `launcher-app-${e.tile.id}`); /** A rounded square with the app's gradient and glyph, or its pictogram at 58%; its install's bar over its bottom. */ function Icon({ t }: { t: Tile }) { return ( ); } /** * Where the cards behind a folder's front one sit (transform-origin at their * bottom middle): the second out to the right, the third to the left, each a * little smaller; spread by 1.4 under the pointer and while selected. */ const FAN: CSSProperties[] = [ { "--x": "15%", "--r": "11deg", "--s": "0.92" } as CSSProperties, { "--x": "-15%", "--r": "-11deg", "--s": "0.86" } as CSSProperties, ]; /** * A folder's face: its first apps' icons as a fan of cards, back to front, * in a box as large as an app's icon (the tilted cards overhang it a few px * at the sides, so rows stay even). Each card is the app's own gradient and * glyph, dimmed while the app cannot run, with a shadow that parts it from * the card behind. data-face is the box an opening folder grows from. */ function FolderFace({ f }: { f: Folder }) { const shown = f.tiles.slice(0, 3); const card = (t: Tile, i: number) => ( ); return ( ); } /** A long press on touch: how long (ms), as on the Dock's tiles. */ const LONG_PRESS = 500; /** * A cell's menu triggers: a right click, and a long press on touch * (hooks.ts useLongPress: a move past 8 px, a scroll or a lift cancels it). * `onMenu` gets the point; without it the browser's menu is still kept * away. `consumed()`: the click ends a long press that opened the menu, so * it does nothing else. */ function useCellMenu(onMenu: ((at: DOMRect) => void) | undefined) { const long = useLongPress(onMenu && ((at) => onMenu(new DOMRect(at.x, at.y, 0, 0))), { ms: LONG_PRESS }); const handlers = { onContextMenu: (e: ReactMouseEvent) => { e.preventDefault(); if (!onMenu) return; long.context(); onMenu(new DOMRect(e.clientX, e.clientY, 0, 0)); }, ...long.handlers, }; return { handlers, consumed: long.consumed }; } /** A cell of a grid: a folder's or an app's face, its title, and the running dot; `onMenu`: its menu (useCellMenu). */ function Cell({ e, idx, on, onPick, onHover, onMenu }: { e: Entry; idx: number; on: boolean; onPick(): void; onHover(): void; onMenu?(at: DOMRect): void }) { const { handlers, consumed } = useCellMenu(onMenu); const f = e.folder, t = e.tile; const dim = f ? f.off : !!t!.why; const running = f ? f.running : !!t!.running; const title = f ? f.title : t!.title; return ( ); } /** * An open folder over the top level: its title and a panel of its apps. It * grows from its tile on the top level and, `leaving`, shrinks back into it, * then calls onGone. */ function FolderLayer({ f, leaving, listRef, onClose, onGone, children }: { f: Folder; leaving: boolean; listRef: RefObject; onClose(): void; onGone(): void; children: ReactNode; }) { const layer = useRef(null), panel = useRef(null), head = useRef(null); /** The transform that puts the panel over the folder's face (null when either is not laid out). */ const atFace = (): string | null => { const face = document.querySelector(`#launcher [data-folder="${CSS.escape(f.id)}"] [data-face]`); const p = panel.current; if (!face || !p) return null; const a = face.getBoundingClientRect(), b = p.getBoundingClientRect(); if (!a.width || !b.width) return null; return `translate(${a.left + a.width / 2 - (b.left + b.width / 2)}px, ${a.top + a.height / 2 - (b.top + b.height / 2)}px) scale(${a.width / b.width})`; }; useLayoutEffect(() => { if (reducedMotion()) return; const ease = "cubic-bezier(.2,.8,.2,1)"; layer.current?.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 200, easing: "ease-out" }); head.current?.animate([{ opacity: 0, transform: "translateY(6px)" }, { opacity: 1, transform: "none" }], { duration: 260, easing: ease }); const from = atFace(); panel.current?.animate([{ opacity: 0.5, transform: from ?? "scale(.92)" }, { opacity: 1, transform: "none" }], { duration: 280, easing: ease }); }, []); useEffect(() => { if (!leaving) return; let done = false; const end = () => { if (!done) { done = true; onGone(); } }; if (reducedMotion()) { end(); return; } const opts: KeyframeAnimationOptions = { duration: 180, easing: "cubic-bezier(.4,0,.7,.2)", fill: "forwards" }; const to = atFace(); const anims = [ layer.current?.animate([{ opacity: 1 }, { opacity: 0 }], opts), panel.current?.animate([{ opacity: 1, transform: "none" }, { opacity: 0, transform: to ?? "scale(.92)" }], opts), ]; if (anims[1]) anims[1].onfinish = end; const timer = setTimeout(end, 400); // a finish that never comes (a hidden tab) // Opened again before it was gone: it stays. return () => { done = true; clearTimeout(timer); for (const a of anims) a?.cancel(); }; }, [leaving]); return (
e.preventDefault()} onClick={(e) => { if (e.target === e.currentTarget || (e.target as HTMLElement).dataset.backdrop) onClose(); }} className={`absolute inset-0 z-10 flex flex-col items-center justify-center bg-bg/40 pt-[max(16px,var(--st))] pr-[max(16px,var(--sr))] pb-[max(16px,var(--sb))] pl-[max(16px,var(--sl))] backdrop-blur-xl ${leaving ? "pointer-events-none" : ""}`} >

{f.title}

9 ? "grid-cols-3 sm:grid-cols-4" : "grid-cols-3"}`}> {children}
); } export function Launchpad() { const open = useStore((s) => s.launcher); return open ? : null; } function Pad() { const stopped = useStore(isStopped); const terms = useTermTabs((s) => s.terms); const windows = useWm((s) => s.windows); const installs = useInstalls(); const coarse = !!KW.config.coarse; const [query, setQuery] = useState(""); /** The open folder (its group's id); `layer` is the one drawn, which stays while it closes. */ const [folder, setFolder] = useState(null); const [layer, setLayer] = useState(null); const [sel, setSel] = useState(0); /** The selection and the line under the grid show: from the start with a keyboard, on touch once a dimmed tile is tapped. */ const [hint, setHint] = useState(!coarse); /** The top level's selection, back when the folder closes. */ const topSel = useRef(0); const root = useRef(null); const input = useRef(null); const grid = useRef(null); const folderList = useRef(null); const q = query.trim().toLowerCase(); usePins(); // a menu's Keep in Dock or Remove from Dock follows the pins /** The open menu: its app's tile or its folder (by id), the cell's index, and the point it opened at. */ const [ctx, setCtx] = useState<{ id: string; folder: boolean; idx: number; at: DOMRect } | null>(null); /** A press outside the menu closed it: the launcher's own handlers leave that press alone. */ const ignoreDown = useRef(false); const { top, folders, all } = useMemo(() => { const tileOf = (a: App): Tile => { const page = isPageApp(a); const why = !page && stopped ? "the system is not running" : unavailable(a); let running: string | null = null; if (page) running = a.win && windows.some((w) => w.id === a.win) ? "open" : null; else { const n = terms.find((x) => x.name === a.id)?.n; running = n ? `running in terminal ${n}` : null; } const pkgs = page ? [] : appPackages(a); const install = installs[a.id]; const tip = install ? `${a.title}: ${install.state === "failed" ? "the install failed" : "installing"}, shows it` : why ? `${a.title}: ${why}` : running ? `${a.title}: ${running}, shows it` : page ? `${a.title}: ${a.desc}` : `${a.desc}${pkgs.length ? `\nInstalls: ${pkgs.join(" ")}` : ""}\nRuns: ${a.commands.join("; ")}`; return { app: a, id: a.id, title: a.title, desc: a.desc, icon: a.icon ?? a.title[0]!.toUpperCase(), colors: a.colors ?? GRAY, why, running, tip, group: GROUP_TITLE.get(a.group) ?? "", install, }; }; const pinned: Entry[] = [], folders: Folder[] = [], all: Tile[] = []; for (const g of GROUPS) { const tiles = g.apps.filter((a) => !a.hidden).map(tileOf); if (!tiles.length) continue; all.push(...tiles); if (g.inline) pinned.push(...tiles.map((tile) => ({ tile }))); else folders.push({ id: g.id, title: g.title, desc: g.desc ?? tiles.map((t) => t.title).join(", "), icon: g.icon ?? g.title[0]!.toUpperCase(), colors: g.colors ?? GRAY, tiles, running: tiles.some((t) => t.running), off: tiles.every((t) => t.why), }); } return { top: [...pinned, ...folders.map((f): Entry => ({ folder: f }))], folders, all }; }, [stopped, terms, windows, installs]); const results = q ? all.filter((t) => matches(t, q)).map((tile): Entry => ({ tile })) : null; const opened = folders.find((f) => f.id === folder); const items: Entry[] = results ?? (opened ? opened.tiles.map((tile) => ({ tile })) : top); const cur = items.length ? items[Math.min(sel, items.length - 1)] : undefined; const drawn = folders.find((f) => f.id === layer); /** The grid the selection moves in. */ const list = () => (opened && !results ? folderList.current : grid.current); useEffect(() => { if (!coarse) input.current?.focus({ preventScroll: true }); else { // Not the field (the on-screen keyboard): the overlay, so a hardware keyboard's keys stay here, not in a terminal. (document.activeElement as HTMLElement | null)?.blur?.(); root.current?.focus({ preventScroll: true }); } }, []); // Keep the selected tile in view as the keys move it. useEffect(() => { if (hint) list()?.querySelector(`[data-idx="${Math.min(sel, items.length - 1)}"]`)?.scrollIntoView({ block: "nearest" }); }, [sel, folder, q, hint]); const openFolder = (id: string, idx: number) => { topSel.current = idx; setFolder(id); setLayer(id); setSel(0); }; const closeFolder = () => { if (!folder) return; setFolder(null); setSel(topSel.current); }; const pick = (e: Entry | undefined, idx: number) => { if (!e) return; if (e.folder) return openFolder(e.folder.id, idx); if (e.tile.why) { setSel(idx); setHint(true); return; } setLauncher(false); void launch(e.tile.app); }; /** The focus back where the launcher keeps it: the search field, on touch the overlay. */ const refocus = () => (coarse ? root.current : input.current)?.focus({ preventScroll: true }); const openMenu = (e: Entry, at: DOMRect, idx: number) => { const id = e.folder ? e.folder.id : e.tile.id, folder = !!e.folder; setSel(idx); setCtx((c) => (c?.id === id && c.folder === folder ? c : { id, folder, idx, at })); // Android: a long press opens it twice }; const closeMenu = (_how: MenuClose) => { setCtx(null); refocus(); }; /** Keep in Dock or Remove from Dock, for an app or a folder's group. */ const dockItem = (t: PinTarget): MenuEntry => (isPinned(t) ? { label: "Remove from Dock", run: () => unpin(t) } : { label: "Keep in Dock", run: () => pin(t) }); /** An app's menu: Open (closes the launcher), then Keep in Dock or Remove from Dock. */ const menuItems = (t: Tile): MenuEntry[] => { const items: MenuEntry[] = [{ label: "Open", disabled: !!t.why, run: () => { setLauncher(false); void launch(t.app); } }]; if (canPin(t.app)) items.push(null, dockItem({ app: t.app })); return items; }; /** A folder's menu: Open (the folder), then Keep in Dock or Remove from Dock (its group). */ const folderItems = (f: Folder, idx: number): MenuEntry[] => { const items: MenuEntry[] = [{ label: "Open", run: () => openFolder(f.id, idx) }]; const group = GROUPS.find((g) => g.id === f.id); if (canPinGroup(group)) items.push(null, dockItem({ group })); return items; }; const ctxTile = ctx && !ctx.folder ? all.find((t) => t.id === ctx.id) : undefined; const ctxFolder = ctx?.folder && !q && !folder ? folders.find((f) => f.id === ctx.id) : undefined; const ctxHead = ctxTile?.title ?? ctxFolder?.title; useEffect(() => { if (ctx && !ctxHead) setCtx(null); }, [ctx, ctxHead]); // A press outside the open menu (which closes it, Menu.tsx) does nothing else: not the // backdrop's close, not a tile's or a folder's click. On the window, ahead of Menu's listener. useEffect(() => { if (!ctx) return; const down = (e: PointerEvent) => { if ((e.target as Element | null)?.closest?.("#launcher-menu")) return; ignoreDown.current = true; const eat = (ev: Event) => { ev.stopPropagation(); ev.preventDefault(); }; const done = () => { removeEventListener("pointerup", done, true); removeEventListener("pointercancel", done, true); // The click, if any, comes right after the pointerup. setTimeout(() => { ignoreDown.current = false; removeEventListener("click", eat, true); }, 0); }; addEventListener("click", eat, { capture: true, once: true }); addEventListener("pointerup", done, true); addEventListener("pointercancel", done, true); }; addEventListener("pointerdown", down, true); return () => removeEventListener("pointerdown", down, true); }, [ctx]); /** From tile i, the nearest tile in the next row up or down (the grids' columns vary with the width). */ const vertical = (i: number, down: boolean): number => { const els = [...(list()?.querySelectorAll("[data-idx]") ?? [])]; const c = els[i]; if (!c) return i; const r = c.getBoundingClientRect(), cx = r.left + r.width / 2; const below = els.map((el) => { const b = el.getBoundingClientRect(); return { j: Number(el.dataset.idx), dy: down ? b.top - r.bottom : r.top - b.bottom, dx: Math.abs(b.left + b.width / 2 - cx) }; }).filter((x) => x.dy > -2); if (!below.length) return i; const row = Math.min(...below.map((x) => x.dy)); return below.filter((x) => x.dy < row + 4).sort((a, b) => a.dx - b.dx)[0]!.j; }; const onKey = (e: ReactKeyboardEvent) => { const n = items.length; const i = Math.max(0, Math.min(sel, n - 1)); const arrow = e.key.startsWith("Arrow"); // With a modifier an arrow edits the text (select, by word), as in any field. if (arrow && (e.shiftKey || e.altKey || e.ctrlKey || e.metaKey)) return; if (e.key === "ContextMenu" || (e.key === "F10" && e.shiftKey)) { // The selected app's or folder's menu, at its tile's middle. e.preventDefault(); if (!cur) return; const r = list()?.querySelector(`[data-idx="${i}"]`)?.getBoundingClientRect(); setHint(true); openMenu(cur, r ? new DOMRect(r.left + r.width / 2, r.top + r.height / 2, 0, 0) : new DOMRect(innerWidth / 2, innerHeight / 2, 0, 0), i); } else if (e.key === "ArrowLeft" || e.key === "ArrowRight") { if (!n) return; e.preventDefault(); setHint(true); setSel(Math.min(n - 1, Math.max(0, i + (e.key === "ArrowRight" ? 1 : -1)))); } else if (e.key === "ArrowUp" || e.key === "ArrowDown") { if (!n) return; e.preventDefault(); setHint(true); setSel(vertical(i, e.key === "ArrowDown")); } else if (e.key === "Enter") { if (e.nativeEvent.isComposing) return; // an input method's Enter commits its text e.preventDefault(); pick(cur, i); } else if (e.key === "Escape") { e.preventDefault(); e.stopPropagation(); if (folder && !results) closeFolder(); else if (query) { setQuery(""); setSel(0); } else setLauncher(false); } else if (e.key === "Tab") { e.preventDefault(); // nothing else here takes the focus, and the page under it must not } }; /** The hint's click: close the launcher (the focus goes back where it was) and arm the prefix, so the shortcuts HUD shows (KeysHud.tsx). */ const showShortcuts = () => { setLauncher(false); KW.ui!.setPrefix(true); }; const onType = (v: string) => { setQuery(v); setSel(0); // A search covers every group: the folder closes. if (v.trim() && folder) setFolder(null); }; const cells = (entries: Entry[]) => entries.map((e, idx) => ( setSel(idx)} onPick={() => pick(e, idx)} onMenu={(at) => openMenu(e, at, idx)} /> )); const line = !cur ? "" : cur.folder ? cur.folder.tiles.map((t) => t.title).join(" · ") : cur.tile.why ? `${cur.tile.title}: ${cur.tile.why}` : `${cur.tile.desc}${cur.tile.running ? ` · ${cur.tile.running}: ↵ shows it` : ""}`; return ( ); }