cosmic/widget/button/
mod.rs

1// Copyright 2023 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4//! Button widgets for COSMIC applications.
5
6pub use crate::theme::Button as ButtonClass;
7
8pub mod link;
9use derive_setters::Setters;
10#[doc(inline)]
11pub use link::Button as LinkButton;
12#[doc(inline)]
13pub use link::link;
14
15mod icon;
16#[doc(inline)]
17pub use icon::Button as IconButton;
18#[doc(inline)]
19pub use icon::icon;
20
21mod image;
22#[doc(inline)]
23pub use image::Button as ImageButton;
24#[doc(inline)]
25pub use image::image;
26
27mod style;
28#[doc(inline)]
29pub use style::{Catalog, Style};
30
31mod text;
32#[doc(inline)]
33pub use text::Button as TextButton;
34#[doc(inline)]
35pub use text::{destructive, standard, suggested, text};
36
37mod widget;
38#[doc(inline)]
39pub use widget::{Button, draw, focus, layout, mouse_interaction};
40
41use iced_core::font::Weight;
42use iced_core::widget::Id;
43use iced_core::{Length, Padding};
44use std::borrow::Cow;
45
46/// A button with a custom element for its content.
47pub fn custom<'a, Message: Clone + 'a>(
48    content: impl Into<crate::Element<'a, Message>>,
49) -> Button<'a, Message> {
50    Button::new(content.into())
51}
52
53/// An image button which may contain any widget as its content.
54pub fn custom_image_button<'a, Message: Clone + 'a>(
55    content: impl Into<crate::Element<'a, Message>>,
56    on_remove: Option<Message>,
57) -> Button<'a, Message> {
58    Button::new_image(content.into(), on_remove)
59}
60
61/// A builder for constructing a custom [`Button`].
62#[must_use]
63#[derive(Setters)]
64pub struct Builder<'a, Message, Variant> {
65    /// Sets the [`Id`] of the button.
66    id: Id,
67
68    /// The label to display within the button.
69    #[setters(into)]
70    label: Cow<'a, str>,
71
72    // Adds a tooltip to the button.
73    #[setters(into)]
74    tooltip: Cow<'a, str>,
75
76    /// Sets the message that will be produced when the button is pressed.
77    ///
78    /// If `None`, the button will be disabled.
79    #[setters(strip_option)]
80    on_press: Option<Message>,
81
82    /// Sets the preferred width of the button.
83    #[setters(into)]
84    width: Length,
85
86    /// Sets the preferred height of the button.
87    #[setters(into)]
88    height: Length,
89
90    /// Sets the preferred padding of the button.
91    #[setters(into)]
92    padding: Padding,
93
94    /// Sets the preferred spacing between elements in the button.
95    spacing: u16,
96
97    /// Sets the preferred size of icons.
98    icon_size: u16,
99
100    /// Sets the prefered font line height.
101    line_height: u16,
102
103    /// Sets the preferred font size.
104    font_size: u16,
105
106    /// Sets the preferred font weight.
107    font_weight: Weight,
108
109    /// The preferred style of the button.
110    class: ButtonClass,
111
112    #[setters(skip)]
113    variant: Variant,
114}
115
116impl<Message, Variant> Builder<'_, Message, Variant> {
117    /// Set the value of [`on_press`] as either `Some` or `None`.
118    pub fn on_press_maybe(mut self, on_press: Option<Message>) -> Self {
119        self.on_press = on_press;
120        self
121    }
122}