2019-09-20 19:15:31 +02:00
|
|
|
//! Create choices using radio buttons.
|
|
|
|
|
use crate::input::{mouse, ButtonState};
|
2019-11-10 01:55:32 +01:00
|
|
|
use crate::{layout, Element, Event, Hasher, Layout, Point, Widget};
|
2019-09-20 19:15:31 +02:00
|
|
|
|
|
|
|
|
use std::hash::Hash;
|
|
|
|
|
|
|
|
|
|
pub use iced_core::Radio;
|
|
|
|
|
|
|
|
|
|
impl<Message, Renderer> Widget<Message, Renderer> for Radio<Message>
|
|
|
|
|
where
|
|
|
|
|
Renderer: self::Renderer,
|
2019-11-02 02:15:17 +01:00
|
|
|
Message: Clone + std::fmt::Debug,
|
2019-09-20 19:15:31 +02:00
|
|
|
{
|
2019-11-10 01:55:32 +01:00
|
|
|
fn layout(&self, renderer: &Renderer, limits: &layout::Limits) -> Layout {
|
|
|
|
|
renderer.layout(&self, limits)
|
2019-09-20 19:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_event(
|
|
|
|
|
&mut self,
|
|
|
|
|
event: Event,
|
2019-11-10 01:55:32 +01:00
|
|
|
layout: &Layout,
|
2019-09-20 19:15:31 +02:00
|
|
|
cursor_position: Point,
|
|
|
|
|
messages: &mut Vec<Message>,
|
2019-10-29 03:34:21 +01:00
|
|
|
_renderer: &Renderer,
|
2019-09-20 19:15:31 +02:00
|
|
|
) {
|
|
|
|
|
match event {
|
|
|
|
|
Event::Mouse(mouse::Event::Input {
|
|
|
|
|
button: mouse::Button::Left,
|
|
|
|
|
state: ButtonState::Pressed,
|
|
|
|
|
}) => {
|
|
|
|
|
if layout.bounds().contains(cursor_position) {
|
2019-11-02 02:15:17 +01:00
|
|
|
messages.push(self.on_click.clone());
|
2019-09-20 19:15:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw(
|
|
|
|
|
&self,
|
|
|
|
|
renderer: &mut Renderer,
|
2019-11-10 01:55:32 +01:00
|
|
|
layout: &Layout,
|
2019-09-20 19:15:31 +02:00
|
|
|
cursor_position: Point,
|
2019-10-11 22:15:39 +02:00
|
|
|
) -> Renderer::Output {
|
2019-09-20 19:15:31 +02:00
|
|
|
renderer.draw(&self, layout, cursor_position)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn hash_layout(&self, state: &mut Hasher) {
|
|
|
|
|
self.label.hash(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The renderer of a [`Radio`] button.
|
|
|
|
|
///
|
|
|
|
|
/// Your [renderer] will need to implement this trait before being
|
|
|
|
|
/// able to use a [`Radio`] button in your user interface.
|
|
|
|
|
///
|
|
|
|
|
/// [`Radio`]: struct.Radio.html
|
|
|
|
|
/// [renderer]: ../../renderer/index.html
|
2019-10-05 03:56:18 +02:00
|
|
|
pub trait Renderer: crate::Renderer {
|
2019-09-20 19:15:31 +02:00
|
|
|
/// Creates a [`Node`] for the provided [`Radio`].
|
|
|
|
|
///
|
|
|
|
|
/// [`Node`]: ../../struct.Node.html
|
|
|
|
|
/// [`Radio`]: struct.Radio.html
|
2019-11-10 01:55:32 +01:00
|
|
|
fn layout<Message>(
|
|
|
|
|
&self,
|
|
|
|
|
radio: &Radio<Message>,
|
|
|
|
|
limits: &layout::Limits,
|
|
|
|
|
) -> Layout;
|
2019-09-20 19:15:31 +02:00
|
|
|
|
|
|
|
|
/// Draws a [`Radio`] button.
|
|
|
|
|
///
|
|
|
|
|
/// It receives:
|
|
|
|
|
/// * the current cursor position
|
|
|
|
|
/// * the bounds of the [`Radio`]
|
|
|
|
|
/// * the bounds of the label of the [`Radio`]
|
|
|
|
|
/// * whether the [`Radio`] is selected or not
|
|
|
|
|
///
|
|
|
|
|
/// [`Radio`]: struct.Radio.html
|
|
|
|
|
fn draw<Message>(
|
|
|
|
|
&mut self,
|
|
|
|
|
radio: &Radio<Message>,
|
2019-11-10 01:55:32 +01:00
|
|
|
layout: &Layout,
|
2019-09-20 19:15:31 +02:00
|
|
|
cursor_position: Point,
|
2019-10-11 22:15:39 +02:00
|
|
|
) -> Self::Output;
|
2019-09-20 19:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, Message, Renderer> From<Radio<Message>>
|
|
|
|
|
for Element<'a, Message, Renderer>
|
|
|
|
|
where
|
|
|
|
|
Renderer: self::Renderer,
|
2019-11-02 02:15:17 +01:00
|
|
|
Message: 'static + Clone + std::fmt::Debug,
|
2019-09-20 19:15:31 +02:00
|
|
|
{
|
|
|
|
|
fn from(checkbox: Radio<Message>) -> Element<'a, Message, Renderer> {
|
|
|
|
|
Element::new(checkbox)
|
|
|
|
|
}
|
|
|
|
|
}
|