feat(radio): internal method for radio without label

Also adds the related settings item builder.
This commit is contained in:
Vukašin Vojinović 2026-04-16 15:59:37 +02:00
parent 6aeaca49c8
commit 36e6d10cc3
2 changed files with 116 additions and 65 deletions

View file

@ -1,5 +1,5 @@
//! Create choices using radio buttons. //! Create choices using radio buttons.
use crate::Theme; use crate::{Theme, theme};
use iced::border; use iced::border;
use iced_core::event::{self, Event}; use iced_core::event::{self, Event};
use iced_core::layout; use iced_core::layout;
@ -92,7 +92,7 @@ where
{ {
is_selected: bool, is_selected: bool,
on_click: Message, on_click: Message,
label: Element<'a, Message, Theme, Renderer>, label: Option<Element<'a, Message, Theme, Renderer>>,
width: Length, width: Length,
size: f32, size: f32,
spacing: f32, spacing: f32,
@ -106,9 +106,6 @@ where
/// The default size of a [`Radio`] button. /// The default size of a [`Radio`] button.
pub const DEFAULT_SIZE: f32 = 16.0; pub const DEFAULT_SIZE: f32 = 16.0;
/// The default spacing of a [`Radio`] button.
pub const DEFAULT_SPACING: f32 = 8.0;
/// Creates a new [`Radio`] button. /// Creates a new [`Radio`] button.
/// ///
/// It expects: /// It expects:
@ -126,10 +123,29 @@ where
Radio { Radio {
is_selected: Some(value) == selected, is_selected: Some(value) == selected,
on_click: f(value), on_click: f(value),
label: label.into(), label: Some(label.into()),
width: Length::Shrink, width: Length::Shrink,
size: Self::DEFAULT_SIZE, size: Self::DEFAULT_SIZE,
spacing: Self::DEFAULT_SPACING, spacing: theme::spacing().space_xs as f32,
}
}
/// Creates a new [`Radio`] button without a label.
///
/// This is intended for internal use with the settings item builder,
/// where the label comes from the settings item title instead.
pub(crate) fn new_no_label<V, F>(value: V, selected: Option<V>, f: F) -> Self
where
V: Eq + Copy,
F: FnOnce(V) -> Message,
{
Radio {
is_selected: Some(value) == selected,
on_click: f(value),
label: None,
width: Length::Shrink,
size: Self::DEFAULT_SIZE,
spacing: theme::spacing().space_xs as f32,
} }
} }
@ -161,11 +177,17 @@ where
Renderer: iced_core::Renderer, Renderer: iced_core::Renderer,
{ {
fn children(&self) -> Vec<Tree> { fn children(&self) -> Vec<Tree> {
vec![Tree::new(&self.label)] if let Some(label) = &self.label {
vec![Tree::new(label)]
} else {
vec![]
}
} }
fn diff(&mut self, tree: &mut Tree) { fn diff(&mut self, tree: &mut Tree) {
tree.diff_children(std::slice::from_mut(&mut self.label)); if let Some(label) = &mut self.label {
tree.diff_children(std::slice::from_mut(label));
}
} }
fn size(&self) -> Size<Length> { fn size(&self) -> Size<Length> {
Size { Size {
@ -180,16 +202,20 @@ where
renderer: &Renderer, renderer: &Renderer,
limits: &layout::Limits, limits: &layout::Limits,
) -> layout::Node { ) -> layout::Node {
layout::next_to_each_other( if let Some(label) = &mut self.label {
&limits.width(self.width), layout::next_to_each_other(
self.spacing, &limits.width(self.width),
|_| layout::Node::new(Size::new(self.size, self.size)), self.spacing,
|limits| { |_| layout::Node::new(Size::new(self.size, self.size)),
self.label |limits| {
.as_widget_mut() label
.layout(&mut tree.children[0], renderer, limits) .as_widget_mut()
}, .layout(&mut tree.children[0], renderer, limits)
) },
)
} else {
layout::Node::new(Size::new(self.size, self.size))
}
} }
fn operate( fn operate(
@ -199,12 +225,14 @@ where
renderer: &Renderer, renderer: &Renderer,
operation: &mut dyn iced_core::widget::Operation<()>, operation: &mut dyn iced_core::widget::Operation<()>,
) { ) {
self.label.as_widget_mut().operate( if let Some(label) = &mut self.label {
&mut tree.children[0], label.as_widget_mut().operate(
layout.children().nth(1).unwrap(), &mut tree.children[0],
renderer, layout.children().nth(1).unwrap(),
operation, renderer,
); operation,
);
}
} }
fn update( fn update(
@ -218,24 +246,25 @@ where
shell: &mut Shell<'_, Message>, shell: &mut Shell<'_, Message>,
viewport: &Rectangle, viewport: &Rectangle,
) { ) {
self.label.as_widget_mut().update( if let Some(label) = &mut self.label {
&mut tree.children[0], label.as_widget_mut().update(
event, &mut tree.children[0],
layout.children().nth(1).unwrap(), event,
cursor, layout.children().nth(1).unwrap(),
renderer, cursor,
clipboard, renderer,
shell, clipboard,
viewport, shell,
); viewport,
);
}
if !shell.is_event_captured() { if !shell.is_event_captured() {
match event { match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
| Event::Touch(touch::Event::FingerPressed { .. }) => { | Event::Touch(touch::Event::FingerLifted { .. }) => {
if cursor.is_over(layout.bounds()) { if cursor.is_over(layout.bounds()) {
shell.publish(self.on_click.clone()); shell.publish(self.on_click.clone());
shell.capture_event(); shell.capture_event();
return; return;
} }
@ -253,13 +282,17 @@ where
viewport: &Rectangle, viewport: &Rectangle,
renderer: &Renderer, renderer: &Renderer,
) -> mouse::Interaction { ) -> mouse::Interaction {
let interaction = self.label.as_widget().mouse_interaction( let interaction = if let Some(label) = &self.label {
&tree.children[0], label.as_widget().mouse_interaction(
layout.children().nth(1).unwrap(), &tree.children[0],
cursor, layout.children().nth(1).unwrap(),
viewport, cursor,
renderer, viewport,
); renderer,
)
} else {
mouse::Interaction::default()
};
if interaction == mouse::Interaction::default() { if interaction == mouse::Interaction::default() {
if cursor.is_over(layout.bounds()) { if cursor.is_over(layout.bounds()) {
@ -284,8 +317,6 @@ where
) { ) {
let is_mouse_over = cursor.is_over(layout.bounds()); let is_mouse_over = cursor.is_over(layout.bounds());
let mut children = layout.children();
let custom_style = if is_mouse_over { let custom_style = if is_mouse_over {
theme.style( theme.style(
&(), &(),
@ -302,16 +333,21 @@ where
) )
}; };
{ let (dot_bounds, label_layout) = if self.label.is_some() {
let layout = children.next().unwrap(); let mut children = layout.children();
let bounds = layout.bounds(); let dot_bounds = children.next().unwrap().bounds();
(dot_bounds, children.next())
} else {
(layout.bounds(), None)
};
let size = bounds.width; {
let size = dot_bounds.width;
let dot_size = 6.0; let dot_size = 6.0;
renderer.fill_quad( renderer.fill_quad(
renderer::Quad { renderer::Quad {
bounds, bounds: dot_bounds,
border: Border { border: Border {
radius: (size / 2.0).into(), radius: (size / 2.0).into(),
width: custom_style.border_width, width: custom_style.border_width,
@ -326,8 +362,8 @@ where
renderer.fill_quad( renderer.fill_quad(
renderer::Quad { renderer::Quad {
bounds: Rectangle { bounds: Rectangle {
x: bounds.x + (size - dot_size) / 2.0, x: dot_bounds.x + (size - dot_size) / 2.0,
y: bounds.y + (size - dot_size) / 2.0, y: dot_bounds.y + (size - dot_size) / 2.0,
width: dot_size, width: dot_size,
height: dot_size, height: dot_size,
}, },
@ -339,9 +375,8 @@ where
} }
} }
{ if let (Some(label), Some(label_layout)) = (&self.label, label_layout) {
let label_layout = children.next().unwrap(); label.as_widget().draw(
self.label.as_widget().draw(
&tree.children[0], &tree.children[0],
renderer, renderer,
theme, theme,
@ -361,7 +396,7 @@ where
viewport: &Rectangle, viewport: &Rectangle,
translation: Vector, translation: Vector,
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> { ) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
self.label.as_widget_mut().overlay( self.label.as_mut()?.as_widget_mut().overlay(
&mut tree.children[0], &mut tree.children[0],
layout.children().nth(1).unwrap(), layout.children().nth(1).unwrap(),
renderer, renderer,
@ -377,12 +412,14 @@ where
renderer: &Renderer, renderer: &Renderer,
dnd_rectangles: &mut iced_core::clipboard::DndDestinationRectangles, dnd_rectangles: &mut iced_core::clipboard::DndDestinationRectangles,
) { ) {
self.label.as_widget().drag_destinations( if let Some(label) = &self.label {
&state.children[0], label.as_widget().drag_destinations(
layout.children().nth(1).unwrap(), &state.children[0],
renderer, layout.children().nth(1).unwrap(),
dnd_rectangles, renderer,
); dnd_rectangles,
);
}
} }
} }

View file

@ -103,7 +103,7 @@ pub struct Item<'a, Message> {
icon: Option<Element<'a, Message>>, icon: Option<Element<'a, Message>>,
} }
impl<'a, Message: 'static> Item<'a, Message> { impl<'a, Message: Clone + 'static> Item<'a, Message> {
/// Assigns a control to the item. /// Assigns a control to the item.
pub fn control(self, widget: impl Into<Element<'a, Message>>) -> Row<'a, Message, Theme> { pub fn control(self, widget: impl Into<Element<'a, Message>>) -> Row<'a, Message, Theme> {
item_row(self.control_(widget.into())) item_row(self.control_(widget.into()))
@ -205,4 +205,18 @@ impl<'a, Message: 'static> Item<'a, Message> {
) )
.on_press_maybe(on_press) .on_press_maybe(on_press)
} }
pub fn radio<V, F>(self, value: V, selected: Option<V>, f: F) -> list::ListButton<'a, Message>
where
V: Eq + Copy + 'static,
F: Fn(V) -> Message + 'static,
{
let on_press = f(value);
list::button(
self.control_start(crate::widget::radio::Radio::new_no_label(
value, selected, f,
)),
)
.on_press(on_press)
}
} }