pelican_ui 1.0.6

A modern UI/UX toolkit optimized for the RAMP stack.
Documentation
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use rust_on_rails::prelude::*;
use crate::ElementID;
use crate::components::avatar::{Avatar, AvatarContent};
use crate::elements::images::Icon;
use crate::elements::shapes::OutlinedRectangle;
use crate::elements::text::{Text, TextStyle};
use crate::events::RemoveContactEvent;
use crate::layout::{Offset, Padding, Row, Size, Stack, Wrap};

use super::{ButtonSize, ButtonState, ButtonStyle};

/// Defines the width behavior for the button.
#[derive(Debug, Clone, Copy)]
pub enum ButtonWidth {
    /// The button expands to fill the available space.
    Expand,
    
    /// The button's width hugs its content.
    Hug,
}

/// A clickable button component with customizable content, size, and styles.
#[derive(Component)]
pub struct Button(
    Stack, 
    OutlinedRectangle, 
    ButtonContent, 
    #[skip] ButtonStyle, 
    #[skip] ButtonState,
    #[skip] pub Box<dyn FnMut(&mut Context)>, 
);

impl Button {
    /// Creates a new `Button` component.
    ///
    /// # Parameters:
    /// - `ctx`: The [`Context`] for accessing the app's theme.
    /// - `avatar`: An optional [`Avatar`] to display inside the button.
    /// - `icon_l`: An optional [`Icon`] to display on the left side of the button.
    /// - `label`: An optional [`Text`] label.
    /// - `icon_r`: An optional [`Icon`] to display on the right side of the button.
    /// - `size`: Defines the size of the button.
    /// - `width`: Defines the width of the button.
    /// - `style`: Defines the style of the button.
    /// - `state`: Specifies the initial state of the button.
    /// - `offset`: Specifies the button's content's offset. (usually [`Offset::Center`])
    /// - `on_click`: A closure that will be executed when the button is clicked.
    ///
    /// # Returns:
    /// A UI ready [`Button`] instance.
    ///
    /// # Example:
    /// ```
    /// let button = Button::new(
    ///     ctx, 
    ///     Some(avatar_data),
    ///     Some("left"),
    ///     Some("Click Me"),
    ///     Some("right"),
    ///     ButtonSize::Medium,
    ///     ButtonWidth::Hug,
    ///     ButtonStyle::Primary,
    ///     ButtonState::Default,
    ///     Offset::Center,
    ///     |ctx: &mut Context| println!("Button clicked!"),
    /// );
    /// ```
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        ctx: &mut Context,
        avatar: Option<AvatarContent>,
        icon_l: Option<&'static str>,
        label: Option<&'static str>,
        icon_r: Option<&'static str>,
        size: ButtonSize,
        width: ButtonWidth,
        style: ButtonStyle,
        state: ButtonState,
        offset: Offset,
        on_click: impl FnMut(&mut Context) + 'static,
    ) -> Self {
        let (height, padding) = size.background();
        let colors = state.color(ctx, style);
        let content = ButtonContent::new(ctx, avatar, icon_l, label, icon_r, size, colors.label, padding);

        let width = match width {
            ButtonWidth::Hug => Size::custom(move |widths: Vec<(f32, f32)>|
                (widths[1].0, widths[1].1)
            ),
            ButtonWidth::Expand => Size::custom(move |widths: Vec<(f32, f32)>|
                (widths[1].0, f32::MAX)
            ),
        };

        let background = OutlinedRectangle::new(colors.background, colors.outline, height/2.0, 1.0);
        let layout = Stack(offset, Offset::Center, width, Size::Static(height), Padding::default());

        Button(layout, background, content, style, state, Box::new(on_click))
    }

    /// Updates the color of the button based on its current state and style.
    pub fn color(&mut self, ctx: &mut Context) {
        let colors = self.4.color(ctx, self.3);
        self.2.set_color(colors.label);
        *self.1.outline() = colors.outline;
        *self.1.background() = colors.background;
    }

    /// Returns a mutable reference to the current state of the button.
    pub fn status(&mut self) -> &mut ButtonState {&mut self.4}
}

impl OnEvent for Button {
    fn on_event(&mut self, ctx: &mut Context, event: &mut dyn Event) -> bool {
        if let Some(event) = event.downcast_ref::<MouseEvent>() {
            if self.4.handle(ctx, *event).is_some() {
                self.color(ctx);
            }

            if let MouseEvent{state: MouseState::Pressed, position: Some(_)} = event {
                match self.4 {
                    ButtonState::Default | ButtonState::Hover | ButtonState::Pressed => (self.5)(ctx),
                    _ => {}
                }
            }
        }
        false
    }
}

impl std::fmt::Debug for Button {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Button")
    }
}

#[derive(Debug, Component)]
struct ButtonContent(Row, Option<Avatar>, Option<Image>, Option<Text>, Option<Image>);
impl OnEvent for ButtonContent {}

impl ButtonContent {
    #[allow(clippy::too_many_arguments)]
    fn new(
        ctx: &mut Context,
        avatar: Option<AvatarContent>,
        icon_l: Option<&'static str>,
        label: Option<&'static str>,
        icon_r: Option<&'static str>,
        size: ButtonSize,
        color: Color,
        padding: f32,
    ) -> Self {
        let (text_size, icon_size, spacing) = size.content(ctx);
        ButtonContent(
            Row(spacing, Offset::Center, Size::Fit, Padding(padding, 0.0, padding, 0.0)),
            avatar.map(|content| Avatar::new(ctx, content, None, false, icon_size, None)),
            icon_l.map(|icon| Icon::new(ctx, icon, color, icon_size)),
            label.map(|label| Text::new(ctx, label, TextStyle::Label(color), text_size, Align::Left)),
            icon_r.map(|icon| Icon::new(ctx, icon, color, icon_size)),
        )
    }

    fn set_color(&mut self, color: Color) {
        if let Some(icon) = &mut self.2 { icon.color = Some(color); }
        if let Some(text) = &mut self.3 { text.text().set_color(color); }
        if let Some(icon) = &mut self.4 { icon.color = Some(color); }
    }
}

impl Button {
    /// Creates a primary style button. Typically used for the main actions in the UI.
    ///
    /// # Parameters
    /// - `ctx`: The [`Context`] for accessing the app's theme.
    /// - `label`: The text displayed on the button.
    /// - `on_click`:  A closure that will be executed when the button is clicked.
    ///
    /// # Returns
    /// A [`Button`] with style [`ButtonStyle::Primary`] and state [`ButtonState::Default`].
    pub fn primary (
        ctx: &mut Context,
        label: &'static str,
        on_click: impl FnMut(&mut Context) + 'static,
    ) -> Self {
        Button::new(
            ctx,
            None,
            None,
            Some(label),
            None,
            ButtonSize::Large,
            ButtonWidth::Expand,
            ButtonStyle::Primary,
            ButtonState::Default,
            Offset::Center,
            on_click,
        )
    }

    /// Creates a secondary style button with optional left and right icons.
    ///
    /// # Parameters
    /// - `ctx`: The [`Context`] for accessing the app's theme.
    /// - `icon_l`: An optional [`Icon`] to display on the left side of the button.
    /// - `label`: The text displayed on the button.
    /// - `icon_r`: An optional [`Icon`] to display on the right side of the button.
    /// - `on_click`: A closure that will be executed when the button is clicked.
    ///
    /// # Returns
    /// A [`Button`] with style [`ButtonStyle::Secondary`] and state [`ButtonState::Default`].
    pub fn secondary(
        ctx: &mut Context,
        icon_l: Option<&'static str>,
        label: &'static str,
        icon_r: Option<&'static str>,
        on_click: impl FnMut(&mut Context) + 'static,
    ) -> Self {
        Button::new(
            ctx,
            None,
            icon_l,
            Some(label),
            icon_r,
            ButtonSize::Medium,
            ButtonWidth::Hug,
            ButtonStyle::Secondary,
            ButtonState::Default,
            Offset::Center,
            on_click,
        )
    }

    /// Creates a ghost style button, typically used for non-intrusive actions.
    ///
    /// # Parameters
    /// - `ctx`: The [`Context`] for accessing the app's theme.
    /// - `label`: The text displayed on the button.
    /// - `on_click` A closure that will be executed when the button is clicked.
    ///
    /// # Returns
    /// A [`Button`] with style [`ButtonStyle::Ghost`] and state [`ButtonState::Default`].
    pub fn ghost(
        ctx: &mut Context,
        label: &'static str,
        on_click: impl FnMut(&mut Context) + 'static,
    ) -> Self {
        Button::new(
            ctx,
            None,
            None,
            Some(label),
            None,
            ButtonSize::Medium,
            ButtonWidth::Hug,
            ButtonStyle::Ghost,
            ButtonState::Default,
            Offset::Center,
            on_click,
        )
    }

    /// Creates a disabled button, which cannot be interacted with.
    ///
    /// # Parameters
    /// - `ctx`: The [`Context`] for accessing the app's theme.
    /// - `label`: The text displayed on the button.
    /// - `on_click`: A closure that defines the action when the button is clicked (this will not be triggered as the button is disabled).
    ///
    /// # Returns
    /// A [`Button`] with style [`ButtonStyle::Primary`] and state [`ButtonState::Disabled`].
    pub fn disabled(
        ctx: &mut Context,
        label: &'static str,
        on_click: impl FnMut(&mut Context) + 'static,
    ) -> Self {
        Button::new(
            ctx,
            None,
            None,
            Some(label),
            None,
            ButtonSize::Large,
            ButtonWidth::Expand,
            ButtonStyle::Primary,
            ButtonState::Disabled,
            Offset::Center,
            on_click,
        )
    }

    /// Creates a numeric keypad style button, typically used for numbers or symbols on a keypad.
    ///
    /// # Parameters
    /// - `ctx`: The [`Context`] for accessing the app's theme.
    /// - `label`: The text displayed on the button.
    /// - `icon`: An optional icon displayed on the button.
    /// - `on_click` A closure that will be executed when the button is clicked.
    ///
    /// # Returns
    /// A [`Button`] with style [`ButtonStyle::Ghost`], state [`ButtonState::Default`], and size [`ButtonSize::Large`].
    pub fn keypad(
        ctx: &mut Context,
        label: Option<&'static str>,
        icon: Option<&'static str>,
        on_click: impl FnMut(&mut Context) + 'static,
    ) -> Self {
        Button::new(
            ctx,
            None,
            icon,
            label,
            None,
            ButtonSize::Large,
            ButtonWidth::Expand,
            ButtonStyle::Ghost,
            ButtonState::Default,
            Offset::Center,
            on_click,
        )
    }

    /// Creates a navigation button for desktop-style navigators, with optional selection.
    ///
    /// # Parameters
    /// - `ctx`: The [`Context`] for accessing the app's theme.
    /// - `icon`: The icon to display on the button.
    /// - `label`: The text displayed on the button.
    /// - `selected`: A flag that determines if the button should be in the selected state.
    /// - `on_click` A closure that will be executed when the button is clicked.
    ///
    /// # Returns
    /// A [`Button`] with style [`ButtonStyle::Ghost`] and either state [`ButtonState::Selected`] or [`ButtonState::Default`].
    pub fn navigation(
        ctx: &mut Context,
        icon: &'static str,
        label: &'static str,
        selected: bool,
        on_click: impl FnMut(&mut Context) + 'static,
    ) -> Self {
        Button::new(
            ctx,
            None,
            Some(icon),
            Some(label),
            None,
            ButtonSize::Large,
            ButtonWidth::Expand,
            ButtonStyle::Ghost,
            if selected {ButtonState::Selected} else {ButtonState::Default},
            Offset::Start,
            on_click,
        )
    }

    /// Creates a profile photo button for desktop-style navigation with a photo.
    ///
    /// # Parameters
    /// - `ctx`: The [`Context`] for accessing the app's theme.
    /// - `label`: The text displayed on the button.
    /// - `photo`: The photo or avatar content for the button.
    /// - `selected`: A flag that determines if the button should be in the pressed state.
    /// - `on_click` A closure that will be executed when the button is clicked.
    ///
    /// # Returns
    /// A [`Button`] with style [`ButtonStyle::Ghost`] and either state [`ButtonState::Selected`] or [`ButtonState::Default`].
    pub fn photo(
        ctx: &mut Context,
        label: &'static str,
        photo: AvatarContent,
        selected: bool,
        on_click: impl FnMut(&mut Context) + 'static,
    ) -> Self {
        Button::new(
            ctx,
            Some(photo),
            None,
            Some(label),
            None,
            ButtonSize::Large,
            ButtonWidth::Expand,
            ButtonStyle::Ghost,
            if selected {ButtonState::Selected} else {ButtonState::Default},
            Offset::Start,
            on_click,
        )
    }

    /// Creates a close page button, typically used for closing dialogs or pages.
    ///
    /// # Parameters
    /// - `ctx`: The [`Context`] for accessing the app's theme.
    /// - `label`: The text displayed on the button.
    /// - `on_click` A closure that will be executed when the button is clicked.
    ///
    /// # Returns
    /// A [`Button`] with style [`ButtonStyle::Secondary`] and state [`ButtonState::Default`].
    pub fn close(
        ctx: &mut Context,
        label: &'static str,
        on_click: impl FnMut(&mut Context) + 'static,
    ) -> Self {
        Button::new(
            ctx,
            None,
            None,
            Some(label),
            None,
            ButtonSize::Large,
            ButtonWidth::Expand,
            ButtonStyle::Secondary,
            ButtonState::Default,
            Offset::Center,
            on_click,
        )
    }
}

/// A component that represents a set of quick action buttons displayed in a wrap layout.
///
/// The [`QuickActions`] component is used to display multiple buttons in a flexible wrap layout, where the buttons
/// can be customized and interact with various events. The component is designed to allow easy addition and removal
/// of buttons. Each button is displayed with a default margin between them for easy organization.
///
/// # Fields
/// - `Wrap`: A layout component that arranges the buttons in a wrap style, with adjustable spacing and alignment.
/// - `Vec<Button>`: A vector of buttons that represent the quick actions. Each button can trigger different actions when clicked.
///
/// # Example
/// ```rust
/// let quick_actions = QuickActions::new(vec![Button::primary(...), Button::secondary(...)]);
/// ```
#[derive(Debug, Component)]
pub struct QuickActions(Wrap, Vec<Button>);

impl OnEvent for QuickActions {}

impl QuickActions {
    /// Creates a new `QuickActions` component with a list of action buttons.
    pub fn new(buttons: Vec<Button>) -> Self {
        QuickActions(Wrap(8.0, 8.0, Offset::Start, Offset::Center, Padding::default()), buttons)
    }
}

/// A component that represents a button for deselecting or removing a contact.
#[derive(Debug, Component)]
pub struct QuickDeselectButton(Stack, Button, #[skip] ElementID);

impl OnEvent for QuickDeselectButton {}

impl QuickDeselectButton {
    /// Creates a new [`QuickDeselectButton`] component with a remove contact action.
    pub fn new(ctx: &mut Context, name: &'static str, id: ElementID) -> Self {
        let button = Button::secondary(ctx, None, name, Some("close"), move |ctx: &mut Context| ctx.trigger_event(RemoveContactEvent(id)));
        QuickDeselectButton(Stack::default(), button, id)
    }

    /// Returns the [`ElementID`] of the contact associated with the button.
    pub fn id(&self) -> ElementID {self.2}
}