cosmic/widget/button/
text.rs1use super::{Builder, ButtonClass};
5use crate::widget::{icon, row, tooltip};
6use crate::{Apply, Element};
7use iced_core::{Alignment, Length, Padding, font::Weight, text::LineHeight, widget::Id};
8use std::borrow::Cow;
9
10pub type Button<'a, Message> = Builder<'a, Message, Text>;
11
12pub fn destructive<'a, Message>(label: impl Into<Cow<'a, str>>) -> Button<'a, Message> {
14 Button::new(Text::new())
15 .label(label)
16 .class(ButtonClass::Destructive)
17}
18
19pub fn suggested<'a, Message>(label: impl Into<Cow<'a, str>>) -> Button<'a, Message> {
21 Button::new(Text::new())
22 .label(label)
23 .class(ButtonClass::Suggested)
24}
25
26pub fn standard<'a, Message>(label: impl Into<Cow<'a, str>>) -> Button<'a, Message> {
28 Button::new(Text::new()).label(label)
29}
30
31pub fn text<'a, Message>(label: impl Into<Cow<'a, str>>) -> Button<'a, Message> {
33 Button::new(Text::new())
34 .label(label)
35 .class(ButtonClass::Text)
36}
37
38pub struct Text {
40 pub(super) leading_icon: Option<icon::Handle>,
41 pub(super) trailing_icon: Option<icon::Handle>,
42}
43
44impl Default for Text {
45 fn default() -> Self {
46 Self::new()
47 }
48}
49
50impl Text {
51 pub const fn new() -> Self {
52 Self {
53 leading_icon: None,
54 trailing_icon: None,
55 }
56 }
57}
58
59impl<Message> Button<'_, Message> {
60 pub fn new(text: Text) -> Self {
61 let guard = crate::theme::THEME.lock().unwrap();
62 let theme = guard.cosmic();
63 Self {
64 id: Id::unique(),
65 label: Cow::Borrowed(""),
66 #[cfg(feature = "a11y")]
67 name: Cow::Borrowed(""),
68 #[cfg(feature = "a11y")]
69 description: Cow::Borrowed(""),
70 tooltip: Cow::Borrowed(""),
71 on_press: None,
72 width: Length::Shrink,
73 height: Length::Fixed(theme.space_l().into()),
74 padding: Padding::from([0, theme.space_s()]),
75 spacing: theme.space_xxxs(),
76 icon_size: 16,
77 line_height: 20,
78 font_size: 14,
79 font_weight: Weight::Normal,
80 class: ButtonClass::Standard,
81 variant: text,
82 }
83 }
84
85 pub fn leading_icon(mut self, icon: impl Into<icon::Handle>) -> Self {
86 self.variant.leading_icon = Some(icon.into());
87 self
88 }
89
90 pub fn trailing_icon(mut self, icon: impl Into<icon::Handle>) -> Self {
91 self.variant.trailing_icon = Some(icon.into());
92 self
93 }
94}
95
96impl<'a, Message: Clone + 'static> From<Button<'a, Message>> for Element<'a, Message> {
97 fn from(mut builder: Button<'a, Message>) -> Element<'a, Message> {
98 let trailing_icon = builder
99 .variant
100 .trailing_icon
101 .map(crate::widget::icon::Handle::icon);
102
103 let leading_icon = builder
104 .variant
105 .leading_icon
106 .map(crate::widget::icon::Handle::icon);
107
108 let label: Option<Element<'_, _>> = (!builder.label.is_empty()).then(|| {
109 let font = crate::font::Font {
110 weight: builder.font_weight,
111 ..crate::font::default()
112 };
113
114 crate::widget::text(builder.label.to_string())
116 .size(builder.font_size)
117 .line_height(LineHeight::Absolute(builder.line_height.into()))
118 .font(font)
119 .into()
120 });
121
122 let mut button: super::Button<'a, Message> = row::with_capacity(3)
123 .push_maybe(leading_icon)
125 .push_maybe(label)
127 .push_maybe(trailing_icon)
129 .padding(builder.padding)
130 .width(builder.width)
131 .height(builder.height)
132 .spacing(builder.spacing)
133 .align_y(Alignment::Center)
134 .apply(super::custom)
135 .padding(0)
136 .id(builder.id)
137 .on_press_maybe(builder.on_press.take())
138 .class(builder.class);
139
140 #[cfg(feature = "a11y")]
141 {
142 if !builder.label.is_empty() {
143 button = button.name(builder.label)
144 }
145
146 button = button.description(builder.description);
147 }
148
149 if builder.tooltip.is_empty() {
150 button.into()
151 } else {
152 tooltip(
153 button,
154 crate::widget::text(builder.tooltip)
155 .size(builder.font_size)
156 .font(crate::font::Font {
157 weight: builder.font_weight,
158 ..crate::font::default()
159 }),
160 tooltip::Position::Top,
161 )
162 .into()
163 }
164 }
165}