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
use std::rc::Rc;

use dces::prelude::Entity;

use crate::{css_engine::*, event::EventHandler};

pub use self::build_context::*;
pub use self::context::*;
pub use self::message::*;
pub use self::state::*;
pub use self::template::*;
pub use self::widget_container::*;

mod build_context;
mod context;
mod message;
mod state;
mod template;
mod widget_container;

/// Adds the given `pseudo_class` to the css selector of the given `widget`.
pub fn add_selector_to_widget(pseudo_class: &str, widget: &mut WidgetContainer<'_>) {
    if let Some(selector) = widget.try_get_mut::<Selector>("selector") {
        selector.pseudo_classes.insert(String::from(pseudo_class));
        selector.set_dirty(true);
    }
}

/// Removes the given `pseudo_class` from the css selector of the given `widget`.
pub fn remove_selector_from_widget(pseudo_class: &str, widget: &mut WidgetContainer<'_>) {
    if let Some(selector) = widget.try_get_mut::<Selector>("selector") {
        selector.pseudo_classes.remove(pseudo_class);
        selector.set_dirty(true);
    }
}

/// Used to define the `parent_type`of a widget.
pub enum ParentType {
    /// None children could add to the widget.
    None,

    /// Only one child could be added to the widget.
    Single,

    /// Multiple children could be added tot the widget.
    Multi,
}

/// The `Widget` trait is used to define a new widget.
pub trait Widget: Template {
    /// Creates a new widget.
    fn create() -> Self;

    /// Builds the widget and returns the template of the widget.
    fn build(self, ctx: &mut BuildContext) -> Entity;

    /// Inerts a new event handler.
    fn insert_handler(self, handler: impl Into<Rc<dyn EventHandler>>) -> Self;

    /// Returns the state of the widget.
    fn state(&self) -> Option<Rc<dyn State>> {
        None
    }

    /// Appends a child ot the widget.
    fn child(self, child: Entity) -> Self;
}