Skip to main content

kui_native/
lib.rs

1//! The windowed kui runner: a winit window and a wgpu renderer around
2//! `kui_core`, with the [`App`] trait an application implements.
3//!
4//! kui is a Rust UI library whose view is a plain data tree rebuilt every
5//! frame. `kui_core` holds the model, the layout and the widgets, `kui_wgpu`
6//! draws them, and this crate puts a window around both; it is the crate
7//! most apps depend on. It re-exports all of kui-core, so `kui_native::`
8//! reaches everything, and adds the [`App`] trait, the [`app`] launcher
9//! builder, multiple windows, the clipboard, file dialogs, audio, AccessKit
10//! and a headless [`testing`] driver. `kui-derive` supplies
11//! `#[derive(Message)]`; kui-lua, kui-ffi and kui-node are bindings over the
12//! same core.
13//!
14//! # Quick start
15//!
16//! ```rust,no_run
17//! use kui_native::{App, TextStyle, Ui};
18//!
19//! struct Hello;
20//!
21//! impl App for Hello {
22//!     // Called once per frame. Everything on screen is declared here,
23//!     // from scratch, every time.
24//!     fn view(&mut self, ui: &mut Ui<'_>) {
25//!         let theme = ui.theme();
26//!         ui.text("Hello, kui", TextStyle::new(24.0).color(theme.fg));
27//!     }
28//! }
29//!
30//! fn main() -> Result<(), Box<dyn std::error::Error>> {
31//!     kui_native::app("Hello").size(360.0, 200.0).run(Hello)
32//! }
33//! ```
34//!
35//! # State and events
36//!
37//! A button carries a message. After the frame, each event it produced
38//! reaches [`App::on_event`], and [`UiEvent::message`] reads the message
39//! back as the app's own enum.
40//!
41//! ```rust,no_run
42//! use kui_native::widgets;
43//! use kui_native::{App, Message, NodeSpec, TextStyle, Ui, UiEvent};
44//!
45//! // `derive(Message)` turns each variant into plain data on the way out
46//! // and back into `Msg` on the way in.
47//! #[derive(Message, Clone, Debug, PartialEq)]
48//! enum Msg {
49//!     Inc,
50//!     Dec,
51//! }
52//!
53//! #[derive(Default)]
54//! struct Counter {
55//!     count: i64,
56//! }
57//!
58//! impl App for Counter {
59//!     fn view(&mut self, ui: &mut Ui<'_>) {
60//!         let t = ui.theme();
61//!         ui.with(NodeSpec::column().fill().center().gap(16.0).bg(t.bg), |ui| {
62//!             ui.text(&self.count.to_string(), TextStyle::new(56.0).color(t.fg));
63//!             ui.with(NodeSpec::row().gap(8.0), |ui| {
64//!                 widgets::button(ui, "-1", Msg::Dec);
65//!                 widgets::button(ui, "+1", Msg::Inc);
66//!             });
67//!         });
68//!     }
69//!
70//!     fn on_event(&mut self, ev: UiEvent) {
71//!         match ev.message::<Msg>() {
72//!             Some(Msg::Inc) => self.count += 1,
73//!             Some(Msg::Dec) => self.count -= 1,
74//!             // Not ours: a resize, a focus change, a window event.
75//!             None => {}
76//!         }
77//!     }
78//! }
79//!
80//! fn main() -> Result<(), Box<dyn std::error::Error>> {
81//!     kui_native::app("Counter").size(360.0, 240.0).run(Counter::default())
82//! }
83//! ```
84//!
85//! The book walks from here through controls, lists, the keyboard, floating
86//! windows, motion, effects and tests, one step per chapter.
87//!
88//! # Where to look
89//!
90//! - [`App`]: the trait an app implements; `view`, `on_event`, `setup`,
91//!   `teardown`.
92//! - [`app`] and [`Launcher`]: title, size, chrome, icon, extensions, then
93//!   [`Launcher::run`]; [`Launcher::open`] gives a host that owns the loop a
94//!   [`PumpRunner`] instead.
95//! - [`Ui`]: what `view` is given; [`Ui::with`], [`Ui::text`],
96//!   [`Ui::window`].
97//! - [`NodeSpec`] and [`TextStyle`]: a box's layout, look and handlers; a
98//!   text's size, font and colour.
99//! - [`widgets`]: buttons, checkboxes, text inputs, selects, sliders,
100//!   lists, tables and menus.
101//! - [`UiEvent`]: what `on_event` receives, with the readers on it.
102//! - [`testing`]: a headless driver for an `App`, for `cargo test`.
103//! - From kui-core: [`theme`], [`anim`] (transitions and springs),
104//!   [`window`] (several windows), [`message`] and [`Value`] (the payload
105//!   behind a message), [`Core`] (one window's runtime, for hosts and
106//!   tests).
107//!
108//! # Windows
109//!
110//! One event loop, any number of windows. The launcher opens the main
111//! window; a frame that declares another with [`Ui::window`] opens it on the
112//! same session and GPU device, and a frame that stops declaring it closes
113//! it. `view` runs once per open window per frame, [`Ui::window_name`] says
114//! which (`"main"` for the launcher's), and every event carries its window's
115//! [`WindowId`].
116//!
117//! # Features
118//!
119//! - `audio` (default): plays sounds through kira and cpal. Off, audio
120//!   commands are dropped and no audio library is linked (ALSA on Linux).
121//! - `accesskit` (default): exposes the access tree to screen readers
122//!   through AccessKit. Off, the tree is still built and nothing reaches
123//!   the OS.
124//! - `derive` (default): `#[derive(Message)]`. Off, messages are built and
125//!   matched as [`Value`]s by hand.
126//! - `dialogs` (default): the platform's file dialogs through rfd (the XDG
127//!   portal on Linux). Off, every dialog answers at once as cancelled.
128//! - `smoke`: honours `KUI_SMOKE_FRAMES=n`, which closes the window after
129//!   `n` frames, in a release build. A debug build honours it regardless.
130//!
131//! # Linux
132//!
133//! Building needs `pkg-config` and ALSA's headers (`libasound2-dev` on
134//! Debian and Ubuntu). A window loads the rest at run time: `libxkbcommon`
135//! (and `libxkbcommon-x11` on X11); `libX11`, `libXcursor`, `libXrandr` and
136//! `libXi` on X11 or `libwayland-client` on Wayland; `libvulkan` with a
137//! driver, or `libEGL`. A missing one fails when the window opens, naming
138//! the library. macOS and Windows need only a Rust toolchain.
139//!
140//! The book: <https://kui-book.qxuken.dev>. Repository and design records
141//! (under `docs/adr`): <https://github.com/qxuken/kui>.
142
143use std::sync::Arc;
144
145pub use kui_core::widgets;
146pub use kui_core::*;
147
148mod access_bridge;
149pub mod audio;
150mod axis_lock;
151/// What shows through the window: a blur, a tint, the desktop, or nothing.
152mod backdrop;
153mod clipboard;
154mod decode;
155pub use decode::{Animation, AnimationFrame, Pixels, Showing, decode_animation, decode_image};
156mod dialogs;
157/// The wallpaper a `Tinted` backdrop draws where the OS has no material.
158mod ground;
159mod icon;
160mod keys;
161/// A `Blur` backdrop asked of a Linux compositor.
162#[cfg(not(any(target_os = "macos", target_os = "windows")))]
163mod linux_blur;
164/// The traffic lights' keep-out and the OS titlebar's height, measured.
165#[cfg(target_os = "macos")]
166mod macos_chrome;
167/// Files dragged in from the Finder, with where they are.
168#[cfg(target_os = "macos")]
169mod macos_drop;
170#[cfg(target_os = "macos")]
171mod macos_force;
172/// A non-activating window that refuses to become key (the popup flick).
173#[cfg(target_os = "macos")]
174mod macos_key;
175/// The platform's own context menu, where there is one.
176#[cfg(target_os = "macos")]
177mod macos_menu;
178/// The documents the Finder, the Dock and `open -a` hand the app.
179#[cfg(target_os = "macos")]
180mod macos_open;
181/// The palette's and dictation's inserts, which winit's view drops.
182#[cfg(target_os = "macos")]
183mod macos_text_input;
184mod menus;
185mod pacer;
186mod pane;
187mod popups;
188mod retarget;
189mod retry;
190mod scroll_gesture;
191mod secure_input;
192pub mod testing;
193mod windows;
194
195use pane::{
196    Pane, appearance_of, apply_ime_off, ime_off_change, level_change, level_supported,
197    option_as_alt_change, sync_env, theme_appearance,
198};
199/// The OS settings winit has no call for, asked once and re-asked when the
200/// user has evidently been in a settings app.
201mod system_env;
202/// The installed fonts changing while the app runs: the platform's signal,
203/// turned into a rescan (`Core::reload_system_fonts`).
204mod system_fonts;
205#[cfg(target_os = "windows")]
206mod windows_anim;
207/// The terminal a `windows_subsystem = "windows"` app was launched from,
208/// given back to it (`attach_parent`).
209#[cfg(target_os = "windows")]
210mod windows_console;
211#[cfg(target_os = "windows")]
212mod windows_nc;
213/// The OS's light/dark switch reaching winit while the app runs.
214#[cfg(target_os = "windows")]
215mod windows_theme;
216
217use winit::application::ApplicationHandler;
218use winit::dpi::{LogicalPosition, LogicalSize};
219use winit::event::{ElementState, Ime, MouseButton as WinitButton, WindowEvent};
220use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop, EventLoopProxy};
221use winit::keyboard::{Key as WinitKey, ModifiersState, NamedKey};
222// `WindowId` is `kui_core`'s here (re-exported above); winit's own is the
223// OS handle the event loop routes by, and only this file names it.
224use winit::window::{CursorIcon, ResizeDirection, Window, WindowId as WinitWindowId};
225
226/// What a kui application implements: a `view` that declares each frame
227/// and an `on_event` that answers what the frame produced.
228///
229/// Only [`view`](Self::view) is required. Hand a value of the type to
230/// [`Launcher::run`], or to [`testing::Drive`] in a test.
231pub trait App {
232    /// Builds one window's frame. Called once per open window per frame;
233    /// `ui.window_name()` says which (`"main"` for the launcher's).
234    fn view(&mut self, ui: &mut Ui<'_>);
235    /// Receives one event the frame produced: a click's message, a key
236    /// press, a resize, a window event. Called after the frame, once per
237    /// event; [`UiEvent::message`] reads a `#[derive(Message)]` back out.
238    /// The default ignores every event.
239    fn on_event(&mut self, _ev: UiEvent) {}
240    /// [`Self::on_event`] with the [`Core`] of the window the event came
241    /// from. Override it when answering an event means calling the core:
242    /// writing the clipboard, asking for a paste, moving focus, revealing a
243    /// row, asking for a frame. A clipboard write goes out with this turn's
244    /// actions; a focus move or a reveal is seen by the next frame. The
245    /// default calls `on_event`, so an app that overrides only that one is
246    /// unchanged.
247    fn on_event_with(&mut self, ev: UiEvent, _core: &mut Core) {
248        self.on_event(ev)
249    }
250    /// Called once, before the window opens, with a [`Waker`] the app may
251    /// clone into any thread. The loop parks between events, so a thread
252    /// that changed what `view` will show (a PTY reader, a file watcher, a
253    /// socket) calls [`Waker::wake`] to get a frame drawn. The default
254    /// drops the waker.
255    fn setup(&mut self, _waker: Waker) {}
256    /// Called once, when the main window is going for good (its close
257    /// button, Quit, `WindowCommand::Close` on it, a pumped runner ending),
258    /// before `run` returns. Save a session, a draft or a position here;
259    /// the frame is over, there is no `Ui` and nothing draws. A crash under
260    /// `run` does not reach it; a panic unwinding through a [`PumpRunner`]
261    /// does, and a `teardown` that panics there aborts. The default does
262    /// nothing.
263    fn teardown(&mut self) {}
264}
265
266/// A handle into the event loop that any thread may hold; [`Waker::wake`]
267/// asks for a frame from wherever the app's data arrived. Cheap to clone,
268/// and harmless after the loop has ended (a wake nobody hears is dropped).
269/// An app gets one in [`App::setup`], a host from [`PumpRunner::waker`].
270#[derive(Clone)]
271pub struct Waker(EventLoopProxy<access_bridge::UserEvent>);
272
273impl Waker {
274    /// Asks every window for a frame: the loop wakes, `view` runs and the
275    /// frame is drawn. Safe from any thread and at any rate, since wakes
276    /// coalesce into the loop's next turn rather than queueing frames.
277    pub fn wake(&self) {
278        let _ = self.0.send_event(access_bridge::UserEvent::Wake);
279    }
280
281    /// Asks every window for a frame at `at` (backlog F135): the loop
282    /// sleeps until then and `view` runs, with no thread waiting for it.
283    /// The earliest time asked for wins; one already past is a wake now.
284    /// From the view itself `Ui::request_frame_at`, on the frame clock,
285    /// says the same thing without a `Waker`.
286    pub fn wake_at(&self, at: std::time::Instant) {
287        let _ = self.0.send_event(access_bridge::UserEvent::WakeAt(at));
288    }
289}
290
291impl std::fmt::Debug for Waker {
292    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
293        f.write_str("Waker")
294    }
295}
296
297/// Who draws the window chrome.
298#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
299pub enum Chrome {
300    /// The OS titlebar and buttons (the default).
301    #[default]
302    Native,
303    /// The app draws its own titlebar (`widgets::titlebar`). On macOS the
304    /// native traffic lights stay, overlaid on the content (their rect is
305    /// reported in `env.window.native_controls`); elsewhere the window is
306    /// undecorated and the runner synthesizes edge resizing, double-click
307    /// maximize, and applies the `WindowCommand`s chrome nodes produce.
308    Custom,
309    /// No decorations and no chrome expectations (splash screens, popups).
310    Borderless,
311}
312
313/// How outline glyphs are antialiased.
314#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
315pub enum TextAa {
316    /// LCD subpixel coverage when the GPU can blend per channel, grayscale
317    /// otherwise (the default). `KUI_TEXT_AA=gray|subpixel` overrides.
318    #[default]
319    Auto,
320    Grayscale,
321    Subpixel,
322}
323
324/// Starts a [`Launcher`] for a window titled `title`.
325///
326/// Chain the window's options, then [`Launcher::run`]:
327///
328/// ```rust,no_run
329/// # use kui_native::{App, Ui};
330/// # struct Mine;
331/// # impl App for Mine { fn view(&mut self, _ui: &mut Ui<'_>) {} }
332/// kui_native::app("Mine")
333///     .size(800.0, 600.0)
334///     .min_size(320.0, 240.0)
335///     .devtools(true)
336///     .run(Mine)
337///     .unwrap();
338/// ```
339///
340/// The default window is 960 by 640 logical pixels with the OS's own
341/// chrome. `KUI_WINDOW=WxH` in the environment overrides the size.
342pub fn app(title: &str) -> Launcher {
343    Launcher {
344        title: title.to_string(),
345        chrome: Chrome::Native,
346        size: (960.0, 640.0),
347        min_size: None,
348        max_size: None,
349        extensions: Extensions::new(),
350        text_aa: TextAa::Auto,
351        diagnostics: None,
352        core: None,
353        setup_core: Vec::new(),
354        deferred_events: false,
355        system: SystemEnv::default(),
356        icon: None,
357        icon_resource: None,
358        frame_latency: kui_wgpu::DEFAULT_FRAME_LATENCY,
359        backdrop: Backdrop::Opaque,
360    }
361}
362
363/// One [`Launcher::setup_core`] step.
364type CoreSetup = Box<dyn FnOnce(&mut Core)>;
365
366/// The windowed runner's builder, made by [`app`].
367///
368/// Every method takes and returns the launcher; [`Launcher::run`] opens the
369/// window and runs the event loop, [`Launcher::open`] opens it and hands the
370/// loop back as a [`PumpRunner`].
371pub struct Launcher {
372    title: String,
373    chrome: Chrome,
374    size: (f64, f64),
375    /// Inner-size bounds (logical px) handed to the OS, which enforces them
376    /// for user resizing; `None` leaves that side unbounded.
377    min_size: Option<(f64, f64)>,
378    max_size: Option<(f64, f64)>,
379    extensions: Extensions,
380    text_aa: TextAa,
381    /// The main window's core, when the host made it ahead
382    /// ([`Launcher::core`]); the launcher makes one otherwise.
383    core: Option<Core>,
384    /// What to do to the main core before its first frame
385    /// ([`Launcher::setup_core`]).
386    setup_core: Vec<CoreSetup>,
387    /// Whether the core's diagnostics run (see `kui_core::diag`); None =
388    /// on in debug builds, off in release.
389    diagnostics: Option<bool>,
390    /// Whether the host answers events after the loop has handed them over
391    /// (see [`Launcher::deferred_events`]).
392    deferred_events: bool,
393    /// The OS settings the app pinned ([`Launcher::system`]); unknown is
394    /// not pinned.
395    system: SystemEnv,
396    /// The windows' icon ([`Launcher::icon`]), checked when it was given.
397    icon: Option<winit::window::Icon>,
398    /// The executable's icon resource on Windows
399    /// ([`Launcher::icon_resource`]).
400    icon_resource: Option<u16>,
401    /// Frames queued ahead of the one on screen
402    /// ([`Launcher::frame_latency`]).
403    frame_latency: u32,
404    /// What shows through the app's windows ([`Launcher::backdrop`]).
405    backdrop: Backdrop,
406}
407
408impl Launcher {
409    /// Who draws the window chrome; see [`Chrome`]. The OS by default.
410    pub fn chrome(mut self, chrome: Chrome) -> Self {
411        self.chrome = chrome;
412        self
413    }
414
415    /// Glyph antialiasing; see [`TextAa`].
416    pub fn text_aa(mut self, aa: TextAa) -> Self {
417        self.text_aa = aa;
418        self
419    }
420
421    /// How many frames may be queued ahead of the one on screen, for every
422    /// window. Two by default ([`kui_wgpu::DEFAULT_FRAME_LATENCY`]), which
423    /// keeps every vsync fed at light load; one on Windows, where one
424    /// already does. On macOS 14+ frames are paced to the display, so the
425    /// second queued frame costs no latency; on Linux and under a pumped
426    /// runner it reaches the screen one vsync later. `KUI_FRAME_LATENCY`
427    /// overrides the value and `KUI_FRAME_PACING=0` turns the pacing off.
428    /// Values below one are one.
429    pub fn frame_latency(mut self, frames: u32) -> Self {
430        self.frame_latency = frames.max(1);
431        self
432    }
433
434    /// Whether the core looks for silent misconfigurations and the runner
435    /// prints them to stderr (see [`diag`]). On in debug builds and off in
436    /// release unless set, so a shipped app stays quiet.
437    pub fn diagnostics(mut self, on: bool) -> Self {
438        self.diagnostics = Some(on);
439        self
440    }
441
442    /// Opens the app with the devtools panel docked beside it: the event
443    /// stream, the runtime's facts and the node tree. `KUI_DEVTOOLS=1` in
444    /// the environment asks the same of an app that never called this.
445    /// Sugar for `setup_core(|c| c.set_devtools(on))`.
446    pub fn devtools(self, on: bool) -> Self {
447        self.setup_core(move |core| core.set_devtools(on))
448    }
449
450    /// Respells the chord that moves the keyboard into the devtools panel
451    /// and back, and brings a hidden panel back, from its default
452    /// `Ctrl+Shift+I`: `Accel::parse("f12")`, `"mod+shift+d"`, any spelling
453    /// a menu item takes. The panel's other chords stay
454    /// `Ctrl+Shift+<letter>`, and `Ctrl+Shift+I` is the app's again. Sugar
455    /// for `setup_core(|c| c.set_devtools_key(key))`.
456    pub fn devtools_key(self, key: Accel) -> Self {
457        self.setup_core(move |core| core.set_devtools_key(key))
458    }
459
460    /// Runs `f` on the main window's [`Core`] before its first frame: the
461    /// place for what a core is told rather than declared in a view, such
462    /// as a pinned theme, `set_native_menus` or the devtools. Every call
463    /// adds one; they run in order.
464    pub fn setup_core(mut self, f: impl FnOnce(&mut Core) + 'static) -> Self {
465        self.setup_core.push(Box::new(f));
466        self
467    }
468
469    /// Opens the main window on `core` rather than on one the launcher
470    /// makes. Everything registered on it beforehand (fonts, images,
471    /// sounds, tokens, a pinned theme, menus) reaches the window, and its
472    /// session becomes the app's, so a second window joins it and handles
473    /// minted by an earlier headless frame keep drawing. The launcher's own
474    /// settings still apply on top, in order: [`Launcher::diagnostics`],
475    /// then `KUI_DEVTOOLS`, then every [`Launcher::setup_core`]. For a host
476    /// that drew headless first, or built the core through a binding.
477    pub fn core(mut self, core: Core) -> Self {
478        self.core = Some(core);
479        self
480    }
481
482    /// Declares that the app answers an event after `on_event` returns,
483    /// which only a host driving the loop itself can do ([`Launcher::open`],
484    /// [`PumpRunner`]): `on_event` keeps the event, the pump returns, the
485    /// host runs its handler and submits the next view.
486    ///
487    /// With it set, an input whose events reached the app does not ask for
488    /// a frame itself; the host asks with [`PumpRunner::request_redraw`]
489    /// once its handler has run, so a button's release and what the release
490    /// did land in one frame instead of two. Nothing else changes: a
491    /// transition, a caret blink, a [`Waker`] wake or the OS's own repaint
492    /// still paint when they ask, and an input that reached nobody still
493    /// asks for its own frame.
494    pub fn deferred_events(mut self) -> Self {
495        self.deferred_events = true;
496        self
497    }
498
499    /// Pins part of `env.system` for this app's windows. Every field of
500    /// `pinned` that is not "cannot tell" is what the views read, over
501    /// whatever the OS says, for as long as the app runs; the fields left
502    /// at their default keep following the OS, and a change to one of
503    /// those still arrives as the `system` event.
504    ///
505    /// ```rust,no_run
506    /// # use kui_native::{SystemEnv, MotionPref};
507    /// kui_native::app("mine").system(SystemEnv { motion: MotionPref::Reduced, ..Default::default() });
508    /// ```
509    ///
510    /// Use it to see the window a user who asked for less motion, or a
511    /// dark appearance, would get. It is on the launcher rather than an
512    /// environment variable so that a shipped app's motion is decided in
513    /// its own code; a headless core takes the same reading through
514    /// `core.env.system`.
515    pub fn system(mut self, pinned: SystemEnv) -> Self {
516        self.system = pinned;
517        self
518    }
519
520    /// The icon every window of the app is created with: `rgba` is
521    /// `width` by `height` pixels, four bytes each, row by row from the top
522    /// left, alpha not premultiplied. Windows shows it in the title bar,
523    /// Alt-Tab and the taskbar, X11 in the window manager's; macOS and
524    /// Wayland take the app's icon from the bundle or the `.desktop` file
525    /// and ignore this. Pass something a taskbar can shrink cleanly, 64 to
526    /// 256 px. Panics when the pixels are not that size;
527    /// [`Launcher::try_icon`] returns the reason instead.
528    ///
529    /// ```rust,no_run
530    /// # let rgba = vec![0u8; 64 * 64 * 4];
531    /// kui_native::app("mine").icon(rgba, 64, 64);
532    /// ```
533    ///
534    /// On Windows, [`Launcher::icon_resource`] names the icon linked into
535    /// the executable and wins over these pixels.
536    pub fn icon(self, rgba: Vec<u8>, width: u32, height: u32) -> Self {
537        match self.try_icon(rgba, width, height) {
538            Ok(this) => this,
539            Err(e) => panic!("kui: {e}"),
540        }
541    }
542
543    /// [`Launcher::icon`] for pixels that came from outside the program,
544    /// refused with the reason rather than a panic. The launcher is
545    /// consumed either way.
546    pub fn try_icon(mut self, rgba: Vec<u8>, width: u32, height: u32) -> Result<Self, String> {
547        self.icon = Some(icon::from_rgba(rgba, width, height)?);
548        Ok(self)
549    }
550
551    /// [`Launcher::icon`] from an image file's bytes — a PNG, JPEG, WebP
552    /// or GIF, decoded by [`decode_image`] (backlog F138):
553    /// `.icon_bytes(include_bytes!("icon.png"))`. Panics, naming why, on
554    /// bytes that are not an image; [`Launcher::try_icon_bytes`] returns
555    /// the reason instead.
556    pub fn icon_bytes(self, bytes: &[u8]) -> Self {
557        match self.try_icon_bytes(bytes) {
558            Ok(this) => this,
559            Err(e) => panic!("kui: {e}"),
560        }
561    }
562
563    /// [`Launcher::icon_bytes`] for a file that came from outside the
564    /// program, refused with the reason rather than a panic.
565    pub fn try_icon_bytes(self, bytes: &[u8]) -> Result<Self, String> {
566        let p = decode_image(bytes).map_err(|e| format!("an icon: {e}"))?;
567        self.try_icon(p.rgba, p.width, p.height)
568    }
569
570    /// The executable's icon resource `id` as every window's icon, on
571    /// Windows: the `.ico` a `1 ICON "app.ico"` line in the program's `.rc`
572    /// links in, from which the title bar and the taskbar each load the
573    /// frame drawn for their size. A resource the executable does not have
574    /// is reported once on stderr, and [`Launcher::icon`]'s pixels are used
575    /// if there are any. Nothing on other platforms, so an app passes both
576    /// and each OS takes its own.
577    pub fn icon_resource(mut self, id: u16) -> Self {
578        self.icon_resource = Some(id);
579        self
580    }
581
582    /// What shows through the app's windows where a frame paints nothing,
583    /// or paints with alpha (backlog F126), by effect: `Transparent` (the
584    /// desktop as it is), `Blur` (a live blur of what is behind the
585    /// window), `Tinted` (the desktop's colour, steady). `Backdrop::Opaque`,
586    /// the default, is the window every app had before. Which regions show
587    /// it is the app's: paint them with alpha and the rest opaque.
588    ///
589    /// ```rust,no_run
590    /// # use kui_native::{App, Backdrop, Ui};
591    /// # struct Notes;
592    /// # impl App for Notes { fn view(&mut self, _ui: &mut Ui<'_>) {} }
593    /// kui_native::app("Notes")
594    ///     .custom_titlebar()
595    ///     .backdrop(Backdrop::Blur)
596    ///     .run(Notes)
597    ///     .unwrap();
598    /// ```
599    ///
600    /// How each platform gives it:
601    ///
602    /// | | `Blur` | `Tinted` |
603    /// |---|---|---|
604    /// | macOS | `NSVisualEffectView`, behind-window (the sidebar material) | `NSVisualEffectView`, the window-background material |
605    /// | Windows 11 22H2+ | Acrylic | Mica |
606    /// | KDE Plasma | the compositor's blur (`ext-background-effect-v1`, `org_kde_kwin_blur`, `_KDE_NET_WM_BLUR_BEHIND_REGION`) | the wallpaper, drawn by kui |
607    /// | GNOME, other Linux, Windows 10 | the wallpaper, drawn by kui (reads `Tinted`) | the wallpaper, drawn by kui |
608    ///
609    /// "The wallpaper, drawn by kui" is the desktop's picture — GNOME's
610    /// `picture-uri` (or `-dark`), Plasma's config, Windows'
611    /// `SPI_GETDESKWALLPAPER` — read on a thread, scaled down and blurred
612    /// once, and drawn under the frame aligned to where the window sits on
613    /// its monitor (centred on Wayland, which does not say); with no
614    /// wallpaper to read, the window is opaque. Hyprland, SwayFX and picom
615    /// blur translucent windows themselves when configured to: ask for
616    /// `Transparent` there. `KUI_BACKDROP_EMULATE=1` draws the wallpaper
617    /// for `Blur` and `Tinted` on Windows and Linux, to look at it (macOS
618    /// reads no wallpaper, so the window there is `Opaque`).
619    ///
620    /// Asking changes three things. Where the OS draws the effect the
621    /// frame is cleared to nothing rather than to the theme's `bg`, so what
622    /// a view paints is all that sits over it: a region with a translucent
623    /// `bg` (`theme.bg.with_alpha(0.6)`) shows the effect through it, and a
624    /// region with an opaque `bg` does not. Glyphs are antialiased in
625    /// grayscale under `TextAa::Auto`, since an LCD mask cannot be
626    /// composited over what the window cannot see. And on Windows the
627    /// device presents through DirectComposition, the one D3D12 swapchain
628    /// that takes alpha.
629    ///
630    /// What the platform gave is `ui.env().window.backdrop`, every frame,
631    /// and is what a view decides by — less where the platform has less
632    /// (a `Blur` asked of GNOME reads `Tinted`, or `Opaque` with no
633    /// wallpaper). Paint the translucent regions opaque when it reads
634    /// `Opaque`:
635    ///
636    /// ```rust
637    /// # use kui_native::{Backdrop, Color, Env};
638    /// fn pane_bg(env: &Env, base: Color) -> Color {
639    ///     match env.window.backdrop {
640    ///         Backdrop::Opaque => base,
641    ///         // The desktop unblurred: text over it is a lottery.
642    ///         Backdrop::Transparent => base.with_alpha(0.92),
643    ///         Backdrop::Blur | Backdrop::Tinted => base.with_alpha(0.55),
644    ///     }
645    /// }
646    /// # let _ = pane_bg(&Env::default(), Color::hex(0x202020ff));
647    /// ```
648    ///
649    /// It applies to the main window and to every window the app's frames
650    /// declare (`Ui::window`) — not to a popup, which is a menu surface,
651    /// nor to the devtools' window.
652    pub fn backdrop(mut self, backdrop: Backdrop) -> Self {
653        self.backdrop = backdrop;
654        self
655    }
656
657    /// Shorthand for `.chrome(Chrome::Custom)`.
658    pub fn custom_titlebar(self) -> Self {
659        self.chrome(Chrome::Custom)
660    }
661
662    /// Shorthand for `.chrome(Chrome::Borderless)`.
663    pub fn borderless(self) -> Self {
664        self.chrome(Chrome::Borderless)
665    }
666
667    /// Initial inner size, logical px (`KUI_WINDOW=WxH` still overrides).
668    /// Clamped into the `min_size`/`max_size` bounds, as the OS would.
669    pub fn size(mut self, w: f64, h: f64) -> Self {
670        self.size = (w, h);
671        self
672    }
673
674    /// Smallest inner size the user may resize the window to, logical px.
675    /// The OS enforces it; the initial size is clamped up into it.
676    pub fn min_size(mut self, w: f64, h: f64) -> Self {
677        self.min_size = Some((w, h));
678        self
679    }
680
681    /// Largest inner size the user may resize the window to, logical px.
682    /// A bound below the matching `min_size` loses to it, as on the OS side.
683    pub fn max_size(mut self, w: f64, h: f64) -> Self {
684        self.max_size = Some((w, h));
685        self
686    }
687
688    /// Loads `ext` under its own name as its namespace. The slots it fills
689    /// are declared as `ui.slot("<name>/<slot>")`. Panics when the name is
690    /// already another extension's namespace; two of one name need
691    /// [`Launcher::extension_as`].
692    pub fn extension(self, ext: impl Extension + 'static) -> Self {
693        let ns = ext.name().to_owned();
694        self.extension_as(ns, ext)
695    }
696
697    /// Loads `ext` under `namespace`, so the same extension loaded twice is
698    /// two namespaces with two sets of slots and params. Panics on a
699    /// namespace already taken, an empty one, or an extension whose slot
700    /// names contain `/`.
701    pub fn extension_as(self, namespace: impl Into<String>, ext: impl Extension + 'static) -> Self {
702        match self.try_extension_as(namespace, ext) {
703            Ok(this) => this,
704            Err(e) => panic!("kui: {e}"),
705        }
706    }
707
708    /// [`Launcher::extension_as`] for a caller that reports the refusal
709    /// rather than panicking, such as a plugin named from outside the
710    /// program. The launcher is consumed either way.
711    pub fn try_extension_as(
712        mut self,
713        namespace: impl Into<String>,
714        ext: impl Extension + 'static,
715    ) -> Result<Self, String> {
716        self.extensions.push_as(namespace, Box::new(ext))?;
717        Ok(self)
718    }
719
720    /// Takes an [`Extensions`] list already loaded, replacing any
721    /// `extension` calls before it. For a host that built the list
722    /// elsewhere, as the C binding does.
723    pub fn with_extensions(mut self, extensions: Extensions) -> Self {
724        self.extensions = extensions;
725        self
726    }
727
728    /// [`Launcher::extension`] for each, in order.
729    pub fn extensions(mut self, exts: Vec<Box<dyn Extension>>) -> Self {
730        for ext in exts {
731            if let Err(e) = self.extensions.push(ext) {
732                panic!("kui: {e}");
733            }
734        }
735        self
736    }
737
738    /// The shell for `app`, boxed: the one generic step between an app
739    /// and the runner, kept to moving fields. Everything after it
740    /// takes the box unsized, as a [`DynShell`].
741    fn shell<A: App>(mut self, app: A) -> Box<Shell<A>> {
742        let (diagnostics, session, core) = self.main_core();
743        Box::new(Shell {
744            title: self.title,
745            icon: icon::AppIcon::new(self.icon, self.icon_resource),
746            chrome: self.chrome,
747            size: clamp_size(self.size, self.min_size, self.max_size),
748            min_size: self.min_size,
749            max_size: self.max_size,
750            text_aa: self.text_aa,
751            frame_latency: wanted_frame_latency(self.frame_latency),
752            backdrop: self.backdrop,
753            diagnostics,
754            subpixel: false,
755            extensions: self.extensions,
756            session,
757            main_core: Some(core),
758            panes: Vec::new(),
759            gpu: None,
760            reopened: None,
761            reopen_owed: false,
762            pretended_loss: false,
763            locks: kui_core::KeyLocks::default(),
764            script: kui_core::LayoutScript::default(),
765            epoch: std::time::Instant::now(),
766            system: system_env::query(),
767            pinned_system: self.system,
768            clipboard: arboard::Clipboard::new().ok(),
769            #[cfg(target_os = "macos")]
770            native_menu: macos_menu::MacMenu::new(),
771            #[cfg(target_os = "macos")]
772            menu_shown: false,
773            #[cfg(target_os = "macos")]
774            native_menu_bar: macos_menu::MacMenuBar::new(),
775            #[cfg(target_os = "macos")]
776            applied_menu_bar: None,
777            audio: audio::Audio::new(),
778            audio_touch: std::time::Instant::now(),
779            smoke_frames: Self::smoke_frames(),
780            frames_drawn: 0,
781            exit_requested: false,
782            startup_error: None,
783            torn_down: false,
784            secure_input: secure_input::SecureInput::default(),
785            pumped: false,
786            opened: false,
787            primary_down: None,
788            armed: Vec::new(),
789            swallowed_press: None,
790            proxy: None,
791            next_deadline: None,
792            wake_at: None,
793            saw_event: false,
794            woke: false,
795            deferred_events: self.deferred_events,
796            owed: std::cell::Cell::new(false),
797            app,
798        })
799    }
800
801    /// A shell as the runner sees it, for the tests.
802    #[cfg(test)]
803    fn dyn_shell<A: App + 'static>(self, app: A) -> Box<DynShell<'static>> {
804        self.shell(app)
805    }
806
807    /// Whether diagnostics are on, the session, and the main window's core
808    /// with the launcher's setup applied — the part of `shell` that does
809    /// not need the app's type.
810    fn main_core(&mut self) -> (bool, Session, Core) {
811        // Before anything prints: a windows-subsystem app started from a
812        // shell has no stdout until it takes its parent's, and the
813        // diagnostics, the panics and `report_faults` are all worth
814        // reading there (`mod windows_console`).
815        #[cfg(target_os = "windows")]
816        windows_console::attach_parent();
817        // Diagnostics are a development aid: on in debug builds unless the
818        // launcher says otherwise, so a shipped app pays and prints nothing.
819        let diagnostics = self.diagnostics.unwrap_or(cfg!(debug_assertions));
820        // A handed core brings its session; a made one gets a fresh one.
821        let (session, mut core) = match self.core.take() {
822            Some(core) => (core.session().clone(), core),
823            None => {
824                let session = Session::new();
825                let core = Core::new_in(&session);
826                (session, core)
827            }
828        };
829        core.set_diagnostics(diagnostics);
830        // `KUI_DEVTOOLS=1` opens the panel for a program that never asked
831        // (ADR 0024); read here, for a window, and never by a headless
832        // core. What the launcher was told comes after, and wins.
833        core.devtools_from_env();
834        for f in std::mem::take(&mut self.setup_core) {
835            f(&mut core);
836        }
837        (diagnostics, session, core)
838    }
839
840    /// `KUI_SMOKE_FRAMES=n`, in a build that honours it.
841    ///
842    /// A development aid, gated the way `shell` gates the diagnostics: an app you
843    /// ship should not close its own window because something in the
844    /// environment it was launched from happened to set a variable, and
845    /// the app's author never asked for that behaviour. Live in a dev
846    /// build, which is where it is used by hand (`KUI_SMOKE_FRAMES=120
847    /// cargo run --example fragment`), and in a release build that asks
848    /// for it with `--features smoke` — which is what
849    /// the smoke round passes under `--release`
850    /// (examples/devtools/src/bin/smoke.rs), and
851    /// the whole reason this is a feature rather than `debug_assertions`
852    /// alone: the round is worth running against what actually ships.
853    fn smoke_frames() -> Option<u32> {
854        if !(cfg!(debug_assertions) || cfg!(feature = "smoke")) {
855            return None;
856        }
857        std::env::var("KUI_SMOKE_FRAMES")
858            .ok()
859            .and_then(|s| s.parse().ok())
860    }
861
862    /// Opens the main window and runs the event loop until the main window
863    /// closes. Returns `Ok` then, or the error when the window or its
864    /// renderer could not be made. One event loop per process: a process
865    /// that has used [`Launcher::open`] opens again rather than calling
866    /// `run`.
867    pub fn run<A: App>(self, app: A) -> Result<(), Box<dyn std::error::Error>> {
868        let event_loop = run_loop()?;
869        run_shell(event_loop, self.shell(app))
870    }
871
872    /// Opens the window but keeps the event loop in the caller's hands: the
873    /// returned [`PumpRunner`] processes OS events only when
874    /// [`PumpRunner::pump`] is called, so a foreign loop (libuv, a game
875    /// loop, a test harness) can interleave with winit on the main thread.
876    /// winit allows one event loop per process, but any number of runners
877    /// in turn: a runner whose main window has closed parks the loop, and
878    /// the next `open` on the thread takes it back.
879    pub fn open<A: App>(self, app: A) -> Result<PumpRunner<A>, Box<dyn std::error::Error>> {
880        let event_loop = take_event_loop()?;
881        let mut shell = self.shell(app);
882        let state = PumpState::open(event_loop, &mut *shell);
883        let mut runner = PumpRunner {
884            state,
885            shell: std::mem::ManuallyDrop::new(shell),
886        };
887        if !runner.state.alive {
888            runner.retire();
889        }
890        // Retired above, so the loop is parked and the next `open` can
891        // try again (backlog RG47).
892        if let Some(why) = runner.shell.startup_error.take() {
893            return Err(why.into());
894        }
895        Ok(runner)
896    }
897}
898
899/// The event loop `run` owns. Not a parked loop: a pumped runner leaves
900/// winit's loop *running* (it never exits it — see `PARKED_LOOP`), and
901/// `run_app` on a running loop is a `debug_assert` in winit's macOS path.
902/// `run` is the one-shot runner; a process that has pumped opens again.
903fn run_loop() -> Result<EventLoop<access_bridge::UserEvent>, Box<dyn std::error::Error>> {
904    if PARKED_LOOP.with(|p| p.borrow().is_some()) {
905        return Err(
906            "kui: `run` cannot follow a pumped runner in this process — a loop \
907                    a `PumpRunner` parked is still running; open another `PumpRunner`"
908                .into(),
909        );
910    }
911    Ok(EventLoop::<access_bridge::UserEvent>::with_user_event().build()?)
912}
913
914/// `Launcher::run` past the one generic step: compiled here, once.
915fn run_shell(
916    event_loop: EventLoop<access_bridge::UserEvent>,
917    mut shell: Box<DynShell<'_>>,
918) -> Result<(), Box<dyn std::error::Error>> {
919    event_loop.set_control_flow(ControlFlow::Wait);
920    shell.attach(&event_loop);
921    event_loop.run_app(&mut Handler(&mut shell))?;
922    match shell.startup_error.take() {
923        Some(why) => Err(why.into()),
924        None => Ok(()),
925    }
926}
927
928impl DynShell<'_> {
929    /// The main window, or its renderer, could not be made: `why` becomes
930    /// the error `run` or `open` returns, and the runner ends. `run`'s
931    /// loop is asked to exit; a pumped one is not — it is parked for the
932    /// next runner, as when a window closes.
933    fn fail_open(&mut self, event_loop: &ActiveEventLoop, why: String) {
934        self.startup_error = Some(why);
935        self.exit_requested = true;
936        if !self.pumped {
937            event_loop.exit();
938        }
939    }
940
941    /// What a shell takes from the loop it runs on before the first
942    /// event: the proxy, the app's waker, and the wakers of the platform
943    /// paths no winit event carries.
944    fn attach(&mut self, event_loop: &EventLoop<access_bridge::UserEvent>) {
945        self.proxy = Some(event_loop.create_proxy());
946        self.app.setup(Waker(event_loop.create_proxy()));
947        // A menu-bar item chosen by its ⌘-shortcut is the whole of the
948        // event as far as winit is concerned — AppKit consumed the key —
949        // so the bar rings the loop itself.
950        #[cfg(target_os = "macos")]
951        if let Some(bar) = &self.native_menu_bar {
952            bar.set_waker(Waker(event_loop.create_proxy()));
953        }
954        // And so does an insert the platform makes from the palette or
955        // dictation: no winit event carries it.
956        #[cfg(target_os = "macos")]
957        macos_text_input::set_waker(Waker(event_loop.create_proxy()));
958        // And a file drag's position (ADR 0031), which winit's do not.
959        #[cfg(target_os = "macos")]
960        macos_drop::set_waker(Waker(event_loop.create_proxy()));
961        // And the documents the OS asks the app to open (backlog F124),
962        // which winit's application delegate does not answer: installed
963        // here, before `run_app` launches the app, since the launch-time
964        // ones come inside `finishLaunching`.
965        #[cfg(target_os = "macos")]
966        {
967            macos_open::set_waker(Waker(event_loop.create_proxy()));
968            macos_open::install();
969        }
970        // And the installed fonts changing, which winit has no event for.
971        system_fonts::watch(event_loop.create_proxy());
972    }
973}
974
975thread_local! {
976    /// The process's one event loop, parked between runners. winit refuses
977    /// to build a second (`EventLoopError::RecreationAttempt`, a static
978    /// flag it never clears), so a host that opens a window, closes it
979    /// and opens another — a smoke test running two configurations in
980    /// sequence, an app whose second launch is in-process — needs the
981    /// first runner to hand the loop back rather than drop it. The pump
982    /// path never calls winit's `exit()` for the same reason: an exited
983    /// loop answers every later pump with `Exit` and nothing public clears
984    /// that; the runner ends itself on `exit_requested` instead, and the
985    /// loop stays live for the next shell.
986    static PARKED_LOOP: std::cell::RefCell<Option<EventLoop<access_bridge::UserEvent>>> =
987        const { std::cell::RefCell::new(None) };
988}
989
990/// The parked loop if an earlier runner left one, else a new one — which
991/// winit allows once per process. For `open` only: `run` builds its own
992/// (above), since a parked loop is a running one.
993fn take_event_loop() -> Result<EventLoop<access_bridge::UserEvent>, winit::error::EventLoopError> {
994    if let Some(parked) = PARKED_LOOP.with(|p| p.borrow_mut().take()) {
995        return Ok(parked);
996    }
997    EventLoop::<access_bridge::UserEvent>::with_user_event().build()
998}
999
1000/// Whether this input is one whose own visible effect is finished, so the
1001/// app's answer to it belongs in the same frame.
1002///
1003/// A press or a release changes what the core itself paints — the button
1004/// goes down, the button comes up — and that change reads as the whole of
1005/// what happened, so a frame showing the button let go with the count
1006/// unchanged is a frame that lies. A keystroke, an IME commit and an
1007/// assistive-technology action are discrete the same way.
1008///
1009/// A pointer moving, a wheel turning and a preedit being revised are not:
1010/// they arrive as a stream, every frame during one is superseded by the
1011/// next, and an app's content trailing the pointer by a frame is what
1012/// every toolkit does. Waiting on those would halve the frame rate of a
1013/// drag for nothing — measured at 18 frames against 36 in a 578 ms drag —
1014/// so they never wait.
1015/// Puts a copy on the system clipboard, with the formatting beside the
1016/// words where there is any.
1017///
1018/// Both flavours or neither: `set_html` writes the HTML *and* the plain
1019/// text it is given as an alternative, so an app that understands one
1020/// takes it and everything else takes the words. A clipboard holding only
1021/// HTML pastes markup into every plain-text field on the machine, which is
1022/// the failure mode this shape exists to avoid.
1023fn set_clipboard(clipboard: Option<&mut arboard::Clipboard>, text: String, html: Option<String>) {
1024    let Some(cb) = clipboard else { return };
1025    match html {
1026        Some(html) => {
1027            let _ = cb.set_html(html, Some(text));
1028        }
1029        None => {
1030            let _ = cb.set_text(text);
1031        }
1032    }
1033}
1034
1035fn input_completes(ev: &InputEvent) -> bool {
1036    match ev {
1037        InputEvent::MouseDown { .. }
1038        | InputEvent::MouseUp { .. }
1039        // A force click is one moment and one answer, like a press.
1040        | InputEvent::ForceClick(_)
1041        | InputEvent::Key(..)
1042        | InputEvent::KeyDown(_)
1043        | InputEvent::KeyUp(_)
1044        | InputEvent::Text(_)
1045        | InputEvent::Commit(_)
1046        | InputEvent::Paste { .. }
1047        | InputEvent::Access(_)
1048        // A drop is a release; a cancel ends the drag the same way.
1049        | InputEvent::DropFiles { .. }
1050        | InputEvent::DragCancel
1051        // A dialog's answer is one moment, as a paste is, and so are the
1052        // documents the OS hands over.
1053        | InputEvent::Files(_)
1054        | InputEvent::Open(_) => true,
1055        InputEvent::CursorMoved(_)
1056        | InputEvent::CursorLeft
1057        | InputEvent::Scroll(_)
1058        | InputEvent::ScrollGesture { .. }
1059        | InputEvent::Preedit(..)
1060        | InputEvent::Modifiers(_)
1061        // Files moving over the window is the pointer moving.
1062        | InputEvent::DragFiles { .. } => false,
1063    }
1064}
1065
1066/// Whether a redraw waits for the host's answer instead of painting now.
1067///
1068/// Two conditions, and the second is the one that makes this safe. `owed`
1069/// says an input reached an app that answers later, so what would be
1070/// painted predates that input ([`Launcher::deferred_events`]).
1071/// `deferred_last` says this window's previous redraw already waited — and
1072/// a frame never waits twice running.
1073///
1074/// That bound is not a nicety. Waiting until the host says otherwise is
1075/// the obvious rule and it starves the window: winit hands a pump its
1076/// input before that pump's redraw, so under a stream of input that
1077/// reaches the app — a drag, an auto-repeating key — each pump re-arms the
1078/// wait before the frame the host just asked for is delivered, and the
1079/// window paints **nothing** until the stream ends (measured: one frame in
1080/// a 578 ms drag). The same unbounded rule freezes a window for a whole
1081/// title-bar drag, since a platform's modal move loop never returns to the
1082/// host that would end the wait. Never twice running costs at worst half
1083/// the frame rate under continuous input, and bounds every one of those to
1084/// a single frame.
1085fn frame_waits_for_host(owed: bool, deferred_last: bool) -> bool {
1086    owed && !deferred_last
1087}
1088
1089fn pump_once(
1090    event_loop: &mut EventLoop<access_bridge::UserEvent>,
1091    shell: &mut DynShell<'_>,
1092) -> bool {
1093    use winit::platform::pump_events::{EventLoopExtPumpEvents, PumpStatus};
1094    match event_loop.pump_app_events(Some(std::time::Duration::ZERO), &mut Handler(shell)) {
1095        PumpStatus::Continue => !shell.exit_requested,
1096        PumpStatus::Exit(_) => false,
1097    }
1098}
1099
1100/// A windowed runner driven from outside, from [`Launcher::open`]: the
1101/// same runner as [`Launcher::run`] (input mapping, IME, clipboard, chrome,
1102/// caret blink), but the host calls [`pump`](Self::pump) or
1103/// [`pump_until`](Self::pump_until) on its own cadence instead of parking
1104/// in the event loop.
1105///
1106/// Ask [`next_deadline`](Self::next_deadline) after each pump for how long
1107/// to sleep, and [`request_redraw`](Self::request_redraw) after changing
1108/// what `view` will produce. The runner ends when the main window closes;
1109/// dropping it earlier closes the windows and parks the loop for the next
1110/// `open`.
1111pub struct PumpRunner<A: App> {
1112    state: PumpState,
1113    /// Dropped by hand, unsized (`drop_shell`): as a plain field its drop
1114    /// glue — every window, core and store the shell owns — was generated
1115    /// in the app's crate.
1116    shell: std::mem::ManuallyDrop<Box<Shell<A>>>,
1117}
1118
1119/// What a [`PumpRunner`] keeps beside its shell, and does to it, without
1120/// its app's type.
1121struct PumpState {
1122    /// `None` once the runner has retired: the loop is parked for the next
1123    /// runner on this thread (see `PARKED_LOOP`).
1124    event_loop: Option<EventLoop<access_bridge::UserEvent>>,
1125    alive: bool,
1126    /// Every turn this runner has taken — [`pump`](PumpRunner::pump) and
1127    /// [`pump_until`](PumpRunner::pump_until) alike, the first one that
1128    /// opened the window included. What a driver's backoff is measured in
1129    ///: the runner knows how often it pumped where the app
1130    /// could only read a process monitor.
1131    pumps: u64,
1132    /// The turns among `pumps` whose batch carried an OS event or a wake
1133    ///: what `saw_event` marks, counted once per turn.
1134    woken_pumps: u64,
1135}
1136
1137impl PumpState {
1138    fn open(
1139        mut event_loop: EventLoop<access_bridge::UserEvent>,
1140        shell: &mut DynShell<'_>,
1141    ) -> PumpState {
1142        event_loop.set_control_flow(ControlFlow::Wait);
1143        shell.pumped = true;
1144        shell.attach(&event_loop);
1145        // First pump delivers `resumed`, creating the window + renderer —
1146        // or, on a loop taken back from an earlier runner, `about_to_wait`
1147        // does, since winit's init events came and went with the first.
1148        let alive = pump_once(&mut event_loop, shell);
1149        let mut state = PumpState {
1150            event_loop: Some(event_loop),
1151            alive,
1152            pumps: 1,
1153            woken_pumps: 0,
1154        };
1155        state.tally(shell);
1156        state
1157    }
1158
1159    /// Counts the turn that just ran as woken if its batch saw an event.
1160    /// The flag is taken, so the quiet turns after it are not counted too.
1161    fn tally(&mut self, shell: &mut DynShell<'_>) {
1162        if std::mem::take(&mut shell.woke) {
1163            self.woken_pumps += 1;
1164        }
1165    }
1166
1167    fn pump(&mut self, shell: &mut DynShell<'_>) -> bool {
1168        let Some(event_loop) = &mut self.event_loop else {
1169            return false;
1170        };
1171        self.pumps += 1;
1172        self.alive = pump_once(event_loop, shell);
1173        self.tally(shell);
1174        if !self.alive {
1175            self.retire(shell);
1176        }
1177        self.alive
1178    }
1179
1180    /// The end of this runner: every window closed (dropping the panes
1181    /// is what closes them — the pump path never asks winit to exit) and
1182    /// the loop handed back for the next `Launcher::open` on the thread.
1183    fn retire(&mut self, shell: &mut DynShell<'_>) {
1184        self.alive = false;
1185        shell.teardown_once();
1186        // The main core outlives its window, as it predated it: a host
1187        // still reads events, warnings and the tree off a runner that
1188        // has ended (`core_mut`), and the Node driver does so for the
1189        // pump that returned false.
1190        let mut panes = std::mem::take(&mut shell.panes);
1191        // The facts the platform's text input reads are keyed by the
1192        // view's address, which the next window's view may get.
1193        #[cfg(target_os = "macos")]
1194        for pane in &panes {
1195            macos_text_input::detach(&pane.window);
1196            macos_drop::detach(&pane.window);
1197        }
1198        if !panes.is_empty() {
1199            shell.main_core = Some(panes.remove(0).core);
1200        }
1201        if let Some(event_loop) = self.event_loop.take() {
1202            PARKED_LOOP.with(|p| *p.borrow_mut() = Some(event_loop));
1203        }
1204    }
1205
1206    fn pump_until(&mut self, shell: &mut DynShell<'_>, deadline: std::time::Instant) -> bool {
1207        use winit::platform::pump_events::{EventLoopExtPumpEvents, PumpStatus};
1208        let Some(event_loop) = &mut self.event_loop else {
1209            return false;
1210        };
1211        let timeout = deadline.saturating_duration_since(std::time::Instant::now());
1212        self.pumps += 1;
1213        self.alive = match event_loop.pump_app_events(Some(timeout), &mut Handler(shell)) {
1214            PumpStatus::Continue => !shell.exit_requested,
1215            PumpStatus::Exit(_) => false,
1216        };
1217        self.tally(shell);
1218        if !self.alive {
1219            self.retire(shell);
1220        }
1221        self.alive
1222    }
1223
1224    fn waker(&self, shell: &DynShell<'_>) -> Waker {
1225        match &self.event_loop {
1226            Some(event_loop) => Waker(event_loop.create_proxy()),
1227            // Retired: the proxy the shell kept still names the loop.
1228            None => Waker(shell.proxy.clone().expect("a pump runner keeps its proxy")),
1229        }
1230    }
1231}
1232
1233impl DynShell<'_> {
1234    fn core_mut_of(&mut self, id: WindowId) -> Option<&mut Core> {
1235        if id == WindowId::MAIN {
1236            return Some(self.core_mut());
1237        }
1238        self.panes
1239            .iter_mut()
1240            .find(|p| p.id == id)
1241            .map(|p| &mut p.core)
1242    }
1243
1244    fn window_id(&mut self, name: &str) -> Option<WindowId> {
1245        self.core_mut()
1246            .windows()
1247            .into_iter()
1248            .find(|(_, n)| &**n == name)
1249            .map(|(id, _)| id)
1250    }
1251
1252    fn request_redraw(&self) {
1253        self.owed.set(false);
1254        for p in &self.panes {
1255            p.redraw_for(FrameCause::HOST);
1256        }
1257    }
1258}
1259
1260impl<A: App> Drop for PumpRunner<A> {
1261    /// A runner dropped while alive — a host that let go of it without
1262    /// pumping to the end — parks the loop too, so the next `open` on the
1263    /// thread is not refused for its sake.
1264    fn drop(&mut self) {
1265        // `retire` reaches `App::teardown` too, so a runner a panic unwinds
1266        // through hears the window go (backlog RG1); a second panic out of
1267        // that `teardown` is an abort, as any panic in a drop is.
1268        self.retire();
1269        // SAFETY: taken once, here, and the field is not read again.
1270        let shell: Box<Shell<A>> = unsafe { std::mem::ManuallyDrop::take(&mut self.shell) };
1271        drop_shell(shell);
1272    }
1273}
1274
1275/// Drops a shell unsized, so its drop glue is kui's (see `PumpRunner`).
1276fn drop_shell(shell: Box<DynShell<'_>>) {
1277    drop(shell);
1278}
1279
1280impl<A: App> PumpRunner<A> {
1281    fn shell(&self) -> &DynShell<'_> {
1282        &**self.shell
1283    }
1284
1285    fn shell_mut(&mut self) -> &mut DynShell<'_> {
1286        &mut **self.shell
1287    }
1288
1289    fn retire(&mut self) {
1290        self.state.retire(&mut **self.shell);
1291    }
1292
1293    /// Processes all pending OS events without blocking. Returns false once
1294    /// the main window has closed — and from that pump on the windows are
1295    /// gone and the loop is parked for the next runner; further pumps are
1296    /// no-ops.
1297    pub fn pump(&mut self) -> bool {
1298        self.state.pump(&mut **self.shell)
1299    }
1300
1301    /// How many turns this runner has taken, the one that opened the
1302    /// window included: every `pump` and `pump_until` that ran, not the
1303    /// no-ops after it retired. Monotonic, so two readings a second apart
1304    /// give the pump rate over that second. How many of them something
1305    /// outside the app caused is [`woken_pumps`](Self::woken_pumps).
1306    pub fn pumps(&self) -> u64 {
1307        self.state.pumps
1308    }
1309
1310    /// How many of those [`pumps`](Self::pumps) found an OS event or a
1311    /// wake in their batch: any window event but a redraw (a key, the
1312    /// pointer crossing the window, a focus change, a resize, the window
1313    /// moved or occluded), or anything that came through the loop's proxy
1314    /// (a [`Waker::wake`], assistive technology asking, a file dialog's
1315    /// answer). Monotonic, and never more than `pumps`.
1316    ///
1317    /// Unmoved over a second, the desktop left the window alone and
1318    /// whatever it drew was the app's own doing; moved, the desktop or a
1319    /// person reached in, so an idle-window measurement should restart
1320    /// rather than fail.
1321    pub fn woken_pumps(&self) -> u64 {
1322        self.state.woken_pumps
1323    }
1324
1325    /// [`pump`](Self::pump), but parked until an OS event, a
1326    /// [`Waker::wake`] or `deadline`, whichever comes first, so a host that
1327    /// owns the loop blocks on all three instead of polling on a timer.
1328    /// Returns false once the main window has closed.
1329    pub fn pump_until(&mut self, deadline: std::time::Instant) -> bool {
1330        self.state.pump_until(&mut **self.shell, deadline)
1331    }
1332
1333    /// When the runner next needs pumping, as the last [`pump`](Self::pump)
1334    /// left it: a caret blink, a tick, a transition's next frame, the audio
1335    /// poll or a first-frame retry. `None` when nothing it knows about is
1336    /// due. Ask after each pump and sleep until the answer rather than
1337    /// pumping on a fixed interval.
1338    ///
1339    /// It does not say whether an OS event is waiting, since nothing short
1340    /// of pumping can, so a driver still needs a ceiling of its own; this
1341    /// only ever says to come back sooner.
1342    pub fn next_deadline(&self) -> Option<std::time::Instant> {
1343        self.shell.next_deadline
1344    }
1345
1346    /// A [`Waker`] for this loop, to clone into the threads the host's
1347    /// data arrives on.
1348    pub fn waker(&self) -> Waker {
1349        self.state.waker(&**self.shell)
1350    }
1351
1352    /// The app, for a host that reads or changes its state between pumps.
1353    pub fn app_mut(&mut self) -> &mut A {
1354        &mut self.shell.app
1355    }
1356
1357    /// Delivers `events` the way this runner's own loop does: an
1358    /// extension's event to the extension, and the rest, with whatever the
1359    /// extension replied, to `to_app`. Use it for events the loop never
1360    /// saw, such as those a host produced by driving the core directly
1361    /// (`core_mut().press`, an access action, a drained
1362    /// `take_pending_events`); pushing those straight at the app would
1363    /// hand it an extension's clicks and leave the extension deaf to them.
1364    ///
1365    /// `to_app` is lent the core of the window each event came from, as
1366    /// [`App::on_event_with`] is.
1367    pub fn route_events(
1368        &mut self,
1369        events: impl IntoIterator<Item = UiEvent>,
1370        mut to_app: impl FnMut(&mut A, UiEvent, &mut Core),
1371    ) {
1372        let Shell {
1373            extensions,
1374            app,
1375            panes,
1376            main_core,
1377            ..
1378        } = &mut **self.shell;
1379        extensions.route(events, |ev| {
1380            if let Some(core) = window_core(panes, main_core, ev.window) {
1381                to_app(app, ev, core);
1382            }
1383        });
1384    }
1385
1386    /// The main window's core. Every window of the app shares its session,
1387    /// so resources registered through it draw in all of them, and
1388    /// `Core::windows` on it lists them.
1389    pub fn core_mut(&mut self) -> &mut Core {
1390        self.shell_mut().core_mut()
1391    }
1392
1393    /// The core of the window `id` names, if that window is open; the main
1394    /// window's for `WindowId::MAIN`. Everything that is one window's
1395    /// rather than the session's (its focus, its editors' text, its scroll
1396    /// offsets, its tokens) is answered by this core and no other.
1397    pub fn core_mut_of(&mut self, id: WindowId) -> Option<&mut Core> {
1398        self.shell_mut().core_mut_of(id)
1399    }
1400
1401    /// The id of the open window named `name` (`"main"` for the launcher's),
1402    /// or `None` while no window of that name is open — before its first
1403    /// frame's diff, or after the user closed it.
1404    pub fn window_id(&mut self, name: &str) -> Option<WindowId> {
1405        self.shell_mut().window_id(name)
1406    }
1407
1408    /// The main window's inner size (logical px) and its scale factor.
1409    /// Unlike `core_mut().viewport()` this is known before the first frame,
1410    /// so a host can size its model at setup. `core_mut().host_area(size)`
1411    /// is what the next frame lays out against: the window less the
1412    /// devtools' dock while the panel is docked, the window otherwise.
1413    pub fn window_size(&self) -> (Size, f32) {
1414        self.shell().window_size()
1415    }
1416
1417    /// Schedules a redraw of every window; call it after changing what
1418    /// `view` will produce. Under [`Launcher::deferred_events`] it also
1419    /// ends a frame's wait: the host is saying its view is current, so the
1420    /// waiting frame is painted with the answer in it.
1421    pub fn request_redraw(&self) {
1422        self.shell().request_redraw();
1423    }
1424
1425    /// Asks the app to close; the next `pump` observes it and returns false.
1426    pub fn request_exit(&mut self) {
1427        self.shell.exit_requested = true;
1428    }
1429
1430    /// Hands the core's queued audio commands to the device now rather than
1431    /// at the next pump, for a host that calls `Core::play` between pumps
1432    /// and wants the sound to start at once.
1433    pub fn flush_audio(&mut self) {
1434        self.shell_mut().apply_audio();
1435    }
1436}
1437
1438/// `app(title).extensions(extensions).run(application)` in one call.
1439pub fn run<A: App>(
1440    title: &str,
1441    application: A,
1442    extensions: Vec<Box<dyn Extension>>,
1443) -> Result<(), Box<dyn std::error::Error>> {
1444    app(title).extensions(extensions).run(application)
1445}
1446
1447/// Clamps a requested inner size into `min`/`max` (logical px), the way the
1448/// OS clamps a resize once the window exists — so a host reading
1449/// `PumpRunner::window_size` before the first frame sees the real size.
1450/// `min` wins where the two bounds cross, matching the platforms.
1451fn clamp_size(size: (f64, f64), min: Option<(f64, f64)>, max: Option<(f64, f64)>) -> (f64, f64) {
1452    let (mut w, mut h) = size;
1453    if let Some((mw, mh)) = max {
1454        w = w.min(mw);
1455        h = h.min(mh);
1456    }
1457    if let Some((mw, mh)) = min {
1458        w = w.max(mw);
1459        h = h.max(mh);
1460    }
1461    (w, h)
1462}
1463
1464/// Width of the invisible resize band synthesized on undecorated windows.
1465const RESIZE_BAND: f32 = 6.0;
1466/// A second titlebar press within this window toggles maximize.
1467const DOUBLE_CLICK_MS: u128 = 350;
1468/// Presses within this window (and `MULTI_CLICK_SLOP` px) count up the
1469/// multi-click sent with `InputEvent::MouseDown` (double = word select).
1470const MULTI_CLICK_MS: u128 = 400;
1471const MULTI_CLICK_SLOP: f32 = 4.0;
1472/// Caret blink half-period while an edit widget is focused.
1473const BLINK_INTERVAL: std::time::Duration = std::time::Duration::from_millis(500);
1474/// How often the loop wakes to notice a playing sound finishing.
1475const AUDIO_POLL: std::time::Duration = std::time::Duration::from_millis(50);
1476
1477/// How long the output device is held open after the last input or sound.
1478/// An open device is a real-time thread the OS runs ~94 times a second
1479/// whether or not anything is playing, which is the entire idle CPU cost of
1480/// an app that owns a sound — the counter example sat at 0.3% doing nothing.
1481/// Held rather than closed at once because the point of opening early is
1482/// that a click finds it open (the ~90 ms open used to stall the counter's
1483/// first click), and any input at all re-warms it: the pointer moving
1484/// towards a button is minutes of warning before the button is pressed.
1485const AUDIO_IDLE_CLOSE: std::time::Duration = std::time::Duration::from_secs(5);
1486/// How long after one try at opening a new device (`Shell::reopen_device`)
1487/// the next may be made. A device that will not open — a driver still
1488/// being installed, an adapter gone — is tried once a second, and the
1489/// loop sleeps until then rather than asking for frames that could only
1490/// ask again.
1491const REOPEN_INTERVAL: std::time::Duration = std::time::Duration::from_secs(1);
1492/// Frames in a row a surface may refuse for being configured wrong, each
1493/// answered by configuring it again, before it is given up with its device.
1494const SURFACE_TRIES: u8 = 3;
1495
1496/// When a new device asked for at `now` may be opened: at once if none
1497/// has been tried, else `REOPEN_INTERVAL` after the last try and never
1498/// sooner. A time not after `now` means now; a later one is the wake the
1499/// loop sleeps until (`about_to_wait`), since nothing else is bound to ask
1500/// — an idle app's windows draw nothing on their own, and an animating
1501/// one's frames, with no surface to present to, are not paced by vsync
1502/// and would spin the loop until the second was up.
1503fn reopen_due(last_try: Option<std::time::Instant>, now: std::time::Instant) -> std::time::Instant {
1504    last_try.map_or(now, |t| (t + REOPEN_INTERVAL).max(now))
1505}
1506
1507/// What a frame does with a surface refused for being configured wrong.
1508#[derive(Debug, PartialEq, Eq)]
1509enum Refused {
1510    /// Configure it to the window's size and draw again at once.
1511    Retry,
1512    /// Give it up with its device (`Shell::reopen_device`); `say` on the
1513    /// first frame past the tries, not on each one refused after it while
1514    /// the reopen waits out its second.
1515    GiveUp { say: bool },
1516}
1517
1518/// Counts one more refusal in `tries` — saturating, since while a reopen
1519/// is owed every frame input asks for is refused again and counted, and
1520/// a `u8` that wrapped would start the retries over (or, in a debug
1521/// build, panic) — and says what the frame does with it: the first
1522/// `SURFACE_TRIES` are retried, the rest give the surface up.
1523fn surface_refused(tries: &mut u8) -> Refused {
1524    *tries = tries.saturating_add(1);
1525    if *tries <= SURFACE_TRIES {
1526        Refused::Retry
1527    } else {
1528        Refused::GiveUp {
1529            say: *tries == SURFACE_TRIES + 1,
1530        }
1531    }
1532}
1533
1534/// Whether text is drawn with LCD subpixel masks: what was asked for
1535/// (`KUI_TEXT_AA` over the launcher's `text_aa`, `wanted_text_aa`), and
1536/// for `Auto` or `Subpixel` only where the device blends per channel —
1537/// a mask the blend cannot split draws coloured fringes. Decided with each
1538/// device, the first and every one opened after a loss, since a reopen can
1539/// land on another adapter.
1540fn subpixel_on(wanted: TextAa, dual_source: bool) -> bool {
1541    match wanted {
1542        TextAa::Grayscale => false,
1543        TextAa::Subpixel | TextAa::Auto => dual_source,
1544    }
1545}
1546
1547/// The antialiasing `Auto` means for an app that asked for a backdrop
1548/// (backlog F126): grayscale, since an LCD mask is a blend against the
1549/// pixels under it and the window cannot see what the OS composites
1550/// behind a translucent one — subpixel text over a sidebar's material
1551/// draws colour fringes the size of a stem. One decision for every
1552/// window of the app, made before any knows what it got. A `Subpixel`
1553/// the app or `KUI_TEXT_AA` named is kept.
1554fn aa_under(wanted: TextAa, backdrop: Backdrop) -> TextAa {
1555    match wanted {
1556        TextAa::Auto if backdrop.is_translucent() => TextAa::Grayscale,
1557        other => other,
1558    }
1559}
1560
1561/// The frame latency asked for: `KUI_FRAME_LATENCY` if it is a positive
1562/// number, for comparing without a rebuild, else the launcher's.
1563fn wanted_frame_latency(launcher: u32) -> u32 {
1564    std::env::var("KUI_FRAME_LATENCY")
1565        .ok()
1566        .and_then(|v| v.trim().parse::<u32>().ok())
1567        .filter(|n| *n >= 1)
1568        .unwrap_or(launcher)
1569}
1570
1571/// The text antialiasing asked for: `KUI_TEXT_AA` if set, for quick A/B
1572/// comparisons, else what the launcher was told.
1573fn wanted_text_aa(launcher: TextAa) -> TextAa {
1574    match std::env::var("KUI_TEXT_AA").ok().as_deref() {
1575        Some("gray") | Some("grayscale") => TextAa::Grayscale,
1576        Some("subpixel") | Some("lcd") => TextAa::Subpixel,
1577        _ => launcher,
1578    }
1579}
1580
1581/// The core's derived pointer shape in winit's vocabulary. One-to-one:
1582/// `CursorShape` is spelled after the platform names on purpose.
1583/// Chromeless, in the spelling each platform actually honours.
1584///
1585/// Everywhere but macOS that is `with_decorations(false)`. On macOS it is
1586/// **not**: `with_decorations(false)` gives a window with the borderless
1587/// style mask, and AppKit never sends `mouseUp:` to one — every press in it
1588/// lands and never releases, so nothing in the window can be clicked, no
1589/// drag ever ends, and a pressed style never clears. A hidden titlebar over
1590/// a fullsize content view looks the same, is a real window, and is what
1591/// `Chrome::Custom` already asks for; chromeless is that plus the traffic
1592/// lights hidden. It keeps the rounded corners, the drop shadow and the
1593/// native edge-resizing that a borderless window has none of.
1594///
1595/// Found by pressing a menu item and watching nothing happen;
1596/// the popup surface and `Chrome::Borderless` share this because they were
1597/// separately wrong in the same way.
1598fn undecorated(attrs: winit::window::WindowAttributes) -> winit::window::WindowAttributes {
1599    #[cfg(target_os = "macos")]
1600    {
1601        use winit::platform::macos::WindowAttributesExtMacOS;
1602        attrs
1603            .with_titlebar_transparent(true)
1604            .with_fullsize_content_view(true)
1605            .with_title_hidden(true)
1606            .with_titlebar_buttons_hidden(true)
1607    }
1608    #[cfg(not(target_os = "macos"))]
1609    {
1610        attrs.with_decorations(false)
1611    }
1612}
1613
1614/// A popup this press is about : one that opened while the primary button was down, or the
1615/// one the button went down inside. For the rest of that press the owner's
1616/// moves are retargeted into it and its release is classified against it.
1617struct Armed {
1618    /// The popup. Its owner and its anchor are the pane's, so nothing here
1619    /// can go stale against the window it names.
1620    id: WindowId,
1621    /// Whether the last retargeted move landed inside this popup, so that
1622    /// dragging off the list costs one `CursorLeft` and staying off it
1623    /// costs nothing.
1624    inside: bool,
1625}
1626
1627/// The runner's state for one app, over every window it opens.
1628///
1629/// Generic only in its last field: everything is written against
1630/// [`DynShell`] and compiled once, here; a `Shell<A>` is only built,
1631/// boxed, and handed over unsized. Written `impl<A: App> Shell<A>`,
1632/// the whole runner was instantiated and optimised again inside every app
1633/// crate, on every edit: the counter's release rebuild went from 1.20 s
1634/// to 1.57 s as the runner grew.
1635struct Shell<A: App + ?Sized> {
1636    title: String,
1637    /// What every window is created with (`Launcher::icon`).
1638    icon: icon::AppIcon,
1639    chrome: Chrome,
1640    /// Initial inner size (logical px), already clamped into the bounds.
1641    size: (f64, f64),
1642    min_size: Option<(f64, f64)>,
1643    max_size: Option<(f64, f64)>,
1644    text_aa: TextAa,
1645    /// What every renderer is configured with: `Launcher::frame_latency`
1646    /// under `KUI_FRAME_LATENCY`.
1647    frame_latency: u32,
1648    /// What the app asked to show through its windows
1649    /// (`Launcher::backdrop`); what each window got is its pane's.
1650    backdrop: Backdrop,
1651    /// What every core is created with; see `Launcher::diagnostics`.
1652    diagnostics: bool,
1653    /// Whether the GPU blends per channel, decided by the first renderer,
1654    /// again by each device opened after a loss (`reopen_device`), and
1655    /// applied to every core.
1656    subpixel: bool,
1657    /// Each under the namespace the host gave it; their `Fill` is what fills
1658    /// the slots a view declares.
1659    extensions: Extensions,
1660    /// What every window shares: fonts, images, sounds, the audio queue and
1661    /// the declared window set.
1662    session: Session,
1663    /// The main window's core until `resumed` moves it into its pane, so
1664    /// `PumpRunner::core_mut` has an answer before the first pump.
1665    main_core: Option<Core>,
1666    /// The open windows, main first.
1667    panes: Vec<Pane>,
1668    /// The device every window renders with, from the first renderer.
1669    gpu: Option<kui_wgpu::Gpu>,
1670    /// When the device was last opened again after being lost
1671    /// (`reopen_device`), so a device that will not open is tried once a
1672    /// second rather than once a frame.
1673    reopened: Option<std::time::Instant>,
1674    /// A new device was asked for inside the second after the last try,
1675    /// or the last try left a window without a renderer: `about_to_wait`
1676    /// opens it when `reopen_due` says, and sleeps until then — the ask
1677    /// is kept here rather than in a frame asked for again, which in an
1678    /// idle app nothing would ask for and in an animating one would be
1679    /// asked for every turn with no vsync to pace it.
1680    reopen_owed: bool,
1681    /// Whether `KUI_LOSE_DEVICE` has had its one loss.
1682    pretended_loss: bool,
1683    /// Caps Lock and Num Lock as the lock keys' presses have turned them,
1684    /// in any window — what a press reports where the OS is not asked
1685    /// (`keys::lock_state`). One keyboard, one record: it
1686    /// was each pane's, so a popup began at off and a toggle in one window
1687    /// never reached another.
1688    locks: kui_core::KeyLocks,
1689    /// The layout's script as the letter keys have shown it — what a
1690    /// press goes by where the OS is not asked (`keys::layout_script`).
1691    /// One keyboard, one record, as `locks`.
1692    script: kui_core::LayoutScript,
1693    /// Origin of the frame clock handed to the cores for transitions.
1694    epoch: std::time::Instant,
1695    /// What the OS was asked for at startup — the accent colour, the
1696    /// reduce-motion setting and the language (`mod system_env`) — pushed
1697    /// into every pane's env each frame and re-asked when the user has
1698    /// evidently been somewhere else. The appearance is not here: it is
1699    /// per-window and comes off the window itself.
1700    system: system_env::Queried,
1701    /// What the app pinned over it (`Launcher::system`); merged in
1702    /// `sync_env`, so it is never lost to the per-frame write.
1703    pinned_system: SystemEnv,
1704    clipboard: Option<arboard::Clipboard>,
1705    /// The platform's context menu, where the platform has one.
1706    /// `None` on every other platform and on a macOS build that
1707    /// could not reach the main thread, and then the core draws its own.
1708    #[cfg(target_os = "macos")]
1709    native_menu: Option<macos_menu::MacMenu>,
1710    /// Whether a menu the core opened has been handed to the platform and
1711    /// is waiting for an answer. One per app: only one menu can be up.
1712    /// Beside `native_menu` because it means nothing without one.
1713    #[cfg(target_os = "macos")]
1714    menu_shown: bool,
1715    /// The platform's application menu bar, where the platform has one
1716    ///. One per process,
1717    /// because that is what macOS has: it carries the declaration of the
1718    /// window that has the keyboard, or of whichever window made one.
1719    #[cfg(target_os = "macos")]
1720    native_menu_bar: Option<macos_menu::MacMenuBar>,
1721    /// What it currently carries: a declaration — the window it came from
1722    /// and that core's `menu_bar_revision` — or the standard bar. The diff
1723    /// that keeps a re-declared bar from being rebuilt sixty times a second.
1724    #[cfg(target_os = "macos")]
1725    applied_menu_bar: Option<AppliedBar>,
1726    /// The audio device the core's audio commands drive; see `audio`.
1727    audio: audio::Audio,
1728    /// When the app was last doing something that could lead to a sound:
1729    /// any input, or any audio command. The device is warmed while this is
1730    /// recent and let go once it is not — see `AUDIO_IDLE_CLOSE`. Starts at
1731    /// launch, so a session holding sounds still opens the device before
1732    /// its first click the way it always did.
1733    audio_touch: std::time::Instant,
1734    /// `KUI_SMOKE_FRAMES=n`: quit after the main window has presented `n`
1735    /// frames, so an example is a self-terminating check — a real window
1736    /// on a real GPU, driven by the real loop, that exits 0 when it drew
1737    /// and non-zero when it did not. It is what the smoke round
1738    /// (examples/devtools/src/bin/smoke.rs) runs; the wgpu validation
1739    /// error that made `fragments` panic on
1740    /// first paint (an alignment the adapter and the device disagreed
1741    /// about) is exactly the class of bug no headless test can see.
1742    /// `None` — unset, or unparseable — is the ordinary endless run.
1743    smoke_frames: Option<u32>,
1744    /// Main-window frames presented so far, counted only while
1745    /// `smoke_frames` is set.
1746    frames_drawn: u32,
1747    /// Set by `WindowCommand::Close` on the main window; honored at the end
1748    /// of the event.
1749    exit_requested: bool,
1750    /// Why the main window or its renderer could not be made, when they
1751    /// could not: `run` and `open` return it as their error. It was an
1752    /// `expect`, and a panic there aborted a Node process outright — the
1753    /// unwind cannot cross the addon's boundary — when a compositor that
1754    /// had just gone away refused the window.
1755    startup_error: Option<String>,
1756    /// Whether `App::teardown` has run: once, whichever of the loop's
1757    /// exit and the runner's retirement comes first.
1758    torn_down: bool,
1759    /// The one count of secure keyboard entry this runner may hold, moved
1760    /// at the end of every batch to whether a window whose frame asked
1761    /// (`Ui::secure_input`) has the keyboard, given back at teardown and
1762    /// on drop (`mod secure_input`).
1763    secure_input: secure_input::SecureInput,
1764    /// Driven by a `PumpRunner` rather than `run_app`: the main window's
1765    /// close ends the runner (`exit_requested`) instead of exiting winit's
1766    /// loop, which the next runner on this thread reuses.
1767    pumped: bool,
1768    /// Whether `resumed` has opened the main window — once per shell, so a
1769    /// reused loop that delivers no `resumed` opens it from `about_to_wait`
1770    /// and a main window the user closed is not reopened from there.
1771    opened: bool,
1772    /// Which pane the primary button is down in, if any. A
1773    /// popup is armed against it: a non-activating popup that opens while this is
1774    /// set joins that press, which is the observable form of "the drag
1775    /// whose press opened the popup" and needs no geometry.
1776    primary_down: Option<WindowId>,
1777    /// The popups armed into the press `primary_down` names, in opening
1778    /// order — where two overlap the last is on top. Emptied by the release
1779    /// that classifies it, and by `close_pane` for a window that goes
1780    /// first.
1781    armed: Vec<Armed>,
1782    /// A primary press that dismissed a non-activating popup and was
1783    /// consumed rather than dispatched, by the pane
1784    /// it landed in. Its release is swallowed with it: the core never saw
1785    /// the `down`, so nothing should see the `up`.
1786    swallowed_press: Option<WindowId>,
1787    /// Hands AccessKit a way back into the loop; set before the window
1788    /// exists.
1789    proxy: Option<EventLoopProxy<access_bridge::UserEvent>>,
1790    /// What the last `about_to_wait` decided the control flow should be, kept
1791    /// so a host that owns the loop can read it (`PumpRunner::next_deadline`).
1792    /// `None` is `ControlFlow::Wait`: nothing the shell knows about is due.
1793    next_deadline: Option<std::time::Instant>,
1794    /// The earliest `Waker::wake_at` not yet reached (backlog F135).
1795    wake_at: Option<std::time::Instant>,
1796    /// Whether this batch carried an OS event the shell acted on. A driver
1797    /// cannot see most of them — a pointer crossing a window that declares no
1798    /// hover produces no *app* event at all, and neither does a key nothing
1799    /// is listening for — so a driver pacing itself on what the app saw
1800    /// concludes that a window being moved across is idle. It is the reason
1801    /// `next_deadline` reports "now" after an event: the shell asked for a
1802    /// redraw and wants pumping to present it, and that is also the honest
1803    /// signal for "somebody is using this window".
1804    saw_event: bool,
1805    /// `saw_event` as `about_to_wait` took it, left for the pump runner to
1806    /// take in turn and count (`PumpRunner::woken_pumps`). A
1807    /// loop that owns itself never reads it.
1808    woke: bool,
1809    /// The host answers events after the loop hands them over
1810    /// (`Launcher::deferred_events`).
1811    deferred_events: bool,
1812    /// An input reached the app and the host has not answered yet, so the
1813    /// next frame would be painted from a view that predates that input.
1814    /// Set only under `deferred_events`; cleared when the host says its
1815    /// view is current ([`PumpRunner::request_redraw`], which every
1816    /// `setView` and every drained `pollEvents` reaches).
1817    ///
1818    /// A `Cell` because the clearing is the host's `&self` call, and the
1819    /// alternative — taking `&mut self` there — is a signature break for
1820    /// what is bookkeeping.
1821    owed: std::cell::Cell<bool>,
1822    /// Last, so a `Box<Shell<A>>` unsizes to a `Box<DynShell>`.
1823    app: A,
1824}
1825
1826/// A shell over any app: what the runner is written against. Generic only
1827/// in a lifetime, which is erased, so its code is kui's and not the app
1828/// crate's, and an app that borrows is still an app.
1829type DynShell<'a> = Shell<dyn App + 'a>;
1830
1831/// The core of window `id`, from a shell's two homes for one: the panes,
1832/// and the main window's core before its pane exists — the main window's
1833/// for a window that has closed since its event was made. Over the fields
1834/// rather than `&mut Shell`, so the app can be lent beside it.
1835fn window_core<'a>(
1836    panes: &'a mut [Pane],
1837    main_core: &'a mut Option<Core>,
1838    id: WindowId,
1839) -> Option<&'a mut Core> {
1840    let at = panes
1841        .iter()
1842        .position(|p| p.id == id)
1843        .or_else(|| panes.iter().position(|p| p.id == WindowId::MAIN));
1844    match at {
1845        Some(i) => Some(&mut panes[i].core),
1846        None => main_core.as_mut(),
1847    }
1848}
1849
1850/// What winit's loop drives: it wants a sized handler, and a `DynShell`
1851/// is not one.
1852struct Handler<'s, 'a>(&'s mut DynShell<'a>);
1853
1854impl ApplicationHandler<access_bridge::UserEvent> for Handler<'_, '_> {
1855    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
1856        self.0.resumed(event_loop);
1857    }
1858
1859    fn window_event(
1860        &mut self,
1861        event_loop: &ActiveEventLoop,
1862        window: WinitWindowId,
1863        event: WindowEvent,
1864    ) {
1865        self.0.window_event(event_loop, window, event);
1866    }
1867
1868    fn exiting(&mut self, event_loop: &ActiveEventLoop) {
1869        self.0.exiting(event_loop);
1870    }
1871
1872    fn user_event(&mut self, event_loop: &ActiveEventLoop, event: access_bridge::UserEvent) {
1873        self.0.user_event(event_loop, event);
1874    }
1875
1876    fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
1877        self.0.about_to_wait(event_loop);
1878    }
1879}
1880
1881impl DynShell<'_> {
1882    /// The main window's core: its pane's once it exists, the one the
1883    /// launcher built before that.
1884    fn core_mut(&mut self) -> &mut Core {
1885        match self.panes.first_mut() {
1886            Some(p) => &mut p.core,
1887            None => self
1888                .main_core
1889                .as_mut()
1890                .expect("the main core exists until its pane takes it"),
1891        }
1892    }
1893
1894    /// What the device is opened for: surfaces that can take alpha when
1895    /// the app asked for a backdrop, which on Windows is another D3D12
1896    /// swapchain (backlog F126) — so only then.
1897    fn gpu_options(&self) -> kui_wgpu::GpuOptions {
1898        kui_wgpu::GpuOptions::transparent(self.backdrop.is_translucent())
1899    }
1900
1901    /// Main inner size in logical px plus the scale factor; the launcher's
1902    /// requested size until the window exists.
1903    fn window_size(&self) -> (Size, f32) {
1904        match self.panes.first() {
1905            Some(p) => p.size(),
1906            None => (Size::new(self.size.0 as f32, self.size.1 as f32), 1.0),
1907        }
1908    }
1909
1910    /// `App::teardown`, once: from `exiting` — the loop's
1911    /// last word, which the OS's Quit reaches too, on macOS through
1912    /// `applicationWillTerminate` where the process ends without `run`
1913    /// ever returning — and from a pumped runner's retirement, whichever
1914    /// comes first.
1915    fn teardown_once(&mut self) {
1916        // The keyboard is given back before the app's teardown runs, and
1917        // on every path here: a process that ends from `exiting` never
1918        // drops the runner (backlog F85).
1919        self.secure_input.set(false);
1920        if !self.torn_down {
1921            self.torn_down = true;
1922            self.app.teardown();
1923        }
1924    }
1925
1926    /// The main window is done: `run_app`'s loop exits; a pumped loop is
1927    /// left running for the next runner, and the runner ends itself on
1928    /// the flag (see `PARKED_LOOP`).
1929    fn exit_main(&mut self, event_loop: &ActiveEventLoop) {
1930        self.exit_requested = true;
1931        if !self.pumped {
1932            event_loop.exit();
1933        }
1934    }
1935
1936    /// Whether the platform's own drag callbacks are answering for every
1937    /// window, so winit's positionless file events are the duplicates.
1938    fn file_drag_is_overridden(&self) -> bool {
1939        #[cfg(target_os = "macos")]
1940        {
1941            macos_drop::installed()
1942        }
1943        #[cfg(not(target_os = "macos"))]
1944        {
1945            false
1946        }
1947    }
1948
1949    /// The file drags the platform reported since the last turn:
1950    /// on macOS the delegate override's messages, each with the
1951    /// position AppKit gave it, dispatched to the pane whose delegate
1952    /// spoke and followed by the stamp that delegate answers the OS from;
1953    /// elsewhere the batch winit's per-file events built, at the pane's
1954    /// last cursor.
1955    fn pump_file_drag(&mut self, event_loop: &ActiveEventLoop) {
1956        #[cfg(target_os = "macos")]
1957        for (delegate, msg) in macos_drop::take_messages() {
1958            let Some(i) = self
1959                .panes
1960                .iter()
1961                .position(|p| macos_drop::delegate_ptr(&p.window) == Some(delegate))
1962            else {
1963                continue;
1964            };
1965            let ev = match msg {
1966                macos_drop::DragMsg::Over(paths, at) => InputEvent::DragFiles { paths, at },
1967                macos_drop::DragMsg::Drop(paths, at) => InputEvent::DropFiles { paths, at },
1968                macos_drop::DragMsg::Cancel => InputEvent::DragCancel,
1969            };
1970            if let Some(i) = self.dispatch(event_loop, i, ev) {
1971                let pane = &self.panes[i];
1972                macos_drop::stamp(&pane.window, pane.core.drop_target().is_some());
1973            }
1974        }
1975        // Collected first, then dispatched by window id: a drop's handler
1976        // may close its window, and a pane's index is not its identity
1977        // (backlog AR39).
1978        let batches: Vec<(WindowId, InputEvent)> = self
1979            .panes
1980            .iter_mut()
1981            .filter_map(|pane| {
1982                let dropped = pane.file_drag_pending.take()?;
1983                let paths = std::mem::take(&mut pane.file_drag);
1984                let at = pane.cursor;
1985                let ev = if dropped {
1986                    InputEvent::DropFiles { paths, at }
1987                } else {
1988                    InputEvent::DragFiles { paths, at }
1989                };
1990                Some((pane.id, ev))
1991            })
1992            .collect();
1993        for (id, ev) in batches {
1994            if let Some(i) = self.pane_of(id) {
1995                self.dispatch(event_loop, i, ev);
1996            }
1997        }
1998    }
1999
2000    /// The documents the OS asked the app to open since the last turn
2001    /// (backlog F124), each list as one `InputEvent::Open` to the main
2002    /// window's core. Before that window has opened — the launch-time
2003    /// open, which AppKit sends inside `finishLaunching` — they stay
2004    /// queued for the first turn after it has. Nothing sends them off
2005    /// macOS: there the documents are in the process's arguments.
2006    fn pump_open_documents(&mut self, event_loop: &ActiveEventLoop) {
2007        #[cfg(target_os = "macos")]
2008        {
2009            if !macos_open::pending() || self.pane_of(WindowId::MAIN).is_none() {
2010                return;
2011            }
2012            for paths in macos_open::take() {
2013                // Looked up per list: a handler may close a window.
2014                let Some(i) = self.pane_of(WindowId::MAIN) else {
2015                    // Gone between two lists: the rest wait, as the
2016                    // launch-time ones do, for a main window to hear them.
2017                    macos_open::requeue(paths);
2018                    continue;
2019                };
2020                self.dispatch(event_loop, i, InputEvent::Open(paths));
2021            }
2022        }
2023        #[cfg(not(target_os = "macos"))]
2024        let _ = event_loop;
2025    }
2026
2027    /// Hands one input to pane `i`'s core and does what followed from it:
2028    /// the events routed, the menu actions, the window commands, the
2029    /// audio. Returns where that pane is afterwards — the commands may
2030    /// have closed a window, and a close in front of it moves it down,
2031    /// so its index is not its identity; `None` when the
2032    /// input closed the pane itself. A caller that goes on addressing the
2033    /// pane goes on with the returned index.
2034    fn dispatch(
2035        &mut self,
2036        event_loop: &ActiveEventLoop,
2037        i: usize,
2038        ev: InputEvent,
2039    ) -> Option<usize> {
2040        let t0 = std::time::Instant::now();
2041        // Someone is using the app, so a sound may be moments away: keep
2042        // the device warm (`AUDIO_IDLE_CLOSE`).
2043        self.audio_touch = t0;
2044        let completes = input_completes(&ev);
2045        let here = self.panes[i].id;
2046        let events = self.panes[i].core.handle_input(ev);
2047        let reached_app = self.route_events(events);
2048        self.owe_for(reached_app && completes);
2049        self.apply_menu_actions(event_loop, i);
2050        self.apply_window_commands(event_loop);
2051        self.apply_audio();
2052        self.pump_native_menu(event_loop);
2053        let i = self.pane_of(here)?;
2054        let pane = &mut self.panes[i];
2055        pane.apply_cursor();
2056        // Hover styling depends on input too, so any input redraws. A
2057        // damage pass can tighten this later.
2058        pane.core.stats.pending_input_ms += t0.elapsed().as_secs_f32() * 1e3;
2059        pane.window.request_redraw();
2060        Some(i)
2061    }
2062
2063    /// Hands the session's queued audio commands to the device. A session
2064    /// that holds a sound is going to play one: the device starts opening
2065    /// here, on its own thread, so the first play finds it open instead of
2066    /// stalling the frame for the ~90 ms the open takes.
2067    fn apply_audio(&mut self) {
2068        let core = self.core_mut();
2069        let cmds = core.take_audio_commands();
2070        let resources = core.resources.clone();
2071        if !cmds.is_empty() {
2072            self.audio_touch = std::time::Instant::now();
2073        }
2074        // Warmed while the app is being used and let go when it is not.
2075        // Not every frame: a frame is drawn for the caret, for a
2076        // transition, for a window moving over the top — none of which is
2077        // anybody about to play anything, and re-warming on one of those
2078        // would reopen the device the moment `about_to_wait` closed it.
2079        if resources.has_sounds() && self.audio_touch.elapsed() < AUDIO_IDLE_CLOSE {
2080            self.audio.warm();
2081        }
2082        // Two things only the device knows come back here. A `Stop` it found
2083        // still playing is a one-shot cut off, which the core turns into
2084        // `truncated-playback` on the node that went away; a refused play
2085        // never starts and so never ends, so the core turns that into a
2086        // `refused` event and a warning, or a tagged node waits on an
2087        // `ended` that cannot come.
2088        let answered = self.audio.apply(cmds, &resources);
2089        if answered.truncated.is_empty() && answered.refused.is_empty() {
2090            return;
2091        }
2092        let core = self.core_mut();
2093        for (playback, at) in answered.truncated {
2094            core.audio_truncated(playback, at);
2095        }
2096        for playback in answered.refused {
2097            core.audio_refused(playback);
2098        }
2099        self.route_playback_events();
2100    }
2101
2102    /// Folds playbacks that finished on their own back into the core, and
2103    /// routes the `sound` events tagged ones become.
2104    fn poll_audio(&mut self) {
2105        let ended = self.audio.poll_ended();
2106        if ended.is_empty() {
2107            return;
2108        }
2109        let core = self.core_mut();
2110        for playback in ended {
2111            core.audio_ended(playback);
2112        }
2113        self.route_playback_events();
2114    }
2115
2116    /// Routes whatever the audio fold-back queued (`ended`, `refused`) and
2117    /// redraws for what handling it changed.
2118    fn route_playback_events(&mut self) {
2119        let pending = self.core_mut().take_pending_events();
2120        if pending.is_empty() {
2121            return;
2122        }
2123        self.route_events(pending);
2124        for p in &self.panes {
2125            p.redraw_for(FrameCause::AUDIO);
2126        }
2127    }
2128
2129    /// An input's events reached the app: under `deferred_events` the next
2130    /// frame owes the host's answer (see `frame_waits_for_host`).
2131    fn owe_for(&self, reached_app: bool) {
2132        if reached_app && self.deferred_events {
2133            self.owed.set(true);
2134        }
2135    }
2136
2137    /// Returns whether anything reached the app (as against an extension
2138    /// answering for itself).
2139    fn route_events(&mut self, events: Vec<UiEvent>) -> bool {
2140        let mut reached_app = false;
2141        // An extension's replies go to whoever declared its slot (ADR 0014
2142        // decision 6): not routed by origin — a reply is addressed by being
2143        // one — and carrying the extension's origin, the window and the key
2144        // of the event it answered, so the receiver knows who spoke and
2145        // from where. For every extension this host placed itself, the
2146        // receiver is this host; for one a guest placed, it is the guest,
2147        // and `route` is the walk up.
2148        let Shell {
2149            extensions,
2150            app,
2151            panes,
2152            main_core,
2153            ..
2154        } = self;
2155        extensions.route(events, |ev| {
2156            reached_app = true;
2157            // Lent the core of the window the event came from (ADR 0036).
2158            match window_core(panes, main_core, ev.window) {
2159                Some(core) => app.on_event_with(ev, core),
2160                None => app.on_event(ev),
2161            }
2162        });
2163        // One app, one model, N windows: a handler that ran in answer to
2164        // input in *this* window can change what *another* window declares
2165        // — choosing an item in a popup is the app closing the popup, and
2166        // the declaration that closes it lives in the window that opened
2167        // it. So anything that reached the app redraws every window; the
2168        // caller has already redrawn the one the input landed in. Guarded
2169        // on there being more than one, so the single-window path — every
2170        // hover, every keystroke — is exactly what it was.
2171        if reached_app && self.panes.len() > 1 {
2172            for p in &self.panes {
2173                p.redraw_for(FrameCause::ELSEWHERE);
2174            }
2175        }
2176        reached_app
2177    }
2178
2179    /// Direct edits (cut) mutate the document outside `handle_input`, so
2180    /// the `changed` the core queued for it is routed here, and the window
2181    /// redrawn. The core posts the event (AR15: this used to build one by
2182    /// hand, and the menu's Cut posted none), so it is stamped and routed
2183    /// like the ones a keystroke makes.
2184    fn after_direct_edit(&mut self, i: usize) {
2185        let events = self.panes[i].core.take_pending_events();
2186        // A cut is input too: the frame that shows the text gone should
2187        // show what the app made of `changed`.
2188        let reached_app = self.route_events(events);
2189        self.owe_for(reached_app);
2190        // The one caller is the runner's own ⌘X, which the core never
2191        // hears as a key.
2192        self.panes[i].redraw_for(FrameCause::KEY);
2193    }
2194
2195    /// Builds and draws one window's frame.
2196    fn redraw(&mut self, i: usize) {
2197        let Shell {
2198            app,
2199            extensions,
2200            panes,
2201            min_size,
2202            max_size,
2203            epoch,
2204            system,
2205            pinned_system,
2206            audio,
2207            pretended_loss,
2208            ..
2209        } = self;
2210        let pane = &mut panes[i];
2211        // The wallpaper kui draws where the OS has no material, found and
2212        // read on a thread: an answer that there is none to draw, or none
2213        // that decodes, is said once, and the window is opaque from then
2214        // on — before the view runs, so this frame's already reads it.
2215        if let Some(g) = pane.ground.as_mut()
2216            && let Err(why) = g.settle()
2217        {
2218            eprintln!("kui: no wallpaper to draw behind the window: {why}");
2219            pane.ground = None;
2220            pane.backdrop = Backdrop::Opaque;
2221        }
2222        // What the driver knows and the view only reads, refreshed for
2223        // this frame (it was already filled in when the pane opened).
2224        sync_env(pane, system, *pinned_system, audio.env());
2225        let window = &pane.window;
2226        // The core turns a changed viewport into a `resize` event, routed
2227        // with the rest of the pending events after this frame.
2228        let (viewport, scale) = pane.size();
2229        let size = window.inner_size();
2230        let t_view = std::time::Instant::now();
2231        pane.core.set_time(epoch.elapsed().as_secs_f64());
2232        // Why the runner asked, beside the input the core recorded: the
2233        // view reads both as `frame_cause` (backlog F111).
2234        pane.core.note_frame_cause(pane.cause.take());
2235        // The extensions fill the slots the host's view declares, in place
2236        // (`Ui::slot`), and `"root"` after it unless the host placed that
2237        // too — `finish` below does the latter and reports slots nobody
2238        // declared (ADR 0014). The core numbers their origins.
2239        let mut ui = pane.core.frame_with(viewport, scale, extensions);
2240        app.view(&mut ui);
2241        let view_ms = t_view.elapsed().as_secs_f32() * 1e3;
2242
2243        let t_layout = std::time::Instant::now();
2244        ui.finish();
2245        let layout_ms = t_layout.elapsed().as_secs_f32() * 1e3;
2246
2247        // Silent misconfigurations the core noticed (a grow weight with
2248        // nothing to split against, a transition on a positional key, two
2249        // nodes on one key): each once, to stderr, so they stop looking
2250        // like "the feature is broken".
2251        for w in pane.core.take_warnings() {
2252            eprintln!(
2253                "kui: warning [{}] node {:016x}: {}",
2254                w.code, w.key.0, w.message
2255            );
2256        }
2257
2258        // Mirror this frame's hit regions into the WM_NCHITTEST answerer.
2259        #[cfg(target_os = "windows")]
2260        if let Some(nc) = &pane.nc {
2261            nc.update(
2262                scale,
2263                window.is_maximized(),
2264                pane.core
2265                    .interaction
2266                    .hits()
2267                    .iter()
2268                    .map(|h| (h.window, h.rect.intersect(&h.clip))),
2269            );
2270        }
2271
2272        if let Some(t) = pane.core.window_title()
2273            && t != pane.applied_title
2274        {
2275            pane.applied_title = t.to_string();
2276            window.set_title(&pane.applied_title);
2277        }
2278
2279        // The level, the same way (backlog C30): a per-frame fact, applied
2280        // when it differs from what this pane has, and a popup's is never
2281        // touched. What the OS made of it is `env.window.always_on_top`
2282        // on the next `sync_env`, from the record `level_change` keeps.
2283        if let Some(level) = level_change(
2284            pane.kind,
2285            pane.core.always_on_top(),
2286            &mut pane.applied_on_top,
2287        ) {
2288            window.set_window_level(level);
2289        }
2290
2291        // Which Option keys are Alt, the same way (backlog F113): applied
2292        // when it differs from what this window has, never per frame. A
2293        // popup's own ask is applied too and never read — it cannot
2294        // become key on macOS, so its keys come through its owner.
2295        if let Some(_option_as_alt) =
2296            option_as_alt_change(pane.core.option_as_alt(), &mut pane.applied_option_as_alt)
2297        {
2298            #[cfg(target_os = "macos")]
2299            {
2300                use winit::platform::macos::{OptionAsAlt as Winit, WindowExtMacOS};
2301                window.set_option_as_alt(match _option_as_alt {
2302                    kui_core::OptionAsAlt::None => Winit::None,
2303                    kui_core::OptionAsAlt::Left => Winit::OnlyLeft,
2304                    kui_core::OptionAsAlt::Right => Winit::OnlyRight,
2305                    kui_core::OptionAsAlt::Both => Winit::Both,
2306                });
2307            }
2308        }
2309
2310        // The input method, the same way (backlog F125). Off, winit's
2311        // `keyDown:` stops calling `interpretKeyEvents:`, and everything
2312        // the OS composes there goes with it — an IME, a dead key, and
2313        // macOS's press-and-hold, which is an input method that swallows a
2314        // held letter's repeats; the key still arrives with the layout's
2315        // character as its text. A key still held from a press the IME
2316        // never saw keeps it off until it comes up (`Pane::ime_off_held`).
2317        if let Some(off) = ime_off_change(pane.ime_off_wanted(), &mut pane.applied_ime_off) {
2318            apply_ime_off(window, off);
2319        }
2320
2321        // The floor the app declared is a floor on the *app*: while the
2322        // devtools are docked in the main window, the pane's extent goes
2323        // on top of it, so the OS stops the window where the app is at
2324        // its minimum and not where the app less the dock is. After the
2325        // frame, since the handle's drag and the placement buttons land
2326        // in one; on change only, since it is a window-manager call.
2327        if pane.id == WindowId::MAIN
2328            && let Some((mw, mh)) = *min_size
2329        {
2330            let inset = pane.core.devtools_inset();
2331            let want = clamp_size((mw + inset.w as f64, mh + inset.h as f64), None, *max_size);
2332            if pane.applied_min != Some(want) {
2333                pane.applied_min = Some(want);
2334                window.set_min_inner_size(Some(LogicalSize::new(want.0, want.1)));
2335            }
2336        }
2337
2338        // Anchor the OS IME candidate window at the focused caret.
2339        if let Some(r) = pane.core.ime_rect() {
2340            window.set_ime_cursor_area(
2341                winit::dpi::LogicalPosition::new(r.x, r.y),
2342                winit::dpi::LogicalSize::new(r.w.max(1.0), r.h),
2343            );
2344        }
2345        // And say what the platform's own text input may ask the view:
2346        // whether there is somewhere to type, and where the caret is.
2347        #[cfg(target_os = "macos")]
2348        macos_text_input::stamp(window, &pane.core);
2349
2350        let t_render = std::time::Instant::now();
2351        // KUI_LOSE_DEVICE=SECS marks the device lost that long after
2352        // launch, once, to see the reopening below happen without a
2353        // driver update to cause it.
2354        if let Some(secs) = lose_device_at()
2355            && !*pretended_loss
2356            && epoch.elapsed().as_secs_f64() >= secs
2357            && let Some(r) = pane.renderer.as_ref()
2358        {
2359            *pretended_loss = true;
2360            r.gpu().mark_lost();
2361        }
2362        // The ground under the frame is a theme role like any other
2363        // (ADR 0019). Without this a view that paints no root background
2364        // — every example that lets the window show through — would show
2365        // the renderer's own near-black on a light desktop, which is the
2366        // one surface a `bg` prop cannot reach.
2367        // Written straight through: the renderer asks for a non-sRGB
2368        // surface, so a clear component is the byte it lands as, the same
2369        // way a quad's colour is.
2370        // Over a backdrop the ground is nothing at all (backlog F126): what
2371        // the view paints is what covers the material, and a view that
2372        // paints no root background shows it whole.
2373        let ground = pane.core.theme().bg;
2374        // The wallpaper kui draws where the OS has no material: handed to
2375        // the renderer once it has loaded, and the part behind the window
2376        // set when the window moved.
2377        if let (Some(g), Some(r)) = (pane.ground.as_mut(), pane.renderer.as_mut()) {
2378            g.prepare(window, r);
2379        }
2380        let clear = if pane.backdrop.is_translucent() && pane.ground.is_none() {
2381            kui_wgpu::wgpu::Color::TRANSPARENT
2382        } else {
2383            kui_wgpu::wgpu::Color {
2384                r: ground.r as f64,
2385                g: ground.g as f64,
2386                b: ground.b as f64,
2387                a: ground.a as f64,
2388            }
2389        };
2390        let (dl, atlas) = pane.core.output();
2391        let mut wait_ms = 0.0;
2392        let mut reopen = false;
2393        let drawn = pane.renderer.as_mut().map(|r| {
2394            r.clear_color = clear;
2395            r.render(dl, atlas)
2396        });
2397        match drawn {
2398            Some(Ok(report)) => {
2399                wait_ms = report.vsync_wait_ms;
2400                pane.pacer
2401                    .presented(std::time::Instant::now(), (size.width, size.height));
2402                pane.retry.presented();
2403                pane.surface_tries = 0;
2404                pane.awaits_device = false;
2405            }
2406            Some(Err(kui_wgpu::RenderError::Reconfigure)) => {
2407                if let Some(r) = pane.renderer.as_mut() {
2408                    r.resize(size.width, size.height);
2409                }
2410                pane.redraw_for(FrameCause::RETRY);
2411            }
2412            // The surface is configured wrong for the window — a size the
2413            // platform never told us, a swapchain a driver update left
2414            // behind: configure it to the window's size and draw again;
2415            // a surface that stays wrong is given up with its device.
2416            // Past the tries the frame asks for no other: the reopen is
2417            // what asks for the next one, and if it has to wait out its
2418            // second, a frame asked for now would only be refused again —
2419            // at once, with no vsync to pace it. Said once per surface
2420            // given up, not once per refused frame.
2421            Some(Err(kui_wgpu::RenderError::Validation)) => {
2422                match surface_refused(&mut pane.surface_tries) {
2423                    Refused::Retry => {
2424                        if let Some(r) = pane.renderer.as_mut() {
2425                            r.resize(size.width, size.height);
2426                        }
2427                        pane.redraw_for(FrameCause::RETRY);
2428                    }
2429                    Refused::GiveUp { say } => {
2430                        if say {
2431                            eprintln!("kui: the surface stays invalid; reopening the device");
2432                        }
2433                        reopen = true;
2434                    }
2435                }
2436            }
2437            // Occluded or timed out: nothing to present, and the frame is
2438            // owed — *scheduled*, since nothing else may ask for one. A
2439            // window ordered front, or brought back with ⌘-Tab, reports
2440            // itself occluded for a beat or two before the platform
2441            // catches up; its frame dropped, it showed the one from
2442            // before until a stray mouse move woke it (F102). Only for a
2443            // bounded number of tries, and not while the platform says
2444            // the window is covered, so a hidden window does not spin.
2445            Some(Err(kui_wgpu::RenderError::Skip)) => {
2446                let now = std::time::Instant::now();
2447                pane.retry.skipped(now);
2448                // A skipped frame paces the next as a presented one does:
2449                // what asks while the surface skips (a waker, the host) is
2450                // held for the display, or its fallback, instead of drawn
2451                // at once into another skip (backlog RG98).
2452                pane.pacer.presented(now, (size.width, size.height));
2453            }
2454            // The device is gone (a driver update, a GPU reset), or a
2455            // frame ago it was and no new one could be opened: open one.
2456            Some(Err(kui_wgpu::RenderError::DeviceLost)) | None => reopen = true,
2457        }
2458        pane.awaits_device |= reopen;
2459        let render_ms = (t_render.elapsed().as_secs_f32() * 1e3 - wait_ms).max(0.0);
2460
2461        pane.core.stats.push(FrameSample {
2462            input_ms: 0.0,
2463            view_ms,
2464            layout_ms,
2465            render_ms,
2466            wait_ms,
2467        });
2468        if reopen {
2469            self.reopen_device();
2470        }
2471    }
2472
2473    /// The device is gone — a driver update or a GPU reset took it, and
2474    /// every swapchain with it — so one new device is opened, and a
2475    /// renderer on it for every window, each window's old one dropped
2476    /// before its new surface is made (DXGI gives a window one flip-model
2477    /// swapchain). The windows keep their cores: the next frame draws
2478    /// what the last one would have. A window whose renderer cannot be
2479    /// made has none, and the device is tried again a second later; a
2480    /// device that cannot be opened is tried, and said, once a second, not
2481    /// once a frame. Asked for inside that second, the ask is owed
2482    /// (`reopen_owed`) and `about_to_wait` makes it when the second is up.
2483    fn reopen_device(&mut self) {
2484        let now = std::time::Instant::now();
2485        if reopen_due(self.reopened, now) > now {
2486            self.reopen_owed = true;
2487            return;
2488        }
2489        self.reopened = Some(now);
2490        // Dropped, not leaked: the old swapchain has to be released for
2491        // DXGI to allow the window a new one (leaked, the new surface's
2492        // configure fails with "invalid surface").
2493        for pane in &mut self.panes {
2494            pane.renderer = None;
2495            pane.surface_tries = 0;
2496        }
2497        self.gpu = None;
2498        let mut gpu: Option<kui_wgpu::Gpu> = None;
2499        let options = self.gpu_options();
2500        for pane in &mut self.panes {
2501            let px = pane.window.inner_size();
2502            let made = match &gpu {
2503                None => pollster::block_on(kui_wgpu::Renderer::new_with(
2504                    pane.window.clone(),
2505                    px.width,
2506                    px.height,
2507                    options,
2508                )),
2509                Some(gpu) => kui_wgpu::Renderer::new_in_with(
2510                    gpu,
2511                    pane.window.clone(),
2512                    px.width,
2513                    px.height,
2514                    pane.backdrop.is_translucent(),
2515                ),
2516            };
2517            match made {
2518                Ok(mut r) => {
2519                    r.set_frame_latency(self.frame_latency);
2520                    // The window keeps its backdrop where the new surface
2521                    // takes alpha too; one that will not any more leaves
2522                    // the window opaque, and says so.
2523                    if pane.backdrop.is_translucent() && pane.ground.is_none() && !r.transparent() {
2524                        pane.backdrop = Backdrop::Opaque;
2525                    }
2526                    // A wallpaper drawn by kui goes to the new renderer too.
2527                    if let Some(g) = pane.ground.as_mut() {
2528                        g.renderer_replaced();
2529                    }
2530                    let opened = gpu.is_none();
2531                    gpu.get_or_insert_with(|| r.gpu().clone());
2532                    if opened {
2533                        eprintln!("kui: device reopened");
2534                    }
2535                    pane.renderer = Some(r);
2536                    pane.redraw_for(FrameCause::DEVICE);
2537                }
2538                Err(err) => eprintln!("kui: cannot reopen window {}: {err}", pane.id.0),
2539            }
2540            pane.awaits_device = pane.renderer.is_none();
2541        }
2542        // A window left without a renderer — or every window, when no
2543        // device would open — is tried again a second from now, whether
2544        // or not anything asks it for a frame.
2545        self.reopen_owed = self.panes.iter().any(|p| p.awaits_device);
2546        // The new device may be another adapter's, with or without the
2547        // per-channel blend LCD masks need: decided again, for every core,
2548        // or a core keeps rasterising masks the new pipeline cannot split.
2549        // A changed decision empties each core's atlas (`set_subpixel_text`),
2550        // which the fresh renderer was going to upload whole anyway.
2551        if let Some(gpu) = &gpu {
2552            let wanted = aa_under(wanted_text_aa(self.text_aa), self.backdrop);
2553            let on = subpixel_on(wanted, gpu.dual_source());
2554            if on != self.subpixel {
2555                self.subpixel = on;
2556                for pane in &mut self.panes {
2557                    pane.core.set_subpixel_text(on);
2558                }
2559            }
2560        }
2561        self.gpu = gpu;
2562    }
2563}
2564
2565/// Which bar `pump_menu_bar` last handed the platform.
2566#[cfg(target_os = "macos")]
2567#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2568enum AppliedBar {
2569    /// The standard bar: no window declared one, or the front
2570    /// window draws its own declaration and the platform's is the standard.
2571    Standard,
2572    /// A window's declaration, at that revision.
2573    Declared(WindowId, u64),
2574}
2575
2576impl ApplicationHandler<access_bridge::UserEvent> for DynShell<'_> {
2577    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
2578        if !self.panes.is_empty() || self.opened {
2579            return;
2580        }
2581        self.opened = true;
2582        // KUI_WINDOW=WxH overrides the initial size (useful for testing).
2583        let size = std::env::var("KUI_WINDOW")
2584            .ok()
2585            .and_then(|s| {
2586                let (w, h) = s.split_once('x')?;
2587                Some((w.parse().ok()?, h.parse().ok()?))
2588            })
2589            .map(|s| clamp_size(s, self.min_size, self.max_size))
2590            .unwrap_or(self.size);
2591        let mut attrs = self.window_attrs(&self.title.clone(), size, self.chrome, self.backdrop);
2592        if let Some((mw, mh)) = self.min_size {
2593            attrs = attrs.with_min_inner_size(LogicalSize::new(mw, mh));
2594        }
2595        if let Some((mw, mh)) = self.max_size {
2596            attrs = attrs.with_max_inner_size(LogicalSize::new(mw, mh));
2597        }
2598        // KUI_WINDOW_AT=X,Y places it, logical px from the screen's top
2599        // left: the smoke round's `--jobs` cascades its windows by it, so
2600        // none is wholly covered — a covered window is `Occluded` and
2601        // presents nothing, and the round counts presents.
2602        if let Some((x, y)) = std::env::var("KUI_WINDOW_AT").ok().and_then(|s| {
2603            let (x, y) = s.split_once(',')?;
2604            Some((x.parse::<f64>().ok()?, y.parse::<f64>().ok()?))
2605        }) {
2606            attrs = attrs.with_position(LogicalPosition::new(x, y));
2607        }
2608        let window = match event_loop.create_window(attrs) {
2609            Ok(w) => Arc::new(w),
2610            Err(e) => return self.fail_open(event_loop, format!("cannot open the window: {e}")),
2611        };
2612        self.icon.set_on(&window);
2613        let px = window.inner_size();
2614        let renderer = pollster::block_on(kui_wgpu::Renderer::new_with(
2615            window.clone(),
2616            px.width,
2617            px.height,
2618            self.gpu_options(),
2619        ));
2620        let mut renderer = match renderer {
2621            Ok(r) => r,
2622            Err(e) => return self.fail_open(event_loop, format!("cannot draw in the window: {e}")),
2623        };
2624        renderer.set_frame_latency(self.frame_latency);
2625        // What shows through it, now that there is a surface to present
2626        // with alpha (backlog F126); before it is shown.
2627        let backdrop = backdrop::apply(&window, &renderer, self.backdrop);
2628        // Subpixel text only where the renderer blends per channel; the
2629        // env var wins over the builder for quick A/B comparisons.
2630        self.subpixel = subpixel_on(
2631            aa_under(wanted_text_aa(self.text_aa), self.backdrop),
2632            renderer.subpixel_text(),
2633        );
2634        self.gpu = Some(renderer.gpu().clone());
2635        let mut core = self
2636            .main_core
2637            .take()
2638            .expect("the main core is built once, by the launcher");
2639        core.set_subpixel_text(self.subpixel);
2640        core.env.window.id = WindowId::MAIN;
2641        self.push_pane(
2642            event_loop,
2643            WindowId::MAIN,
2644            WindowConfig::default(),
2645            WindowId::MAIN,
2646            self.chrome,
2647            core,
2648            window,
2649            renderer,
2650            backdrop,
2651        );
2652        self.panes[0].applied_title = self.title.clone();
2653        self.panes[0].applied_min = self.min_size;
2654    }
2655
2656    fn window_event(
2657        &mut self,
2658        event_loop: &ActiveEventLoop,
2659        id: WinitWindowId,
2660        event: WindowEvent,
2661    ) {
2662        let Some(i) = self.pane_index(id) else { return };
2663        // Everything but the redraw itself: a redraw is the *answer* to an
2664        // event, so counting it would make a frame its own reason for the
2665        // next one and an animation would report activity for ever.
2666        if !matches!(event, WindowEvent::RedrawRequested) {
2667            self.saw_event = true;
2668        }
2669        {
2670            let pane = &mut self.panes[i];
2671            if let Some(bridge) = &mut pane.access {
2672                bridge.process_event(&pane.window, &event);
2673            }
2674        }
2675        match event {
2676            WindowEvent::CloseRequested => {
2677                if self.panes[i].id == WindowId::MAIN {
2678                    self.exit_main(event_loop);
2679                } else {
2680                    let id = self.panes[i].id;
2681                    self.close_pane(event_loop, id);
2682                }
2683            }
2684            // On Windows minimizing and restoring are each a `WM_SIZE`:
2685            // the first is where the window went dark, the second the
2686            // frame that brings it back (backlog RG97).
2687            WindowEvent::Resized(size) => {
2688                let pane = &mut self.panes[i];
2689                if let Some(r) = pane.renderer.as_mut() {
2690                    r.resize(size.width, size.height);
2691                }
2692                if let Some(g) = pane.ground.as_mut() {
2693                    g.moved();
2694                }
2695                if pane.minimized() {
2696                    pane.cause.went_dark();
2697                    pane.redraw_for(FrameCause::RESIZE);
2698                } else if pane.cause.is_dark() && cfg!(target_os = "windows") {
2699                    pane.came_back(FrameCause::RESIZE);
2700                } else {
2701                    // Elsewhere a window resized while covered is still
2702                    // covered; `Occluded(false)` is its way back.
2703                    pane.redraw_for(FrameCause::RESIZE);
2704                }
2705            }
2706            WindowEvent::ScaleFactorChanged { .. } => {
2707                self.panes[i].redraw_for(FrameCause::SCALE);
2708            }
2709            // The wallpaper kui draws behind a window stays put on the
2710            // screen as the window moves over it (backlog F126); nothing
2711            // else here cares where the window is.
2712            WindowEvent::Moved(_) => {
2713                let pane = &mut self.panes[i];
2714                if let Some(g) = pane.ground.as_mut() {
2715                    g.moved();
2716                    pane.redraw_for(FrameCause::RESIZE);
2717                }
2718            }
2719            WindowEvent::CursorMoved { position, .. } => {
2720                let pane = &mut self.panes[i];
2721                let scale = pane.window.scale_factor() as f32;
2722                let p = Vec2::new(position.x as f32 / scale, position.y as f32 / scale);
2723                if pane.synthesizes_resize() {
2724                    pane.resize_edge = pane.resize_edge_at(p);
2725                }
2726                if pane.cursor != p {
2727                    pane.scroll_gesture.pointer_moved();
2728                }
2729                pane.cursor = p;
2730                let from = pane.id;
2731                self.dispatch(event_loop, i, InputEvent::CursorMoved(p));
2732                // And then, if this pane is holding a press that a popup
2733                // joined, the same move again in that popup's coordinates.
2734                self.retarget_move(event_loop, from, p);
2735            }
2736            WindowEvent::CursorLeft { .. } => {
2737                self.dispatch(event_loop, i, InputEvent::CursorLeft);
2738            }
2739            // Files dragged in from the OS, as winit reports them: one
2740            // event per file and no position (ADR 0031, decision 5). On
2741            // macOS the delegate override supersedes all three with the
2742            // position AppKit has (`mod macos_drop`); elsewhere the files
2743            // are collected per batch and dispatched at its end, at the
2744            // pane's last reported cursor — the point at enter and at
2745            // release, since no platform here reports the drag moving.
2746            WindowEvent::HoveredFile(path) => {
2747                if !self.file_drag_is_overridden() {
2748                    let pane = &mut self.panes[i];
2749                    pane.file_drag.push(path.to_string_lossy().into_owned());
2750                    pane.file_drag_pending = Some(false);
2751                }
2752            }
2753            WindowEvent::DroppedFile(path) => {
2754                if !self.file_drag_is_overridden() {
2755                    let pane = &mut self.panes[i];
2756                    // A hover batch still waiting in the same turn is the
2757                    // same files: the drop's list starts over.
2758                    if pane.file_drag_pending != Some(true) {
2759                        pane.file_drag.clear();
2760                    }
2761                    pane.file_drag.push(path.to_string_lossy().into_owned());
2762                    pane.file_drag_pending = Some(true);
2763                }
2764            }
2765            WindowEvent::HoveredFileCancelled => {
2766                if !self.file_drag_is_overridden() {
2767                    let pane = &mut self.panes[i];
2768                    pane.file_drag.clear();
2769                    pane.file_drag_pending = None;
2770                    self.dispatch(event_loop, i, InputEvent::DragCancel);
2771                }
2772            }
2773            // Recorded, not acted on: what a view reads is derived from
2774            // every window's copy at the end of the batch (`settle_focus`),
2775            // because focus *moving* is two events and neither alone is the
2776            // answer.
2777            WindowEvent::Focused(focused) => {
2778                self.panes[i].os_focused = focused;
2779                // A modifier let go elsewhere never comes up here: what
2780                // was down is forgotten with the keyboard (backlog F108).
2781                if !focused {
2782                    self.panes[i].modifier_keys_down.clear();
2783                    // And so is a key held through a mode change: its
2784                    // release lands elsewhere, so the IME it was keeping
2785                    // off is the frame's to decide again.
2786                    self.panes[i].ime_off_held.clear();
2787                    self.panes[i].sync_ime();
2788                }
2789                // Coming back to the app is the cheap, reliable sign that
2790                // the user may have been in a settings app: the accent and
2791                // the reduce-motion setting have no event to subscribe to
2792                // here, and re-asking costs microseconds against something
2793                // that happens when a human switches windows.
2794                if focused {
2795                    let before = (self.system, self.panes[i].appearance);
2796                    self.system = system_env::query();
2797                    // And the window's own setting, in case the platform
2798                    // changed it without an event while we were away.
2799                    self.panes[i].appearance = appearance_of(&self.panes[i].window);
2800                    // A change found this way has no event of its own
2801                    // behind it, so ask for the frame that reports it:
2802                    // `begin_frame` turns the difference into a `system`
2803                    // event, and a host that only draws on input would
2804                    // otherwise not learn of it until it drew for
2805                    // something else (F40).
2806                    if before != (self.system, self.panes[i].appearance) {
2807                        for p in &self.panes {
2808                            p.redraw_for(FrameCause::APPEARANCE);
2809                        }
2810                    }
2811                }
2812            }
2813            // Covered or uncovered — where the platform says (macOS, X11):
2814            // uncovered, the window shows what it presented before it was
2815            // covered, so a frame is owed now (F102); covered, frames the
2816            // surface skips are not retried.
2817            // Covered is where a window goes dark, minimized on macOS
2818            // included, and uncovered where it comes back (RG97).
2819            WindowEvent::Occluded(covered) => {
2820                self.panes[i]
2821                    .retry
2822                    .occluded(covered, std::time::Instant::now());
2823                if covered {
2824                    self.panes[i].cause.went_dark();
2825                } else {
2826                    self.panes[i].came_back(FrameCause::OCCLUSION);
2827                }
2828            }
2829            // The theme changing is the other one, and the only one that
2830            // arrives while the app is in front. The next frame reads the
2831            // appearance off the window anyway; the redraw is what makes
2832            // there *be* a next frame in an app that only draws on input.
2833            WindowEvent::ThemeChanged(theme) => {
2834                self.panes[i].appearance = theme_appearance(Some(theme));
2835                self.system = system_env::query();
2836                self.panes[i].redraw_for(FrameCause::APPEARANCE);
2837            }
2838            // The four keyboard events go to `key_target`, which is this
2839            // pane unless it is lending its keyboard to a non-activating
2840            // popup. The modifier mirror follows them, or the popup would
2841            // read a stale Shift.
2842            WindowEvent::ModifiersChanged(m) => {
2843                // The keyboard's state is one fact for the pair: the OS
2844                // delivers the edge to the owner, the target reads it for
2845                // the keys it borrows, and the owner keeps reading it once
2846                // the popup is gone — written to the target alone, a Shift
2847                // released while a menu was up left the owner's next key a
2848                // Shift chord (AR23).
2849                let t = self.key_target(i);
2850                self.panes[i].modifiers = m.state();
2851                self.panes[t].modifiers = m.state();
2852                use winit::keyboard::ModifiersKeyState::Pressed;
2853                let alt = (m.lalt_state() == Pressed, m.ralt_state() == Pressed);
2854                self.panes[i].alt_held = alt;
2855                self.panes[t].alt_held = alt;
2856                let kmods = self.panes[t].kmods();
2857                self.dispatch(event_loop, t, InputEvent::Modifiers(kmods));
2858            }
2859            // Not a press winit made up: on Windows and X11 a window
2860            // gaining focus is handed a press of every key already held —
2861            // made in another window or another app — and a key whose
2862            // press closed a window pressed again in the one beneath it
2863            // (backlog RG100). The releases it makes up as a window loses
2864            // focus are kept: they are what lets go of a key a sink held.
2865            WindowEvent::KeyboardInput {
2866                event,
2867                is_synthetic,
2868                ..
2869            } => {
2870                if !(is_synthetic && event.state == ElementState::Pressed) {
2871                    let t = self.key_target(i);
2872                    self.on_key(event_loop, i, t, event)
2873                }
2874            }
2875            WindowEvent::Ime(Ime::Commit(text)) => {
2876                // Its own channel, not `Text`: a sink hears a commit as a
2877                // `text` event and a keystroke as a `key` event, once each
2878                // (backlog C17); a stock editor takes both the same way.
2879                self.panes[i].preedit_open = false;
2880                let t = self.key_target(i);
2881                self.dispatch(event_loop, t, InputEvent::Commit(text));
2882            }
2883            WindowEvent::Ime(Ime::Preedit(text, cursor)) => {
2884                self.panes[i].preedit_open = !text.is_empty();
2885                let t = self.key_target(i);
2886                self.dispatch(event_loop, t, InputEvent::Preedit(text, cursor));
2887            }
2888            // The IME went away — the frame turned it off (`imeOff`), or
2889            // the user switched input source — and winit's contract is
2890            // that a pending preedit is cleared: a composition still open
2891            // ends without a commit, as an empty preedit, the way an IME
2892            // that cancels says it.
2893            WindowEvent::Ime(Ime::Disabled) => {
2894                if std::mem::take(&mut self.panes[i].preedit_open) {
2895                    let t = self.key_target(i);
2896                    self.dispatch(event_loop, t, InputEvent::Preedit(String::new(), None));
2897                }
2898            }
2899            // Force Touch: stage 2 is the deepened press macOS calls a
2900            // force click. winit reports the whole ramp, and only the
2901            // *edge* into stage 2 is the gesture — a stage that stays at 2
2902            // while the finger presses harder is the same click still
2903            // happening (ADR 0017, decision 6). macOS-only: no other
2904            // winit backend reports pressure at all.
2905            WindowEvent::TouchpadPressure { stage, .. } => {
2906                let pane = &mut self.panes[i];
2907                let was = std::mem::replace(&mut pane.pressure_stage, stage);
2908                if stage >= 2 && was < 2 {
2909                    let at = pane.cursor;
2910                    self.dispatch(event_loop, i, InputEvent::ForceClick(at));
2911                }
2912            }
2913            WindowEvent::MouseWheel { delta, phase, .. } => {
2914                let started = phase == winit::event::TouchPhase::Started;
2915                let pane = &mut self.panes[i];
2916                let scale = pane.window.scale_factor() as f32;
2917                let now = std::time::Instant::now();
2918                let (d, kind) = match delta {
2919                    winit::event::MouseScrollDelta::LineDelta(x, y) => {
2920                        pane.axis_lock.end();
2921                        (
2922                            Vec2::new(Core::lines_to_px(x), Core::lines_to_px(y)),
2923                            scroll_gesture::Kind::Line,
2924                        )
2925                    }
2926                    // A trackpad's swipe keeps to its axis (`mod axis_lock`),
2927                    // and a finger put down begins the next, glide or no
2928                    // glide (backlog F117).
2929                    winit::event::MouseScrollDelta::PixelDelta(p) => {
2930                        if pane.scroll_gesture.phase(phase, now) {
2931                            pane.axis_lock.end();
2932                        }
2933                        (
2934                            pane.axis_lock
2935                                .pixel(Vec2::new(p.x as f32 / scale, p.y as f32 / scale), now),
2936                            scroll_gesture::Kind::Pixel,
2937                        )
2938                    }
2939                };
2940                // The gesture it is part of keeps the targets it began
2941                // with (`mod scroll_gesture`, backlog F107).
2942                if d != Vec2::ZERO {
2943                    let begins = pane.scroll_gesture.begins(kind, started, now);
2944                    self.dispatch(
2945                        event_loop,
2946                        i,
2947                        InputEvent::ScrollGesture { delta: d, begins },
2948                    );
2949                } else {
2950                    pane.scroll_gesture.note(kind, started, now);
2951                }
2952            }
2953            WindowEvent::MouseInput { state, button, .. } => {
2954                let button = match button {
2955                    WinitButton::Left => MouseButton::Primary,
2956                    WinitButton::Right => MouseButton::Secondary,
2957                    WinitButton::Middle => MouseButton::Middle,
2958                    // `onButton` hears these as `Other`'s code (backlog
2959                    // F105), so the numbering has to be stable: back,
2960                    // forward, then whatever the platform reports beyond
2961                    // them.
2962                    WinitButton::Back => MouseButton::Other(0),
2963                    WinitButton::Forward => MouseButton::Other(1),
2964                    WinitButton::Other(n) => {
2965                        MouseButton::Other(n.saturating_add(2).min(u8::MAX as u16) as u8)
2966                    }
2967                };
2968                let primary = button == MouseButton::Primary;
2969                let here = self.panes[i].id;
2970                // Which pane holds a primary press, for ADR 0009's arming.
2971                // Set for a consumed press too: the button is down whatever
2972                // the core was told, and the release below clears it.
2973                if primary {
2974                    self.primary_down = (state == ElementState::Pressed).then_some(here);
2975                    if state == ElementState::Pressed {
2976                        // A consumed press whose release never arrived — the
2977                        // window lost the keyboard mid-gesture, say — must
2978                        // not swallow the next one instead.
2979                        self.swallowed_press = None;
2980                    }
2981                }
2982                // A press anywhere but inside a popup is "outside" it —
2983                // the owner's own window included, which is exactly the
2984                // line a separate surface draws. Reported before the press
2985                // is dispatched, so an app that stops declaring the popup
2986                // on the dismissal still sees the click it was dismissed
2987                // by, the way a modal's dismissal works (ADR 0003).
2988                //
2989                // And then a primary press that dismissed a
2990                // **non-activating** popup is **consumed** — not dispatched
2991                // to the window it landed in, and its release swallowed with
2992                // it (ADR 0009 decision 5). The pass-through this replaces
2993                // claimed to mirror ADR 0003 and had it backwards: under an
2994                // in-window modal everything outside emits no hit region, so
2995                // the outside press dismisses and lands on nothing, while a
2996                // popup's owner is live (ADR 0004 decision 10) and the press
2997                // dismisses *and* acts. Invisible with click-to-open;
2998                // decisive with open-on-press, where a press on the open
2999                // field would otherwise dismiss and reopen in one gesture,
3000                // and no ordering of the two events lets the app tell that
3001                // press from the first. An **activating** popup — a tear-off
3002                // panel — keeps the pass-through, since someone working in a
3003                // panel beside the app expects a click in the app to act.
3004                if state == ElementState::Pressed {
3005                    let mut consumed = false;
3006                    let mut reached_app = false;
3007                    for (id, activates) in self.popups_outside(i) {
3008                        reached_app |= self.dismiss(id, DismissReason::Outside);
3009                        consumed |= primary && !activates;
3010                    }
3011                    if consumed {
3012                        self.swallowed_press = Some(here);
3013                        // Choosing in a menu closes it *and* does what it
3014                        // says: one frame, so this one waits for the host.
3015                        self.owe_for(reached_app);
3016                        // The press the core never saw.
3017                        for p in &self.panes {
3018                            p.redraw_for(FrameCause::BUTTON);
3019                        }
3020                        return;
3021                    }
3022                }
3023                // A press inside a non-activating popup arms that popup for
3024                // its own release (ADR 0009 decision 4's last paragraph):
3025                // the OS keeps the drag here whatever it wanders over, so a
3026                // drag off the top edge and a release on the desktop has to
3027                // be classified rather than ignored — the measured case in
3028                // backlog W2. It gets no retargeted moves, having the real
3029                // ones already.
3030                if primary
3031                    && state == ElementState::Pressed
3032                    && self.panes[i].kind == WindowKind::Popup
3033                    && !self.panes[i].activates
3034                    && !self.armed.iter().any(|a| a.id == here)
3035                {
3036                    self.armed.push(Armed {
3037                        id: here,
3038                        inside: true,
3039                    });
3040                }
3041                if state == ElementState::Released && primary {
3042                    // The release of a press the driver ate goes with it:
3043                    // the core never saw the `down`, so nothing should see
3044                    // this `up`.
3045                    if self.swallowed_press == Some(here) {
3046                        self.swallowed_press = None;
3047                        self.primary_down = None;
3048                        return;
3049                    }
3050                    let at = self.panes[i].cursor;
3051                    self.classify_release(event_loop, here, at);
3052                }
3053                // The frame the classification ran may have closed a window
3054                // and moved this one down the list.
3055                let Some(i) = self.pane_of(here) else { return };
3056                let pane = &mut self.panes[i];
3057                // A press on the synthesized resize band starts an OS resize
3058                // instead of reaching the UI.
3059                if primary
3060                    && state == ElementState::Pressed
3061                    && let Some(dir) = pane.resize_edge
3062                {
3063                    let _ = pane.window.drag_resize_window(dir);
3064                    return;
3065                }
3066                let ev = match state {
3067                    ElementState::Pressed => {
3068                        // Multi-click is the primary button's: a right
3069                        // press between two left ones does not break the
3070                        // run, and never counts up one of its own.
3071                        let clicks = if primary {
3072                            let now = std::time::Instant::now();
3073                            let clicks = match pane.last_click {
3074                                Some((t, p, n))
3075                                    if now.duration_since(t).as_millis() < MULTI_CLICK_MS
3076                                        && (p.x - pane.cursor.x).abs() < MULTI_CLICK_SLOP
3077                                        && (p.y - pane.cursor.y).abs() < MULTI_CLICK_SLOP =>
3078                                {
3079                                    // Cycle 1 → 2 → 3 → 1 like most editors.
3080                                    n % 3 + 1
3081                                }
3082                                _ => 1,
3083                            };
3084                            pane.last_click = Some((now, pane.cursor, clicks));
3085                            clicks
3086                        } else {
3087                            1
3088                        };
3089                        InputEvent::MouseDown { button, clicks }
3090                    }
3091                    ElementState::Released => InputEvent::MouseUp { button },
3092                };
3093                self.dispatch(event_loop, i, ev);
3094            }
3095            WindowEvent::RedrawRequested
3096                if frame_waits_for_host(self.owed.get(), self.panes[i].deferred_frame) =>
3097            {
3098                // What would be painted predates an input the app has been
3099                // told about and not yet answered — the release of a
3100                // button, with the count still at its old value. The host
3101                // asks again the moment its handler has run, and that
3102                // frame carries both. Never twice running, so nothing here
3103                // can stop a window painting.
3104                self.panes[i].deferred_frame = true;
3105            }
3106            // A frame asked for while frames run back to back waits for
3107            // the display, which asks again at the vsync (`mod pacer`).
3108            WindowEvent::RedrawRequested if !self.panes[i].admit_frame() => {}
3109            WindowEvent::RedrawRequested => {
3110                self.panes[i].deferred_frame = false;
3111                self.redraw(i);
3112                // KUI_SMOKE_FRAMES: count what the main window actually
3113                // landed and quit at the target. Counted only once a
3114                // present succeeded, so a window that never draws never
3115                // counts and the run fails on the caller's timeout rather
3116                // than passing quietly. Asking for the next
3117                // one keeps an app that paints only on input painting, so
3118                // every example reaches the count at the same speed.
3119                if let Some(n) = self.smoke_frames
3120                    && let Some(p) = self.panes.get(i)
3121                    && p.id == WindowId::MAIN
3122                    && p.retry.presented_once()
3123                {
3124                    self.frames_drawn += 1;
3125                    if self.frames_drawn >= n {
3126                        self.exit_requested = true;
3127                    } else {
3128                        self.panes[i].redraw_for(FrameCause::SMOKE);
3129                    }
3130                }
3131                self.panes[i].publish_access();
3132                // Views can declare windows and window commands too
3133                // (ui.window, ui.window_command); apply them the same frame
3134                // they were declared. Likewise the sounds a frame started
3135                // (audio nodes, ui.play), and the clipboard work a view
3136                // queued (ui.set_clipboard, ui.request_paste — backlog
3137                // C33), which until now only an input's drain reached.
3138                self.apply_menu_actions(event_loop, i);
3139                self.apply_window_commands(event_loop);
3140                self.apply_audio();
3141                // The frame may have closed this very pane.
3142                let Some(i) = self.pane_index(id) else { return };
3143                // A new frame can put something else under a still cursor.
3144                self.panes[i].apply_cursor();
3145                // A frame can resize the viewport, and can change what sits
3146                // under a still cursor; route the resulting resize / hover
3147                // events now rather than with the next input, and redraw for
3148                // what they change.
3149                let pending = self.panes[i].core.take_pending_events();
3150                if !pending.is_empty() {
3151                    self.route_events(pending);
3152                    if let Some(p) = self.panes.get(i) {
3153                        p.redraw_for(FrameCause::AFTER_FRAME);
3154                    }
3155                }
3156                // Windows moves and resizes a window inside its own modal
3157                // loop, where `about_to_wait` — the pacing that asks for
3158                // the next frame of an animation — does not run, so a
3159                // transition froze for as long as the title bar was held
3160                // (backlog W3). The pane's own timer is what answers that,
3161                // and this is where it learns whether there is anything to
3162                // animate; `mod windows_anim` is why it is a timer and not
3163                // a redraw asked for from right here. Not while the window
3164                // waits for a device: `about_to_wait` asks it for no
3165                // animation frames then (RG29), and the timer asked for 64
3166                // a second, each a view built for no renderer (RG40). The
3167                // reopen's own `request_redraw` arms it again. Nor while
3168                // it is minimized (RG45); the restore's `Resized` does.
3169                #[cfg(target_os = "windows")]
3170                if let Some(p) = self.panes.get_mut(i) {
3171                    let animating = p.animates_now();
3172                    if let Some(t) = &mut p.anim_timer {
3173                        t.set(animating);
3174                    }
3175                }
3176            }
3177            _ => {}
3178        }
3179        if self.exit_requested {
3180            self.exit_main(event_loop);
3181        }
3182    }
3183
3184    /// AccessKit's side of the conversation: assistive technology attaching
3185    /// (send it the tree, and let the view know), detaching, or asking for
3186    /// an action (input).
3187    /// The loop's last event: the app's `teardown`, before the process
3188    /// goes (a Quit from the OS) or `run` returns (the main window
3189    /// closed).
3190    fn exiting(&mut self, _event_loop: &ActiveEventLoop) {
3191        self.teardown_once();
3192    }
3193
3194    fn user_event(&mut self, event_loop: &ActiveEventLoop, event: access_bridge::UserEvent) {
3195        // A wake is the app saying "what `view` shows has changed": every
3196        // window draws, as after any input. Coalesced by the platform's
3197        // queue, so a thread waking a thousand times a frame costs one.
3198        // A wake for later is a deadline, not a frame: the loop sleeps to
3199        // it in `about_to_wait`.
3200        if let access_bridge::UserEvent::WakeAt(at) = event {
3201            self.wake_at = Some(self.wake_at.map_or(at, |w| w.min(at)));
3202            return;
3203        }
3204        self.saw_event = true;
3205        if matches!(event, access_bridge::UserEvent::Wake) {
3206            for pane in &self.panes {
3207                pane.redraw_for(FrameCause::WAKE);
3208            }
3209            return;
3210        }
3211        // The installed fonts changed: the session scans again, through
3212        // any one window's core since every window shares it, and the
3213        // windows draw — each shapes its text again on that frame
3214        // (`weights_rev`), fallback being free to land on a new face. A
3215        // signal that found nothing new (a second window's copy of a
3216        // Windows broadcast) changes nothing and draws nothing.
3217        if matches!(event, access_bridge::UserEvent::FontsChanged) {
3218            system_fonts::handled();
3219            let changed = self
3220                .panes
3221                .first_mut()
3222                .map_or(0, |pane| pane.core.reload_system_fonts());
3223            if changed > 0 {
3224                for pane in &self.panes {
3225                    pane.redraw_for(FrameCause::APPEARANCE);
3226                }
3227            }
3228            return;
3229        }
3230        let Some(i) = access_bridge::window_of(&event).and_then(|w| self.pane_index(w)) else {
3231            return;
3232        };
3233        // A file dialog's answer, from the thread that waited on it: the
3234        // window that asked hears it as input.
3235        let event = match event {
3236            access_bridge::UserEvent::Files { paths, .. } => {
3237                self.dispatch(event_loop, i, InputEvent::Files(paths));
3238                return;
3239            }
3240            other => other,
3241        };
3242        let Some(bridge) = &mut self.panes[i].access else {
3243            return;
3244        };
3245        let was_listening = bridge.active();
3246        let req = bridge.on_event(event);
3247        // A client attaching or leaving is a fact the view reads
3248        // (`env.system.assistive`, backlog F48): `sync_env` writes it on
3249        // the next frame and the core reports it as a `system` event, so
3250        // the frame has to happen — an app that only redraws on input
3251        // would otherwise hear it with the next click.
3252        if bridge.active() != was_listening {
3253            self.panes[i].redraw_for(FrameCause::APPEARANCE);
3254        }
3255        if let Some(req) = req {
3256            self.dispatch(event_loop, i, InputEvent::Access(req));
3257        }
3258        if let Some(pane) = self.panes.get_mut(i) {
3259            pane.publish_access();
3260        }
3261    }
3262
3263    /// Runs after every event batch (including timer wake-ups): the caret
3264    /// blink clock, the transition clock, and the audio poll. Any caret
3265    /// activity re-arms the blink timer with the caret solid; each expiry
3266    /// toggles the phase and schedules the next. A transition mid-flight
3267    /// asks for the next frame right away (vsync paces it). While a sound
3268    /// plays, the loop wakes every `AUDIO_POLL` to notice it finishing.
3269    fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
3270        // A loop taken back from an earlier runner delivered its init
3271        // events — `resumed` among them — to that runner's shell; this one
3272        // opens its window from the first turn it gets instead.
3273        if self.pumped && !self.opened && !self.exit_requested {
3274            self.resumed(event_loop);
3275        }
3276        // The platform's menu comes and goes between turns of the loop, so
3277        // this is where its answer is collected. The menu bar is handed
3278        // over and read back from the same place, for the same reason.
3279        self.pump_native_menu(event_loop);
3280        self.pump_menu_bar(event_loop);
3281        self.pump_text_input(event_loop);
3282        self.pump_file_drag(event_loop);
3283        self.pump_open_documents(event_loop);
3284        self.settle_focus();
3285        self.apply_secure_input();
3286        self.dismiss_popups_if_deactivated();
3287        self.poll_audio();
3288        self.apply_audio();
3289        let now = std::time::Instant::now();
3290        let mut deadline: Option<std::time::Instant> = None;
3291        // A frame asked for at a time (backlog F135): by a `Waker` for
3292        // every window, by a view for its own (`Core::next_frame_at`, on
3293        // the frame clock, whose origin is `epoch`). Drawn once it is due,
3294        // and slept to until then.
3295        if let Some(at) = self.wake_at {
3296            if at <= now {
3297                self.wake_at = None;
3298                for pane in &self.panes {
3299                    pane.redraw_for(FrameCause::WAKE);
3300                }
3301            } else {
3302                deadline = Some(deadline.map_or(at, |d| d.min(at)));
3303            }
3304        }
3305        for pane in &self.panes {
3306            let Some(secs) = pane.core.next_frame_at().filter(|s| s.is_finite()) else {
3307                continue;
3308            };
3309            let at = self.epoch + std::time::Duration::from_secs_f64(secs.max(0.0));
3310            if at <= now {
3311                pane.redraw_for(FrameCause::WAKE);
3312            } else {
3313                deadline = Some(deadline.map_or(at, |d| d.min(at)));
3314            }
3315        }
3316        // A new device owed (`reopen_owed`): made now if its second is
3317        // up, and otherwise — or when this try left a window without one —
3318        // woken for when it is. This deadline is the only thing that
3319        // brings the loop back to it: a window waiting for a device asks
3320        // for no frames of its own, below.
3321        if self.reopen_owed {
3322            if reopen_due(self.reopened, now) <= now {
3323                self.reopen_device();
3324            }
3325            if self.reopen_owed {
3326                let at = reopen_due(self.reopened, now);
3327                deadline = Some(deadline.map_or(at, |d| d.min(at)));
3328            }
3329        }
3330        for pane in &mut self.panes {
3331            // Not while the window waits for a device: with nothing to
3332            // present to there is no vsync to pace the frame, and each one
3333            // would only find the device still owed. Nor while it is
3334            // minimized or covered (`Pane::animates_now`, RG45, RG98).
3335            // Nor while the pacer holds one for the display: the link
3336            // brings that frame back at the next vsync, `overdue` below
3337            // is the wake if it does not, and asking again in between is
3338            // held again at once — the loop went round as fast as it
3339            // could until the vsync came (backlog F103). Nor straight
3340            // after a frame the surface skipped: that returned at once,
3341            // with no vsync behind it, and the next is asked for a retry
3342            // apart instead of on the loop's next turn (`mod retry`).
3343            if pane.animates_now() && !pane.pacer.holding() {
3344                match pane.retry.animation_waits(now) {
3345                    None => pane.redraw_for(FrameCause::OWED),
3346                    Some(at) => deadline = Some(deadline.map_or(at, |d| d.min(at))),
3347                }
3348            }
3349            // A frame held for a display that stopped firing is drawn
3350            // anyway once it has waited too long (`mod pacer`).
3351            if let Some((at, due)) = pane.pacer.overdue(now) {
3352                if due {
3353                    pane.redraw_for(FrameCause::OVERDUE);
3354                } else {
3355                    deadline = Some(deadline.map_or(at, |d| d.min(at)));
3356                }
3357            }
3358            // A frame the surface skipped asks again — a retry apart, and
3359            // only so many times (`mod retry`).
3360            let (ask, wake) = pane.retry.poll(now);
3361            if ask {
3362                pane.redraw_for(FrameCause::RETRY);
3363            }
3364            if let Some(at) = wake {
3365                deadline = Some(deadline.map_or(at, |d| d.min(at)));
3366            }
3367            // A caret to blink: the stock editor's, or the `caret` a
3368            // custom editor declares on one of its lines (backlog C35).
3369            // None, or a solid one (`caret_solid`, F68): the phase is
3370            // parked on — focused window or not, since the clock owns
3371            // the phase only of a caret it blinks; what a solid caret
3372            // looks like without the keyboard (hollow, as a GUI editor's
3373            // block goes; dimmed; gone) is the view's own reading of
3374            // `env.focused`, which the hiding below is not a substitute
3375            // for (RG14 (j), in F68's entry). Caught mid-blink — Escape
3376            // from insert mode on the off phase — the frame that handled
3377            // the key read the phase off, so one more is asked for; once,
3378            // not a loop.
3379            if !pane.core.has_caret() {
3380                if !pane.blink_visible {
3381                    pane.blink_visible = true;
3382                    pane.core.set_caret_visible(true);
3383                    pane.redraw_for(FrameCause::CARET);
3384                }
3385                pane.blink_deadline = None;
3386                continue;
3387            }
3388            // A caret in a window that does not have the keyboard is not
3389            // blinking on any platform, and the blink is the one thing in
3390            // an idle app that asks for a frame twice a second forever: an
3391            // editor left in the background drew 120 frames a minute for a
3392            // caret nobody could type into. Hidden rather than parked
3393            // solid, because solid is what a *focused* field looks like.
3394            // The caret comes back with the keyboard: `blink_deadline` is
3395            // cleared here, and a cleared deadline is what the arm below
3396            // reads as "start blinking", so the first `about_to_wait`
3397            // after focus returns shows it and re-arms.
3398            if !pane.core.env.focused {
3399                if pane.blink_visible {
3400                    pane.blink_visible = false;
3401                    pane.core.set_caret_visible(false);
3402                    pane.redraw_for(FrameCause::CARET);
3403                }
3404                pane.blink_deadline = None;
3405                continue;
3406            }
3407            let stamp = pane.core.caret_stamp();
3408            if stamp != pane.caret_stamp_seen || pane.blink_deadline.is_none() {
3409                pane.caret_stamp_seen = stamp;
3410                pane.blink_deadline = Some(now + BLINK_INTERVAL);
3411                if !pane.blink_visible {
3412                    pane.blink_visible = true;
3413                    pane.core.set_caret_visible(true);
3414                    pane.redraw_for(FrameCause::CARET);
3415                }
3416            } else if now >= pane.blink_deadline.unwrap() {
3417                pane.blink_visible = !pane.blink_visible;
3418                pane.core.set_caret_visible(pane.blink_visible);
3419                pane.blink_deadline = Some(now + BLINK_INTERVAL);
3420                pane.redraw_for(FrameCause::CARET);
3421            }
3422            if let Some(d) = pane.blink_deadline {
3423                deadline = Some(deadline.map_or(d, |e| e.min(d)));
3424            }
3425        }
3426        if self.audio.active() {
3427            let poll = now + AUDIO_POLL;
3428            deadline = Some(deadline.map_or(poll, |d| d.min(poll)));
3429        }
3430        // An output device nothing has used for `AUDIO_IDLE_CLOSE` is let
3431        // go: it is a real-time thread the OS keeps calling, and it is what
3432        // an idle app that owns a sound spends its whole CPU on. The wake
3433        // this schedules is the point — under `ControlFlow::Wait` a truly
3434        // idle app is never called again, so without a deadline the close
3435        // would be scheduled and never run.
3436        if self.audio.holds_device() {
3437            if !self.audio.active() && self.audio_touch.elapsed() >= AUDIO_IDLE_CLOSE {
3438                self.audio.close();
3439            }
3440            if self.audio.holds_device() {
3441                // Either it is still in use (wake when the hold expires) or
3442                // the close found it mid-open (ask again shortly).
3443                let at = (self.audio_touch + AUDIO_IDLE_CLOSE).max(now + AUDIO_POLL);
3444                deadline = Some(deadline.map_or(at, |d| d.min(at)));
3445            }
3446        }
3447        // A batch that carried an event has left a redraw asked for, so the
3448        // shell wants pumping at once to present it — and a driver reading
3449        // this is also being told that the window is in use, which is the
3450        // one thing it cannot work out from the events *it* was handed.
3451        if std::mem::take(&mut self.saw_event) {
3452            deadline = Some(now);
3453            self.woke = true;
3454        }
3455        self.next_deadline = deadline;
3456        event_loop.set_control_flow(match deadline {
3457            Some(d) => ControlFlow::WaitUntil(d),
3458            None => ControlFlow::Wait,
3459        });
3460    }
3461}
3462
3463// Re-exported so apps can reach the renderer without depending on kui-wgpu.
3464pub use kui_wgpu::{RenderError, Renderer, wgpu};
3465
3466/// `KUI_LOSE_DEVICE=SECS`, read once: when after launch to pretend the
3467/// device was lost (`Shell::redraw`), or never.
3468fn lose_device_at() -> Option<f64> {
3469    static AT: std::sync::OnceLock<Option<f64>> = std::sync::OnceLock::new();
3470    *AT.get_or_init(|| std::env::var("KUI_LOSE_DEVICE").ok()?.parse().ok())
3471}
3472
3473#[cfg(test)]
3474mod tests {
3475    use super::*;
3476
3477    struct Empty;
3478    impl App for Empty {
3479        fn view(&mut self, _ui: &mut Ui<'_>) {}
3480    }
3481
3482    /// An app that borrows is still an app: the runner is compiled once
3483    /// against `Shell<dyn App + 'a>`, and neither `run` nor
3484    /// `open` asks for `'static`. Checked by the compiler alone: a loop
3485    /// cannot be built on a test's worker thread.
3486    #[test]
3487    fn an_app_that_borrows_still_runs() {
3488        struct Borrowing<'a>(&'a str);
3489        impl App for Borrowing<'_> {
3490            fn view(&mut self, ui: &mut Ui<'_>) {
3491                ui.text(self.0, TextStyle::new(12.0));
3492            }
3493        }
3494        let title = String::from("t");
3495        let _run = || app("t").run(Borrowing(&title));
3496        let _open = || {
3497            let mut runner = app("t").open(Borrowing(&title))?;
3498            let Borrowing(s) = runner.app_mut();
3499            assert_eq!(*s, "t");
3500            Ok::<_, Box<dyn std::error::Error>>(())
3501        };
3502    }
3503
3504    /// `App::teardown` runs once, whichever of the runner's ends comes
3505    /// first and however many come after:
3506    /// `retire` — what a pump returning false, `request_exit` and the
3507    /// runner's drop all reach — and `teardown_once` itself, which is what
3508    /// the loop's `exiting` calls. The runner is built without a loop
3509    /// here: winit builds its loop on the main thread only, and a test
3510    /// runs on a worker.
3511    #[test]
3512    fn teardown_runs_once_across_retire_and_drop() {
3513        use std::cell::Cell;
3514        use std::rc::Rc;
3515        struct Counting(Rc<Cell<u32>>);
3516        impl App for Counting {
3517            fn view(&mut self, _ui: &mut Ui<'_>) {}
3518            fn teardown(&mut self) {
3519                self.0.set(self.0.get() + 1);
3520            }
3521        }
3522        let count = Rc::new(Cell::new(0));
3523        let runner_of = |app: Counting| PumpRunner {
3524            state: PumpState {
3525                event_loop: None,
3526                alive: true,
3527                pumps: 0,
3528                woken_pumps: 0,
3529            },
3530            shell: std::mem::ManuallyDrop::new(super::app("t").diagnostics(false).shell(app)),
3531        };
3532        let mut runner = runner_of(Counting(count.clone()));
3533        assert_eq!(count.get(), 0, "nothing before the end");
3534        runner.request_exit();
3535        assert_eq!(count.get(), 0, "asking is not the end");
3536        runner.retire();
3537        assert_eq!(count.get(), 1, "retiring is");
3538        assert!(!runner.state.alive);
3539        runner.retire();
3540        runner.shell_mut().teardown_once();
3541        assert_eq!(count.get(), 1, "once, however many ends come after");
3542        drop(runner);
3543        assert_eq!(
3544            count.get(),
3545            1,
3546            "the drop retires again, and it is still once"
3547        );
3548
3549        // The drop alone — a runner let go of while alive — is an end too.
3550        let count = Rc::new(Cell::new(0));
3551        drop(runner_of(Counting(count.clone())));
3552        assert_eq!(count.get(), 1);
3553    }
3554
3555    /// A turn whose batch saw an event is counted once, and the flag is
3556    /// taken with it: left set, every quiet turn after the first event
3557    /// would count, and a test reading `woken_pumps` would
3558    /// see the desktop in every idle second. Driven through `tally`, the
3559    /// step each pump ends with, since `about_to_wait` — which sets the
3560    /// flag — needs a live loop, and winit builds one on the main thread
3561    /// only.
3562    #[test]
3563    fn a_woken_turn_is_counted_once() {
3564        let mut runner = PumpRunner {
3565            state: PumpState {
3566                event_loop: None,
3567                alive: true,
3568                pumps: 0,
3569                woken_pumps: 0,
3570            },
3571            shell: std::mem::ManuallyDrop::new(super::app("t").diagnostics(false).shell(Empty)),
3572        };
3573        let tally = |r: &mut PumpRunner<Empty>| r.state.tally(&mut **r.shell);
3574        tally(&mut runner);
3575        assert_eq!(runner.woken_pumps(), 0, "a quiet turn is not woken");
3576        runner.shell_mut().woke = true;
3577        tally(&mut runner);
3578        assert_eq!(runner.woken_pumps(), 1);
3579        assert!(!runner.shell().woke, "taken by the turn that counted it");
3580        tally(&mut runner);
3581        tally(&mut runner);
3582        assert_eq!(
3583            runner.woken_pumps(),
3584            1,
3585            "and the quiet turns after it are quiet"
3586        );
3587    }
3588
3589    /// A frame waits only for an answer that is actually owed, and never
3590    /// twice running — the bound that keeps a stream of input, or a
3591    /// platform modal loop the host cannot interrupt, from stopping the
3592    /// window altogether.
3593    /// A press and a release finish something the core drew; a pointer
3594    /// moving and a wheel turning do not, and a frame that waited on those
3595    /// would cost a drag half its frames.
3596    #[test]
3597    fn only_a_discrete_input_is_worth_waiting_for() {
3598        assert!(input_completes(&InputEvent::mouse_down(1)));
3599        assert!(input_completes(&InputEvent::mouse_up()));
3600        assert!(input_completes(&InputEvent::Text("x".into())));
3601        assert!(!input_completes(&InputEvent::CursorMoved(Vec2::ZERO)));
3602        assert!(!input_completes(&InputEvent::Scroll(Vec2::ZERO)));
3603        assert!(!input_completes(&InputEvent::CursorLeft));
3604    }
3605
3606    #[test]
3607    fn a_frame_never_waits_twice_running() {
3608        assert!(frame_waits_for_host(true, false));
3609        assert!(!frame_waits_for_host(true, true));
3610        assert!(!frame_waits_for_host(false, false));
3611        assert!(!frame_waits_for_host(false, true));
3612    }
3613
3614    /// The default is an app that answers inside `on_event`; only a host
3615    /// driving the loop itself opts out.
3616    #[test]
3617    fn deferred_events_is_opt_in() {
3618        assert!(!app("t").deferred_events);
3619        assert!(app("t").deferred_events().deferred_events);
3620    }
3621
3622    #[test]
3623    fn clamp_size_honors_each_bound() {
3624        let bounds = (Some((400.0, 300.0)), Some((1200.0, 900.0)));
3625        assert_eq!(
3626            clamp_size((800.0, 600.0), bounds.0, bounds.1),
3627            (800.0, 600.0)
3628        );
3629        assert_eq!(
3630            clamp_size((100.0, 100.0), bounds.0, bounds.1),
3631            (400.0, 300.0)
3632        );
3633        assert_eq!(
3634            clamp_size((4000.0, 4000.0), bounds.0, bounds.1),
3635            (1200.0, 900.0)
3636        );
3637        // Per-axis, and unbounded sides pass through untouched.
3638        assert_eq!(
3639            clamp_size((100.0, 4000.0), bounds.0, bounds.1),
3640            (400.0, 900.0)
3641        );
3642        assert_eq!(clamp_size((10.0, 10.0), None, None), (10.0, 10.0));
3643        // A max below the min loses to it, as the platforms resolve it.
3644        assert_eq!(
3645            clamp_size((800.0, 600.0), Some((500.0, 500.0)), Some((200.0, 200.0))),
3646            (500.0, 500.0)
3647        );
3648    }
3649
3650    #[test]
3651    fn diagnostics_follow_the_build_unless_told_otherwise() {
3652        assert_eq!(
3653            app("t").dyn_shell(Empty).core_mut().diagnostics(),
3654            cfg!(debug_assertions)
3655        );
3656        assert!(
3657            app("t")
3658                .diagnostics(true)
3659                .dyn_shell(Empty)
3660                .core_mut()
3661                .diagnostics()
3662        );
3663        assert!(
3664            !app("t")
3665                .diagnostics(false)
3666                .dyn_shell(Empty)
3667                .core_mut()
3668                .diagnostics()
3669        );
3670    }
3671
3672    /// `Launcher::core`: what the host registered on the
3673    /// core it hands over is the window's, its session is the app's, and
3674    /// what the launcher was told still lands on top, in order.
3675    #[test]
3676    fn a_handed_core_is_the_main_window_s_and_its_session_the_app_s() {
3677        let session = Session::new();
3678        let mut core = Core::new_in(&session);
3679        core.set_devtools(true);
3680        core.set_native_menus(false);
3681        let image = core.resources.add_image(1, 1, vec![0; 4]);
3682        let mut shell = app("t").diagnostics(false).core(core).dyn_shell(Empty);
3683        assert!(shell.session.is(&session), "the session came along");
3684        assert!(shell.core_mut().devtools(), "the devtools door held");
3685        assert!(!shell.core_mut().native_menus(), "so did the menus one");
3686        assert!(
3687            !shell.core_mut().diagnostics(),
3688            "the launcher's own setting lands after"
3689        );
3690        assert_eq!(
3691            shell.core_mut().resources.image_size(image),
3692            Some((1, 1)),
3693            "a resource the host registered draws in the window"
3694        );
3695
3696        // `setup_core` runs on the handed core, last.
3697        let mut core = Core::new();
3698        core.set_devtools(true);
3699        let mut shell = app("t")
3700            .core(core)
3701            .setup_core(|c| c.set_devtools(false))
3702            .dyn_shell(Empty);
3703        assert!(!shell.core_mut().devtools());
3704    }
3705
3706    /// `Launcher::devtools_key` is the `set_devtools_key` door in builder
3707    /// form: the chord lands on the main window's core before its first
3708    /// frame, and the default stands for an app that never asked.
3709    #[test]
3710    fn the_devtools_chord_is_the_launcher_s_to_respell() {
3711        let mut shell = app("t").diagnostics(false).dyn_shell(Empty);
3712        assert_eq!(shell.core_mut().devtools_key().spelling(), "ctrl+shift+i");
3713        let mut shell = app("t")
3714            .diagnostics(false)
3715            .devtools_key(Accel::parse("f12").unwrap())
3716            .dyn_shell(Empty);
3717        assert_eq!(shell.core_mut().devtools_key().spelling(), "f12");
3718    }
3719
3720    /// An icon from a PNG's bytes (backlog F138), and bytes that are not
3721    /// one refused with the reason.
3722    #[test]
3723    fn launcher_takes_an_icon_from_a_png() {
3724        let img = image::RgbaImage::from_pixel(16, 16, image::Rgba([10, 20, 30, 255]));
3725        let mut png = std::io::Cursor::new(Vec::new());
3726        img.write_to(&mut png, image::ImageFormat::Png).unwrap();
3727        let l = app("t").try_icon_bytes(png.get_ref()).unwrap();
3728        assert!(l.icon.is_some());
3729        let e = app("t").try_icon_bytes(b"not a png").err().unwrap();
3730        assert!(e.starts_with("an icon: not an image"), "{e}");
3731    }
3732
3733    #[test]
3734    fn launcher_clamps_the_initial_size_into_the_bounds() {
3735        let shell = app("t")
3736            .size(320.0, 240.0)
3737            .min_size(640.0, 480.0)
3738            .dyn_shell(Empty);
3739        assert_eq!(shell.size, (640.0, 480.0));
3740        assert_eq!(shell.min_size, Some((640.0, 480.0)));
3741
3742        let shell = app("t")
3743            .size(1600.0, 1200.0)
3744            .max_size(800.0, 600.0)
3745            .dyn_shell(Empty);
3746        assert_eq!(shell.size, (800.0, 600.0));
3747        assert_eq!(shell.max_size, Some((800.0, 600.0)));
3748
3749        // Builder order is irrelevant: the clamp happens once, at `shell`.
3750        let shell = app("t")
3751            .min_size(640.0, 480.0)
3752            .size(320.0, 240.0)
3753            .dyn_shell(Empty);
3754        assert_eq!(shell.size, (640.0, 480.0));
3755    }
3756
3757    /// A device that will not open is tried once a second (RG29): the
3758    /// first ask runs at once, an ask inside the second after a try waits
3759    /// for the second to be up — the instant `about_to_wait` sleeps until,
3760    /// not a frame asked for every turn — and one after it runs at once.
3761    #[test]
3762    fn a_reopen_waits_out_the_second_after_the_last_try() {
3763        let now = std::time::Instant::now();
3764        assert_eq!(reopen_due(None, now), now, "the first try is at once");
3765        let recent = now - std::time::Duration::from_millis(300);
3766        assert_eq!(
3767            reopen_due(Some(recent), now),
3768            recent + REOPEN_INTERVAL,
3769            "inside the second: woken when it is up"
3770        );
3771        assert!(reopen_due(Some(recent), now) > now, "and not before");
3772        let old = now - std::time::Duration::from_secs(5);
3773        assert_eq!(reopen_due(Some(old), now), now, "past it: at once");
3774        let edge = now - REOPEN_INTERVAL;
3775        assert_eq!(reopen_due(Some(edge), now), now, "at it: at once");
3776    }
3777
3778    /// A surface refused frame after frame while its reopen waits (RG30):
3779    /// retried `SURFACE_TRIES` times, then given up — said once — and the
3780    /// count saturates rather than wrapping back into retries or, in a
3781    /// debug build, panicking, however many frames input asks for.
3782    #[test]
3783    fn a_refused_surface_is_retried_then_given_up_once_and_the_count_saturates() {
3784        let mut tries = 0u8;
3785        let answers: Vec<Refused> = (0..1000).map(|_| surface_refused(&mut tries)).collect();
3786        let retries = SURFACE_TRIES as usize;
3787        assert!(answers[..retries].iter().all(|a| *a == Refused::Retry));
3788        assert_eq!(answers[retries], Refused::GiveUp { say: true });
3789        assert!(
3790            answers[retries + 1..]
3791                .iter()
3792                .all(|a| *a == Refused::GiveUp { say: false }),
3793            "given up, and said only the once"
3794        );
3795        assert_eq!(tries, u8::MAX);
3796    }
3797
3798    /// Subpixel text follows the device it is drawn on (RG32): asked for
3799    /// or left to `Auto`, it is on only where the device blends per
3800    /// channel — so a reopen onto an adapter without dual-source blending
3801    /// turns it off — and grayscale asked for is grayscale everywhere.
3802    #[test]
3803    fn subpixel_text_is_decided_by_each_device() {
3804        assert!(subpixel_on(TextAa::Auto, true));
3805        assert!(!subpixel_on(TextAa::Auto, false));
3806        assert!(subpixel_on(TextAa::Subpixel, true));
3807        assert!(!subpixel_on(TextAa::Subpixel, false));
3808        assert!(!subpixel_on(TextAa::Grayscale, true));
3809        assert!(!subpixel_on(TextAa::Grayscale, false));
3810    }
3811
3812    /// Over a backdrop `Auto` is grayscale (backlog F126): an LCD mask is
3813    /// a blend against pixels the window cannot see. What the app named
3814    /// stands, and an opaque window keeps `Auto`.
3815    #[test]
3816    fn a_backdrop_takes_auto_to_grayscale() {
3817        assert_eq!(aa_under(TextAa::Auto, Backdrop::Opaque), TextAa::Auto);
3818        for b in [Backdrop::Transparent, Backdrop::Blur, Backdrop::Tinted] {
3819            assert_eq!(aa_under(TextAa::Auto, b), TextAa::Grayscale);
3820            assert_eq!(aa_under(TextAa::Subpixel, b), TextAa::Subpixel);
3821        }
3822        assert!(!subpixel_on(aa_under(TextAa::Auto, Backdrop::Tinted), true));
3823    }
3824
3825    /// An app that never asked opens its device as it always did: no
3826    /// transparent surfaces, so on Windows the swapchain on the window's
3827    /// handle it always had.
3828    #[test]
3829    fn only_a_backdrop_opens_a_transparent_device() {
3830        let shell = app("plain").dyn_shell(Empty);
3831        assert!(!shell.gpu_options().transparent);
3832        let shell = app("glass").backdrop(Backdrop::Blur).dyn_shell(Empty);
3833        assert!(shell.gpu_options().transparent);
3834    }
3835}