iced-yoda/native/src/widget/radio.rs

301 lines
8.1 KiB
Rust
Raw Normal View History

2019-09-20 19:15:31 +02:00
//! Create choices using radio buttons.
2021-10-20 19:07:09 +07:00
use crate::alignment;
use crate::event::{self, Event};
use crate::layout;
use crate::mouse;
use crate::renderer;
use crate::text;
use crate::touch;
use crate::widget::{self, Row, Text};
use crate::{
Alignment, Clipboard, Color, Element, Layout, Length, Point, Rectangle,
Shell, Widget,
};
2019-09-20 19:15:31 +02:00
2022-05-27 01:26:57 +02:00
pub use iced_style::radio::{Appearance, StyleSheet};
/// A circular button representing a choice.
///
/// # Example
/// ```
2022-05-27 01:26:57 +02:00
/// # type Radio<Message> =
/// # iced_native::widget::Radio<Message, iced_native::renderer::Null>;
2019-11-24 10:44:55 +01:00
/// #
/// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// pub enum Choice {
/// A,
/// B,
/// }
///
/// #[derive(Debug, Clone, Copy)]
/// pub enum Message {
/// RadioSelected(Choice),
/// }
///
/// let selected_choice = Some(Choice::A);
///
/// Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected);
///
/// Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected);
/// ```
///
/// ![Radio buttons drawn by `iced_wgpu`](https://github.com/iced-rs/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/radio.png?raw=true)
#[allow(missing_debug_implementations)]
2022-05-27 01:26:57 +02:00
pub struct Radio<Message, Renderer>
where
Renderer: text::Renderer,
Renderer::Theme: StyleSheet,
{
is_selected: bool,
on_click: Message,
label: String,
width: Length,
size: u16,
spacing: u16,
text_size: Option<u16>,
font: Renderer::Font,
2022-05-27 01:26:57 +02:00
style: <Renderer::Theme as StyleSheet>::Style,
}
2022-05-27 01:26:57 +02:00
impl<Message, Renderer> Radio<Message, Renderer>
where
Message: Clone,
2022-05-27 01:26:57 +02:00
Renderer: text::Renderer,
Renderer::Theme: StyleSheet,
{
/// The default size of a [`Radio`] button.
pub const DEFAULT_SIZE: u16 = 28;
/// The default spacing of a [`Radio`] button.
pub const DEFAULT_SPACING: u16 = 15;
/// Creates a new [`Radio`] button.
///
/// It expects:
/// * the value related to the [`Radio`] button
/// * the label of the [`Radio`] button
/// * the current selected value
/// * a function that will be called when the [`Radio`] is selected. It
/// receives the value of the radio and must produce a `Message`.
pub fn new<F, V>(
value: V,
label: impl Into<String>,
selected: Option<V>,
f: F,
) -> Self
where
V: Eq + Copy,
2022-02-13 22:13:47 +07:00
F: FnOnce(V) -> Message,
{
Radio {
is_selected: Some(value) == selected,
on_click: f(value),
label: label.into(),
width: Length::Shrink,
size: Self::DEFAULT_SIZE,
spacing: Self::DEFAULT_SPACING, //15
text_size: None,
font: Default::default(),
2022-05-27 01:26:57 +02:00
style: Default::default(),
}
}
/// Sets the size of the [`Radio`] button.
pub fn size(mut self, size: u16) -> Self {
self.size = size;
self
}
/// Sets the width of the [`Radio`] button.
pub fn width(mut self, width: Length) -> Self {
self.width = width;
self
}
/// Sets the spacing between the [`Radio`] button and the text.
pub fn spacing(mut self, spacing: u16) -> Self {
self.spacing = spacing;
self
}
/// Sets the text size of the [`Radio`] button.
pub fn text_size(mut self, text_size: u16) -> Self {
self.text_size = Some(text_size);
self
}
/// Sets the text font of the [`Radio`] button.
pub fn font(mut self, font: Renderer::Font) -> Self {
self.font = font;
self
}
2020-01-07 02:25:57 +01:00
/// Sets the style of the [`Radio`] button.
pub fn style(
mut self,
2022-05-27 01:26:57 +02:00
style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
) -> Self {
2022-05-27 01:26:57 +02:00
self.style = style.into();
self
}
}
2019-09-20 19:15:31 +02:00
2022-05-27 01:26:57 +02:00
impl<Message, Renderer> Widget<Message, Renderer> for Radio<Message, Renderer>
2019-09-20 19:15:31 +02:00
where
Message: Clone,
Renderer: text::Renderer,
2022-05-27 01:26:57 +02:00
Renderer::Theme: StyleSheet,
2019-09-20 19:15:31 +02:00
{
fn width(&self) -> Length {
self.width
}
fn height(&self) -> Length {
Length::Shrink
}
fn layout(
&self,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
Row::<(), Renderer>::new()
.width(self.width)
.spacing(self.spacing)
.align_items(Alignment::Center)
.push(
Row::new()
.width(Length::Units(self.size))
.height(Length::Units(self.size)),
)
.push(
Text::new(&self.label)
.width(self.width)
.size(self.text_size.unwrap_or(renderer.default_size())),
)
.layout(renderer, limits)
2019-09-20 19:15:31 +02:00
}
fn on_event(
&mut self,
event: Event,
layout: Layout<'_>,
2019-09-20 19:15:31 +02:00
cursor_position: Point,
_renderer: &Renderer,
_clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
) -> event::Status {
2019-09-20 19:15:31 +02:00
match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(touch::Event::FingerPressed { .. }) => {
2019-09-20 19:15:31 +02:00
if layout.bounds().contains(cursor_position) {
shell.publish(self.on_click.clone());
2020-11-12 00:47:58 +01:00
return event::Status::Captured;
2019-09-20 19:15:31 +02:00
}
}
_ => {}
}
event::Status::Ignored
2019-09-20 19:15:31 +02:00
}
fn mouse_interaction(
&self,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
_renderer: &Renderer,
) -> mouse::Interaction {
if layout.bounds().contains(cursor_position) {
mouse::Interaction::Pointer
} else {
mouse::Interaction::default()
}
}
2019-09-20 19:15:31 +02:00
fn draw(
&self,
renderer: &mut Renderer,
theme: &Renderer::Theme,
style: &renderer::Style,
layout: Layout<'_>,
2019-09-20 19:15:31 +02:00
cursor_position: Point,
_viewport: &Rectangle,
) {
2021-10-20 19:07:09 +07:00
let bounds = layout.bounds();
let is_mouse_over = bounds.contains(cursor_position);
let mut children = layout.children();
let custom_style = if is_mouse_over {
2022-05-27 01:26:57 +02:00
theme.hovered(self.style)
} else {
2022-05-27 01:26:57 +02:00
theme.active(self.style)
};
2021-10-20 19:07:09 +07:00
{
let layout = children.next().unwrap();
let bounds = layout.bounds();
let size = bounds.width;
let dot_size = size / 2.0;
renderer.fill_quad(
renderer::Quad {
bounds,
border_radius: size / 2.0,
border_width: custom_style.border_width,
border_color: custom_style.border_color,
},
custom_style.background,
);
2021-10-20 19:07:09 +07:00
if self.is_selected {
renderer.fill_quad(
renderer::Quad {
bounds: Rectangle {
x: bounds.x + dot_size / 2.0,
y: bounds.y + dot_size / 2.0,
width: bounds.width - dot_size,
height: bounds.height - dot_size,
},
border_radius: dot_size / 2.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
2021-10-20 19:07:09 +07:00
},
custom_style.dot_color,
);
2021-10-20 19:07:09 +07:00
}
}
{
let label_layout = children.next().unwrap();
widget::text::draw(
2021-10-20 19:07:09 +07:00
renderer,
style,
label_layout,
&self.label,
self.font.clone(),
2021-10-20 19:07:09 +07:00
self.text_size,
custom_style.text_color,
2021-10-20 19:07:09 +07:00
alignment::Horizontal::Left,
alignment::Vertical::Center,
);
}
2019-09-20 19:15:31 +02:00
}
}
2022-05-27 01:26:57 +02:00
impl<'a, Message, Renderer> From<Radio<Message, Renderer>>
2019-09-20 19:15:31 +02:00
for Element<'a, Message, Renderer>
where
Message: 'a + Clone,
Renderer: 'a + text::Renderer,
2022-05-27 01:26:57 +02:00
Renderer::Theme: StyleSheet,
2019-09-20 19:15:31 +02:00
{
2022-05-27 01:26:57 +02:00
fn from(radio: Radio<Message, Renderer>) -> Element<'a, Message, Renderer> {
Element::new(radio)
2019-09-20 19:15:31 +02:00
}
}