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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use rust_on_rails::prelude::*;
use crate::Callback;
use crate::elements::images::Icon;
use crate::elements::shapes::OutlinedRectangle;
use crate::layout::{Offset, Padding, Size, Row, Stack};
use super::{ButtonSize, ButtonState, ButtonStyle};
/// The [`IconButton`] is a type of button that contains a singular icon instead of a label.
#[derive(Component)]
pub struct IconButton(Stack, OutlinedRectangle, Image, #[skip] ButtonStyle, #[skip] ButtonState, #[skip] pub Box<dyn FnMut(&mut Context)>);
impl IconButton {
/// Creates a new [`IconButton`] with specified parameters.
///
/// # Parameters
/// - `ctx`: The [`Context`] for accessing the app's theme.
/// - `icon`: A string representing the [`Icon`]'s name.
/// - `size`: The size of the button.
/// - `style`: The style of the button. secondary or ghost. (primary is not supported).
/// - `state`: The initial state of the button.
/// - `on_click`: A closure that will be executed when the button is clicked.
///
/// # Returns
/// A new [`IconButton`] instance configured with the given parameters.
pub fn new(
ctx: &mut Context,
icon: &'static str,
size: ButtonSize,
style: ButtonStyle,
state: ButtonState,
on_click: Box<dyn FnMut(&mut Context)>,
) -> Self {
let colors = state.color(ctx, style);
let (size, icon_size, radius) = match (style, size) {
(ButtonStyle::Secondary, ButtonSize::Large) => (52.0, 32.0, 12.0),
(ButtonStyle::Secondary, ButtonSize::Medium) => (36.0, 20.0, 8.0),
(ButtonStyle::Ghost, ButtonSize::Large) => (52.0, 48.0, 12.0),
(ButtonStyle::Ghost, ButtonSize::Medium) => (36.0, 32.0, 8.0),
_ => panic!("{:?} is not a valid IconButton tyle", style)
};
let icon = Icon::new(ctx, icon, colors.label, icon_size);
let background = OutlinedRectangle::new(colors.background, colors.outline, radius, 1.0);
IconButton(
Stack(Offset::Center, Offset::Center, Size::Static(size), Size::Static(size), Padding::default()),
background, icon, style, state, on_click
)
}
/// Updates the colors of the `IconButton` based on the button's state.
pub fn color(&mut self, ctx: &mut Context, state: ButtonState) {
let colors = state.color(ctx, self.3);
*self.1.background() = colors.background;
*self.1.outline() = colors.outline;
self.2.color = Some(colors.label);
}
/// Returns a mutable reference to the [`ButtonState`] of the button.
pub fn status(&mut self) -> &mut ButtonState {&mut self.4}
}
impl OnEvent for IconButton {
fn on_event(&mut self, ctx: &mut Context, event: &mut dyn Event) -> bool {
if let Some(event) = event.downcast_ref::<MouseEvent>() {
if let Some(state) = self.4.handle(ctx, *event) {
self.color(ctx, state);
}
if let MouseEvent{state: MouseState::Pressed, position: Some(_)} = event {
match self.4 {
ButtonState::Default | ButtonState::Hover | ButtonState::Pressed => (self.5)(ctx),
_ => {}
}
}
false
} else {true}
}
}
/// A row of secondary style [`IconButton`]s, spaced `24.0` apart.
#[derive(Debug, Component)]
pub struct IconButtonRow(Row, Vec<IconButton>);
impl OnEvent for IconButtonRow {}
impl IconButtonRow {
/// Creates a new [`IconButtonRow`] component with a list of icon buttons.
///
/// This function initializes the [`IconButtonRow`] by creating a row layout and adding the provided list of
/// icon buttons. Each button is created with a label and an associated callback function, which is triggered
/// when the button is clicked. The buttons are arranged in a row with 24.0 units of space between them.
///
/// # Parameters
/// - `ctx`: The [`Context`] for accessing the app's theme.
/// - `buttons`: A vector of tuples, where each tuple contains a label (`&'static str`) and a callback function
/// (`Box<[`Callback`]>`) to be executed when the corresponding button is clicked.
///
/// # Returns
/// A new [`IconButtonRow`] component containing the list of icon buttons arranged in a centered row layout.
///
/// # Example
/// ```rust
/// let icon_button_row = IconButtonRow::new(ctx, vec![
/// ("Button 1", Box::new(|ctx: &mut Context| { /* some action */ })),
/// ("Button 2", Box::new(|ctx: &mut Context| { /* some action */ }))
/// ]);
/// ```
pub fn new(ctx: &mut Context, buttons: Vec<(&'static str, Box<Callback>)>) -> Self {
let buttons = buttons.into_iter().map(|(i, on_click)| IconButton::secondary(ctx, i, on_click)).collect();
IconButtonRow(Row::center(24.0), buttons)
}
}
impl IconButton {
/// Creates a new [`IconButton`] preset with a secondary style.
///
/// # Parameters
/// - `ctx`: The [`Context`] for accessing the app's theme.
/// - `icon`: The icon for the button, represented by it's name as a string.
/// - `on_click`: A [`Callback`] that will be executed when the button is clicked.
///
/// # Returns
/// - A new `IconButton` with a secondary style, large size, and default state.
pub fn secondary(
ctx: &mut Context,
icon: &'static str,
on_click: Box<Callback>
) -> Self {
IconButton::new(
ctx,
icon,
ButtonSize::Large,
ButtonStyle::Secondary,
ButtonState::Default,
on_click,
)
}
/// Creates a new `IconButton` preset for input fields.
///
/// # Parameters
/// - `ctx`: The [`Context`] for accessing the app's theme.
/// - `icon`: The icon for the button, represented as a string (e.g., a file name or path).
/// - `on_click`: A closure that will be executed when the button is clicked.
///
/// # Returns
/// - A new [`IconButton`] with style [`ButtonStyle::Secondary`], size [`ButtonSize::Medium`], and state [`ButtonState::Default`].
pub fn input(
ctx: &mut Context,
icon: &'static str,
on_click: impl FnMut(&mut Context) + 'static
) -> Self {
IconButton::new(
ctx,
icon,
ButtonSize::Medium,
ButtonStyle::Secondary,
ButtonState::Default,
Box::new(on_click),
)
}
/// Creates a new `IconButton` preset for use in a mobile keyboard.
///
/// # Parameters
/// - `ctx`: The [`Context`] for accessing the app's theme.
/// - `icon`: The icon for the button, represented as a string (e.g., a file name or path).
/// - `on_click`: A closure that will be executed when the button is clicked.
///
/// # Returns
/// - A new [`IconButton`] with style [`ButtonStyle::Ghost`], size [`ButtonSize::Medium`], and state [`ButtonState::Default`].
pub fn keyboard(
ctx: &mut Context,
icon: &'static str,
on_click: impl FnMut(&mut Context) + 'static
) -> Self {
IconButton::new(
ctx,
icon,
ButtonSize::Medium,
ButtonStyle::Ghost,
ButtonState::Default,
Box::new(on_click),
)
}
/// Creates a new [`IconButton`] preset for header navigation.
///
/// # Parameters
/// - `ctx`: The [`Context`] for accessing the app's theme.
/// - `icon`: The icon for the button, represented as a string (e.g., a file name or path).
/// - `on_click`: A closure that will be executed when the button is clicked.
///
/// # Returns
/// - A new [`IconButton`] with style [`ButtonStyle::Ghost`], size [`ButtonStyle::Medium`], and state [`ButtonState::Default`].
pub fn navigation(
ctx: &mut Context,
icon: &'static str,
on_click: impl FnMut(&mut Context) + 'static
) -> Self {
IconButton::new(
ctx,
icon,
ButtonSize::Medium,
ButtonStyle::Ghost,
ButtonState::Default,
Box::new(on_click),
)
}
/// Creates a new [`IconButton`] preset for closing a page.
///
/// # Parameters
/// - `ctx`: The [`Context`] for accessing the app's theme.
/// - `on_click`: A closure that will be executed when the button is clicked.
///
/// # Returns
/// - A new [`IconButton`] with style [`ButtonStyle::Ghost`], size [`ButtonSize::Medium`], and state [`ButtonState::Default`]. The icon is set to "close".
pub fn close(
ctx: &mut Context,
on_click: impl FnMut(&mut Context) + 'static
) -> Self {
IconButton::new(
ctx,
"close",
ButtonSize::Medium,
ButtonStyle::Ghost,
ButtonState::Default,
Box::new(on_click),
)
}
/// Creates a new [`IconButton`] preset for the app's mobile navigator.
///
/// # Parameters
/// - `ctx`: The [`Context`] for accessing the app's theme.
/// - `icon`: The name of the icon to display.
/// - `selected`: Indicates whether the icon is initially selected.
/// - `on_click`: A closure that will be executed when the button is clicked.
///
/// # Returns
/// - A new [`IconButton`] with style [`ButtonStyle::Ghost`], size [`ButtonSize::Medium`].
pub fn tab_nav(
ctx: &mut Context,
icon: &'static str,
selected: bool,
on_click: impl FnMut(&mut Context) + 'static,
) -> Self {
let state = if selected {ButtonState::Selected} else {ButtonState::UnSelected};
IconButton::new(
ctx,
icon,
ButtonSize::Medium,
ButtonStyle::Ghost,
state,
Box::new(on_click),
)
}
}
impl std::fmt::Debug for IconButton {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "IconButton(...)")
}
}