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