Skip to main content

teksilo_core/
environment.rs

1// SPDX-License-Identifier: MPL-2.0
2// SPDX-FileCopyrightText: 2026 FernTech
3
4use crate::styles::Theme;
5
6/// Layout direction for RTL/LTR support.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
8pub enum LayoutDirection {
9    #[default]
10    LeftToRight,
11    RightToLeft,
12}
13
14/// Environment data that flows down the widget tree.
15/// Subtrees can override parts of the environment.
16#[derive(Debug, Clone)]
17pub struct Environment {
18    pub theme: Theme,
19    pub layout_direction: LayoutDirection,
20    pub scale_factor: f32,
21    pub prefers_high_contrast: bool,
22    pub prefers_reduced_motion: bool,
23    pub prefers_large_text: bool,
24}
25
26impl Environment {
27    pub fn new(theme: Theme) -> Self {
28        Self {
29            theme,
30            layout_direction: LayoutDirection::default(),
31            scale_factor: 1.0,
32            prefers_high_contrast: false,
33            prefers_reduced_motion: false,
34            prefers_large_text: false,
35        }
36    }
37
38    /// Apply a theme override function, returning a new Environment with the
39    /// modified theme while preserving all other fields.
40    pub fn with_theme_override(&self, f: &dyn Fn(&mut Theme)) -> Self {
41        let mut env = self.clone();
42        f(&mut env.theme);
43        env
44    }
45}
46
47impl Default for Environment {
48    fn default() -> Self {
49        Self::new(crate::presets::intui::light())
50    }
51}
52
53/// A stored theme override closure for a widget node.
54/// When present on a node, its subtree sees a modified theme.
55pub(crate) struct ThemeOverride {
56    pub func: Box<dyn Fn(&mut Theme)>,
57}
58
59impl std::fmt::Debug for ThemeOverride {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        f.write_str("ThemeOverride(..)")
62    }
63}