Skip to main content

strata/runtime/
types.rs

1//! Runtime data structures for active focus, scrollbars, hit targets, and editing.
2
3/// Hit-test target result representing a clickable text input element in the blueprint tree.
4#[derive(Clone)]
5pub struct InputHitTarget {
6    pub input_index: usize,
7    pub input_id: Option<String>,
8    pub entity_id: Option<strata_primitives::EntityId>,
9    pub current_value: String,
10    pub font_size: f32,
11    pub line_height: f32,
12    pub is_mono: bool,
13    pub font_family: strata_primitives::AtomId,
14    pub content_box_x: f32,
15    pub content_box_y: f32,
16    pub content_box_width: f32,
17    pub on_input: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
18    pub on_change: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
19    pub on_submit: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
20    pub on_focus: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
21    pub on_blur: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
22}
23
24/// Active text input state tracked by the runtime while an input has keyboard focus.
25#[derive(Clone)]
26pub struct ActiveInput {
27    pub input_index: usize,
28    pub input_id: Option<String>,
29    pub entity_id: Option<strata_primitives::EntityId>,
30    pub state: strata_dom::text_input::TextInputState,
31    pub on_input: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
32    pub on_change: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
33    pub on_submit: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
34    pub on_blur: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
35}
36
37impl std::fmt::Debug for ActiveInput {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        f.debug_struct("ActiveInput")
40            .field("text", &self.state.text.get())
41            .field("cursor", &self.state.cursor_index.get())
42            .finish()
43    }
44}
45
46/// Active scrollbar state for an overflow container.
47#[derive(Debug, Clone)]
48pub struct ActiveScrollbar {
49    pub bounds: strata_layout::Rect,
50    pub clip_bounds: strata_layout::Rect,
51    pub border_radius: strata_style::types::BorderRadius,
52    pub is_top_layer: bool,
53    pub track_bounds: strata_layout::Rect,
54    pub thumb_bounds: strata_layout::Rect,
55    pub scroll_y: f32,
56    pub max_scroll_y: f32,
57    pub content_h: f32,
58    pub is_id: bool,
59    pub id: String,
60    pub path: Vec<usize>,
61    pub available_travel: f32,
62}
63
64/// Active drag operation on a scrollbar thumb.
65#[derive(Debug, Clone)]
66pub struct ScrollbarDrag {
67    pub is_id: bool,
68    pub id: String,
69    pub path: Vec<usize>,
70    pub start_pointer_y: f32,
71    pub start_scroll_y: f32,
72    pub available_travel: f32,
73    pub max_scroll_y: f32,
74}
75
76/// Active kinetic smooth scroll animation tracking state.
77#[derive(Debug, Clone)]
78pub struct SmoothScrollTracker {
79    pub container_path: Vec<usize>,
80    pub container_id: String,
81    pub is_id: bool,
82    pub max_scroll_y: f32,
83    pub available_travel: f32,
84    pub bounds_y: f32,
85    /// Current interpolated scroll offset in pixels.
86    pub current_y: f32,
87    /// Target scroll position in pixels.
88    pub target_y: f32,
89    /// Initial position at the start of the current impulse.
90    pub start_y: f32,
91    /// Initial velocity in px/s at the start of the current impulse.
92    pub initial_velocity: f32,
93    /// Timestamp when the current impulse started.
94    pub start_time: std::time::Instant,
95    /// Timestamp of the last incoming wheel event (for flick velocity calculation).
96    pub last_event_time: std::time::Instant,
97    /// Critically damped angular frequency (rad/s).
98    pub omega: f32,
99}
100
101/// Internal descriptor of a scrollable target container under the cursor.
102#[derive(Debug, Clone)]
103pub struct ScrollTarget {
104    pub path: Vec<usize>,
105    pub id: Option<String>,
106    pub bounds: strata_layout::Rect,
107    pub max_scroll_x: f32,
108    pub max_scroll_y: f32,
109}
110/// Computes a deterministic 64-bit ID from a hierarchical tree path.
111pub fn path_to_node_id(path: &[usize]) -> u64 {
112    let mut hash = 0xcbf29ce484222325u64;
113    for &idx in path {
114        hash ^= idx as u64;
115        hash = hash.wrapping_mul(0x100000001b3u64);
116    }
117    hash
118}
119
120/// Editing commands that can be applied to the focused text input from any UI surface.
121#[derive(Debug, Clone, Copy, PartialEq, Eq)]
122pub enum EditCommand {
123    Undo,
124    Redo,
125    Cut,
126    Copy,
127    Paste,
128    SelectAll,
129}
130
131/// Action result from a pointer-down event dispatch.
132#[derive(Debug, Clone, Copy, PartialEq, Eq)]
133pub enum PointerDownAction {
134    /// Not handled by any element or active input.
135    Ignored,
136    /// Caret or selection inside the already-active focused input moved. Requires redraw only.
137    CaretMoved,
138    /// Focus transitioned to a new or different input. Requires layout/rebuild update.
139    FocusChanged,
140    /// DOM PointerDown or MouseDown event listener was invoked.
141    DomListenerInvoked,
142}
143
144impl PointerDownAction {
145    #[inline]
146    pub fn is_handled(self) -> bool {
147        self != PointerDownAction::Ignored
148    }
149
150    #[inline]
151    pub fn needs_rebuild(self) -> bool {
152        matches!(
153            self,
154            PointerDownAction::FocusChanged | PointerDownAction::DomListenerInvoked
155        )
156    }
157
158    #[inline]
159    pub fn needs_redraw(self) -> bool {
160        self != PointerDownAction::Ignored
161    }
162}
163
164/// Action result from a pointer-move event dispatch.
165#[derive(Debug, Clone, Copy, PartialEq, Eq)]
166pub enum PointerMoveAction {
167    /// Not handled.
168    Ignored,
169    /// Mouse drag text selection range updated in active input. Requires redraw only.
170    SelectionDragged,
171    /// DOM PointerMove or MouseMove event listener was invoked.
172    DomListenerInvoked,
173}
174
175impl PointerMoveAction {
176    #[inline]
177    pub fn is_handled(self) -> bool {
178        self != PointerMoveAction::Ignored
179    }
180
181    #[inline]
182    pub fn needs_rebuild(self) -> bool {
183        self == PointerMoveAction::DomListenerInvoked
184    }
185
186    #[inline]
187    pub fn needs_redraw(self) -> bool {
188        self != PointerMoveAction::Ignored
189    }
190}
191
192/// Action result from a pointer-up event dispatch.
193#[derive(Debug, Clone, Copy, PartialEq, Eq)]
194pub enum PointerUpAction {
195    /// Not handled.
196    Ignored,
197    /// Mouse drag text selection finished. Requires redraw only.
198    SelectionEnded,
199    /// DOM PointerUp or MouseUp event listener was invoked.
200    DomListenerInvoked,
201}
202
203impl PointerUpAction {
204    #[inline]
205    pub fn is_handled(self) -> bool {
206        self != PointerUpAction::Ignored
207    }
208
209    #[inline]
210    pub fn needs_rebuild(self) -> bool {
211        self == PointerUpAction::DomListenerInvoked
212    }
213
214    #[inline]
215    pub fn needs_redraw(self) -> bool {
216        self != PointerUpAction::Ignored
217    }
218}
219
220/// Outcome of dispatching a platform keyboard or text event.
221#[derive(Debug, Clone, Copy, PartialEq, Eq)]
222pub enum KeyDispatchResult {
223    /// Key was not handled by any focused input or container scroll.
224    Ignored,
225    /// Caret moved, selection range updated, or page scrolled without mutating component state. Requires redraw only.
226    RedrawOnly,
227    /// Text buffer was modified or form submitted/blurred, requiring layout/signal recalculation.
228    NeedsRebuild,
229}
230
231impl KeyDispatchResult {
232    #[inline]
233    pub fn is_handled(self) -> bool {
234        self != KeyDispatchResult::Ignored
235    }
236
237    #[inline]
238    pub fn needs_rebuild(self) -> bool {
239        self == KeyDispatchResult::NeedsRebuild
240    }
241
242    #[inline]
243    pub fn needs_redraw(self) -> bool {
244        self != KeyDispatchResult::Ignored
245    }
246}
247
248/// Binding between a dynamic text closure and its target location in the layout hierarchy.
249#[derive(Clone)]
250pub struct DynamicTextBinding {
251    pub path: Vec<usize>,
252    pub closure: std::rc::Rc<dyn Fn() -> String>,
253    pub last_value: String,
254}
255
256impl std::fmt::Debug for DynamicTextBinding {
257    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
258        f.debug_struct("DynamicTextBinding")
259            .field("path", &self.path)
260            .field("last_value", &self.last_value)
261            .finish()
262    }
263}
264
265/// Interaction context describing active pointer, keyboard focus, and theme interaction state.
266#[derive(Debug, Clone, Default, PartialEq, Eq)]
267pub struct InteractionContext<'a> {
268    pub hovered_path: Option<&'a [usize]>,
269    pub focused_path: Option<&'a [usize]>,
270    pub is_mouse_down: bool,
271    pub is_dark_mode: bool,
272}
273
274/// Element selector metadata used when matching `<style>` rules across ancestors and siblings.
275#[derive(Debug, Clone)]
276pub(crate) struct CascadeElementMeta {
277    pub tag: &'static str,
278    pub classes: Vec<String>,
279    pub id: Option<String>,
280    pub attributes: Vec<(String, String)>,
281    pub pseudo_state: strata_style::selector::PseudoState,
282}
283
284/// Active DOM drag-and-drop interaction state (`ondragstart`, `ondrag`, `ondragenter`, `ondragover`, `ondragleave`, `ondrop`, `ondragend`).
285#[derive(Debug, Clone)]
286pub struct ActiveDomDrag {
287    pub source_path: Vec<usize>,
288    pub start_pos: (f32, f32),
289    pub started: bool,
290    pub last_over_path: Option<Vec<usize>>,
291    pub data_transfer: std::rc::Rc<std::cell::RefCell<std::collections::HashMap<String, String>>>,
292}