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    /// A name for screen reader support
73    #[cfg(feature = "a11y")]
74    #[setters(into)]
75    name: Cow<'a, str>,
76
77    /// A description for screen reader support
78    #[cfg(feature = "a11y")]
79    #[setters(into)]
80    description: Cow<'a, str>,
81
82    // Adds a tooltip to the button.
83    #[setters(into)]
84    tooltip: Cow<'a, str>,
85
86    /// Sets the message that will be produced when the button is pressed.
87    ///
88    /// If `None`, the button will be disabled.
89    #[setters(strip_option)]
90    on_press: Option<Message>,
91
92    /// Sets the preferred width of the button.
93    #[setters(into)]
94    width: Length,
95
96    /// Sets the preferred height of the button.
97    #[setters(into)]
98    height: Length,
99
100    /// Sets the preferred padding of the button.
101    #[setters(into)]
102    padding: Padding,
103
104    /// Sets the preferred spacing between elements in the button.
105    spacing: u16,
106
107    /// Sets the preferred size of icons.
108    icon_size: u16,
109
110    /// Sets the prefered font line height.
111    line_height: u16,
112
113    /// Sets the preferred font size.
114    font_size: u16,
115
116    /// Sets the preferred font weight.
117    font_weight: Weight,
118
119    /// The preferred style of the button.
120    class: ButtonClass,
121
122    #[setters(skip)]
123    variant: Variant,
124}
125
126impl<Message, Variant> Builder<'_, Message, Variant> {
127    /// Set the value of [`on_press`] as either `Some` or `None`.
128    pub fn on_press_maybe(mut self, on_press: Option<Message>) -> Self {
129        self.on_press = on_press;
130        self
131    }
132}