pixels_graphics_lib/ui/
label.rs

1use crate::prelude::*;
2use crate::ui::prelude::*;
3
4#[derive(Debug)]
5pub struct Label {
6    text: Text,
7}
8
9impl Label {
10    pub fn new(text: Text) -> Self {
11        Self { text }
12    }
13
14    pub fn full(
15        str: &str,
16        pos: TextPos,
17        color: Color,
18        font: PixelFont,
19        positioning: Positioning,
20        wrapping: WrappingStrategy,
21    ) -> Self {
22        Self {
23            text: Text::new(
24                str,
25                pos,
26                TextFormat::new(wrapping, font, color, positioning),
27            ),
28        }
29    }
30
31    pub fn singleline<P: Into<Coord>>(
32        str: &str,
33        pos: P,
34        color: Color,
35        font: PixelFont,
36        width_px: usize,
37    ) -> Self {
38        Self {
39            text: Text::new(
40                str,
41                TextPos::px(pos.into()),
42                TextFormat::new(
43                    WrappingStrategy::Cutoff(font.px_to_cols(width_px)),
44                    font,
45                    color,
46                    Positioning::LeftTop,
47                ),
48            ),
49        }
50    }
51
52    pub fn multiline<P: Into<Coord>>(
53        str: &str,
54        pos: P,
55        color: Color,
56        font: PixelFont,
57        width_px: usize,
58    ) -> Self {
59        Self {
60            text: Text::new(
61                str,
62                TextPos::px(pos.into()),
63                TextFormat::new(
64                    WrappingStrategy::SpaceBeforeCol(font.px_to_cols(width_px)),
65                    font,
66                    color,
67                    Positioning::LeftTop,
68                ),
69            ),
70        }
71    }
72}
73
74impl Label {
75    pub fn update_text(&mut self, content: &str) {
76        self.text = Text::new(content, self.text.pos(), self.text.formatting().clone());
77    }
78}
79
80impl PixelView for Label {
81    fn set_position(&mut self, top_left: Coord) {
82        self.text = self.text.with_pos(TextPos::px(top_left));
83    }
84
85    fn bounds(&self) -> &Rect {
86        self.text.bounds()
87    }
88
89    fn render(&self, graphics: &mut Graphics, _: &MouseData) {
90        self.text.render(graphics);
91    }
92
93    fn update(&mut self, _: &Timing) {}
94
95    fn set_state(&mut self, _: ViewState) {
96        unimplemented!("Label doesn't support state");
97    }
98
99    fn get_state(&self) -> ViewState {
100        ViewState::Normal
101    }
102}
103
104impl LayoutView for Label {
105    fn set_bounds(&mut self, bounds: Rect) {
106        let lines = self
107            .text
108            .contents()
109            .iter()
110            .map(|arr| String::from_utf8_lossy(arr).to_string())
111            .collect::<Vec<String>>()
112            .join("\n");
113        let cols = self.text.formatting().font().px_to_cols(bounds.width());
114        let strat = match self.text.formatting().wrapping() {
115            WrappingStrategy::None => WrappingStrategy::None,
116            WrappingStrategy::SpaceBeforeCol(_) => WrappingStrategy::SpaceBeforeCol(cols),
117            WrappingStrategy::AtColWithHyphen(_) => WrappingStrategy::AtColWithHyphen(cols),
118            WrappingStrategy::Cutoff(_) => WrappingStrategy::Cutoff(cols),
119            WrappingStrategy::Ellipsis(_) => WrappingStrategy::Ellipsis(cols),
120            WrappingStrategy::AtCol(_) => WrappingStrategy::AtCol(cols),
121        };
122
123        self.text = Text::new(
124            &lines,
125            self.text.pos(),
126            TextFormat::new(
127                strat,
128                self.text.formatting().font(),
129                self.text.formatting().color(),
130                self.text.formatting().positioning(),
131            ),
132        );
133    }
134}