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
use std::borrow::BorrowMut;
use std::fmt::{Debug, Formatter};

use dyn_clone::DynClone;

use crate::{DefaultModifiers, Renderable};
use crate::components::{Alignment, Appendable, HStack, Icon, Text, TextStyle};
use crate::components::badge::{Badge, BadgeSupport, BadgeModifiers};
use crate::components::icons::IconPack;
use crate::node::{Node, NodeContainer};

#[derive(Debug, Clone)]
pub struct Tag {
    node: Node,
    pub label: String,
    pub icon: Option<Box<dyn IconPack>>,
    pub badge: Option<Badge>,
}


impl Tag {
    pub fn new(label: &str) -> Self {
        Self {
            node: Node::default(),
            label: label.to_string(),
            icon: None,
            badge: None,
        }
    }

    /// Set tag's icon
    pub fn icon<T>(&mut self, icon: T) -> Self
        where
            T: 'static + IconPack {
        self.icon = Some(Box::new(icon));
        self.clone()
    }

    pub fn destructive(&mut self) -> Self {
        self.add_class("tag--destructive")
    }
}

impl DefaultModifiers<Tag> for Tag {}


impl BadgeSupport for Tag {
    fn add_badge(&mut self, badge: Badge) {
        self.badge = Some(badge);
    }
}

impl BadgeModifiers for Tag {}

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

impl Renderable for Tag {
    fn render(&self) -> Node {
        let mut tag = self.clone();
        tag.get_node().class_list.insert("tag".to_string());

        if let Some(icon) = tag.icon {
            let mut icon = Icon::new(icon)
                .size(16)
                .stroke_width(2);
            tag.node.children.push(icon.render());
        }

        tag.node.children.push({
            Text::new(&self.label, TextStyle::Overline)
        }.render());

        if let Some(badge) = tag.badge {
            tag.node.children.push(badge.render());
        }

        tag.node
    }
}