retroglyph_window/lib.rs
1//! A shared layer for window-based backends (software, GL, wgpu).
2//!
3//! # Architecture
4//!
5//! [`retroglyph_core::backend::Input`] and [`retroglyph_core::backend::Output`] are two
6//! independent facets of [`Backend`](retroglyph_core::Backend), which fits a terminal process
7//! (one type implements both) but not a window: there, an event loop owns input and a renderer
8//! owns output separately. This crate keeps that split -- [`Presenter`] is an `Output` supertrait,
9//! [`WindowBackend`] owns its own `Input` event queue -- and reassembles both into one `Backend`:
10//!
11//! ```text
12//! ┌─────────────────────────────┐
13//! │ event loop (winit or │
14//! │ a custom driver) │
15//! └──────────────┬───────────────┘
16//! translated events
17//! │
18//! v
19//! ┌────────────────────────────────────────────────────┐
20//! │ WindowBackend<P: Presenter> │
21//! │ (implements Backend: owns the input event queue, │
22//! │ delegates output to P) │
23//! └───────────────────────┬──────────────────────────────┘
24//! │ draw / flush / resize / present
25//! v
26//! ┌───────────────────────────────┐
27//! │ P: Presenter │
28//! │ (retroglyph-software today; │
29//! │ wgpu/GL renderers planned) │
30//! └───────────────────────────────┘
31//! ```
32//!
33//! - [`Presenter`] is `Output` plus the surface lifecycle
34//! (`init_surface`/`resize_surface`/`present`/`cell_size`). Renderer crates implement only
35//! this trait, which gives them `Output` for free.
36//! - <code>[WindowBackend]<P: Presenter></code> implements `Output` (by delegating to
37//! `P`), `Input` (via its own event queue), and the no-op default `Cursor` (windowed backends
38//! have no text cursor), which together give it `Backend` generically.
39//! - The `winit` module (feature-gated, see below) drives the event loop
40//! that fills that queue and calls `Presenter::present` each frame.
41//!
42//! # Feature flags
43//!
44//! [`Presenter`], [`WindowBackend`], and [`WindowHandle`] depend only on
45//! [`raw-window-handle`](raw_window_handle) and are always available. The
46//! `winit` feature (default on) additionally provides the `winit` module:
47//! the event loop, event translation, and the `run_windowed`/`run_app`
48//! drivers. Disable it to implement or drive `Presenter` with a different
49//! windowing library (SDL2, tao, a custom loop) without pulling in winit.
50//!
51//! # DPI, scale, and the resize contract
52//!
53//! [`Presenter::cell_size`] returns the cell size in **physical pixels** -- the same pixel
54//! space as `winit::dpi::PhysicalSize` -- not logical/DPI-scaled ("CSS" or "point") pixels.
55//! This crate performs no automatic DPI scaling of it: nothing here changes `cell_size()` in
56//! response to a display's scale factor. `SoftwareRenderer`'s cell size, for example, is
57//! fixed at construction (glyph size × its integer `scale` config) and never changes on a
58//! [`Presenter::scale_factor_changed`] notification. A presenter that wants larger cells on a
59//! `HiDPI` display has to opt into that itself from `scale_factor_changed` (e.g. regenerating a
60//! font atlas at a new pixel density); until one does, the grid renders at a fixed physical
61//! pixel size on every display, `HiDPI` or not.
62//!
63//! Window resize is clamped to whole cells: a physical size that isn't an exact multiple of
64//! `cell_size()` has its sub-cell remainder truncated, not centered or cleared, and the OS
65//! window is never resized to compensate -- see [`Presenter::resize_surface`]'s doc comment
66//! for the full contract, including the unpainted trailing strip this can leave on screen.
67//!
68//! # Threading model
69//!
70//! The windowed drivers (`winit::run_windowed`, `winit::run_app`, and their `_with_proxy`
71//! variants) are single-threaded: the event loop, every [`Presenter`] call, and the app
72//! closure/[`App`](retroglyph_core::App) callback all run on the one thread that calls
73//! `run_windowed`/`run_app` -- the main thread, on platforms (e.g. macOS) that require it for
74//! windowing. Neither [`Presenter`] nor [`WindowBackend`] carries a `Send`/`Sync` bound
75//! anywhere in this crate, and a presenter is free to hold thread-affine state accordingly
76//! (an `Rc`, a non-`Send` GPU context handle). The only supported way to reach the loop from
77//! another thread is `winit::EventProxy<T>`, which is `Send + Sync + Clone` for any
78//! `T: Send + 'static` -- it does not give another thread direct access to the `Presenter` or
79//! `Terminal`. With the default `T = u64` (`winit::run_windowed_with_proxy`/
80//! `run_app_with_proxy`), the payload surfaces as an opaque
81//! [`Event::Custom`](retroglyph_core::event::Event::Custom); a custom `T`
82//! (`winit::run_windowed_with_typed_proxy`/`run_app_with_typed_proxy`) bypasses `Event` entirely
83//! and goes straight to a caller-supplied handler, since `Event::Custom` itself stays fixed to
84//! `u64`.
85
86/// The generic [`Backend`](retroglyph_core::Backend) for windowed presenters.
87pub mod backend;
88/// System clipboard read/write ([`Clipboard`], [`SystemClipboard`] on native targets).
89pub mod clipboard;
90/// The [`Presenter`] trait and [`WindowHandle`](presenter::WindowHandle).
91pub mod presenter;
92/// The winit event loop, event translation, and app drivers.
93#[cfg(feature = "winit")]
94pub mod winit;
95
96// Compile the code blocks in this crate's own README as doctests so its quick start is
97// type-checked on every test run and cannot silently rot. The `cfg(doctest)` gate keeps this out
98// of the rendered crate documentation -- see `retroglyph-crossterm`'s matching include for the
99// same pattern applied to the workspace root README.
100#[cfg(doctest)]
101#[doc = include_str!("../README.md")]
102struct ReadmeDoctests;
103
104pub use backend::WindowBackend;
105#[cfg(not(target_arch = "wasm32"))]
106pub use clipboard::SystemClipboard;
107pub use clipboard::{Clipboard, ClipboardError};
108pub use presenter::{Presenter, RecoverableError, WindowHandle};
109
110// Re-exported so presenters can name the handle traits without adding their
111// own raw-window-handle dependency (and so versions can't drift apart).
112pub use raw_window_handle;