# c8ui toolkit

Metro UI toolkit: `libc8ui-core` (widgets, icon themes, no window system) and `libc8ui` (Wayland client host); and `c8-base` (`base/`, a static library and headers with pkg-config `c8-base`, GLib and GIO only: what the apps' cores, tile providers, the shell and PC settings' daemons share under the UI).
Depends on: cairo, pango, glib, xkbcommon, librsvg (`svg` feature, on by default).

Every app is built on this: read it before writing or changing app UI.

Kept apart for their size; read the one the work is in:
- `DRAWING.md` (beside this file): damage, drawing fast (kept
  drawings, pictures), what a draw fn may and must do. Read it before
  writing a custom widget or any draw fn, in the toolkit or an app.
- `WIDGETS.md` (beside this file): what there is to build with: the
  bigger widgets, file pickers and thumbnails, charms, clipboard,
  drag and drop, menus, popups, hub. Read it before building app UI.
- `base/CLAUDE.md`: c8-base (`json.h`, `http.h`, `tiles.h`…).

- Core is host-agnostic: a host implements `struct c8ui_host_impl`
  (queue_redraw, request_frame, timers, cursor, shortcut inhibit,
  clipboard). `c8ui-wayland` is one host (GLib main loop, xdg-shell,
  fractional scale, cursor-shape, clipboard). `c8ui_window_new_bare` +
  `c8ui_window_configure` host a surface whose role the caller gives (how
  c8-shell makes its shell surfaces); `c8ui_window_set_background` (alpha
  < 1 = see-through). A window's two shm buffers (16 MB full screen) are
  let go when it's unmapped, `REST_S` (5 s) after it last drew (`rest`,
  made again for the next frame, a couple of ms), and while
  `c8ui_window_suspend`ed (mapped but not shown, as Start behind an app:
  nothing is drawn until it's lifted); one the compositor still holds
  goes on its release (`buffer.drop`).
- For an app with a surface of its own under its window (c8-video's
  video): `c8ui_window_set_fullscreen`/`_is_fullscreen`/`_on_fullscreen`
  (the toplevel's configured state), `c8ui_window_scale`/`_on_scale`,
  and `C8UI_CURSOR_NONE` with `c8ui_widget_set_cursor` (applied at once
  when the pointer is over the widget; `w->cursor` alone waits for it
  to move).
- Narrow windows (snapped to a quarter, ~474 px): `c8ui_root_is_narrow`
  (below `C8UI_NARROW_WIDTH`, 600). An app builds a page once and the
  widgets reflow by themselves; building wide and narrow apart and
  rebuilding on the flip is a design mistake (the user's rule,
  2026-12-01: Cirrus pages stayed wide in a snapped window because one
  builder never ran again). How: the impl's `narrow` hook, called
  children first when the root crosses the line and when a widget is
  put in a tree whose root has a size (`tell_narrow` in core.c; every
  hook hears its first state, `narrow_told`), and `w->narrow`
  (`c8ui_widget_narrow`) kept current on every rooted widget, hook or
  not. On it: `c8ui_box_set_narrow_padding`/`_narrow_spacing`/
  `_narrow_orientation` (a row that goes down), `c8ui_label_set_narrow_style`,
  `c8ui_scroll_set_narrow_directions`, `c8ui_widget_set_narrow_hidden`
  (`narrow_hides`, applied in `tell_narrow`), a horizontal split stacks
  itself, rich columns become one column of the width given, a scroll
  inside one going down gives way (measures as tall as its child, so a
  sideways list sits in a column), `c8ui_pages` goes one at a time
  (narrow, or under its pane + 460, so 760 for a 300 pane, 800 for PC
  settings' 340; the open page kept), the hub turns into a pivot of its
  named sections or, with `c8ui_hub_set_narrow_column`, one column
  going down (News, Weather; back wide it pans to the section that was
  showing), Miller columns show the last one. An app's own widget does
  the same with the hook (Cirrus's section heads and `mover`).
  `c8ui_root_on_resize` (every size change, before the next layout) is
  for sizes, such as a list's cell width, never for building again;
  showing and hiding panes there (Files' places, Notes) is fine.
  `c8ui_pages_set_narrow` and `c8ui_miller_set_narrow` are gone; every
  app, c8-lua's bundled ones, the gallery and the template build once
  (2026-12-01; the shell's share picker only sizes its band in `resized`, its margins centring it, which is what the hook is for). The impl and the
  widget struct grew for this: rebuild everything. Apps
  narrow go to one column like a phone app: one pane at a time, lists
  before details, a back button between. A `fixed_width` gives way
  when the room given is smaller; the app bar wraps its commands into
  rows (global ones on top) when one row won't hold them.
  `c8ui_nav_set_menu` makes a nav a narrow page list (arrows move a
  cursor, Enter/click opens, the selected item too). `c8ui_pages`
  (`pages.c`) is PC settings' layout once, for any app: a dark page
  list beside the page, or one at a time with a back button (Escape,
  `c8ui_pages_back`), the title (SUBHEADER narrow) and paddings, one
  page built for both widths; the app's build fn fills a page's body.
  `c8ui_pages_set_sub` puts a page over the chosen one (Tailscale's
  device page, Tile maker's editor; `c8ui_pages_on_back` hears it go).
  Back to the list destroys the page, so a page stops what it does in
  its `on_destroy`. PC settings, Tailscale and Tile maker use it.
  Tested headless (nested c8 at 1600x900, Super+Left/Right to ~533 and
  Super+Down back, 2026-12-01): those three (page kept on snap, back to
  the list, unsnapped beside the list), Cirrus's hub, profile, feed and
  thread pages, News, Weather, Files' split, Photos, Video, PC info,
  shortcuts and Night light, Calculator, Notes, both galleries. Untested: touch, a window resized by dragging across
  the line, the Lua bindings over it.
- `c8ui_window_on_close_request`: the compositor's close (Alt+F4, the
  bar's button) asks first, and false keeps the window, for asking
  about unsaved work (c8-lua's `can_close`). Without one it closes as
  before. `win:close`/`c8ui_window_destroy` never ask.
- Scroll events carry `precise` (touchpad) and `mods`;
  `C8UI_EVENT_SCROLL_STOP` is the fingers lifting (wl_pointer.axis_stop).
- Touch (wl_touch): `C8UI_EVENT_TOUCH_DOWN/MOTION/UP/CANCEL` with
  `touch_id` and `time`. The widget that returns true for DOWN owns that
  finger; a finger nobody takes acts as the mouse (press, motion,
  release, then no hover), so mouse-only widgets work by touch.
- Touchpad swipes of 3+ fingers (pointer-gestures):
  `C8UI_EVENT_SWIPE_BEGIN/UPDATE/END` with `fingers`; the widget under
  the pointer that takes BEGIN gets the rest (the compositor sends two
  fingers on the touchscreen as a three-finger one). `c8ui_tab_swipe` is the one rule
  for a sideways one stepping tabs (once per swipe, `C8UI_TAB_SWIPE` px):
  Start's All apps tabs, pivots and hub sections all use it. Pinches the same way:
  `C8UI_EVENT_PINCH_BEGIN/UPDATE/END`, `scale` against the start. Two
  fingers on the touchscreen are one too (`tpinch_*` in core.c): a second
  finger landing while one is down offers PINCH_BEGIN at their midpoint;
  a taker gets both as PINCH_UPDATEs (`dx`/`dy` the midpoint's move), the
  first finger's touch is cancelled, and the finger left when the other
  lifts is ignored until it lifts too. Nobody taking it: plain touches.
- Icons: `c8ui_icon_*` (freedesktop theme lookup, SVG via librsvg, a
  background loader the host polls by fd; c8ui-wayland hooks it up).
- Retained widget tree with measure/layout/draw/event/tick vtables,
  overlays for popups/dialogs, focus chain, Metro entrance animation
  (`c8ui_widget_enter` slides in from the right; `c8ui_widget_enter_up`
  rises from below, for what comes up over a bar: the shell's cards
  over XR's charms bar; `c8ui_widget_leave_down` is the way out, fading
  and dropping over `C8UI_LEAVE_MS`, then not drawn until the caller
  takes it away). `leaving`, `tilts` and `tilt` (the press tilt,
  WIDGETS.md) are fields of `c8ui_widget`: rebuild everything with
  c8ui after they change.
- Widgets destroyed during event dispatch go to a graveyard and are freed
  after dispatch. Callbacks must not keep pointers to widgets past
  `on_destroy`; pages clear their static pointers in the page box's
  `on_destroy`, only if the pointer is still that widget (`if (x == w)`):
  a rebuild sets the new one before the old one is destroyed.
  `c8ui_widget_track(w, &slot)` does exactly that for one pointer (sets
  it, clears it on destroy if it still points at w; the slot must
  outlive the widget): use it instead of a handler that only clears
  pointers.
- Apps: `c8ui_app_main(&info, argc, argv)` (`src/app.c`) makes one copy
  run: it owns the app id on the session bus (DO_NOT_QUEUE) and serves
  `org.freedesktop.Application`; a later launch resolves its arguments
  to URIs (its own cwd; an argument that has a scheme other than
  `file:` goes as it is, since GIO's GFile for an unknown scheme reads
  `at://did:plc:x` as host:port and made it `at://did:0`) and hands them over with Open/Activate, carrying
  its `XDG_ACTIVATION_TOKEN`, then exits. `info.open` runs for every
  launch; `c8ui_window_present` uses that token (or one from input on
  another of our windows; deferred until mapped). The charms then serve
  on that connection instead of queueing. `c8ui_window_open_uri` opens
  in the default app with a token of ours (a GAppLaunchContext
  subclass). The compositor gives everything `c8_launch` runs a token
  (`c8_launch_token`, accepted by `request_activate`), so Start launches
  of a running app bring it forward. Wake-ups (`info.wake`,
  `info.command`, `c8ui_app_wake_at/cancel/done`, the shell's
  `org.cont8nuum.Wake1`): argv's `--c8-wake TAG DUE` (taken out before
  the URIs) is handed to a running copy as ActivateAction `c8-wake`
  ([s tag, x due]); without one the app starts, runs `startup` then
  `wake` instead of `open`, held (`c8ui_app_hold`) until
  `c8ui_app_wake_done` or `C8UI_WAKE_SECONDS`. `c8ui_app_info` grew:
  everything that calls `c8ui_app_main` must be rebuilt with it (the
  library reads `command`). `toolkit/c8ui/template/` is a standalone
  app to copy (meson, `.desktop` with `MimeType=` and
  update-desktop-database, icon); not built by `build.sh`.
- Touch panning: `struct c8ui_drag` + `c8ui_drag_touch` (private, core.c)
  is shared by scroll, hub and pivot: a drag along the widget's axis
  pans, a tap clicks, a hold right-clicks, and a drag the other way is
  offered to the widgets around it (as a fresh TOUCH_DOWN) before it
  becomes the mouse (sliders, text selection).
- Scrolling something into view goes through `c8ui_widget_reveal`, which
  calls the `reveal` hook of every ancestor that has one (scroll, hub);
  don't assume a `clips` parent is a `c8ui_scroll`. Adding fields to
  `c8ui_widget_impl` changes its size: rebuild everything that defines
  widgets (c8-shell) with c8ui, which `build.sh` does.
- Text: Selawik is unhinted (no fpgm/prep), so FreeType autohints it;
  `c8ui_text_layout` uses grey AA + slight hinting, and `c8ui_app_new`
  (and c8's main, exported to the session) sets `FREETYPE_PROPERTIES=
  autofitter:no-stem-darkening=0` first thing, or Light text is faint
  and ragged. Full hinting looked crisper but broke the spacing.
- Theme (accent, background, font) comes from config.ini (`[appearance]`,
  `[general] font`; an old start.ini `[appearance]` is the fallback)
  and is watched with GFileMonitor; c8-settings also takes it from the
  settings snapshot.
