ori_core/style/
styles.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use std::{
    any::{Any, TypeId},
    collections::HashMap,
    fmt::{self, Debug},
    hash::BuildHasherDefault,
};

use seahash::SeaHasher;

use crate::{context::RebuildCx, rebuild::Rebuild};

/// A trait implemented by styles.
///
/// This will allow the style to be used in the arguments of [`StyleBuilder`] functions.
///
/// # Example
/// ```rust
/// # use ori_core::{
/// #    style::{Style, Styles, StyleBuilder, Theme},
/// #    views::ButtonStyle,
/// #    canvas::Color,
/// #    layout::Padding,
/// # };
/// struct MyStyle {
///     my_color: Color,
///     my_padding: Padding,
/// }
///
/// impl Style for MyStyle {
///     fn default_style() -> StyleBuilder<Self> {
///         StyleBuilder::new(|theme: &Theme, button: &ButtonStyle| {
///             MyStyle {
///                 my_color: theme.accent,
///                 my_padding: button.padding,
///             }
///         })
///     }
/// }
///
/// let mut styles = Styles::new()
///     .with(|| Theme {
///         accent: Color::rgb(1.0, 0.0, 0.0),
///         ..Theme::default()
///     })
///     .with(|theme: &Theme| ButtonStyle {
///         padding: Padding::all(10.0),
///         ..Default::default()
///     });
///
/// let my_style = styles.style::<MyStyle>();
///
/// assert_eq!(my_style.my_color, Color::rgb(1.0, 0.0, 0.0));
/// assert_eq!(my_style.my_padding, Padding::all(10.0));
/// ```
pub trait Style: Sized {
    /// The default style of the object.
    fn default_style() -> StyleBuilder<Self>;
}

/// A trait for stylable objects.
pub trait Stylable {
    /// The style type.
    type Style: Style;

    /// Style the object.
    ///
    /// This is done by creating a new style based on the given base style.
    fn style(&self, base: &Self::Style) -> Self::Style;

    /// Rebuild the style of the object.
    fn rebuild_style(&self, cx: &mut RebuildCx, style: &mut Self::Style)
    where
        Self::Style: Rebuild + 'static,
    {
        let new = self.style(cx.style());
        new.rebuild(cx, style);
        *style = new;
    }
}

/// A collection of [`Style`]s.
#[derive(Default)]
pub struct Styles {
    builders: HashMap<TypeId, StyleBuilder<Box<dyn Any>>, StylesHasher>,
    cache: HashMap<TypeId, Box<dyn Any>, StylesHasher>,
}

impl Debug for Styles {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Styles")
            .field("builders", &self.builders)
            .finish()
    }
}

type StylesHasher = BuildHasherDefault<SeaHasher>;

impl Styles {
    /// Create a new collection of styles.
    pub fn new() -> Styles {
        Styles::default()
    }

    /// Check if a style is contained in the collection.
    pub fn contains<T: Any>(&self) -> bool {
        let type_id = TypeId::of::<T>();
        self.cache.contains_key(&type_id) || self.builders.contains_key(&type_id)
    }

    /// Insert a style builder into the collection.
    ///
    /// See [`StyleBuilder::new`] for more information.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use ori_core::{style::{Styles, Theme}, views::ButtonStyle};
    /// Styles::new().insert(|theme: &Theme| ButtonStyle {
    ///     color: theme.accent,
    ///     ..Default::default()
    /// });
    /// ```
    pub fn insert<T, B>(&mut self, builder: B) -> bool
    where
        B: IntoStyleBuilder<T> + 'static,
        B::Output: Style + Any,
    {
        let type_id = TypeId::of::<B::Output>();
        let dyn_builder = StyleBuilder::new(builder).into_dyn();

        if self.builders.contains_key(&type_id) {
            self.cache.clear();
        }

        self.builders.insert(type_id, dyn_builder).is_some()
    }

    /// Insert a style builder into the collection.
    ///
    /// See [`StyleBuilder::new`] for more information.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use ori_core::{style::{Styles, Theme}, views::ButtonStyle};
    /// let styles = Styles::new().with(|theme: &Theme| ButtonStyle {
    ///    color: theme.accent,
    ///    ..Default::default()
    /// });
    /// ```
    pub fn with<T, B>(mut self, builder: B) -> Self
    where
        B: IntoStyleBuilder<T> + 'static,
        B::Output: Style + Any,
    {
        self.insert(builder);
        self
    }

    /// Extend the collection with another collection of styles.
    pub fn extend(&mut self, other: Styles) {
        for (type_id, builder) in other.builders {
            self.builders.insert(type_id, builder);
        }

        self.cache.clear();
    }

    /// Get a style from the collection.
    pub fn style<T: Style + Any>(&mut self) -> &T {
        if self.contains::<T>() {
            return self.get::<T>().unwrap();
        }

        let builder = T::default_style().into_dyn();
        self.builders.insert(TypeId::of::<T>(), builder);
        self.get::<T>().unwrap()
    }

    /// Get a style from the collection.
    pub fn get<T: Any>(&mut self) -> Option<&T> {
        let type_id = TypeId::of::<T>();

        if self.cache.contains_key(&type_id) {
            return self.cache.get(&type_id)?.downcast_ref();
        }

        if let Some(builder) = self.builders.remove(&type_id) {
            let value = (builder.builder)(self);
            self.cache.insert(type_id, value);
            self.builders.insert(type_id, builder);
            return self.cache.get(&type_id)?.downcast_ref();
        }

        None
    }

    fn get_cached<T: Any>(&self) -> Option<&T> {
        let type_id = TypeId::of::<T>();
        self.cache.get(&type_id)?.downcast_ref()
    }
}

/// A style builder.
///
/// See the [`Style`] trait for an example.
pub struct StyleBuilder<T> {
    type_name: &'static str,
    builder: Box<dyn Fn(&mut Styles) -> T>,
    dependencies: Vec<TypeId>,
}

impl<T: Any> StyleBuilder<T> {
    /// Create a new style builder.
    ///
    /// This takes a function that takes any number of references to [`Style`]s and returns a style.
    /// Each [`Style`] referenced will be created if not already present in the [`Styles`] collection.
    /// If a style is dependent on this style, it will be rebuilt when this style is rebuilt.
    ///
    /// __Note:__ Cyclic dependencies are not allowed and will result in a panic.
    pub fn new<U, B>(builder: B) -> Self
    where
        B: IntoStyleBuilder<U, Output = T> + 'static,
    {
        Self {
            type_name: std::any::type_name::<T>(),
            dependencies: B::dependencies(&builder),
            builder: Box::new(move |styles| builder.build(styles)),
        }
    }

    fn into_dyn(self) -> StyleBuilder<Box<dyn Any>> {
        StyleBuilder {
            type_name: self.type_name,
            builder: Box::new(move |styles| Box::new((self.builder)(styles))),
            dependencies: self.dependencies,
        }
    }
}

impl<T> Debug for StyleBuilder<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("StyleBuilder")
            .field("type_name", &self.type_name)
            .finish()
    }
}

/// A trait for converting a function into a style builder.
///
/// See [`StyleBuilder::new`] for more information.
pub trait IntoStyleBuilder<T> {
    /// The type of style built by the builder.
    type Output;

    /// Build the style.
    fn build(&self, styles: &mut Styles) -> Self::Output;

    /// Get the dependencies of the style builder.
    fn dependencies(&self) -> Vec<TypeId> {
        Vec::new()
    }
}

impl<T> IntoStyleBuilder<StyleBuilder<T>> for StyleBuilder<T> {
    type Output = T;

    fn build(&self, styles: &mut Styles) -> Self::Output {
        (self.builder)(styles)
    }

    fn dependencies(&self) -> Vec<TypeId> {
        self.dependencies.clone()
    }
}

macro_rules! impl_style_builder {
    (@) => {};
    (@ $last:ident $(, $ty:ident)*) => {
        impl_style_builder!($($ty),*);
    };
    ($($ty:ident),*) => {
        impl_style_builder!(@ $($ty),*);

        impl<$($ty: Style + Any,)* FN, R> IntoStyleBuilder<fn($($ty,)*) -> R> for FN
        where
            FN: Fn($(&$ty,)*) -> R,
        {
            type Output = R;

            #[allow(non_snake_case, unused_variables)]
            fn build(&self, styles: &mut Styles) -> Self::Output {
                $(styles.style::<$ty>();)*
                $(let $ty = styles.get_cached::<$ty>().unwrap();)*
                (self)($($ty.clone(),)*)
            }

            fn dependencies(&self) -> Vec<TypeId> {
                vec![$(TypeId::of::<$ty>()),*]
            }
        }
    };
}

impl_style_builder!(A, B, C, D, E, F, G, H, I, J, K, L);