nsh_core/core/services.rs
1//! The core's side of the platform services Rust components ask for
2//! ([`crate::element`]'s free functions) beyond commands for the
3//! platform: display frames and the layout direction.
4//!
5//! The surface's direction follows the user's language: its root's
6//! (`Prop::LayoutDirection` on the root, which JS sets from the language),
7//! else the platform's. In a Rust app the core sets the root's direction
8//! from the platform's language from the start, and again whenever the
9//! language changes. A view's `direction` style sets its own subtree's.
10
11use super::Core;
12use crate::{Op, Prop, ROOT_ID, TextEngine, element::AnswerSlot, time::Instant};
13
14/// What the core keeps for the services.
15#[derive(Default)]
16pub(super) struct Services {
17 /// The tasks waiting for the next display frame.
18 frames: Vec<AnswerSlot<Instant>>,
19 /// The core sets the root's direction from the language (a Rust app).
20 owns_direction: bool,
21}
22
23impl<M: TextEngine> Core<M> {
24 /// Whether a component waits for a display frame: ask the platform for
25 /// one, then call [`Self::run_frame`] when it begins.
26 pub fn wants_frame(&self) -> bool {
27 // A blur held for the frame (`blur_held`) lets go at it.
28 (self.blur_held && self.focus_moved)
29 // A slot only the core holds is a task that is gone.
30 || self.components
31 .services
32 .frames
33 .iter()
34 .any(|slot| std::rc::Rc::strong_count(slot) > 1)
35 }
36
37 /// A display frame began at `at`: the tasks waiting for it go on (at
38 /// the next [`Self::run_component_tasks`]).
39 pub fn run_frame(&mut self, at: Instant) {
40 self.blur_held = false;
41 for slot in std::mem::take(&mut self.components.services.frames) {
42 slot.borrow_mut().deliver(at);
43 }
44 }
45
46 pub(super) fn wait_frame(&mut self, slot: AnswerSlot<Instant>) {
47 self.components.services.frames.push(slot);
48 }
49
50 /// The direction components read, the platform's language being
51 /// `platform`; in a Rust app the root follows it.
52 pub(super) fn surface_rtl(&mut self, platform: bool) -> bool {
53 if self.components.services.owns_direction {
54 self.set_root_direction(platform);
55 platform
56 } else {
57 self.nodes[self.root as usize].direction.unwrap_or(platform)
58 }
59 }
60
61 /// A Rust app: the core sets the surface's direction (no JS does).
62 pub(super) fn own_direction(&mut self) {
63 self.components.services.owns_direction = true;
64 let rtl = self.components.environment.rtl;
65 self.set_root_direction(rtl);
66 }
67
68 fn set_root_direction(&mut self, rtl: bool) {
69 let _ = self.apply_op(Op::SetProp {
70 id: ROOT_ID,
71 prop: Prop::LayoutDirection(rtl),
72 });
73 }
74
75 /// The root's direction changed: what read it
76 /// runs again.
77 pub(super) fn root_direction_changed(&mut self, rtl: bool) {
78 if self.components.environment.rtl != rtl {
79 let environment = crate::element::Environment {
80 rtl,
81 ..self.components.environment
82 };
83 self.apply_environment(environment);
84 }
85 }
86}