1use crate::core::alignment;
60use crate::core::border::{self, Border};
61use crate::core::layout;
62use crate::core::mouse;
63use crate::core::renderer;
64use crate::core::text;
65use crate::core::touch;
66use crate::core::widget;
67use crate::core::widget::tree::{self, Tree};
68use crate::core::window;
69use crate::core::{
70 Background, Clipboard, Color, Element, Event, Layout, Length, Pixels,
71 Rectangle, Shell, Size, Theme, Widget,
72};
73
74#[allow(missing_debug_implementations)]
133pub struct Radio<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer>
134where
135 Theme: Catalog,
136 Renderer: text::Renderer,
137{
138 is_selected: bool,
139 on_click: Message,
140 label: String,
141 width: Length,
142 size: f32,
143 spacing: f32,
144 text_size: Option<Pixels>,
145 text_line_height: text::LineHeight,
146 text_shaping: text::Shaping,
147 text_wrapping: text::Wrapping,
148 font: Option<Renderer::Font>,
149 class: Theme::Class<'a>,
150 last_status: Option<Status>,
151}
152
153impl<'a, Message, Theme, Renderer> Radio<'a, Message, Theme, Renderer>
154where
155 Message: Clone,
156 Theme: Catalog,
157 Renderer: text::Renderer,
158{
159 pub const DEFAULT_SIZE: f32 = 16.0;
161
162 pub const DEFAULT_SPACING: f32 = 8.0;
164
165 pub fn new<F, V>(
174 label: impl Into<String>,
175 value: V,
176 selected: Option<V>,
177 f: F,
178 ) -> Self
179 where
180 V: Eq + Copy,
181 F: FnOnce(V) -> Message,
182 {
183 Radio {
184 is_selected: Some(value) == selected,
185 on_click: f(value),
186 label: label.into(),
187 width: Length::Shrink,
188 size: Self::DEFAULT_SIZE,
189 spacing: Self::DEFAULT_SPACING,
190 text_size: None,
191 text_line_height: text::LineHeight::default(),
192 text_shaping: text::Shaping::default(),
193 text_wrapping: text::Wrapping::default(),
194 font: None,
195 class: Theme::default(),
196 last_status: None,
197 }
198 }
199
200 pub fn size(mut self, size: impl Into<Pixels>) -> Self {
202 self.size = size.into().0;
203 self
204 }
205
206 pub fn width(mut self, width: impl Into<Length>) -> Self {
208 self.width = width.into();
209 self
210 }
211
212 pub fn spacing(mut self, spacing: impl Into<Pixels>) -> Self {
214 self.spacing = spacing.into().0;
215 self
216 }
217
218 pub fn text_size(mut self, text_size: impl Into<Pixels>) -> Self {
220 self.text_size = Some(text_size.into());
221 self
222 }
223
224 pub fn text_line_height(
226 mut self,
227 line_height: impl Into<text::LineHeight>,
228 ) -> Self {
229 self.text_line_height = line_height.into();
230 self
231 }
232
233 pub fn text_shaping(mut self, shaping: text::Shaping) -> Self {
235 self.text_shaping = shaping;
236 self
237 }
238
239 pub fn text_wrapping(mut self, wrapping: text::Wrapping) -> Self {
241 self.text_wrapping = wrapping;
242 self
243 }
244
245 pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self {
247 self.font = Some(font.into());
248 self
249 }
250
251 #[must_use]
253 pub fn style(mut self, style: impl Fn(&Theme, Status) -> Style + 'a) -> Self
254 where
255 Theme::Class<'a>: From<StyleFn<'a, Theme>>,
256 {
257 self.class = (Box::new(style) as StyleFn<'a, Theme>).into();
258 self
259 }
260
261 #[cfg(feature = "advanced")]
263 #[must_use]
264 pub fn class(mut self, class: impl Into<Theme::Class<'a>>) -> Self {
265 self.class = class.into();
266 self
267 }
268}
269
270impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer>
271 for Radio<'_, Message, Theme, Renderer>
272where
273 Message: Clone,
274 Theme: Catalog,
275 Renderer: text::Renderer,
276{
277 fn tag(&self) -> tree::Tag {
278 tree::Tag::of::<widget::text::State<Renderer::Paragraph>>()
279 }
280
281 fn state(&self) -> tree::State {
282 tree::State::new(widget::text::State::<Renderer::Paragraph>::default())
283 }
284
285 fn size(&self) -> Size<Length> {
286 Size {
287 width: self.width,
288 height: Length::Shrink,
289 }
290 }
291
292 fn layout(
293 &self,
294 tree: &mut Tree,
295 renderer: &Renderer,
296 limits: &layout::Limits,
297 ) -> layout::Node {
298 layout::next_to_each_other(
299 &limits.width(self.width),
300 self.spacing,
301 |_| layout::Node::new(Size::new(self.size, self.size)),
302 |limits| {
303 let state = tree
304 .state
305 .downcast_mut::<widget::text::State<Renderer::Paragraph>>();
306
307 widget::text::layout(
308 state,
309 renderer,
310 limits,
311 self.width,
312 Length::Shrink,
313 &self.label,
314 self.text_line_height,
315 self.text_size,
316 self.font,
317 text::Alignment::Default,
318 alignment::Vertical::Top,
319 self.text_shaping,
320 self.text_wrapping,
321 )
322 },
323 )
324 }
325
326 fn update(
327 &mut self,
328 _state: &mut Tree,
329 event: &Event,
330 layout: Layout<'_>,
331 cursor: mouse::Cursor,
332 _renderer: &Renderer,
333 _clipboard: &mut dyn Clipboard,
334 shell: &mut Shell<'_, Message>,
335 _viewport: &Rectangle,
336 ) {
337 match event {
338 Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
339 | Event::Touch(touch::Event::FingerPressed { .. }) => {
340 if cursor.is_over(layout.bounds()) {
341 shell.publish(self.on_click.clone());
342 shell.capture_event();
343 }
344 }
345 _ => {}
346 }
347
348 let current_status = {
349 let is_mouse_over = cursor.is_over(layout.bounds());
350 let is_selected = self.is_selected;
351
352 if is_mouse_over {
353 Status::Hovered { is_selected }
354 } else {
355 Status::Active { is_selected }
356 }
357 };
358
359 if let Event::Window(window::Event::RedrawRequested(_now)) = event {
360 self.last_status = Some(current_status);
361 } else if self
362 .last_status
363 .is_some_and(|last_status| last_status != current_status)
364 {
365 shell.request_redraw();
366 }
367 }
368
369 fn mouse_interaction(
370 &self,
371 _state: &Tree,
372 layout: Layout<'_>,
373 cursor: mouse::Cursor,
374 _viewport: &Rectangle,
375 _renderer: &Renderer,
376 ) -> mouse::Interaction {
377 if cursor.is_over(layout.bounds()) {
378 mouse::Interaction::Pointer
379 } else {
380 mouse::Interaction::default()
381 }
382 }
383
384 fn draw(
385 &self,
386 tree: &Tree,
387 renderer: &mut Renderer,
388 theme: &Theme,
389 defaults: &renderer::Style,
390 layout: Layout<'_>,
391 _cursor: mouse::Cursor,
392 viewport: &Rectangle,
393 ) {
394 let mut children = layout.children();
395
396 let style = theme.style(
397 &self.class,
398 self.last_status.unwrap_or(Status::Active {
399 is_selected: self.is_selected,
400 }),
401 );
402
403 {
404 let layout = children.next().unwrap();
405 let bounds = layout.bounds();
406
407 let size = bounds.width;
408 let dot_size = size / 2.0;
409
410 renderer.fill_quad(
411 renderer::Quad {
412 bounds,
413 border: Border {
414 radius: (size / 2.0).into(),
415 width: style.border_width,
416 color: style.border_color,
417 },
418 ..renderer::Quad::default()
419 },
420 style.background,
421 );
422
423 if self.is_selected {
424 renderer.fill_quad(
425 renderer::Quad {
426 bounds: Rectangle {
427 x: bounds.x + dot_size / 2.0,
428 y: bounds.y + dot_size / 2.0,
429 width: bounds.width - dot_size,
430 height: bounds.height - dot_size,
431 },
432 border: border::rounded(dot_size / 2.0),
433 ..renderer::Quad::default()
434 },
435 style.dot_color,
436 );
437 }
438 }
439
440 {
441 let label_layout = children.next().unwrap();
442 let state: &widget::text::State<Renderer::Paragraph> =
443 tree.state.downcast_ref();
444
445 crate::text::draw(
446 renderer,
447 defaults,
448 label_layout,
449 state.0.raw(),
450 crate::text::Style {
451 color: style.text_color,
452 },
453 viewport,
454 );
455 }
456 }
457}
458
459impl<'a, Message, Theme, Renderer> From<Radio<'a, Message, Theme, Renderer>>
460 for Element<'a, Message, Theme, Renderer>
461where
462 Message: 'a + Clone,
463 Theme: 'a + Catalog,
464 Renderer: 'a + text::Renderer,
465{
466 fn from(
467 radio: Radio<'a, Message, Theme, Renderer>,
468 ) -> Element<'a, Message, Theme, Renderer> {
469 Element::new(radio)
470 }
471}
472
473#[derive(Debug, Clone, Copy, PartialEq, Eq)]
475pub enum Status {
476 Active {
478 is_selected: bool,
480 },
481 Hovered {
483 is_selected: bool,
485 },
486}
487
488#[derive(Debug, Clone, Copy, PartialEq)]
490pub struct Style {
491 pub background: Background,
493 pub dot_color: Color,
495 pub border_width: f32,
497 pub border_color: Color,
499 pub text_color: Option<Color>,
501}
502
503pub trait Catalog {
505 type Class<'a>;
507
508 fn default<'a>() -> Self::Class<'a>;
510
511 fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
513}
514
515pub type StyleFn<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Style + 'a>;
517
518impl Catalog for Theme {
519 type Class<'a> = StyleFn<'a, Self>;
520
521 fn default<'a>() -> Self::Class<'a> {
522 Box::new(default)
523 }
524
525 fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
526 class(self, status)
527 }
528}
529
530pub fn default(theme: &Theme, status: Status) -> Style {
532 let palette = theme.extended_palette();
533
534 let active = Style {
535 background: Color::TRANSPARENT.into(),
536 dot_color: palette.primary.strong.color,
537 border_width: 1.0,
538 border_color: palette.primary.strong.color,
539 text_color: None,
540 };
541
542 match status {
543 Status::Active { .. } => active,
544 Status::Hovered { .. } => Style {
545 dot_color: palette.primary.strong.color,
546 background: palette.primary.weak.color.into(),
547 ..active
548 },
549 }
550}