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
use crate::node::{Node, NodeContainer};
use crate::{Renderable};
use std::borrow::BorrowMut;
use crate::DefaultModifiers;
use crate::components::{TextStyle, Text, View};

#[derive(Debug, Clone)]
pub struct TitleBar {
    node: Node,
    pub title: String,
    pub is_sticky: bool,
    pub left_item: Option<Box<dyn Renderable>>,
    pub right_item: Option<Box<dyn Renderable>>,
    pub bottom_item: Option<Box<dyn Renderable>>,
}

impl NodeContainer for TitleBar {
    fn get_node(&mut self) -> &mut Node {
        self.node.borrow_mut()
    }
}

impl DefaultModifiers<TitleBar> for TitleBar {}

impl TitleBar {
    pub fn new(title: &str) -> Self {
        TitleBar {
            node: Default::default(),
            title: title.to_string(),
            is_sticky: true,
            left_item: None,
            right_item: None,
            bottom_item: None,
        }
    }
    fn grid_areas(&mut self, schema: &str) -> Self {
        self.node.node_style.push(("grid-template-areas".to_string(), schema.to_string()));
        self.clone()
    }

    pub fn sticky(&mut self, is_sticky: bool) -> Self {
        self.is_sticky = is_sticky;
        self.clone()
    }

    pub fn left_item<'a, T>(&'a mut self, item: T) -> Self
        where
            T: 'static + Renderable,
    {
        self.left_item = Some(Box::new(item));
        self.clone()
    }
    pub fn right_item<'a, T>(&'a mut self, item: T) -> Self
        where
            T: 'static + Renderable,
    {
        self.right_item = Some(Box::new(item));
        self.clone()
    }
    pub fn bottom_item<'a, T>(&'a mut self, item: T) -> Self
        where
            T: 'static + Renderable,
    {
        self.bottom_item = Some(Box::new(item));
        self.clone()
    }
}

impl Renderable for TitleBar {
    fn render(&self) -> Node {
        let mut areas = String::new();
        if self.left_item.is_some() {
            areas.push_str("'left_item . right_item' 'title title title'");
        } else {
            areas.push_str("'title title right_item'");
        }
        if self.bottom_item.is_some() {
            areas.push_str("'bottom_item bottom_item bottom_item'");
        }
        let mut view = self.clone()
            .add_class("titlebar")
            .grid_areas(areas.as_str());

        if self.is_sticky {
            view.sticky_to_top(0);
        }

        let text = Text::new(self.title.as_str(), TextStyle::LargeTitle)
            .grid_area("title")
            .render();
        view.node.children.push(text);
        if let Some(left_item) = self.left_item.clone() {
            let mut item = left_item.render();
            item.node_style.push(("grid-area".to_string(), "left_item".to_string()));
            view.node.children.push(item);
        }
        if let Some(right_item) = self.right_item.clone() {
            let mut item = right_item.render();
            item.node_style.push(("grid-area".to_string(), "right_item".to_string()));
            view.node.children.push(item);
        }
        if let Some(bottom_item) = self.bottom_item.clone() {
            let mut item = bottom_item.render();
            item.node_style.push(("grid-area".to_string(), "bottom_item".to_string()));
            view.node.children.push(item);
        }
        view.node
    }
}