fyrox-ui 1.0.1

Extendable UI library
Documentation
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#![warn(missing_docs)]

//! Style allows to change the visual appearance of widgets in a centralized manner. It can be considered
//! as a storage for properties that define visual appearance. See [`Style`] docs for more info
//! and usage examples.

pub mod resource;

use crate::{
    brush::Brush,
    button::Button,
    check_box::CheckBox,
    core::{
        color::Color, reflect::prelude::*, type_traits::prelude::*, visitor::prelude::*,
        ImmutableString, Uuid,
    },
    dropdown_list::DropdownList,
    style::resource::{StyleResource, StyleResourceError, StyleResourceExt},
    toggle::ToggleButton,
    Thickness,
};
use fyrox_resource::untyped::ResourceKind;
use fyrox_resource::{
    io::ResourceIo,
    manager::{BuiltInResource, ResourceManager},
};
use fyrox_texture::TextureResource;
use std::{
    any::{Any, TypeId},
    sync::LazyLock,
};
use std::{
    ops::{Deref, DerefMut},
    path::Path,
    sync::Arc,
};
use strum_macros::{AsRefStr, EnumString, VariantNames};

/// A set of potential values for styled properties.
#[derive(Visit, Reflect, Debug, Clone, TypeUuidProvider, AsRefStr, EnumString, VariantNames)]
#[type_uuid(id = "85b8c1e4-03a2-4a28-acb4-1850d1a29227")]
pub enum StyleProperty {
    /// A numeric property.
    Number(f32),
    /// A thickness property that defines the width of four sides of a rectangles.
    Thickness(Thickness),
    /// A color property.
    Color(Color),
    /// A brush property, that defines how to render an arbitrary surface (solid, with gradient, etc.).
    Brush(Brush),
    /// A texture property. Could be used together with [`crate::image::Image`] widget or [`crate::nine_patch::NinePatch`]
    /// widget.
    Texture(TextureResource),
}

impl Default for StyleProperty {
    fn default() -> Self {
        Self::Number(0.0)
    }
}

impl StyleProperty {
    /// Returns type id of the actual value stored in the property. The set of potential types is
    /// finite (see [`StyleProperty`] declaration).
    pub fn value_type_id(&self) -> TypeId {
        match self {
            StyleProperty::Number(v) => v.type_id(),
            StyleProperty::Thickness(v) => v.type_id(),
            StyleProperty::Color(v) => v.type_id(),
            StyleProperty::Brush(v) => v.type_id(),
            StyleProperty::Texture(v) => v.type_id(),
        }
    }
}

/// A trait that provides a method that translates [`StyleProperty`] into a specific primitive value.
pub trait IntoPrimitive<T> {
    /// Tries to convert self into a primitive value `T`.
    fn into_primitive(self) -> Option<T>;
}

macro_rules! impl_casts {
    ($ty:ty => $var:ident) => {
        impl From<$ty> for StyleProperty {
            fn from(value: $ty) -> Self {
                Self::$var(value)
            }
        }

        impl IntoPrimitive<$ty> for StyleProperty {
            fn into_primitive(self) -> Option<$ty> {
                if let StyleProperty::$var(value) = self {
                    Some(value)
                } else {
                    None
                }
            }
        }
    };
}

impl_casts!(f32 => Number);
impl_casts!(Thickness => Thickness);
impl_casts!(Color => Color);
impl_casts!(Brush => Brush);
impl_casts!(TextureResource => Texture);

/// Default style of the library.
pub static DEFAULT_STYLE: LazyLock<BuiltInResource<Style>> = LazyLock::new(|| {
    BuiltInResource::new_no_source(
        "Default Style",
        StyleResource::new_ok(
            uuid!("1e0716e8-e728-491c-a65b-ca11b15048be"),
            ResourceKind::External,
            Style::dark_style(),
        ),
    )
});

/// Light style of the library.
pub static LIGHT_STYLE: LazyLock<BuiltInResource<Style>> = LazyLock::new(|| {
    BuiltInResource::new_no_source(
        "Light Style",
        StyleResource::new_ok(
            uuid!("05141b18-2a27-4fe3-ae6e-7af11c2e7471"),
            ResourceKind::External,
            Style::light_style(),
        ),
    )
});

/// A property, that can bind its value to a style. Why can't we just fetch the actual value from
/// the style and why do we need to store the value as well? The answer is flexibility. In this
/// approach, style becomes not necessary and the value can be hardcoded. Also, the values of such
/// properties can be updated individually.
#[derive(Clone, Debug, Reflect, Default)]
#[reflect(bounds = "T: Reflect + Clone")]
pub struct StyledProperty<T> {
    /// Property value.
    pub property: T,
    /// Name of the property in a style table.
    #[reflect(read_only, display_name = "Property Name")]
    pub name: ImmutableString,
}

impl<T> From<T> for StyledProperty<T> {
    fn from(property: T) -> Self {
        Self {
            property,
            name: Default::default(),
        }
    }
}

impl<T: PartialEq> PartialEq for StyledProperty<T> {
    fn eq(&self, other: &Self) -> bool {
        self.property == other.property
    }
}

impl<T> StyledProperty<T> {
    /// Creates a new styled property with the given value and the name.
    pub fn new(property: T, name: impl Into<ImmutableString>) -> Self {
        Self {
            property,
            name: name.into(),
        }
    }

    /// Tries to sync the property value with its respective value in the given style. This method
    /// will fail if the given style does not contain the property.
    pub fn update(&mut self, style: &StyleResource)
    where
        StyleProperty: IntoPrimitive<T>,
    {
        if let Some(property) = style.get(self.name.clone()) {
            self.property = property;
        }
    }
}

impl<T> Deref for StyledProperty<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.property
    }
}

impl<T> DerefMut for StyledProperty<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.property
    }
}

impl<T: Visit> Visit for StyledProperty<T> {
    fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
        self.property.visit(name, visitor)
    }
}

/// Named style property container.
#[derive(Visit, Reflect, Clone, Default, Debug, TypeUuidProvider)]
#[type_uuid(id = "6238f37c-c067-4dd1-be67-6a8bb8853a59")]
pub struct StylePropertyContainer {
    /// Name of the property.
    pub name: ImmutableString,
    /// The actual value of the property.
    pub value: StyleProperty,
}

/// Style is a simple container for a named properties. Styles can be based on some other styles, thus
/// allowing cascaded styling. Such cascading allows to define some base style with common properties
/// and then create any number of derived styles. For example, you can define a style for Button widget
/// with corner radius, font size, border thickness and then create two derived styles for light and
/// dark themes that will define colors and brushes. Light or dark theme does not affect all of those
/// base properties, but has different colors.
///
/// Styles can contain only specific types of properties (see [`StyleProperty`] enumeration), any
/// more complex properties can be built using these primitives.
///
/// There are three major ways of widget styling:
///
/// 1) During widget building stage - this way involves [`crate::BuildContext`]'s style field. This
/// field defines a style for all widgets that will be built with the context.
/// 2) Message-based style changes - this way is based on [`crate::widget::WidgetMessage::Style`] message
/// that can be sent to a particular widget (or hierarchy) to force them to update styled properties.
/// 3) Global style changes - this way is based on [`crate::UserInterface::set_style`] method, that
/// sends the specified style to all widgets, forcing them to update styled properties.
///
/// The most used methods are 1 and 3. The following examples should clarify how to use these
/// approaches.
///
/// ## Examples
///
/// The following example shows how to use a style during widget building stage:
///
/// ```rust
/// # use fyrox_ui::{
/// #     button::{Button, ButtonBuilder},
/// #     style::{resource::StyleResource, Style},
/// #     widget::WidgetBuilder,
/// #     Thickness, UserInterface,
/// # };
/// #
/// fn build_with_style(ui: &mut UserInterface) {
///     // The context will use UI style by default. You can override it using `ui.set_style(..)`.
///     let ctx = &mut ui.build_ctx();
///
///     // Create a style resource first and assign it to the build context. All widgets built with
///     // the context will use this style.
///     let style = Style::light_style()
///         .with(Button::CORNER_RADIUS, 6.0f32)
///         .with(Button::BORDER_THICKNESS, Thickness::uniform(3.0));
///
///     ctx.style = StyleResource::new_embedded(style);
///
///     // The button will have a corner radius of 6.0 points and border thickness of 3.0 points on
///     // each side.
///     ButtonBuilder::new(WidgetBuilder::new()).build(ctx);
/// }
/// ```
///
/// To change UI style globally after it was built, use something like this:
///
/// ```rust
/// use fyrox_ui::{
///     button::Button,
///     style::{resource::StyleResource, Style},
///     Thickness, UserInterface,
/// };
///
/// fn apply_style(ui: &mut UserInterface) {
///     let style = Style::light_style()
///         .with(Button::CORNER_RADIUS, 3.0f32)
///         .with(Button::BORDER_THICKNESS, Thickness::uniform(1.0));
///
///     ui.set_style(StyleResource::new_embedded(style));
/// }
/// ```
#[derive(Visit, Reflect, Clone, Default, Debug, TypeUuidProvider)]
#[type_uuid(id = "38a63b49-d765-4c01-8fb5-202cc43d607e")]
pub struct Style {
    parent: Option<StyleResource>,
    properties: Vec<StylePropertyContainer>,
}

impl Style {
    /// The name of the darkest brush.
    pub const BRUSH_DARKEST: &'static str = "Global.Brush.Darkest";
    /// The name of the darker brush.
    pub const BRUSH_DARKER: &'static str = "Global.Brush.Darker";
    /// The name of the dark brush.
    pub const BRUSH_DARK: &'static str = "Global.Brush.Dark";
    /// The name of the primary brush that is used for the major amount of surface.
    pub const BRUSH_PRIMARY: &'static str = "Global.Brush.Primary";
    /// The name of the slightly lighter primary brush.
    pub const BRUSH_LIGHTER_PRIMARY: &'static str = "Global.Brush.LighterPrimary";
    /// The name of the light brush.
    pub const BRUSH_LIGHT: &'static str = "Global.Brush.Light";
    /// The name of the lighter brush.
    pub const BRUSH_LIGHTER: &'static str = "Global.Brush.Lighter";
    /// The name of the lightest brush.
    pub const BRUSH_LIGHTEST: &'static str = "Global.Brush.Lightest";
    /// The name of the bright brush.
    pub const BRUSH_BRIGHT: &'static str = "Global.Brush.Bright";
    /// The name of the brightest brush.
    pub const BRUSH_BRIGHTEST: &'static str = "Global.Brush.Brightest";
    /// The name of the bright blue brush.
    pub const BRUSH_BRIGHT_BLUE: &'static str = "Global.Brush.BrightBlue";
    /// The name of the dim blue brush.
    pub const BRUSH_DIM_BLUE: &'static str = "Global.Brush.DimBlue";
    /// The name of the text brush.
    pub const BRUSH_TEXT: &'static str = "Global.Brush.Text";
    /// The name of the foreground brush.
    pub const BRUSH_FOREGROUND: &'static str = "Global.Brush.Foreground";
    /// The name of the information brush.
    pub const BRUSH_INFORMATION: &'static str = "Global.Brush.Information";
    /// The name of the warning brush.
    pub const BRUSH_WARNING: &'static str = "Global.Brush.Warning";
    /// The name of the error brush.
    pub const BRUSH_ERROR: &'static str = "Global.Brush.Error";
    /// The name of the ok brush.
    pub const BRUSH_OK: &'static str = "Global.Brush.Ok";
    /// The name of the highlight brush used to highlight widgets with keyboard focus.
    pub const BRUSH_HIGHLIGHT: &'static str = "Global.Brush.Highlight";
    /// The name of the font size property.
    pub const FONT_SIZE: &'static str = "Global.Font.Size";
    /// The name of the normal state brush of the `ok` context action.
    pub const BRUSH_OK_NORMAL: &'static str = "Global.Brush.Ok.Normal";
    /// The name of the pressed state brush of the `ok` context action.
    pub const BRUSH_OK_PRESSED: &'static str = "Global.Brush.Ok.Pressed";
    /// The name of the hover state brush of the `ok` context action.
    pub const BRUSH_OK_HOVER: &'static str = "Global.Brush.Ok.Hover";
    /// The name of the normal state brush of the `cancel` context action.
    pub const BRUSH_CANCEL_NORMAL: &'static str = "Global.Brush.Cancel.Normal";
    /// The name of the pressed state brush of the `cancel` context action.
    pub const BRUSH_CANCEL_PRESSED: &'static str = "Global.Brush.Cancel.Pressed";
    /// The name of the hover state brush of the `cancel` context action.
    pub const BRUSH_CANCEL_HOVER: &'static str = "Global.Brush.Cancel.Hover";

    fn base_style() -> Style {
        let mut style = Self::default();

        style
            .set(Self::FONT_SIZE, 14.0f32)
            .merge(&Button::style())
            .merge(&CheckBox::style())
            .merge(&DropdownList::style())
            .merge(&ToggleButton::style());

        style
    }

    /// Creates a new dark style.
    pub fn dark_style() -> Style {
        let mut style = Self::base_style();
        style
            .set(Self::BRUSH_DARKEST, Brush::Solid(Color::repeat_opaque(20)))
            .set(Self::BRUSH_DARKER, Brush::Solid(Color::repeat_opaque(30)))
            .set(Self::BRUSH_DARK, Brush::Solid(Color::repeat_opaque(40)))
            .set(Self::BRUSH_PRIMARY, Brush::Solid(Color::repeat_opaque(50)))
            .set(
                Self::BRUSH_LIGHTER_PRIMARY,
                Brush::Solid(Color::repeat_opaque(60)),
            )
            .set(Self::BRUSH_LIGHT, Brush::Solid(Color::repeat_opaque(70)))
            .set(Self::BRUSH_LIGHTER, Brush::Solid(Color::repeat_opaque(85)))
            .set(
                Self::BRUSH_LIGHTEST,
                Brush::Solid(Color::repeat_opaque(100)),
            )
            .set(Self::BRUSH_BRIGHT, Brush::Solid(Color::repeat_opaque(130)))
            .set(
                Self::BRUSH_BRIGHTEST,
                Brush::Solid(Color::repeat_opaque(160)),
            )
            .set(
                Self::BRUSH_BRIGHT_BLUE,
                Brush::Solid(Color::opaque(80, 118, 178)),
            )
            .set(
                Self::BRUSH_HIGHLIGHT,
                Brush::Solid(Color::opaque(80, 118, 178)),
            )
            .set(
                Self::BRUSH_DIM_BLUE,
                Brush::Solid(Color::opaque(66, 99, 149)),
            )
            .set(Self::BRUSH_TEXT, Brush::Solid(Color::opaque(190, 190, 190)))
            .set(Self::BRUSH_FOREGROUND, Brush::Solid(Color::WHITE))
            .set(Self::BRUSH_INFORMATION, Brush::Solid(Color::ANTIQUE_WHITE))
            .set(Self::BRUSH_WARNING, Brush::Solid(Color::GOLD))
            .set(Self::BRUSH_ERROR, Brush::Solid(Color::RED))
            .set(Self::BRUSH_OK, Brush::Solid(Color::GREEN))
            .set(
                Self::BRUSH_OK_NORMAL,
                Brush::Solid(Color::opaque(0, 130, 0)),
            )
            .set(Self::BRUSH_OK_HOVER, Brush::Solid(Color::opaque(0, 150, 0)))
            .set(
                Self::BRUSH_OK_PRESSED,
                Brush::Solid(Color::opaque(0, 170, 0)),
            )
            .set(
                Self::BRUSH_CANCEL_NORMAL,
                Brush::Solid(Color::opaque(130, 0, 0)),
            )
            .set(
                Self::BRUSH_CANCEL_HOVER,
                Brush::Solid(Color::opaque(150, 0, 0)),
            )
            .set(
                Self::BRUSH_CANCEL_PRESSED,
                Brush::Solid(Color::opaque(170, 0, 0)),
            );
        style
    }

    /// Creates a new light style.
    pub fn light_style() -> Style {
        let mut style = Self::base_style();
        style
            .set(Self::BRUSH_DARKEST, Brush::Solid(Color::repeat_opaque(140)))
            .set(Self::BRUSH_DARKER, Brush::Solid(Color::repeat_opaque(150)))
            .set(Self::BRUSH_DARK, Brush::Solid(Color::repeat_opaque(160)))
            .set(Self::BRUSH_PRIMARY, Brush::Solid(Color::repeat_opaque(170)))
            .set(
                Self::BRUSH_LIGHTER_PRIMARY,
                Brush::Solid(Color::repeat_opaque(180)),
            )
            .set(Self::BRUSH_LIGHT, Brush::Solid(Color::repeat_opaque(190)))
            .set(Self::BRUSH_LIGHTER, Brush::Solid(Color::repeat_opaque(205)))
            .set(
                Self::BRUSH_LIGHTEST,
                Brush::Solid(Color::repeat_opaque(220)),
            )
            .set(Self::BRUSH_BRIGHT, Brush::Solid(Color::repeat_opaque(40)))
            .set(
                Self::BRUSH_BRIGHTEST,
                Brush::Solid(Color::repeat_opaque(30)),
            )
            .set(
                Self::BRUSH_BRIGHT_BLUE,
                Brush::Solid(Color::opaque(80, 118, 178)),
            )
            .set(
                Self::BRUSH_HIGHLIGHT,
                Brush::Solid(Color::opaque(80, 118, 178)),
            )
            .set(
                Self::BRUSH_DIM_BLUE,
                Brush::Solid(Color::opaque(66, 99, 149)),
            )
            .set(Self::BRUSH_TEXT, Brush::Solid(Color::repeat_opaque(0)))
            .set(Self::BRUSH_FOREGROUND, Brush::Solid(Color::WHITE))
            .set(Self::BRUSH_INFORMATION, Brush::Solid(Color::ROYAL_BLUE))
            .set(
                Self::BRUSH_WARNING,
                Brush::Solid(Color::opaque(255, 242, 0)),
            )
            .set(Self::BRUSH_ERROR, Brush::Solid(Color::RED));
        style
    }

    /// The same as [`Self::set`], but takes self as value and essentially allows chained calls in
    /// builder-like style:
    ///
    /// ```rust
    /// # use fyrox_core::color::Color;
    /// # use fyrox_ui::brush::Brush;
    /// # use fyrox_ui::style::Style;
    /// Style::default()
    ///     .with("SomeProperty", 0.2f32)
    ///     .with("SomeOtherProperty", Brush::Solid(Color::WHITE));
    /// ```
    pub fn with(
        mut self,
        name: impl Into<ImmutableString>,
        property: impl Into<StyleProperty>,
    ) -> Self {
        self.set(name, property);
        self
    }

    /// Sets the parent style for this style. Parent style will be used in an attempt to fetch properties
    /// that aren't present in this style.
    pub fn set_parent(&mut self, parent: Option<StyleResource>) {
        self.parent = parent;
    }

    /// Returns parent style of this style.
    pub fn parent(&self) -> Option<&StyleResource> {
        self.parent.as_ref()
    }

    /// Returns an index of the variable with the given name.
    pub fn index_of(&self, name: &ImmutableString) -> Option<usize> {
        self.properties
            .binary_search_by(|v| v.name.cached_hash().cmp(&name.cached_hash()))
            .ok()
    }

    /// Checks if there's a variable with the given name.
    pub fn contains(&self, name: &ImmutableString) -> bool {
        self.index_of(name).is_some()
    }

    /// Merges current style with some other style. This method does not overwrite existing values,
    /// instead it only adds missing values from the other style.
    pub fn merge(&mut self, other: &Self) -> &mut Self {
        for other_property in other.properties.iter() {
            if !self.contains(&other_property.name) {
                self.set(other_property.name.clone(), other_property.value.clone());
            }
        }
        self
    }

    /// Registers a new property with the given name and value:
    ///
    /// ```rust
    /// # use fyrox_core::color::Color;
    /// # use fyrox_ui::brush::Brush;
    /// # use fyrox_ui::style::Style;
    /// let mut style = Style::default();
    /// style
    ///     .set("SomeProperty", 0.2f32)
    ///     .set("SomeOtherProperty", Brush::Solid(Color::WHITE));
    /// ```
    pub fn set(
        &mut self,
        name: impl Into<ImmutableString>,
        value: impl Into<StyleProperty>,
    ) -> &mut Self {
        let name = name.into();
        let value = value.into();

        if let Some(existing_index) = self.index_of(&name) {
            self.properties[existing_index] = StylePropertyContainer { name, value };
        } else {
            let index = self
                .properties
                .partition_point(|h| h.name.cached_hash() < name.cached_hash());
            self.properties
                .insert(index, StylePropertyContainer { name, value });
        }

        self
    }

    /// Tries to fetch a property with the given name. If the property is not found, this method will
    /// try to search in the parent style (the search is recursive).
    pub fn get_raw(&self, name: impl Into<ImmutableString>) -> Option<StyleProperty> {
        let name = name.into();
        let index = self.index_of(&name)?;
        if let Some(container) = self.properties.get(index) {
            return Some(container.value.clone());
        } else if let Some(parent) = self.parent.as_ref() {
            let state = parent.state();
            if let Some(data) = state.data_ref() {
                return data.get_raw(name);
            }
        }
        None
    }

    /// Tries to fetch a property with the given name and perform type casting to the requested type.
    /// If the property is not found, this method will try to search in the parent style (the search
    /// is recursive).
    pub fn get<P>(&self, name: impl Into<ImmutableString>) -> Option<P>
    where
        StyleProperty: IntoPrimitive<P>,
    {
        self.get_raw(name)
            .and_then(|property| property.into_primitive())
    }

    /// Tries to fetch a property with the given name. If the property is not found, this method will
    /// try to search in the parent style (the search is recursive). If there's no such property at
    /// all, this method will return its default value (define by [`Default`] trait).
    pub fn get_or_default<P>(&self, name: impl Into<ImmutableString>) -> P
    where
        P: Default,
        StyleProperty: IntoPrimitive<P>,
    {
        self.get_raw(name)
            .and_then(|property| property.into_primitive())
            .unwrap_or_default()
    }

    /// Tries to fetch a property with the given name or, if not found, returns the given default value.
    pub fn get_or<P>(&self, name: impl Into<ImmutableString>, default: P) -> P
    where
        StyleProperty: IntoPrimitive<P>,
    {
        self.get(name).unwrap_or(default)
    }

    /// Tries to find a property with the given name or takes the default value of the property's type
    /// and wraps it into [`StyledProperty`], essentially binding the value to the style property.
    pub fn property<P>(&self, name: impl Into<ImmutableString>) -> StyledProperty<P>
    where
        P: Default,
        StyleProperty: IntoPrimitive<P>,
    {
        let name = name.into();
        StyledProperty::new(self.get_or_default(name.clone()), name)
    }

    /// Tries to load a style from the given path.
    pub async fn from_file(
        path: &Path,
        io: &dyn ResourceIo,
        resource_manager: ResourceManager,
    ) -> Result<Self, StyleResourceError> {
        let bytes = io.load_file(path).await?;
        let mut visitor = Visitor::load_from_memory(&bytes)?;
        visitor.blackboard.register(Arc::new(resource_manager));
        let mut style = Style::default();
        style.visit("Style", &mut visitor)?;
        Ok(style)
    }

    /// Returns an immutable reference to the internal container with the style properties.
    /// Keep in mind that the returned container contains only the properties of the current
    /// style! Properties of the parent style(s) should be obtained separately.
    pub fn inner(&self) -> &Vec<StylePropertyContainer> {
        &self.properties
    }

    /// Collects all the properties in the current and ancestor style chain. Returns a hash map with
    /// all property values with their names. Basically, this method merges all the styles with their
    /// ancestor style chain.
    pub fn all_properties(&self) -> Self {
        let mut properties = self
            .parent
            .as_ref()
            .map(|parent| parent.data_ref().all_properties())
            .unwrap_or_default();
        for property in self.properties.iter() {
            properties.set(property.name.clone(), property.value.clone());
        }
        properties
    }
}

#[cfg(test)]
mod test {
    use crate::brush::Brush;
    use crate::style::Style;
    use fyrox_core::color::Color;
    use fyrox_core::ImmutableString;

    #[test]
    fn test_style() {
        let mut style = Style::default();
        style
            .set("A", 0.2f32)
            .set("D", 0.1f32)
            .set("B", Brush::Solid(Color::WHITE))
            .set("C", Brush::Solid(Color::WHITE));
        assert_eq!(style.index_of(&ImmutableString::new("A")), Some(3));
        assert_eq!(style.index_of(&ImmutableString::new("B")), Some(2));
        assert_eq!(style.index_of(&ImmutableString::new("C")), Some(1));
        assert_eq!(style.index_of(&ImmutableString::new("D")), Some(0));
    }
}