pixels_graphics_lib/ui/
toggle_button.rs1use crate::buffer_graphics_lib::shapes::polyline::Polyline;
2use crate::buffer_graphics_lib::text::format::Positioning::Center;
3use crate::buffer_graphics_lib::text::pos::TextPos;
4use crate::buffer_graphics_lib::text::wrapping::WrappingStrategy;
5use crate::buffer_graphics_lib::text::Text;
6use crate::ui::prelude::*;
7use crate::ui::styles::ToggleButtonStyle;
8use crate::Timing;
9
10#[derive(Debug)]
11pub struct ToggleButton {
12 label: String,
13 text: Text,
14 bounds: Rect,
15 border: Polyline,
16 shadow: Polyline,
17 selected: bool,
18 style: ToggleButtonStyle,
19 state: ViewState,
20}
21
22impl ToggleButton {
23 pub fn new<P: Into<Coord>>(
24 xy: P,
25 text: &'static str,
26 min_width: Option<usize>,
27 style: &ToggleButtonStyle,
28 ) -> Self {
29 let min_width = min_width.unwrap_or_default();
30 let (w, h) = PixelFont::Standard6x7.measure(text);
31 let bounds = Rect::new_with_size(
32 xy,
33 ((w as f32 * 1.2) as usize).max(min_width),
34 (h as f32 * 2.0) as usize,
35 );
36 let label = text;
37 let (text, border, shadow) = Self::layout(&bounds, style, label);
38 Self {
39 label: label.to_string(),
40 text,
41 bounds,
42 border,
43 shadow,
44 selected: false,
45 style: style.clone(),
46 state: ViewState::Normal,
47 }
48 }
49
50 fn layout(bounds: &Rect, style: &ToggleButtonStyle, text: &str) -> (Text, Polyline, Polyline) {
51 let border = Polyline::rounded_rect(
52 bounds.left(),
53 bounds.top(),
54 bounds.right(),
55 bounds.bottom(),
56 style.rounding,
57 WHITE,
58 )
59 .unwrap();
60 let shadow = Polyline::rounded_rect(
61 bounds.left() + 1,
62 bounds.top() + 1,
63 bounds.right() + 1,
64 bounds.bottom() + 1,
65 style.rounding,
66 WHITE,
67 )
68 .unwrap();
69 let text = Text::new(
70 text,
71 TextPos::px(bounds.center() + (0, 1)),
72 (
73 WHITE,
74 style.font,
75 WrappingStrategy::AtCol(style.font.px_to_cols(bounds.width())),
76 Center,
77 ),
78 );
79 (text, border, shadow)
80 }
81}
82
83impl ToggleButton {
84 #[must_use]
85 pub fn is_selected(&self) -> bool {
86 self.selected
87 }
88
89 pub fn set_selected(&mut self, value: bool) {
90 self.selected = value;
91 }
92
93 #[must_use]
94 pub fn on_mouse_click(&mut self, down: Coord, up: Coord) -> bool {
95 println!("down: {down:?}, up: {up:?}");
96 if self.state != ViewState::Disabled
97 && self.bounds.contains(down)
98 && self.bounds.contains(up)
99 {
100 self.selected = true;
101 true
102 } else {
103 false
104 }
105 }
106}
107
108impl PixelView for ToggleButton {
109 fn set_position(&mut self, top_left: Coord) {
110 self.bounds = self.bounds.move_to(top_left);
111 let (text, border, shadow) = Self::layout(&self.bounds, &self.style, &self.label);
112 self.border = border;
113 self.shadow = shadow;
114 self.text = text;
115 }
116
117 #[must_use]
118 fn bounds(&self) -> &Rect {
119 &self.bounds
120 }
121
122 fn render(&self, graphics: &mut Graphics, mouse: &MouseData) {
123 let hovering = self.bounds.contains(mouse.xy);
124 let (error, disabled) = self.state.get_err_dis();
125 if let Some(color) = self
126 .style
127 .shadow
128 .get(hovering, self.selected, error, disabled)
129 {
130 self.shadow.with_color(color).render(graphics);
131 }
132 if let Some(color) = self
133 .style
134 .border
135 .get(hovering, self.selected, error, disabled)
136 {
137 self.border.with_color(color).render(graphics);
138 }
139 if let Some(color) = self
140 .style
141 .text
142 .get(hovering, self.selected, error, disabled)
143 {
144 self.text.with_color(color).render(graphics);
145 }
146 }
147
148 fn update(&mut self, _: &Timing) {}
149
150 #[inline]
151 fn set_state(&mut self, new_state: ViewState) {
152 self.state = new_state;
153 }
154
155 #[inline]
156 fn get_state(&self) -> ViewState {
157 self.state
158 }
159}
160
161impl LayoutView for ToggleButton {
162 fn set_bounds(&mut self, bounds: Rect) {
163 self.bounds = bounds.clone();
164 self.set_position(bounds.top_left());
165 }
166}