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

98 lines
2.5 KiB
Rust
Raw Normal View History

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,
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>,
_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) {
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,
) -> 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
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,
) -> 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,
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)
}
}