1use crate::*;
2
3#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5pub enum SplitButtonSize {
6 ExtraSmall,
7 #[default]
8 Small,
9 Medium,
10 Large,
11 ExtraLarge,
12}
13
14#[derive(Clone, Copy, Debug)]
15pub struct SplitButtonOptions {
16 pub size: SplitButtonSize,
17 pub variant: ButtonVariant,
18 pub primary_enabled: bool,
19 pub menu_enabled: bool,
20 pub expanded: bool,
21 pub rtl: bool,
22 pub leading_icon: Option<MaterialIcon>,
23}
24impl Default for SplitButtonOptions {
25 fn default() -> Self {
26 Self {
27 size: SplitButtonSize::Small,
28 variant: ButtonVariant::Filled,
29 primary_enabled: true,
30 menu_enabled: true,
31 expanded: false,
32 rtl: false,
33 leading_icon: None,
34 }
35 }
36}
37
38impl Frame<'_> {
39 pub fn split_button(
42 &mut self,
43 id: u64,
44 bounds: Rect,
45 label: &str,
46 menu_label: &str,
47 options: Options,
48 ) -> Option<(Response, Response)> {
49 self.split_button_with(
50 id,
51 bounds,
52 label,
53 menu_label,
54 SplitButtonOptions {
55 variant: options.variant,
56 primary_enabled: options.enabled,
57 menu_enabled: options.enabled,
58 expanded: options.selected,
59 ..SplitButtonOptions::default()
60 },
61 )
62 }
63 pub fn split_button_with(
65 &mut self,
66 id: u64,
67 bounds: Rect,
68 label: &str,
69 menu_label: &str,
70 config: SplitButtonOptions,
71 ) -> Option<(Response, Response)> {
72 let (visual_height, inner_default, inner_interactive, icon_size): (f32, f32, f32, f32) =
73 match config.size {
74 SplitButtonSize::ExtraSmall => (32.0, 4.0, 8.0, 22.0),
75 SplitButtonSize::Small => (40.0, 4.0, 12.0, 22.0),
76 SplitButtonSize::Medium => (56.0, 4.0, 12.0, 26.0),
77 SplitButtonSize::Large => (96.0, 8.0, 20.0, 38.0),
78 SplitButtonSize::ExtraLarge => (136.0, 12.0, 20.0, 50.0),
79 };
80 let hit_height = visual_height.max(48.0);
81 let menu_width = hit_height;
82 if !bounds.x.is_finite()
83 || !bounds.y.is_finite()
84 || !bounds.width.is_finite()
85 || !bounds.height.is_finite()
86 || bounds.width < menu_width + 50.0
87 || bounds.height < hit_height
88 || id == u64::MAX
89 {
90 return None;
91 }
92 let lead = Rect {
93 x: bounds.x + if config.rtl { menu_width + 2.0 } else { 0.0 },
94 width: bounds.width - menu_width - 2.0,
95 height: hit_height,
96 ..bounds
97 };
98 let trail = Rect {
99 x: if config.rtl {
100 bounds.x
101 } else {
102 lead.x + lead.width + 2.0
103 },
104 width: menu_width,
105 height: hit_height,
106 ..bounds
107 };
108 let primary = self.target(id, lead, label, config.primary_enabled, Role::Button);
109 let menu_description = format!(
110 "{}, {}",
111 menu_label,
112 if config.expanded {
113 "expanded"
114 } else {
115 "collapsed"
116 }
117 );
118 let menu = self.target(
119 id + 1,
120 trail,
121 &menu_description,
122 config.menu_enabled,
123 Role::Button,
124 );
125 let t = self.ui.theme;
126 for (i, r) in [primary, menu].into_iter().enumerate() {
127 let b = Rect {
128 y: r.bounds.y + (hit_height - visual_height) / 2.0,
129 height: visual_height,
130 ..r.bounds
131 };
132 let interactive = r.pressed || r.hovered;
133 let outer = visual_height / 2.0;
134 let target = if r.pressed {
135 inner_interactive
136 } else if i == 1 && config.expanded {
137 outer
138 } else if interactive {
139 inner_interactive
140 } else {
141 inner_default
142 };
143 let inner = self
144 .spatial(r.id, 60, target / outer, inner_default / outer)
145 .clamp(0.0, 1.0)
146 * outer;
147 let outside_left = (i == 0) != config.rtl;
148 let (left, right) = if outside_left {
149 (outer, inner)
150 } else {
151 (inner, outer)
152 };
153 let (base, foreground) = if !r.enabled {
154 if config.variant == ButtonVariant::Outlined {
155 (t.background, t.on_surface_variant.opacity(0.38))
156 } else {
157 (t.text.opacity(0.12), t.text.opacity(0.38))
158 }
159 } else {
160 match config.variant {
161 ButtonVariant::Tonal => (t.secondary, t.on_secondary),
162 ButtonVariant::Outlined => (t.background, t.primary),
163 ButtonVariant::Elevated => (t.surface_low, t.primary),
164 _ => (t.primary, t.on_primary),
165 }
166 };
167 if r.enabled && config.variant == ButtonVariant::Elevated {
168 self.connected_elevation(b, left, right, 1);
169 }
170 if config.variant == ButtonVariant::Outlined {
171 self.paint_connected_outline(
172 b,
173 left,
174 right,
175 if r.enabled {
176 t.outline
177 } else {
178 t.outline.opacity(0.1)
179 },
180 );
181 } else {
182 self.paint_connected_halves(b, left, right, base);
183 }
184 if i == 1 && config.expanded {
187 self.paint_connected_halves(b, left, right, foreground.opacity(0.12));
188 }
189 let hover = self.effect(r.id, 0, f32::from(r.hovered), 0.0);
190 let alpha = if r.pressed || (r.focused && self.ui.keyboard_focus) {
191 0.12
192 } else {
193 0.08 * hover
194 };
195 if r.enabled && alpha > 0.001 {
196 self.paint_connected_halves(b, left, right, foreground.opacity(alpha));
197 }
198 self.connected_ink(r, b, left, right, foreground);
199 self.focus_ring(r);
200 if i == 0 {
201 if let Some(icon) = config.leading_icon {
202 let glyph_sz = (icon_size * 0.85).min(b.height - 6.0).max(14.0);
203 let gap = 8.0;
204 let text_w = self.text_width(label, TextStyle::LABEL);
205 let total_w = (glyph_sz + gap + text_w).min((b.width - 16.0).max(glyph_sz));
206 let start_x = b.x + (b.width - total_w) * 0.5;
207 let icon_x = if config.rtl { start_x + total_w - glyph_sz } else { start_x };
208 let text_x = if config.rtl { start_x } else { start_x + glyph_sz + gap };
209 self.paint_material_icon(
210 Rect {
211 x: icon_x,
212 y: b.y + (b.height - glyph_sz) * 0.5,
213 width: glyph_sz,
214 height: glyph_sz,
215 },
216 icon,
217 foreground,
218 );
219 self.styled_text(
220 Rect { x: text_x, width: (total_w - glyph_sz - gap).max(0.0), ..b },
221 label,
222 foreground,
223 TextStyle { align: if config.rtl { 2 } else { 0 }, padding: 0.0, ..TextStyle::LABEL },
224 );
225 } else {
226 self.styled_text(
227 b,
228 label,
229 foreground,
230 TextStyle {
231 align: 1,
232 ..TextStyle::LABEL
233 },
234 );
235 }
236 } else {
237 let rotation = self
238 .spatial(
239 r.id,
240 61,
241 f32::from(config.expanded),
242 f32::from(config.expanded),
243 )
244 .clamp(0.0, 1.0)
245 * std::f32::consts::PI;
246 let (sin, cos) = rotation.sin_cos();
247 let cx = b.x + b.width / 2.0;
248 let cy = b.y + b.height / 2.0;
249 let transform = |x: f32, y: f32| [cx + x * cos - y * sin, cy + x * sin + y * cos];
250 let scale = icon_size / 22.0;
251 self.paint_line(
252 transform(-5.0 * scale, -2.5 * scale),
253 transform(0.0, 2.5 * scale),
254 2.0 * scale,
255 foreground,
256 );
257 self.paint_line(
258 transform(0.0, 2.5 * scale),
259 transform(5.0 * scale, -2.5 * scale),
260 2.0 * scale,
261 foreground,
262 );
263 }
264 }
265 Some((primary, menu))
266 }
267}
268
269#[cfg(test)]
270#[path = "split_button.spec.rs"]
271mod tests;