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
use crate::prelude::*;
use crate::ui::prelude::*;

#[derive(Debug)]
pub struct Label {
    text: Text,
}

impl Label {
    pub fn new(text: Text) -> Self {
        Self { text }
    }

    pub fn full(
        str: &str,
        pos: TextPos,
        color: Color,
        font: PixelFont,
        positioning: Positioning,
        wrapping: WrappingStrategy,
    ) -> Self {
        Self {
            text: Text::new(
                str,
                pos,
                TextFormat::new(wrapping, font, color, positioning),
            ),
        }
    }

    pub fn singleline<P: Into<Coord>>(
        str: &str,
        pos: P,
        color: Color,
        font: PixelFont,
        width_px: usize,
    ) -> Self {
        Self {
            text: Text::new(
                str,
                TextPos::px(pos.into()),
                TextFormat::new(
                    WrappingStrategy::Cutoff(font.px_to_cols(width_px)),
                    font,
                    color,
                    Positioning::LeftTop,
                ),
            ),
        }
    }

    pub fn multiline<P: Into<Coord>>(
        str: &str,
        pos: P,
        color: Color,
        font: PixelFont,
        width_px: usize,
    ) -> Self {
        Self {
            text: Text::new(
                str,
                TextPos::px(pos.into()),
                TextFormat::new(
                    WrappingStrategy::SpaceBeforeCol(font.px_to_cols(width_px)),
                    font,
                    color,
                    Positioning::LeftTop,
                ),
            ),
        }
    }
}

impl Label {
    pub fn update_text(&mut self, content: &str) {
        self.text = Text::new(content, self.text.pos(), self.text.formatting().clone());
    }
}

impl PixelView for Label {
    fn set_position(&mut self, top_left: Coord) {
        self.text = self.text.with_pos(TextPos::px(top_left));
    }

    fn bounds(&self) -> &Rect {
        self.text.bounds()
    }

    fn render(&self, graphics: &mut Graphics, _: &MouseData) {
        self.text.render(graphics);
    }

    fn update(&mut self, _: &Timing) {}

    fn set_state(&mut self, _: ViewState) {
        unimplemented!("Label doesn't support state");
    }

    fn get_state(&self) -> ViewState {
        ViewState::Normal
    }
}

impl LayoutView for Label {
    fn set_bounds(&mut self, bounds: Rect) {
        let lines = self
            .text
            .contents()
            .iter()
            .map(|arr| String::from_utf8_lossy(arr).to_string())
            .collect::<Vec<String>>()
            .join("\n");
        let cols = self.text.formatting().font().px_to_cols(bounds.width());
        let strat = match self.text.formatting().wrapping() {
            WrappingStrategy::None => WrappingStrategy::None,
            WrappingStrategy::SpaceBeforeCol(_) => WrappingStrategy::SpaceBeforeCol(cols),
            WrappingStrategy::AtColWithHyphen(_) => WrappingStrategy::AtColWithHyphen(cols),
            WrappingStrategy::Cutoff(_) => WrappingStrategy::Cutoff(cols),
            WrappingStrategy::Ellipsis(_) => WrappingStrategy::Ellipsis(cols),
            WrappingStrategy::AtCol(_) => WrappingStrategy::AtCol(cols),
        };

        self.text = Text::new(
            &lines,
            self.text.pos(),
            TextFormat::new(
                strat,
                self.text.formatting().font(),
                self.text.formatting().color(),
                self.text.formatting().positioning(),
            ),
        );
    }
}