// SPDX-License-Identifier: MIT // Copyright (C) 2026 Pooya Parsa // The serial applet (#tb-serial, its panel #tb-serial-panel): this // browser's Web Serial devices on the guest's /dev/ttyACM0..3 (serial.ts; // Settings, Serial ports, is the same with the help text). // - The item: the plug, dim with no device attached, with the count of // attached ones; red when a port failed (serialUse() says the error), amber // when one hung up. Hidden where the browser has no Web Serial (v.why), and // under sm. // - The panel: the state as the pill (Not running, No devices, N connected, // Error, Hung up); each attached port (#tb-serial-dev-) with its // label, its /dev/ttyACM and what it is doing (Settings.tsx serialUse()), // and its actions: Terminal (#tb-serial-dev--term, an icon: picocom // in the Serial Monitor app; the panel closes) and Disconnect // (#tb-serial-dev--disconnect: detach and forget; the panel stays); // with none, a line on what to connect. // - The header's Monitor (the kernel log, where serial.ts logs the ports // attached) and Settings (Serial ports); the footer: Connect… // (#tb-serial-connect, the primary action), disabled until the system runs and while every port // has a device. It opens the browser's chooser from the click itself // (connectSerial()); the panel stays open, so the device shows up in it // (the chooser is no press in the page: BarPopover keeps it). Should the // panel close meanwhile (opened by hover), the device is attached all the // same and the toast says where. import { connectSerial, disconnectSerial, useSerial, type SerialView } from "../../serial.ts"; import { launch } from "../../launcher.ts"; import { serialTerminal } from "../../apps/tools/serialmon.ts"; import type { SerialPortState } from "../../sdk.ts"; import type { PopoverProps } from "../ControlCenter.tsx"; import { serialUse } from "../Settings.tsx"; import { PlugIcon, PromptIcon } from "../icons.tsx"; import { AppletButton, AppletPanel, ICON, type AppletItemProps, type AppletTone } from "./Applet.tsx"; import { Count, DeviceList, DeviceRow, Empty, RowButton, RowIcon, keepFocus, type DeviceTone } from "./Devices.tsx"; const ID = "serial"; /** A port's state: failed (its error shows), hung up, in use, idle. */ function portTone(p: SerialPortState): DeviceTone { if (p.error && !p.guestOpen) return "err"; if (p.hungUp) return "warn"; return p.guestOpen ? "ok" : "idle"; } /** The applet's state: the pill's word and tone, and a one-line summary (the item's tooltip). */ function summary(v: SerialView): { word: string; tone: AppletTone; line: string; attached: SerialPortState[] } { const attached = v.ports.filter((p) => p.attached); const tones = attached.map(portTone); const n = attached.length; const devices = `${n} serial device${n === 1 ? "" : "s"}`; if (!v.running) return { word: "Not running", tone: "pending", line: "Serial ports: once the system runs", attached }; if (!n) return { word: "No devices", tone: "off", line: "Serial ports: no device connected", attached }; if (tones.includes("err")) return { word: "Error", tone: "err", line: `Serial ports: ${devices}, one failed`, attached }; if (tones.includes("warn")) return { word: "Hung up", tone: "warn", line: `Serial ports: ${devices}, one hung up`, attached }; return { word: `${n} connected`, tone: "ok", line: `Serial ports: ${attached.map((p) => p.device).join(", ")}`, attached }; } export function SerialItem(props: AppletItemProps) { const v = useSerial(); if (v.why) return null; const s = summary(v); return ( {s.attached.length > 0 && } ); } export function SerialPanel(props: PopoverProps) { const v = useSerial(); const s = summary(v); const full = v.running && s.attached.length === v.ports.length; const panelId = `tb-${ID}-panel`; return ( } state={s.word} tone={s.tone} monitor="log" settings="serial" width={360} {...props} actions={() => ( )}> {(pick) => v.why ? Serial devices are not available: {v.why}. : !s.attached.length ? ( {v.running ? <>No serial device. Connect one (an ESP32, an Arduino) and use it as /dev/ttyACM0 in the guest. : "Serial devices attach once the system runs; the ones allowed before come back by themselves."} ) : ( {s.attached.map((p) => ( pick({ label: "Terminal", run: () => void launch(serialTerminal(p.device)) })}> keepFocus(panelId, () => disconnectSerial(p.slot))}>Disconnect ))} )} ); }