2020-09-19 18:44:27 +02:00
|
|
|
//! Show toggle controls using togglers.
|
2023-03-04 05:37:11 +01:00
|
|
|
use crate::core::alignment;
|
|
|
|
|
use crate::core::event;
|
|
|
|
|
use crate::core::layout;
|
|
|
|
|
use crate::core::mouse;
|
|
|
|
|
use crate::core::renderer;
|
|
|
|
|
use crate::core::text;
|
2023-06-29 02:14:21 -04:00
|
|
|
use crate::core::touch;
|
2023-08-30 04:31:21 +02:00
|
|
|
use crate::core::widget;
|
|
|
|
|
use crate::core::widget::tree::{self, Tree};
|
2023-03-04 05:37:11 +01:00
|
|
|
use crate::core::{
|
2023-08-30 04:31:21 +02:00
|
|
|
Clipboard, Element, Event, Layout, Length, Pixels, Rectangle, Shell, Size,
|
|
|
|
|
Widget,
|
2020-09-19 18:44:27 +02:00
|
|
|
};
|
|
|
|
|
|
2023-03-04 05:37:11 +01:00
|
|
|
pub use crate::style::toggler::{Appearance, StyleSheet};
|
2021-10-28 18:17:47 +07:00
|
|
|
|
2022-03-09 14:10:15 +07:00
|
|
|
/// A toggler widget.
|
2020-09-19 18:44:27 +02:00
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
2023-03-05 04:19:31 +01:00
|
|
|
/// ```no_run
|
2023-03-04 05:37:11 +01:00
|
|
|
/// # type Toggler<'a, Message> =
|
|
|
|
|
/// # iced_widget::Toggler<'a, Message, iced_widget::renderer::Renderer<iced_widget::style::Theme>>;
|
2020-09-19 18:44:27 +02:00
|
|
|
/// #
|
|
|
|
|
/// pub enum Message {
|
|
|
|
|
/// TogglerToggled(bool),
|
|
|
|
|
/// }
|
|
|
|
|
///
|
2023-01-02 19:40:20 +01:00
|
|
|
/// let is_toggled = true;
|
2020-09-19 18:44:27 +02:00
|
|
|
///
|
2023-01-02 19:40:20 +01:00
|
|
|
/// Toggler::new(String::from("Toggle me!"), is_toggled, |b| Message::TogglerToggled(b));
|
2020-09-19 18:44:27 +02:00
|
|
|
/// ```
|
|
|
|
|
#[allow(missing_debug_implementations)]
|
2023-03-04 05:37:11 +01:00
|
|
|
pub struct Toggler<'a, Message, Renderer = crate::Renderer>
|
2022-05-31 05:13:57 +02:00
|
|
|
where
|
|
|
|
|
Renderer: text::Renderer,
|
|
|
|
|
Renderer::Theme: StyleSheet,
|
|
|
|
|
{
|
2023-01-02 19:40:20 +01:00
|
|
|
is_toggled: bool,
|
2022-02-13 19:23:15 +07:00
|
|
|
on_toggle: Box<dyn Fn(bool) -> Message + 'a>,
|
2020-09-24 16:31:39 +02:00
|
|
|
label: Option<String>,
|
2020-09-19 18:44:27 +02:00
|
|
|
width: Length,
|
2023-02-04 12:24:13 +01:00
|
|
|
size: f32,
|
2023-08-30 04:31:21 +02:00
|
|
|
text_size: Option<Pixels>,
|
2023-05-04 13:00:16 +02:00
|
|
|
text_line_height: text::LineHeight,
|
2021-09-20 15:09:55 +07:00
|
|
|
text_alignment: alignment::Horizontal,
|
2023-04-19 02:00:45 +02:00
|
|
|
text_shaping: text::Shaping,
|
2023-02-17 16:23:29 +01:00
|
|
|
spacing: f32,
|
2023-02-04 07:33:33 +01:00
|
|
|
font: Option<Renderer::Font>,
|
2022-05-31 05:13:57 +02:00
|
|
|
style: <Renderer::Theme as StyleSheet>::Style,
|
2020-09-19 18:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-31 05:13:57 +02:00
|
|
|
impl<'a, Message, Renderer> Toggler<'a, Message, Renderer>
|
|
|
|
|
where
|
|
|
|
|
Renderer: text::Renderer,
|
|
|
|
|
Renderer::Theme: StyleSheet,
|
|
|
|
|
{
|
2021-10-28 18:17:47 +07:00
|
|
|
/// The default size of a [`Toggler`].
|
2023-02-04 12:24:13 +01:00
|
|
|
pub const DEFAULT_SIZE: f32 = 20.0;
|
2021-10-28 18:17:47 +07:00
|
|
|
|
2020-09-19 18:44:27 +02:00
|
|
|
/// Creates a new [`Toggler`].
|
|
|
|
|
///
|
|
|
|
|
/// It expects:
|
|
|
|
|
/// * a boolean describing whether the [`Toggler`] is checked or not
|
2020-09-24 16:31:39 +02:00
|
|
|
/// * An optional label for the [`Toggler`]
|
2020-09-19 18:44:27 +02:00
|
|
|
/// * a function that will be called when the [`Toggler`] is toggled. It
|
|
|
|
|
/// will receive the new state of the [`Toggler`] and must produce a
|
|
|
|
|
/// `Message`.
|
2020-09-24 16:31:39 +02:00
|
|
|
pub fn new<F>(
|
|
|
|
|
label: impl Into<Option<String>>,
|
2023-01-02 19:40:20 +01:00
|
|
|
is_toggled: bool,
|
2020-09-24 16:31:39 +02:00
|
|
|
f: F,
|
|
|
|
|
) -> Self
|
2020-09-19 18:44:27 +02:00
|
|
|
where
|
2022-02-13 19:23:15 +07:00
|
|
|
F: 'a + Fn(bool) -> Message,
|
2020-09-19 18:44:27 +02:00
|
|
|
{
|
|
|
|
|
Toggler {
|
2023-01-02 19:40:20 +01:00
|
|
|
is_toggled,
|
2020-09-19 18:44:27 +02:00
|
|
|
on_toggle: Box::new(f),
|
|
|
|
|
label: label.into(),
|
|
|
|
|
width: Length::Fill,
|
2021-10-28 18:17:47 +07:00
|
|
|
size: Self::DEFAULT_SIZE,
|
2020-09-19 18:44:27 +02:00
|
|
|
text_size: None,
|
2023-05-04 13:00:16 +02:00
|
|
|
text_line_height: text::LineHeight::default(),
|
2021-09-20 15:09:55 +07:00
|
|
|
text_alignment: alignment::Horizontal::Left,
|
2023-04-19 02:00:45 +02:00
|
|
|
text_shaping: text::Shaping::Basic,
|
2023-08-30 05:34:01 +02:00
|
|
|
spacing: Self::DEFAULT_SIZE / 2.0,
|
2023-02-04 07:33:33 +01:00
|
|
|
font: None,
|
2022-05-31 05:13:57 +02:00
|
|
|
style: Default::default(),
|
2020-09-19 18:44:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the size of the [`Toggler`].
|
2023-02-17 15:56:19 +01:00
|
|
|
pub fn size(mut self, size: impl Into<Pixels>) -> Self {
|
|
|
|
|
self.size = size.into().0;
|
2020-09-19 18:44:27 +02:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the width of the [`Toggler`].
|
2023-02-04 12:24:13 +01:00
|
|
|
pub fn width(mut self, width: impl Into<Length>) -> Self {
|
|
|
|
|
self.width = width.into();
|
2020-09-19 18:44:27 +02:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the text size o the [`Toggler`].
|
2023-02-04 16:41:18 +01:00
|
|
|
pub fn text_size(mut self, text_size: impl Into<Pixels>) -> Self {
|
2023-08-30 04:31:21 +02:00
|
|
|
self.text_size = Some(text_size.into());
|
2020-09-19 18:44:27 +02:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-09 12:24:47 +02:00
|
|
|
/// Sets the text [`text::LineHeight`] of the [`Toggler`].
|
2023-05-04 13:00:16 +02:00
|
|
|
pub fn text_line_height(
|
|
|
|
|
mut self,
|
|
|
|
|
line_height: impl Into<text::LineHeight>,
|
|
|
|
|
) -> Self {
|
|
|
|
|
self.text_line_height = line_height.into();
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-25 16:38:30 +02:00
|
|
|
/// Sets the horizontal alignment of the text of the [`Toggler`]
|
2021-09-20 15:09:55 +07:00
|
|
|
pub fn text_alignment(mut self, alignment: alignment::Horizontal) -> Self {
|
2021-06-03 20:27:32 +07:00
|
|
|
self.text_alignment = alignment;
|
2020-09-24 15:49:48 +02:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 02:00:45 +02:00
|
|
|
/// Sets the [`text::Shaping`] strategy of the [`Toggler`].
|
|
|
|
|
pub fn text_shaping(mut self, shaping: text::Shaping) -> Self {
|
|
|
|
|
self.text_shaping = shaping;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-24 15:49:48 +02:00
|
|
|
/// Sets the spacing between the [`Toggler`] and the text.
|
2023-02-17 16:23:29 +01:00
|
|
|
pub fn spacing(mut self, spacing: impl Into<Pixels>) -> Self {
|
|
|
|
|
self.spacing = spacing.into().0;
|
2020-09-24 15:49:48 +02:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-09 12:24:47 +02:00
|
|
|
/// Sets the [`Renderer::Font`] of the text of the [`Toggler`]
|
2022-04-30 14:20:52 +02:00
|
|
|
///
|
2023-09-09 12:24:47 +02:00
|
|
|
/// [`Renderer::Font`]: crate::core::text::Renderer
|
2023-02-04 07:33:33 +01:00
|
|
|
pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self {
|
|
|
|
|
self.font = Some(font.into());
|
2020-09-19 18:44:27 +02:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the style of the [`Toggler`].
|
2021-10-28 18:17:47 +07:00
|
|
|
pub fn style(
|
|
|
|
|
mut self,
|
2022-05-31 05:13:57 +02:00
|
|
|
style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
|
2021-10-28 18:17:47 +07:00
|
|
|
) -> Self {
|
2022-05-31 05:13:57 +02:00
|
|
|
self.style = style.into();
|
2020-09-19 18:44:27 +02:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 17:48:23 +07:00
|
|
|
impl<'a, Message, Renderer> Widget<Message, Renderer>
|
|
|
|
|
for Toggler<'a, Message, Renderer>
|
2020-09-19 18:44:27 +02:00
|
|
|
where
|
2021-10-31 16:13:03 +07:00
|
|
|
Renderer: text::Renderer,
|
2023-03-04 05:37:11 +01:00
|
|
|
Renderer::Theme: StyleSheet + crate::text::StyleSheet,
|
2020-09-19 18:44:27 +02:00
|
|
|
{
|
2023-08-30 04:31:21 +02:00
|
|
|
fn tag(&self) -> tree::Tag {
|
|
|
|
|
tree::Tag::of::<widget::text::State<Renderer::Paragraph>>()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn state(&self) -> tree::State {
|
|
|
|
|
tree::State::new(widget::text::State::<Renderer::Paragraph>::default())
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-05 17:46:33 +01:00
|
|
|
fn size(&self) -> Size<Length> {
|
|
|
|
|
Size {
|
|
|
|
|
width: self.width,
|
|
|
|
|
height: Length::Shrink,
|
|
|
|
|
}
|
2020-09-19 18:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn layout(
|
|
|
|
|
&self,
|
2023-08-30 06:36:24 +02:00
|
|
|
tree: &mut Tree,
|
2020-09-19 18:44:27 +02:00
|
|
|
renderer: &Renderer,
|
|
|
|
|
limits: &layout::Limits,
|
|
|
|
|
) -> layout::Node {
|
2023-08-30 04:31:21 +02:00
|
|
|
let limits = limits.width(self.width);
|
2020-09-24 16:31:39 +02:00
|
|
|
|
2023-08-30 04:31:21 +02:00
|
|
|
layout::next_to_each_other(
|
|
|
|
|
&limits,
|
|
|
|
|
self.spacing,
|
|
|
|
|
|_| layout::Node::new(Size::new(2.0 * self.size, self.size)),
|
|
|
|
|
|limits| {
|
|
|
|
|
if let Some(label) = self.label.as_deref() {
|
|
|
|
|
let state = tree
|
|
|
|
|
.state
|
2023-08-30 06:36:24 +02:00
|
|
|
.downcast_mut::<widget::text::State<Renderer::Paragraph>>();
|
2020-09-24 16:31:39 +02:00
|
|
|
|
2023-08-30 04:31:21 +02:00
|
|
|
widget::text::layout(
|
|
|
|
|
state,
|
|
|
|
|
renderer,
|
|
|
|
|
limits,
|
|
|
|
|
self.width,
|
|
|
|
|
Length::Shrink,
|
|
|
|
|
label,
|
|
|
|
|
self.text_line_height,
|
|
|
|
|
self.text_size,
|
|
|
|
|
self.font,
|
|
|
|
|
self.text_alignment,
|
|
|
|
|
alignment::Vertical::Top,
|
|
|
|
|
self.text_shaping,
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
layout::Node::new(Size::ZERO)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
2020-09-19 18:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_event(
|
|
|
|
|
&mut self,
|
2022-07-27 06:49:20 +02:00
|
|
|
_state: &mut Tree,
|
2020-09-19 18:44:27 +02:00
|
|
|
event: Event,
|
|
|
|
|
layout: Layout<'_>,
|
2023-06-08 20:11:59 +02:00
|
|
|
cursor: mouse::Cursor,
|
2020-09-19 18:44:27 +02:00
|
|
|
_renderer: &Renderer,
|
|
|
|
|
_clipboard: &mut dyn Clipboard,
|
2021-11-29 16:22:01 +07:00
|
|
|
shell: &mut Shell<'_, Message>,
|
2023-07-15 10:04:25 -07:00
|
|
|
_viewport: &Rectangle,
|
2020-09-19 18:44:27 +02:00
|
|
|
) -> event::Status {
|
|
|
|
|
match event {
|
2023-06-29 02:14:21 -04:00
|
|
|
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
|
|
|
|
| Event::Touch(touch::Event::FingerPressed { .. }) => {
|
2023-06-08 20:16:46 +02:00
|
|
|
let mouse_over = cursor.is_over(layout.bounds());
|
2020-09-19 18:44:27 +02:00
|
|
|
|
|
|
|
|
if mouse_over {
|
2023-01-02 19:40:20 +01:00
|
|
|
shell.publish((self.on_toggle)(!self.is_toggled));
|
2020-09-19 18:44:27 +02:00
|
|
|
|
|
|
|
|
event::Status::Captured
|
|
|
|
|
} else {
|
|
|
|
|
event::Status::Ignored
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => event::Status::Ignored,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 18:19:19 +07:00
|
|
|
fn mouse_interaction(
|
|
|
|
|
&self,
|
2022-07-27 06:49:20 +02:00
|
|
|
_state: &Tree,
|
2021-10-28 18:19:19 +07:00
|
|
|
layout: Layout<'_>,
|
2023-06-08 20:11:59 +02:00
|
|
|
cursor: mouse::Cursor,
|
2021-11-02 15:03:29 +07:00
|
|
|
_viewport: &Rectangle,
|
2022-01-11 14:12:28 +07:00
|
|
|
_renderer: &Renderer,
|
2021-10-28 18:19:19 +07:00
|
|
|
) -> mouse::Interaction {
|
2023-06-08 20:16:46 +02:00
|
|
|
if cursor.is_over(layout.bounds()) {
|
2021-10-28 18:19:19 +07:00
|
|
|
mouse::Interaction::Pointer
|
|
|
|
|
} else {
|
|
|
|
|
mouse::Interaction::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-19 18:44:27 +02:00
|
|
|
fn draw(
|
|
|
|
|
&self,
|
2023-08-30 04:31:21 +02:00
|
|
|
tree: &Tree,
|
2020-09-19 18:44:27 +02:00
|
|
|
renderer: &mut Renderer,
|
2022-05-31 05:13:57 +02:00
|
|
|
theme: &Renderer::Theme,
|
2021-10-18 15:19:04 +07:00
|
|
|
style: &renderer::Style,
|
2020-09-19 18:44:27 +02:00
|
|
|
layout: Layout<'_>,
|
2023-06-08 20:11:59 +02:00
|
|
|
cursor: mouse::Cursor,
|
2023-12-01 16:04:27 +01:00
|
|
|
viewport: &Rectangle,
|
2021-10-14 16:07:22 +07:00
|
|
|
) {
|
2021-10-28 18:17:47 +07:00
|
|
|
/// Makes sure that the border radius of the toggler looks good at every size.
|
|
|
|
|
const BORDER_RADIUS_RATIO: f32 = 32.0 / 13.0;
|
|
|
|
|
|
|
|
|
|
/// The space ratio between the background Quad and the Toggler bounds, and
|
|
|
|
|
/// between the background Quad and foreground Quad.
|
|
|
|
|
const SPACE_RATIO: f32 = 0.05;
|
|
|
|
|
|
|
|
|
|
let mut children = layout.children();
|
2023-08-30 04:31:21 +02:00
|
|
|
let toggler_layout = children.next().unwrap();
|
2021-10-28 18:17:47 +07:00
|
|
|
|
2023-08-30 04:31:21 +02:00
|
|
|
if self.label.is_some() {
|
2021-10-28 18:17:47 +07:00
|
|
|
let label_layout = children.next().unwrap();
|
|
|
|
|
|
2023-03-04 05:37:11 +01:00
|
|
|
crate::text::draw(
|
2021-10-28 18:17:47 +07:00
|
|
|
renderer,
|
|
|
|
|
style,
|
|
|
|
|
label_layout,
|
2023-08-30 04:31:21 +02:00
|
|
|
tree.state.downcast_ref(),
|
2023-09-20 04:51:08 +02:00
|
|
|
crate::text::Appearance::default(),
|
2023-12-01 16:04:27 +01:00
|
|
|
viewport,
|
2021-10-28 18:17:47 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let bounds = toggler_layout.bounds();
|
2023-06-08 20:16:46 +02:00
|
|
|
let is_mouse_over = cursor.is_over(layout.bounds());
|
2021-10-28 18:17:47 +07:00
|
|
|
|
|
|
|
|
let style = if is_mouse_over {
|
2023-01-02 19:40:20 +01:00
|
|
|
theme.hovered(&self.style, self.is_toggled)
|
2021-10-28 18:17:47 +07:00
|
|
|
} else {
|
2023-01-02 19:40:20 +01:00
|
|
|
theme.active(&self.style, self.is_toggled)
|
2021-10-28 18:17:47 +07:00
|
|
|
};
|
|
|
|
|
|
2022-12-20 11:31:25 +01:00
|
|
|
let border_radius = bounds.height / BORDER_RADIUS_RATIO;
|
|
|
|
|
let space = SPACE_RATIO * bounds.height;
|
2021-10-28 18:17:47 +07:00
|
|
|
|
|
|
|
|
let toggler_background_bounds = Rectangle {
|
|
|
|
|
x: bounds.x + space,
|
|
|
|
|
y: bounds.y + space,
|
|
|
|
|
width: bounds.width - (2.0 * space),
|
|
|
|
|
height: bounds.height - (2.0 * space),
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-04 19:22:29 +07:00
|
|
|
renderer.fill_quad(
|
|
|
|
|
renderer::Quad {
|
|
|
|
|
bounds: toggler_background_bounds,
|
2022-11-03 00:35:01 +01:00
|
|
|
border_radius: border_radius.into(),
|
2021-11-04 19:22:29 +07:00
|
|
|
border_width: 1.0,
|
|
|
|
|
border_color: style
|
|
|
|
|
.background_border
|
|
|
|
|
.unwrap_or(style.background),
|
|
|
|
|
},
|
|
|
|
|
style.background,
|
|
|
|
|
);
|
2021-10-28 18:17:47 +07:00
|
|
|
|
|
|
|
|
let toggler_foreground_bounds = Rectangle {
|
|
|
|
|
x: bounds.x
|
2023-01-02 19:40:20 +01:00
|
|
|
+ if self.is_toggled {
|
2021-10-28 18:17:47 +07:00
|
|
|
bounds.width - 2.0 * space - (bounds.height - (4.0 * space))
|
|
|
|
|
} else {
|
|
|
|
|
2.0 * space
|
|
|
|
|
},
|
|
|
|
|
y: bounds.y + (2.0 * space),
|
|
|
|
|
width: bounds.height - (4.0 * space),
|
|
|
|
|
height: bounds.height - (4.0 * space),
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-04 19:22:29 +07:00
|
|
|
renderer.fill_quad(
|
|
|
|
|
renderer::Quad {
|
|
|
|
|
bounds: toggler_foreground_bounds,
|
2022-11-03 00:35:01 +01:00
|
|
|
border_radius: border_radius.into(),
|
2021-11-04 19:22:29 +07:00
|
|
|
border_width: 1.0,
|
|
|
|
|
border_color: style
|
|
|
|
|
.foreground_border
|
|
|
|
|
.unwrap_or(style.foreground),
|
|
|
|
|
},
|
|
|
|
|
style.foreground,
|
|
|
|
|
);
|
2020-09-19 18:44:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 17:48:23 +07:00
|
|
|
impl<'a, Message, Renderer> From<Toggler<'a, Message, Renderer>>
|
2020-09-19 18:44:27 +02:00
|
|
|
for Element<'a, Message, Renderer>
|
|
|
|
|
where
|
|
|
|
|
Message: 'a,
|
2022-05-31 05:13:57 +02:00
|
|
|
Renderer: 'a + text::Renderer,
|
2023-03-04 05:37:11 +01:00
|
|
|
Renderer::Theme: StyleSheet + crate::text::StyleSheet,
|
2020-09-19 18:44:27 +02:00
|
|
|
{
|
|
|
|
|
fn from(
|
2021-10-31 17:48:23 +07:00
|
|
|
toggler: Toggler<'a, Message, Renderer>,
|
2020-09-19 18:44:27 +02:00
|
|
|
) -> Element<'a, Message, Renderer> {
|
|
|
|
|
Element::new(toggler)
|
|
|
|
|
}
|
|
|
|
|
}
|