densa-window 0.1.0

Runs a densa interface in a native window: winit input in, one wgpu draw call per frame out
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
//! Runs a densa interface in a native window.
//!
//! [`run`] opens a window, creates a [`Ui`] for it and hands it to an [`App`]
//! to fill with widgets. From then on it translates the window system's input (winit) into
//! `densa_ui` events, passes the actions widgets report to the app, and paints the interface
//! with `densa_render` (wgpu) in one draw call per frame, presented with V-Sync.
//!
//! Frames are painted when something changed, when another thread asks for one through a
//! [`Waker`] (new data arrived), or continuously while the app asks for it (animation).
//!
//! Screen readers (Narrator, VoiceOver, Orca) read the interface through AccessKit: the
//! window sends the [`Ui`]'s accessibility tree after each frame, only while one is
//! listening, and carries out what it asks.
//!
//! The [`Ui`]'s text uses `densa_paragraph::TextSystem::new`: the bundled font, and the
//! system's fonts for characters it lacks (Korean, Japanese, Chinese, ...). Turning off the
//! default `system-fonts` feature leaves the bundled font alone.
//!
//! ```no_run
//! use std::error::Error;
//! use densa_ui::{Ui, Style};
//! use densa_window::{App, Host, WindowOptions};
//!
//! struct Hello;
//!
//! impl App for Hello {
//!     fn build(&mut self, ui: &mut Ui, host: &mut Host<'_>) -> Result<(), Box<dyn Error>> {
//!         // Add widgets under ui.root() here; hand host.waker() to threads that feed them.
//!         let _ = (ui.root(), host.waker());
//!         Ok(())
//!     }
//! }
//!
//! densa_window::run(WindowOptions::new("Hello"), Hello)?;
//! # Ok::<(), Box<dyn Error>>(())
//! ```

mod access;
mod gpu;
mod translate;

use std::error::Error;
use std::fmt;
use std::sync::Arc;
use std::time::Instant;

use densa_core::{Point, Size};
use densa_draw::{Canvas, InstanceBuffer};
use densa_paragraph::TextSystem;
use densa_text::{AtlasConfig, GlyphCache};
use densa_ui::{Action, Cursor, KeyEvent, PointerEvent, PointerKind, Theme, Ui};
use winit::application::ApplicationHandler;
use winit::dpi::{LogicalSize, PhysicalPosition, PhysicalSize};
use winit::event::{ElementState, WindowEvent};
use winit::event_loop::{ActiveEventLoop, EventLoop, EventLoopProxy};
use winit::keyboard::ModifiersState;
use winit::window::{Window, WindowId};

use crate::gpu::Gpu;

/// An application: it builds its interface once, then reacts to what widgets report and
/// updates live content before each frame. Every call gets the [`Host`]: the window it runs
/// in.
pub trait App: 'static {
    /// Fills the interface: called once, when the window and its [`Ui`] exist.
    ///
    /// # Errors
    /// Any error stops [`run`], which returns it.
    fn build(&mut self, ui: &mut Ui, host: &mut Host<'_>) -> Result<(), Box<dyn Error>>;

    /// Handles an action a widget reported (a click, an edit), right after the input that
    /// caused it.
    fn action(&mut self, ui: &mut Ui, host: &mut Host<'_>, action: Action) {
        let _ = (ui, host, action);
    }

    /// Updates live content before a frame is painted. Return `true` to keep painting frames
    /// continuously (animation), `false` to paint only when something changes or a
    /// [`Waker`] asks for a frame.
    fn frame(&mut self, ui: &mut Ui, host: &mut Host<'_>) -> bool {
        let _ = (ui, host);
        false
    }
}

/// The window an [`App`] runs in.
#[derive(Debug)]
pub struct Host<'a> {
    window: &'a Window,
    proxy: &'a EventLoopProxy<UserEvent>,
    close: &'a mut bool,
}

impl Host<'_> {
    /// A handle any thread can use to ask for a frame: a feed thread calls
    /// [`Waker::wake`] after publishing new data.
    pub fn waker(&self) -> Waker {
        Waker(self.proxy.clone())
    }

    /// Sets the window's title.
    pub fn set_title(&self, title: &str) {
        self.window.set_title(title);
    }

    /// Closes the window once the current call returns, which ends [`run`].
    pub fn close(&mut self) {
        *self.close = true;
    }
}

/// Asks the window's thread for a frame, from any thread.
#[derive(Debug, Clone)]
pub struct Waker(EventLoopProxy<UserEvent>);

impl Waker {
    /// Asks for a frame: [`App::frame`] runs and the interface is painted soon. `false` if
    /// the window is gone.
    pub fn wake(&self) -> bool {
        self.0.send_event(UserEvent::Wake).is_ok()
    }
}

/// What other threads send the window's thread: a [`Waker`]'s request for a frame, or a
/// screen reader's request through AccessKit.
#[derive(Debug)]
enum UserEvent {
    Wake,
    Access(accesskit_winit::Event),
}

impl From<accesskit_winit::Event> for UserEvent {
    fn from(event: accesskit_winit::Event) -> Self {
        Self::Access(event)
    }
}

/// The window [`run`] opens.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct WindowOptions {
    /// The window's title.
    pub title: String,
    /// The window's inner size, in logical pixels.
    pub size: Size,
    /// Colors and sizes of the widgets.
    pub theme: Theme,
    /// Rectangles and glyphs a frame can hold; more are dropped (and counted).
    pub instances: usize,
}

impl WindowOptions {
    /// A 1280x800 window titled `title`, with the default theme.
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            size: Size::new(1280.0, 800.0),
            theme: Theme::default(),
            instances: 65_536,
        }
    }

    /// The options with an inner size in logical pixels.
    #[must_use]
    pub fn with_size(mut self, width: f32, height: f32) -> Self {
        self.size = Size::new(width, height);
        self
    }

    /// The options with `theme`.
    #[must_use]
    pub fn with_theme(mut self, theme: Theme) -> Self {
        self.theme = theme;
        self
    }

    /// The options holding up to `instances` rectangles and glyphs a frame.
    #[must_use]
    pub fn with_instances(mut self, instances: usize) -> Self {
        self.instances = instances;
        self
    }
}

/// Why [`run`] stopped with an error.
#[derive(Debug)]
#[non_exhaustive]
pub enum WindowError {
    /// The window system's event loop or window failed.
    Window(String),
    /// The GPU could not be set up or drawn to.
    Gpu(String),
    /// The fonts could not be loaded.
    Text(String),
    /// The application's [`App::build`] failed.
    App(Box<dyn Error>),
}

impl fmt::Display for WindowError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Window(message) => write!(f, "window system: {message}"),
            Self::Gpu(message) => write!(f, "GPU: {message}"),
            Self::Text(message) => write!(f, "fonts: {message}"),
            Self::App(error) => write!(f, "application: {error}"),
        }
    }
}

impl Error for WindowError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::App(error) => Some(error.as_ref()),
            _ => None,
        }
    }
}

/// Opens a window for `app` and runs until it is closed.
///
/// # Errors
/// [`WindowError`] if the window, the GPU or the fonts cannot be set up, a frame cannot be
/// drawn, or [`App::build`] fails.
pub fn run(options: WindowOptions, app: impl App) -> Result<(), WindowError> {
    let event_loop = EventLoop::<UserEvent>::with_user_event()
        .build()
        .map_err(|e| WindowError::Window(e.to_string()))?;
    let mut shell = Shell {
        app,
        options,
        proxy: event_loop.create_proxy(),
        session: None,
        error: None,
        close: false,
    };
    event_loop
        .run_app(&mut shell)
        .map_err(|e| WindowError::Window(e.to_string()))?;
    shell.error.map_or(Ok(()), Err)
}

struct Shell<A: App> {
    app: A,
    options: WindowOptions,
    proxy: EventLoopProxy<UserEvent>,
    session: Option<Session>,
    error: Option<WindowError>,
    /// The app asked to close the window while building its interface.
    close: bool,
}

/// The window, its GPU surface and its interface.
struct Session {
    window: Arc<Window>,
    /// Hands the interface to screen readers, and their requests back.
    access: accesskit_winit::Adapter,
    gpu: Gpu,
    ui: Ui,
    cache: GlyphCache,
    instances: InstanceBuffer,
    start: Instant,
    pointer: Point,
    modifiers: ModifiersState,
    cursor: Cursor,
    continuous: bool,
}

impl<A: App> Shell<A> {
    fn fail(&mut self, event_loop: &ActiveEventLoop, error: WindowError) {
        self.error = Some(error);
        event_loop.exit();
    }

    fn open(&mut self, event_loop: &ActiveEventLoop) -> Result<Session, WindowError> {
        let size = self.options.size;
        // Hidden until AccessKit's adapter is attached, which must happen before it is shown.
        let attributes = Window::default_attributes()
            .with_title(self.options.title.clone())
            .with_inner_size(LogicalSize::new(size.width, size.height))
            .with_visible(false);
        let window = event_loop
            .create_window(attributes)
            .map_err(|e| WindowError::Window(e.to_string()))?;
        let access = accesskit_winit::Adapter::with_event_loop_proxy(
            event_loop,
            &window,
            self.proxy.clone(),
        );
        let window = Arc::new(window);
        window.set_ime_allowed(true);
        let mut cache = GlyphCache::new(AtlasConfig::DEFAULT);
        let gpu = Gpu::new(event_loop, &window, self.options.instances, &mut cache)?;
        let text = TextSystem::new().map_err(|e| WindowError::Text(e.to_string()))?;
        let mut ui = Ui::new(text);
        ui.set_theme(self.options.theme.clone());
        ui.set_scale(window.scale_factor() as f32);
        let inner = window.inner_size();
        ui.resize(Size::new(inner.width as f32, inner.height as f32));
        let mut host = Host {
            window: &window,
            proxy: &self.proxy,
            close: &mut self.close,
        };
        self.app
            .build(&mut ui, &mut host)
            .map_err(WindowError::App)?;
        window.set_visible(true);
        Ok(Session {
            window,
            access,
            gpu,
            ui,
            cache,
            instances: InstanceBuffer::with_capacity(self.options.instances),
            start: Instant::now(),
            pointer: Point::ORIGIN,
            modifiers: ModifiersState::empty(),
            cursor: Cursor::Default,
            continuous: false,
        })
    }
}

impl<A: App> ApplicationHandler<UserEvent> for Shell<A> {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        if self.session.is_some() {
            return;
        }
        match self.open(event_loop) {
            Ok(session) => {
                session.window.request_redraw();
                self.session = Some(session);
                if self.close {
                    event_loop.exit();
                }
            }
            Err(error) => self.fail(event_loop, error),
        }
    }

    fn window_event(&mut self, event_loop: &ActiveEventLoop, _: WindowId, event: WindowEvent) {
        let Some(session) = self.session.as_mut() else {
            return;
        };
        let window = Arc::clone(&session.window);
        session.access.process_event(&window, &event);
        let redraw = matches!(event, WindowEvent::RedrawRequested);
        let mut close = false;
        let mut failed = None;
        {
            let mut host = Host {
                window: &window,
                proxy: &self.proxy,
                close: &mut close,
            };
            match event {
                WindowEvent::CloseRequested => host.close(),
                WindowEvent::RedrawRequested => {
                    session.continuous = self.app.frame(&mut session.ui, &mut host);
                    failed = session.paint().err();
                }
                other => session.input(&other),
            }
            if failed.is_none() {
                while let Some(action) = session.ui.next_action() {
                    self.app.action(&mut session.ui, &mut host, action);
                }
                session.reflect();
                // After a frame and what the app did about it: the tree screen readers see
                // matches the screen.
                if redraw {
                    session.publish_access();
                }
            }
        }
        if let Some(error) = failed {
            self.fail(event_loop, error);
        } else if close {
            event_loop.exit();
        }
    }

    fn user_event(&mut self, event_loop: &ActiveEventLoop, event: UserEvent) {
        let Some(session) = self.session.as_mut() else {
            return;
        };
        let access = match event {
            UserEvent::Wake => {
                session.window.request_redraw();
                return;
            }
            UserEvent::Access(access) => access.window_event,
        };
        match access {
            accesskit_winit::WindowEvent::InitialTreeRequested => session.publish_access(),
            accesskit_winit::WindowEvent::ActionRequested(request) => {
                access::perform(&mut session.ui, &request);
                let window = Arc::clone(&session.window);
                let mut close = false;
                let mut host = Host {
                    window: &window,
                    proxy: &self.proxy,
                    close: &mut close,
                };
                while let Some(action) = session.ui.next_action() {
                    self.app.action(&mut session.ui, &mut host, action);
                }
                session.reflect();
                session.publish_access();
                if close {
                    event_loop.exit();
                }
            }
            accesskit_winit::WindowEvent::AccessibilityDeactivated => {}
        }
    }

    fn about_to_wait(&mut self, _: &ActiveEventLoop) {
        if let Some(session) = &self.session {
            if session.continuous {
                session.window.request_redraw();
            }
        }
    }
}

impl Session {
    /// Hands a window event to the interface.
    fn input(&mut self, event: &WindowEvent) {
        let modifiers = translate::modifiers(self.modifiers);
        let at = |kind: PointerKind, position: Point, start: Instant| {
            PointerEvent::new(kind, position)
                .with_modifiers(modifiers)
                .at(start.elapsed())
        };
        match event {
            WindowEvent::Resized(size) => {
                self.gpu.resize(size.width, size.height);
                self.ui
                    .resize(Size::new(size.width as f32, size.height as f32));
            }
            WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
                self.ui.set_scale(*scale_factor as f32);
            }
            WindowEvent::ModifiersChanged(state) => self.modifiers = state.state(),
            WindowEvent::CursorMoved { position, .. } => {
                self.pointer = Point::new(position.x as f32, position.y as f32);
                self.ui
                    .pointer(at(PointerKind::Move, self.pointer, self.start));
            }
            WindowEvent::CursorLeft { .. } => self.ui.pointer_left(self.pointer),
            // Alt+Tab mid-drag: no release will come, and modifiers may be released elsewhere.
            WindowEvent::Focused(false) => {
                self.ui.cancel_pointer();
                self.modifiers = ModifiersState::empty();
            }
            WindowEvent::MouseInput { state, button, .. } => {
                let button = translate::button(*button);
                let kind = match state {
                    ElementState::Pressed => PointerKind::Down(button),
                    ElementState::Released => PointerKind::Up(button),
                };
                self.ui.pointer(at(kind, self.pointer, self.start));
            }
            WindowEvent::MouseWheel { delta, .. } => {
                let kind = PointerKind::Scroll(translate::scroll(*delta));
                self.ui.pointer(at(kind, self.pointer, self.start));
            }
            WindowEvent::KeyboardInput {
                event,
                is_synthetic: false,
                ..
            } => {
                let Some(key) = translate::key(&event.logical_key) else {
                    return;
                };
                let mut key = if event.state == ElementState::Pressed {
                    KeyEvent::down(key)
                } else {
                    KeyEvent::up(key)
                }
                .with_modifiers(modifiers);
                if let Some(text) = translate::text(event.text.as_deref()) {
                    key = key.with_text(text);
                }
                if event.repeat {
                    key = key.repeated();
                }
                self.ui.key(&key);
            }
            WindowEvent::Ime(ime) => {
                self.ui.ime(&translate::ime(ime));
            }
            _ => {}
        }
    }

    /// Paints the interface and presents it.
    fn paint(&mut self) -> Result<(), WindowError> {
        {
            let mut canvas = Canvas::begin(&mut self.cache, &mut self.instances);
            self.ui.paint(&mut canvas);
        }
        let background = self.ui.theme().background;
        self.gpu.draw(
            &self.window,
            &mut self.cache,
            self.instances.as_slice(),
            background,
        )?;
        Ok(())
    }

    /// Sends screen readers the interface as it is now; nothing is built unless one is
    /// listening.
    fn publish_access(&mut self) {
        let Self {
            access, ui, window, ..
        } = self;
        access.update_if_active(|| access::tree_update(&ui.access_tree(), &window.title()));
    }

    /// Shows the interface's state in the window: cursor, input method area, repaint.
    fn reflect(&mut self) {
        let cursor = self.ui.cursor();
        if cursor != self.cursor {
            self.cursor = cursor;
            self.window.set_cursor(translate::cursor(cursor));
        }
        if let Some(area) = self.ui.ime_area() {
            self.window.set_ime_cursor_area(
                PhysicalPosition::new(area.left(), area.top()),
                PhysicalSize::new(area.width().max(1.0), area.height().max(1.0)),
            );
        }
        if self.ui.needs_paint() {
            self.window.request_redraw();
        }
    }
}