slate-framework 1.0.1

GPU-accelerated Rust UI framework — umbrella crate
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//! Application container and frame loop.
//!
//! `App` owns all framework resources and provides `run()` to enter
//! the platform event loop with a View.

use std::rc::Rc;
use std::sync::Arc;

use slate_platform::{
    DefaultPlatform, DefaultWindow, Event, Platform, Window, WindowId, WindowOptions, wake_run_loop,
};

use crate::app_state::{AppSignal, AppState, ErasedViewFactory};
use crate::erased_view::ErasedView;
use crate::event::{
    EventCtx, ImeCommitEvent, ImeCommitHandler, ImeLifecycleEvent, ImeLifecycleHandler,
    ImePreeditEvent, ImePreeditHandler, KeyEvent, KeyHandler, TextInputEvent, TextInputHandler,
};
use crate::executor::{BackgroundExecutor, Executor, RedrawRequester};
use crate::types::ElementId;
use crate::view::View;

// Synthetic device-lost trigger on the real visible-window AppState path.
// Setting `SLATE_FORCE_DEVICE_LOST_MS=N` schedules a
// `force_renderer_device_lost()` N milliseconds after the first `Resumed`,
// so the OS WM_PAINT pump can be observed driving the state machine through
// `DetectedLost -> CooldownGate -> Retrying -> Recovered`.
#[cfg(all(target_os = "windows", feature = "test-hooks"))]
static FORCE_DEVICE_LOST_PENDING: std::sync::atomic::AtomicBool =
    std::sync::atomic::AtomicBool::new(false);

#[cfg(all(target_os = "windows", feature = "test-hooks"))]
static FORCE_DEVICE_LOST_ARMED: std::sync::atomic::AtomicBool =
    std::sync::atomic::AtomicBool::new(false);

/// Application context passed to the view factory.
///
/// Provides access to the reactive runtime and background executor for
/// constructing signals and spawning background tasks.
///
/// Focus operations are now per-window; use `set_focus(window, id)` etc.
///
/// Note: `ForegroundExecutor` is intentionally not exposed here because it's `!Send`
/// and bound to the UI thread. UI-thread tasks should use the foreground executor
/// available in element context methods.
#[derive(Clone)]
pub struct AppContext {
    pub(crate) runtime: Arc<slate_reactive::Runtime>,
    pub(crate) background_executor: BackgroundExecutor,
    /// Weak reference back to AppState so focus API can route per-window.
    /// `None` only in unit-test construction via `new_for_test`.
    pub(crate) state: Option<std::rc::Weak<AppState>>,
}

impl AppContext {
    /// Get the reactive runtime for creating signals.
    pub fn runtime(&self) -> Arc<slate_reactive::Runtime> {
        self.runtime.clone()
    }

    /// Open a new window from inside an event handler.
    ///
    /// The platform window (HWND on Win32, NSWindow on AppKit) is allocated
    /// **synchronously** so we can return a stable [`WindowId`] right away.
    /// The framework-side bring-up — `WindowState` insertion, delegate
    /// install, renderer + view init — is **deferred** to the dispatch drain
    /// step at the end of the current event so it never aliases the
    /// `windows.borrow()` already taken by the in-flight handler.
    ///
    /// Returns `None` only when called from a context where the framework
    /// has dropped its platform handle (post-`run`, or a unit-test
    /// `AppContext::new_for_test`).
    pub fn create_window<V: View + 'static>(
        &self,
        opts: WindowOptions,
        mut view_fn: impl FnMut(&AppContext) -> V + 'static,
    ) -> Option<WindowId> {
        let weak = self.state.as_ref()?;
        let state = weak.upgrade()?;
        let platform_rc = state.platform.borrow().clone()?;
        let window = platform_rc.create_window(opts);
        let cx_clone = self.clone();
        let factory: ErasedViewFactory = Box::new(move |_cx: &AppContext| -> Box<dyn ErasedView> {
            Box::new(view_fn(&cx_clone))
        });
        Some(state.push_pending_window_create(window, factory))
    }

    /// Get the background executor for spawning async tasks.
    pub fn background_executor(&self) -> BackgroundExecutor {
        self.background_executor.clone()
    }

    /// Request the run loop to exit at the next event tick.
    ///
    /// Sets the shared `pending_quit` flag that `App::run` checks before each
    /// dispatch. Idempotent. Required by examples that drive their own quit
    /// affordance (`AppKit` keeps the app alive after last-window-close, so
    /// macOS examples must wire Cmd+Q explicitly). No-op outside of a live
    /// `App::run` (e.g. `new_for_test` constructions).
    pub fn request_quit(&self) {
        let Some(weak) = &self.state else { return };
        let Some(state) = weak.upgrade() else { return };
        state.pending_quit.set(true);
    }

    /// Set focus to `id` in the given window immediately (outside-handler
    /// entry point).
    ///
    /// Returns `true` when `id` is currently registered with the focus
    /// registry. Inside an event handler, prefer
    /// [`EventCtx::request_focus`](crate::event::EventCtx::request_focus) so
    /// the change is deferred until after the chain unwinds.
    pub fn set_focus(&self, window: slate_platform::WindowId, id: ElementId) -> bool {
        let Some(weak) = &self.state else {
            return false;
        };
        let Some(state) = weak.upgrade() else {
            return false;
        };
        let guard = state.windows.borrow();
        if let Some(win) = guard.get(&window) {
            win.focus_registry.borrow_mut().set_focus(id)
        } else {
            false
        }
    }

    /// Clear focus immediately in the given window. Inside a handler, prefer
    /// [`EventCtx::blur`](crate::event::EventCtx::blur).
    pub fn clear_focus(&self, window: slate_platform::WindowId) {
        let Some(weak) = &self.state else { return };
        let Some(state) = weak.upgrade() else { return };
        let guard = state.windows.borrow();
        if let Some(win) = guard.get(&window) {
            win.focus_registry.borrow_mut().clear_focus();
        }
    }

    /// Read the currently-focused element id in the given window.
    pub fn focused_element(&self, window: slate_platform::WindowId) -> Option<ElementId> {
        let weak = self.state.as_ref()?;
        let state = weak.upgrade()?;
        let guard = state.windows.borrow();
        guard.get(&window)?.focus_registry.borrow().focused()
    }

    /// Construct an `AppContext` directly. Test-only.
    #[cfg(any(test, feature = "test-hooks"))]
    #[doc(hidden)]
    pub fn new_for_test(
        runtime: Arc<slate_reactive::Runtime>,
        background_executor: BackgroundExecutor,
    ) -> Self {
        Self {
            runtime,
            background_executor,
            state: None,
        }
    }
}

/// A window registered before `App::run` but not yet wired into `AppState`.
/// Built by `App::create_window`, materialised inside `App::run` alongside
/// the first window.
struct PendingPreRunWindow {
    window: Arc<DefaultWindow>,
    view_factory: ErasedViewFactory,
}

/// Application container.
///
/// Owns all framework resources: platform, window, executor,
/// layout tree, hit-test list, accessibility tree, and text system.
///
/// # Examples
///
/// ```ignore
/// use slate_framework::{App, WindowOptions, Text, View, RenderCx, AnyElement, IntoAny};
///
/// struct HelloView;
/// impl View for HelloView {
///     fn render(&mut self, _cx: &mut RenderCx) -> AnyElement {
///         Text::new("Hello, Slate!").into_any()
///     }
/// }
///
/// App::new(WindowOptions::default()).run(|_cx| HelloView);
/// ```
pub struct App {
    platform: Rc<DefaultPlatform>,
    window: Arc<DefaultWindow>,
    pending_windows: Vec<PendingPreRunWindow>,
    on_key_down: Vec<KeyHandler>,
    on_key_up: Vec<KeyHandler>,
    on_text_input: Vec<TextInputHandler>,
    on_ime_preedit: Vec<ImePreeditHandler>,
    on_ime_commit: Vec<ImeCommitHandler>,
    on_ime_enabled: Vec<ImeLifecycleHandler>,
    on_ime_disabled: Vec<ImeLifecycleHandler>,
}

impl App {
    /// Create a new application with the given window options.
    ///
    /// The renderer and text system are initialized lazily on `Event::Resumed`.
    pub fn new(options: WindowOptions) -> Self {
        let platform = Rc::new(DefaultPlatform::new());
        let window = platform.create_window(options);

        Self {
            platform,
            window,
            pending_windows: Vec::new(),
            on_key_down: Vec::new(),
            on_key_up: Vec::new(),
            on_text_input: Vec::new(),
            on_ime_preedit: Vec::new(),
            on_ime_commit: Vec::new(),
            on_ime_enabled: Vec::new(),
            on_ime_disabled: Vec::new(),
        }
    }

    /// Open an additional window before entering the run loop.
    ///
    /// The platform window is allocated immediately so the returned
    /// [`WindowId`] is usable straight away (e.g. capturing it in a handler
    /// closure registered via [`App::on_key_down`]). `WindowState` insertion,
    /// delegate install, and renderer init are deferred until inside
    /// [`App::run`] so the first window and any pre-run extras come up in a
    /// single rendezvous on `Event::Resumed`.
    ///
    /// The `view_fn` is erased to `Box<dyn ErasedView>` at registration so
    /// `AppState` does not need to be generic over `V`.
    pub fn create_window<V: View + 'static>(
        &mut self,
        opts: WindowOptions,
        mut view_fn: impl FnMut(&AppContext) -> V + 'static,
    ) -> WindowId {
        let window = self.platform.create_window(opts);
        let id = window.id();
        let factory: ErasedViewFactory =
            Box::new(move |cx: &AppContext| -> Box<dyn ErasedView> { Box::new(view_fn(cx)) });
        self.pending_windows.push(PendingPreRunWindow {
            window,
            view_factory: factory,
        });
        id
    }

    /// Register an App-level handler for `KeyDown` events. Multiple handlers
    /// may be registered; they fire in registration order. Handlers must not
    /// re-enter `App::run` or other dispatch paths — the framework holds a
    /// `RefCell` borrow on the handler vec for the duration of the call.
    ///
    /// Call before [`App::run`]; the type system enforces this (`run` consumes
    /// `self`).
    pub fn on_key_down(mut self, handler: impl FnMut(&KeyEvent, &mut EventCtx) + 'static) -> Self {
        self.on_key_down.push(Box::new(handler));
        self
    }

    /// Register an App-level handler for `KeyUp` events. See [`App::on_key_down`].
    pub fn on_key_up(mut self, handler: impl FnMut(&KeyEvent, &mut EventCtx) + 'static) -> Self {
        self.on_key_up.push(Box::new(handler));
        self
    }

    /// Register an App-level handler for composed text input. Fires once per
    /// keystroke that produces visible text (or per surrogate pair on Windows).
    /// See [`App::on_key_down`].
    pub fn on_text_input(
        mut self,
        handler: impl FnMut(&TextInputEvent, &mut EventCtx) + 'static,
    ) -> Self {
        self.on_text_input.push(Box::new(handler));
        self
    }

    /// Register an App-level handler for IME preedit (composition) updates.
    /// Fires on each `setMarkedText:` (macOS) / `WM_IME_COMPOSITION GCS_COMPSTR`
    /// (Windows). See [`App::on_key_down`].
    pub fn on_ime_preedit(
        mut self,
        handler: impl FnMut(&ImePreeditEvent, &mut EventCtx) + 'static,
    ) -> Self {
        self.on_ime_preedit.push(Box::new(handler));
        self
    }

    /// Register an App-level handler for IME commits. Empty `text` is the
    /// "clear preedit, no insert" signal; handlers MUST skip `Signal::set`
    /// in that case. See [`App::on_key_down`].
    pub fn on_ime_commit(
        mut self,
        handler: impl FnMut(&ImeCommitEvent, &mut EventCtx) + 'static,
    ) -> Self {
        self.on_ime_commit.push(Box::new(handler));
        self
    }

    /// Register an App-level handler for the start of an IME composition
    /// session. Paired with [`App::on_ime_disabled`]. See [`App::on_key_down`].
    pub fn on_ime_enabled(
        mut self,
        handler: impl FnMut(&ImeLifecycleEvent, &mut EventCtx) + 'static,
    ) -> Self {
        self.on_ime_enabled.push(Box::new(handler));
        self
    }

    /// Register an App-level handler for the end of an IME composition
    /// session. Always preceded by [`App::on_ime_enabled`]. See
    /// [`App::on_key_down`].
    pub fn on_ime_disabled(
        mut self,
        handler: impl FnMut(&ImeLifecycleEvent, &mut EventCtx) + 'static,
    ) -> Self {
        self.on_ime_disabled.push(Box::new(handler));
        self
    }

    /// Run the application with the given view factory.
    ///
    /// `view_fn` receives an [`AppContext`] with access to the reactive runtime
    /// and background executor for constructing signals and spawning tasks.
    ///
    /// This method enters the platform event loop and does not return until
    /// the application exits.
    pub fn run<V: View>(self, mut view_fn: impl FnMut(&AppContext) -> V + 'static) {
        let App {
            platform,
            window,
            pending_windows,
            on_key_down,
            on_key_up,
            on_text_input,
            on_ime_preedit,
            on_ime_commit,
            on_ime_enabled,
            on_ime_disabled,
        } = self;

        // Create executor and reactive runtime.
        let redraw_requester = RedrawRequester::new(wake_run_loop);
        let executor = Executor::new(redraw_requester.clone());
        let runtime = slate_reactive::Runtime::new();

        // Capture handles needed by AppContext before AppState consumes them.
        let background_executor = executor.background.clone();

        // Create shared application state. windows map starts empty.
        let state = Rc::new(AppState::new(
            executor,
            redraw_requester.clone(),
            runtime.clone(),
        ));

        // Install platform handle so `AppContext::create_window` can allocate
        // additional platform windows mid-dispatch.
        state.install_platform(platform.clone());

        // Wire the FIRST window (its WindowState, redraw requester, and
        // render+IME delegates) using the same helper the dynamic
        // `AppContext::create_window` path uses.
        let first_window_id = window.id();
        state.install_window(window.clone());

        let cx = AppContext {
            runtime,
            background_executor,
            state: Some(Rc::downgrade(&state)),
        };

        // Move keyboard/IME handlers into AppState.
        state.install_key_handlers(on_key_down, on_key_up, on_text_input);
        state.install_ime_handlers(
            on_ime_preedit,
            on_ime_commit,
            on_ime_enabled,
            on_ime_disabled,
        );

        // Wrap the FIRST window's typed view factory into a type-erased one.
        let cx_clone = cx.clone();
        let first_erased: ErasedViewFactory =
            Box::new(move |_cx: &AppContext| -> Box<dyn ErasedView> {
                Box::new(view_fn(&cx_clone))
            });

        // Wire each pre-`run` window (registered via `App::create_window`) and
        // build the (id, erased_factory) list consumed on Event::Resumed.
        let mut resumed_factories: Vec<(WindowId, ErasedViewFactory)> =
            Vec::with_capacity(1 + pending_windows.len());
        resumed_factories.push((first_window_id, first_erased));
        for pre in pending_windows {
            let id = pre.window.id();
            state.install_window(pre.window);
            resumed_factories.push((id, pre.view_factory));
        }

        let platform_ref = &*platform;
        let state_ref = state.clone();

        platform.run(move |event| {
            // Check pending_quit flag from sync delegate path.
            if state_ref.pending_quit.get() {
                platform_ref.quit();
                return;
            }

            // Synthetic trigger: env-var-scheduled force_device_lost fires from
            // a background thread that wakes the run loop.
            #[cfg(all(target_os = "windows", feature = "test-hooks"))]
            if FORCE_DEVICE_LOST_PENDING.swap(false, std::sync::atomic::Ordering::AcqRel) {
                log::warn!(
                    target: "slate::test_hooks",
                    "SLATE_FORCE_DEVICE_LOST_MS elapsed - firing force_renderer_device_lost"
                );
                let fired = state_ref.force_renderer_device_lost(first_window_id);
                log::warn!(
                    target: "slate::test_hooks",
                    "force_renderer_device_lost returned fired={fired}"
                );
            }

            let signal = match event {
                Event::Resumed => {
                    // Initialise renderer + view for every window registered
                    // before `run` (first + any `App::create_window` extras).
                    // If ANY window fails to come up, request quit.
                    let mut had_failure = false;
                    for (id, factory) in resumed_factories.iter_mut() {
                        if state_ref
                            .init_surfaces(*id, factory, &cx, platform_ref)
                            .is_err()
                        {
                            had_failure = true;
                        }
                    }
                    if had_failure {
                        AppSignal::RequestQuit
                    } else {
                        #[cfg(all(target_os = "windows", feature = "test-hooks"))]
                        arm_force_device_lost_trigger();
                        AppSignal::RequestRedraw {
                            window: first_window_id,
                        }
                    }
                }
                Event::WindowResized {
                    window,
                    physical_size,
                    ..
                } => {
                    state_ref.handle_window_resized(window, physical_size);
                    AppSignal::None
                }
                Event::WindowRedrawRequested { window, .. } => state_ref.dispatch_redraw(window),
                Event::WindowCloseRequested { .. } => {
                    // Let the platform proceed with destroy; the last-window
                    // quit decision is owned by `handle_window_destroyed`,
                    // which sees the post-destroy windows map and applies the
                    // platform-default policy (Win32 quits, AppKit stays alive).
                    AppSignal::None
                }
                Event::WindowDestroyed { window, .. } => state_ref.handle_window_destroyed(window),
                Event::Wake => state_ref.handle_wake(),
                Event::MouseDown {
                    window,
                    position,
                    button,
                    modifiers,
                    ..
                } => state_ref.dispatch_mouse_down(window, position, button, modifiers),
                Event::MouseUp {
                    window,
                    position,
                    button,
                    modifiers,
                    ..
                } => state_ref.dispatch_mouse_up(window, position, button, modifiers),
                Event::MouseMoved {
                    window,
                    position,
                    modifiers,
                    ..
                } => state_ref.dispatch_mouse_moved(window, position, modifiers),
                Event::MouseScrolled {
                    window,
                    position,
                    delta_x,
                    delta_y,
                    precise,
                    modifiers,
                    ..
                } => state_ref.dispatch_mouse_scrolled(
                    window, position, delta_x, delta_y, precise, modifiers,
                ),
                Event::MouseExited { window, .. } => state_ref.dispatch_mouse_exited(window),
                Event::CaptureLost { window, .. } => state_ref.dispatch_capture_lost(window),
                Event::DeviceLost { window, fatal, .. } => {
                    state_ref.dispatch_device_lost(window, fatal)
                }
                Event::DeviceRestored { window, .. } => state_ref.dispatch_device_restored(window),
                Event::KeyDown {
                    window,
                    code,
                    key,
                    modifiers,
                    is_repeat,
                    ..
                } => state_ref.dispatch_key_down(window, code, key, modifiers, is_repeat),
                Event::KeyUp {
                    window,
                    code,
                    key,
                    modifiers,
                    ..
                } => state_ref.dispatch_key_up(window, code, key, modifiers),
                Event::TextInput { window, text, .. } => {
                    state_ref.dispatch_text_input(window, text)
                }
                Event::ImeEnabled { window, .. } => state_ref.dispatch_ime_enabled(window),
                Event::ImePreedit {
                    window,
                    text,
                    cursor_byte_offset,
                    selection,
                    ..
                } => state_ref.dispatch_ime_preedit(window, text, cursor_byte_offset, selection),
                Event::ImeCommit { window, text, .. } => {
                    state_ref.dispatch_ime_commit(window, text)
                }
                Event::ImeDisabled { window, .. } => state_ref.dispatch_ime_disabled(window),
                Event::Exiting => {
                    log::info!("exiting");
                    AppSignal::None
                }
                // Slate's Event is #[non_exhaustive]; this final arm preserves
                // forward compatibility.
                _ => AppSignal::None,
            };

            match signal {
                AppSignal::RequestQuit => platform_ref.quit(),
                AppSignal::RequestRedraw { window } => state_ref.request_redraw_for(window),
                AppSignal::None => {}
            }

            // Drain any windows requested mid-dispatch via
            // `AppContext::create_window`. Borrow on `windows` taken by the
            // handler has been released by this point.
            state_ref.drain_pending_window_creates(&cx, platform_ref);
        });
    }
}

/// Read `SLATE_FORCE_DEVICE_LOST_MS` once and arm a background thread that
/// sleeps the requested duration, sets `FORCE_DEVICE_LOST_PENDING`, and
/// wakes the run loop.
///
/// Subsequent calls are no-ops (one-shot arming).
#[cfg(all(target_os = "windows", feature = "test-hooks"))]
fn arm_force_device_lost_trigger() {
    use std::sync::atomic::Ordering;

    if FORCE_DEVICE_LOST_ARMED.swap(true, Ordering::AcqRel) {
        return;
    }

    let ms: u64 = match std::env::var("SLATE_FORCE_DEVICE_LOST_MS") {
        Ok(s) => match s.trim().parse() {
            Ok(n) => n,
            Err(_) => {
                log::warn!(
                    target: "slate::test_hooks",
                    "SLATE_FORCE_DEVICE_LOST_MS='{s}' is not a valid u64 - skipping"
                );
                return;
            }
        },
        Err(_) => return,
    };

    log::warn!(
        target: "slate::test_hooks",
        "SLATE_FORCE_DEVICE_LOST_MS armed: will fire force_renderer_device_lost in {ms}ms"
    );

    std::thread::spawn(move || {
        std::thread::sleep(std::time::Duration::from_millis(ms));
        FORCE_DEVICE_LOST_PENDING.store(true, Ordering::Release);
        wake_run_loop();
    });
}