Skip to main content

repose_core/
runtime.rs

1use std::any::Any;
2use std::cell::{Cell, RefCell};
3use std::collections::HashMap;
4use std::rc::Rc;
5
6use crate::scope::Scope;
7use crate::{Rect, Scene, View, semantics::Role};
8
9thread_local! {
10    pub static COMPOSER: RefCell<Composer> = RefCell::new(Composer::default());
11    static ROOT_SCOPE: RefCell<Option<Scope>> = const { RefCell::new(None) };
12
13    /// A programmatic focus request, set by `FocusRequester::request_focus()`.
14    /// Stores the view ID that should receive focus on the next frame.
15    static FOCUS_REQUEST: Cell<Option<u64>> = const { Cell::new(None) };
16}
17
18pub fn take_focus_request() -> Option<u64> {
19    FOCUS_REQUEST.with(|r| r.replace(None))
20}
21
22/// A handle that can programmatically request focus for a widget.
23///
24/// Similar to Compose's `FocusRequester`. Create one via `remember(FocusRequester::new)`,
25/// attach it via `.focus_requester(...)` on a modifier, and call `request_focus()` to
26/// move keyboard focus to the associated widget on the next frame.
27#[derive(Clone)]
28pub struct FocusRequester {
29    /// Target view ID, set during layout/paint by the modifier system.
30    pub target: Rc<RefCell<Option<u64>>>,
31}
32
33impl FocusRequester {
34    pub fn new() -> Self {
35        Self {
36            target: Rc::new(RefCell::new(None)),
37        }
38    }
39
40    /// Request focus for the associated widget on the next frame.
41    pub fn request_focus(&self) {
42        if let Some(id) = *self.target.borrow() {
43            FOCUS_REQUEST.with(|r| r.set(Some(id)));
44        }
45    }
46}
47
48impl Default for FocusRequester {
49    fn default() -> Self {
50        Self::new()
51    }
52}
53
54/// Direction for focus movement.
55#[derive(Clone, Copy, Debug, PartialEq, Eq)]
56pub enum FocusDirection {
57    Next,
58    Previous,
59    Left,
60    Right,
61    Up,
62    Down,
63}
64
65/// A manager for programmatic focus navigation.
66///
67/// Wraps a `&Scheduler` and provides methods to move focus.
68/// Can also be used standalone with a focus chain and focused element.
69#[derive(Clone)]
70pub struct FocusManager {
71    /// The ordered list of focusable element IDs.
72    pub chain: Vec<u64>,
73    /// The currently focused element (if any).
74    pub focused: Option<u64>,
75}
76
77impl FocusManager {
78    pub fn new(chain: Vec<u64>, focused: Option<u64>) -> Self {
79        Self { chain, focused }
80    }
81
82    /// Move focus in the given direction.
83    /// Returns the new focused element ID, or `None` if no movement is possible.
84    pub fn move_focus(&mut self, dir: FocusDirection) -> Option<u64> {
85        match dir {
86            FocusDirection::Next | FocusDirection::Previous => {
87                self.move_tab(dir == FocusDirection::Previous)
88            }
89            _ => None, // use move_focus_spatial when hit regions are available
90        }
91    }
92
93    /// Spatial focus navigation: find the closest focusable element in a given
94    /// direction using bounding rect geometry.
95    pub fn move_focus_spatial(
96        &mut self,
97        dir: FocusDirection,
98        hit_regions: &[HitRegion],
99    ) -> Option<u64> {
100        let next = spatial_focus_next(&self.chain, hit_regions, self.focused, dir)?;
101        self.focused = Some(next);
102        Some(next)
103    }
104
105    /// Tab forward or backward in the focus chain.
106    pub fn move_tab(&mut self, reverse: bool) -> Option<u64> {
107        if self.chain.is_empty() {
108            return None;
109        }
110        let next = if let Some(cur) = self.focused {
111            if let Some(idx) = self.chain.iter().position(|&id| id == cur) {
112                if reverse {
113                    if idx == 0 {
114                        self.chain[self.chain.len() - 1]
115                    } else {
116                        self.chain[idx - 1]
117                    }
118                } else {
119                    self.chain[(idx + 1) % self.chain.len()]
120                }
121            } else {
122                self.chain[0]
123            }
124        } else {
125            self.chain[0]
126        };
127        self.focused = Some(next);
128        Some(next)
129    }
130
131    /// Set target ID on a FocusRequester (called during layout).
132    pub fn set_requester_target(requester: &FocusRequester, id: u64) {
133        *requester.target.borrow_mut() = Some(id);
134    }
135}
136
137/// Find the next focusable element in a given spatial direction.
138///
139/// Uses the bounding rects from `hit_regions` to determine which element is
140/// "next" in the given direction from the currently focused element.
141pub fn spatial_focus_next(
142    chain: &[u64],
143    hit_regions: &[HitRegion],
144    current: Option<u64>,
145    dir: FocusDirection,
146) -> Option<u64> {
147    if chain.is_empty() {
148        return None;
149    }
150
151    let current_rect =
152        current.and_then(|id| hit_regions.iter().find(|h| h.id == id).map(|h| h.rect));
153
154    // For Next/Previous, use tab-order navigation
155    match dir {
156        FocusDirection::Next | FocusDirection::Previous => {
157            let mut fm = FocusManager {
158                chain: chain.to_vec(),
159                focused: current,
160            };
161            return fm.move_tab(dir == FocusDirection::Previous);
162        }
163        _ => {}
164    }
165
166    let (cx, cy) = match current_rect {
167        Some(r) => (r.x + r.w / 2.0, r.y + r.h / 2.0),
168        None => return chain.first().copied(),
169    };
170
171    let mut best: Option<(u64, f32)> = None;
172
173    for &id in chain {
174        if Some(id) == current {
175            continue;
176        }
177        let Some(hr) = hit_regions.iter().find(|h| h.id == id) else {
178            continue;
179        };
180        let r = hr.rect;
181        let other_cx = r.x + r.w / 2.0;
182        let other_cy = r.y + r.h / 2.0;
183        let dx = other_cx - cx;
184        let dy = other_cy - cy;
185
186        let in_direction = match dir {
187            FocusDirection::Left => dx < 0.0 && dy.abs() <= r.h.max(1.0),
188            FocusDirection::Right => dx > 0.0 && dy.abs() <= r.h.max(1.0),
189            FocusDirection::Up => dy < 0.0 && dx.abs() <= r.w.max(1.0),
190            FocusDirection::Down => dy > 0.0 && dx.abs() <= r.w.max(1.0),
191            _ => false,
192        };
193
194        if !in_direction {
195            continue;
196        }
197
198        let dist = dx * dx + dy * dy;
199        let weight = dist / (r.w * r.h + 1.0).max(1.0);
200
201        match best {
202            Some((_, best_weight)) if weight >= best_weight => {}
203            _ => best = Some((id, weight)),
204        }
205    }
206
207    best.map(|(id, _)| id)
208}
209
210#[derive(Default)]
211pub struct Composer {
212    pub slots: Vec<Box<dyn Any>>,
213    pub cursor: usize,
214    pub keyed_slots: HashMap<String, Box<dyn Any>>,
215}
216
217pub struct ComposeGuard {
218    scope: Scope,
219}
220
221impl ComposeGuard {
222    pub fn begin() -> Self {
223        COMPOSER.with(|c| c.borrow_mut().cursor = 0);
224
225        let scope = ROOT_SCOPE.with(|rs| {
226            if let Some(existing) = rs.borrow().clone() {
227                existing
228            } else {
229                let s = Scope::new();
230                *rs.borrow_mut() = Some(s.clone());
231                s
232            }
233        });
234
235        ComposeGuard { scope }
236    }
237
238    pub fn scope(&self) -> &Scope {
239        &self.scope
240    }
241}
242
243impl Drop for ComposeGuard {
244    fn drop(&mut self) {
245        // ROOT_SCOPE.with(|rs| { Do not clear every frame
246        //     *rs.borrow_mut() = None;
247        // });
248    }
249}
250
251/// Slot-based remember (sequential composition only)
252pub fn remember<T: 'static>(init: impl FnOnce() -> T) -> Rc<T> {
253    COMPOSER.with(|c| {
254        let mut c = c.borrow_mut();
255        let cursor = c.cursor;
256        c.cursor += 1;
257
258        if cursor >= c.slots.len() {
259            let rc: Rc<T> = Rc::new(init());
260            c.slots.push(Box::new(rc.clone()));
261            return rc;
262        }
263
264        if let Some(rc) = c.slots[cursor].downcast_ref::<Rc<T>>() {
265            rc.clone()
266        } else {
267            log::warn!(
268                "remember: slot {} type changed {}. \
269                 Use remember_with_key(key, || ...) for conditional branches.",
270                cursor,
271                std::any::type_name::<T>(),
272            );
273            // debug_assert!(
274            //     false,
275            //     "remember slot {} type changed. This is likely a composition order bug.",
276            //     cursor,
277            // );
278            let rc: Rc<T> = Rc::new(init());
279            c.slots[cursor] = Box::new(rc.clone());
280            rc
281        }
282    })
283}
284
285/// Key-based remember
286pub fn remember_with_key<T: 'static>(key: impl Into<String>, init: impl FnOnce() -> T) -> Rc<T> {
287    COMPOSER.with(|c| {
288        let mut c = c.borrow_mut();
289        let key = key.into();
290
291        if let Some(existing) = c.keyed_slots.get(&key) {
292            if let Some(rc) = existing.downcast_ref::<Rc<T>>() {
293                return rc.clone();
294            } else {
295                log::warn!(
296                    "remember_with_key: key '{}' reused with a different type; replacing.",
297                    key
298                );
299            }
300        }
301
302        if cfg!(debug_assertions) && c.keyed_slots.len() > 10_000 {
303            log::warn!(
304                "remember_with_key: more than 10k keys stored; \
305                are you generating unbounded dynamic keys (e.g., using timestamps)?"
306            );
307        }
308
309        let rc: Rc<T> = Rc::new(init());
310        c.keyed_slots.insert(key, Box::new(rc.clone()));
311        rc
312    })
313}
314
315pub fn remember_state<T: 'static>(init: impl FnOnce() -> T) -> Rc<RefCell<T>> {
316    remember(|| RefCell::new(init()))
317}
318
319pub fn remember_state_with_key<T: 'static>(
320    key: impl Into<String>,
321    init: impl FnOnce() -> T,
322) -> Rc<RefCell<T>> {
323    remember_with_key(key, || RefCell::new(init()))
324}
325
326/// Frame - output of composition for a tick: scene + input/semantics.
327pub struct Frame {
328    pub scene: Scene,
329    pub hit_regions: Vec<HitRegion>,
330    pub semantics_nodes: Vec<SemNode>,
331    pub focus_chain: Vec<u64>,
332}
333
334#[derive(Clone, Default)]
335pub struct HitRegion {
336    pub id: u64,
337    pub rect: Rect,
338    pub on_click: Option<Rc<dyn Fn()>>,
339    pub on_scroll: Option<Rc<dyn Fn(crate::Vec2) -> crate::Vec2>>,
340    pub focusable: bool,
341    pub on_pointer_down: Option<Rc<dyn Fn(crate::input::PointerEvent)>>,
342    pub on_pointer_move: Option<Rc<dyn Fn(crate::input::PointerEvent)>>,
343    pub on_pointer_up: Option<Rc<dyn Fn(crate::input::PointerEvent)>>,
344    pub on_pointer_enter: Option<Rc<dyn Fn(crate::input::PointerEvent)>>,
345    pub on_pointer_leave: Option<Rc<dyn Fn(crate::input::PointerEvent)>>,
346    pub z_index: f32,
347    pub on_text_change: Option<Rc<dyn Fn(String)>>,
348    pub on_text_submit: Option<Rc<dyn Fn(String)>>,
349    /// If this hit region belongs to a TextField, this persistent key is used
350    /// for looking up platform-managed TextFieldState. Falls back to `id` if None.
351    pub tf_state_key: Option<u64>,
352
353    /// True if this hit region corresponds to a multiline text input (TextArea).
354    pub tf_multiline: bool,
355
356    // internal
357    pub on_drag_start: Option<Rc<dyn Fn(crate::dnd::DragStart) -> Option<crate::dnd::DragPayload>>>,
358    pub on_drag_end: Option<Rc<dyn Fn(crate::dnd::DragEnd)>>,
359    pub on_drag_enter: Option<Rc<dyn Fn(crate::dnd::DragOver)>>,
360    pub on_drag_over: Option<Rc<dyn Fn(crate::dnd::DragOver)>>,
361    pub on_drag_leave: Option<Rc<dyn Fn(crate::dnd::DragOver)>>,
362    pub on_drop: Option<Rc<dyn Fn(crate::dnd::DropEvent) -> bool>>,
363
364    pub on_action: Option<Rc<dyn Fn(crate::shortcuts::Action) -> bool>>,
365
366    /// Cursor hint for desktop/web.
367    pub cursor: Option<crate::CursorIcon>,
368}
369
370impl HitRegion {
371    /// Seed a HitRegion with all the modifier's event handlers + dnd + cursor.
372    /// Call‑sites should only override the fields that differ (on_click, focusable, etc.)
373    /// via struct‑update syntax: `HitRegion { focusable: true, ..from_modifier(..) }`.
374    pub fn from_modifier(id: u64, rect: Rect, m: &crate::modifier::Modifier) -> Self {
375        Self {
376            id,
377            rect,
378            z_index: m.z_index,
379            on_pointer_down: m.on_pointer_down.clone(),
380            on_pointer_move: m.on_pointer_move.clone(),
381            on_pointer_up: m.on_pointer_up.clone(),
382            on_pointer_enter: m.on_pointer_enter.clone(),
383            on_pointer_leave: m.on_pointer_leave.clone(),
384            on_action: m.on_action.clone(),
385            cursor: m.cursor,
386            on_drag_start: m.on_drag_start.clone(),
387            on_drag_end: m.on_drag_end.clone(),
388            on_drag_enter: m.on_drag_enter.clone(),
389            on_drag_over: m.on_drag_over.clone(),
390            on_drag_leave: m.on_drag_leave.clone(),
391            on_drop: m.on_drop.clone(),
392            ..Default::default()
393        }
394    }
395}
396
397/// Flattened semantics node produced by `layout_and_paint`.
398///
399/// This is the source of truth for accessibility backends: it contains the
400/// resolved screen rect, role, label, and focus/enabled state.
401///
402/// The platform runner should convert this into OS‑specific accessibility trees (when implemented)
403/// (AT‑SPI on Linux, TalkBack on Android, etc.).
404#[derive(Clone)]
405pub struct SemNode {
406    /// Stable id, shared with the associated `HitRegion` / `ViewId`.
407    pub id: u64,
408
409    /// `None` means direct child of the window root.
410    pub parent: Option<u64>,
411
412    pub role: Role,
413    pub label: Option<String>,
414    pub rect: Rect,
415    pub focused: bool,
416    pub enabled: bool,
417}
418
419pub struct Scheduler {
420    next_id: u64,
421    pub focused: Option<u64>,
422    pub size: (u32, u32),
423}
424
425impl Default for Scheduler {
426    fn default() -> Self {
427        Self::new()
428    }
429}
430
431impl Scheduler {
432    pub fn new() -> Self {
433        Self {
434            next_id: 1,
435            focused: None,
436            size: (1280, 800),
437        }
438    }
439
440    pub fn id(&mut self) -> u64 {
441        let id = self.next_id;
442        self.next_id += 1;
443        id
444    }
445
446    pub fn id_count(&self) -> u64 {
447        self.next_id - 1
448    }
449
450    pub fn repose<F>(
451        &mut self,
452        mut build_root: F,
453        layout_paint: impl Fn(&View, (u32, u32)) -> (Scene, Vec<HitRegion>, Vec<SemNode>),
454    ) -> Frame
455    where
456        F: FnMut(&mut Scheduler) -> View,
457    {
458        let guard = ComposeGuard::begin();
459        let root = guard.scope.run(|| build_root(self));
460        let (scene, hits, sem) = layout_paint(&root, self.size);
461
462        let focus_chain: Vec<u64> = hits.iter().filter(|h| h.focusable).map(|h| h.id).collect();
463
464        Frame {
465            scene,
466            hit_regions: hits,
467            semantics_nodes: sem,
468            focus_chain,
469        }
470    }
471}
472
473/// Avoids cross-test pollution
474#[cfg(test)]
475pub fn clear_composer() {
476    COMPOSER.with(|c| {
477        let mut c = c.borrow_mut();
478        c.slots.clear();
479        c.keyed_slots.clear();
480        c.cursor = 0;
481    });
482    ROOT_SCOPE.with(|rs| {
483        *rs.borrow_mut() = None;
484    });
485}