2023-09-01 07:29:19 +02:00
|
|
|
|
// Copyright 2019 H<>ctor Ram<61>n, Iced contributors
|
|
|
|
|
|
// Copyright 2023 System76 <info@system76.com>
|
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
|
|
|
|
//! Allow your users to perform actions by pressing a button.
|
|
|
|
|
|
//!
|
|
|
|
|
|
//! A [`Button`] has some local [`State`].
|
2023-11-15 16:09:17 +01:00
|
|
|
|
|
2023-09-01 07:29:19 +02:00
|
|
|
|
use iced_runtime::core::widget::Id;
|
2025-03-21 03:17:59 +01:00
|
|
|
|
use iced_runtime::{Action, Task, keyboard, task};
|
2023-09-01 07:29:19 +02:00
|
|
|
|
|
|
|
|
|
|
use iced_core::event::{self, Event};
|
2024-05-17 19:08:43 +02:00
|
|
|
|
use iced_core::renderer::{self, Quad, Renderer};
|
2023-09-01 07:29:19 +02:00
|
|
|
|
use iced_core::touch;
|
|
|
|
|
|
use iced_core::widget::Operation;
|
2025-03-21 03:17:59 +01:00
|
|
|
|
use iced_core::widget::tree::{self, Tree};
|
2023-09-01 07:29:19 +02:00
|
|
|
|
use iced_core::{
|
2024-05-17 19:08:43 +02:00
|
|
|
|
Background, Clipboard, Color, Layout, Length, Padding, Point, Rectangle, Shell, Vector, Widget,
|
2023-09-01 07:29:19 +02:00
|
|
|
|
};
|
2025-03-21 03:17:59 +01:00
|
|
|
|
use iced_core::{Border, mouse};
|
|
|
|
|
|
use iced_core::{Shadow, overlay};
|
|
|
|
|
|
use iced_core::{layout, svg};
|
2024-10-16 20:36:46 -04:00
|
|
|
|
use iced_renderer::core::widget::operation;
|
2023-09-01 07:29:19 +02:00
|
|
|
|
|
2023-12-13 12:59:26 -05:00
|
|
|
|
use crate::theme::THEME;
|
|
|
|
|
|
|
2024-10-16 20:36:46 -04:00
|
|
|
|
pub use super::style::{Catalog, Style};
|
2023-09-01 07:29:19 +02:00
|
|
|
|
|
2023-11-15 16:09:17 +01:00
|
|
|
|
/// Internally defines different button widget variants.
|
|
|
|
|
|
enum Variant<Message> {
|
|
|
|
|
|
Normal,
|
|
|
|
|
|
Image {
|
|
|
|
|
|
close_icon: svg::Handle,
|
|
|
|
|
|
on_remove: Option<Message>,
|
|
|
|
|
|
},
|
2023-10-30 16:32:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-15 16:09:17 +01:00
|
|
|
|
/// A generic button which emits a message when pressed.
|
2023-09-01 07:29:19 +02:00
|
|
|
|
#[allow(missing_debug_implementations)]
|
|
|
|
|
|
#[must_use]
|
2024-05-17 19:08:43 +02:00
|
|
|
|
pub struct Button<'a, Message> {
|
2023-09-01 07:29:19 +02:00
|
|
|
|
id: Id,
|
|
|
|
|
|
#[cfg(feature = "a11y")]
|
2024-02-08 19:22:18 -05:00
|
|
|
|
name: Option<std::borrow::Cow<'a, str>>,
|
2023-09-01 07:29:19 +02:00
|
|
|
|
#[cfg(feature = "a11y")]
|
|
|
|
|
|
description: Option<iced_accessibility::Description<'a>>,
|
|
|
|
|
|
#[cfg(feature = "a11y")]
|
|
|
|
|
|
label: Option<Vec<iced_accessibility::accesskit::NodeId>>,
|
2024-05-17 19:08:43 +02:00
|
|
|
|
content: crate::Element<'a, Message>,
|
2025-05-23 12:40:10 -04:00
|
|
|
|
on_press: Option<Box<dyn Fn(Vector, Rectangle) -> Message + 'a>>,
|
|
|
|
|
|
on_press_down: Option<Box<dyn Fn(Vector, Rectangle) -> Message + 'a>>,
|
2023-09-01 07:29:19 +02:00
|
|
|
|
width: Length,
|
|
|
|
|
|
height: Length,
|
|
|
|
|
|
padding: Padding,
|
2023-11-15 16:09:17 +01:00
|
|
|
|
selected: bool,
|
2024-05-17 19:08:43 +02:00
|
|
|
|
style: crate::theme::Button,
|
2023-11-15 16:09:17 +01:00
|
|
|
|
variant: Variant<Message>,
|
2025-03-14 11:56:21 -04:00
|
|
|
|
force_enabled: bool,
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-23 12:40:10 -04:00
|
|
|
|
impl<'a, Message: Clone + 'a> Button<'a, Message> {
|
2023-09-01 07:29:19 +02:00
|
|
|
|
/// Creates a new [`Button`] with the given content.
|
2024-05-20 20:01:47 +02:00
|
|
|
|
pub(super) fn new(content: impl Into<crate::Element<'a, Message>>) -> Self {
|
2023-11-15 16:09:17 +01:00
|
|
|
|
Self {
|
2023-09-01 07:29:19 +02:00
|
|
|
|
id: Id::unique(),
|
|
|
|
|
|
#[cfg(feature = "a11y")]
|
|
|
|
|
|
name: None,
|
|
|
|
|
|
#[cfg(feature = "a11y")]
|
|
|
|
|
|
description: None,
|
|
|
|
|
|
#[cfg(feature = "a11y")]
|
|
|
|
|
|
label: None,
|
|
|
|
|
|
content: content.into(),
|
|
|
|
|
|
on_press: None,
|
2024-05-17 20:46:24 -04:00
|
|
|
|
on_press_down: None,
|
2023-09-01 07:29:19 +02:00
|
|
|
|
width: Length::Shrink,
|
|
|
|
|
|
height: Length::Shrink,
|
|
|
|
|
|
padding: Padding::new(5.0),
|
2023-11-15 16:09:17 +01:00
|
|
|
|
selected: false,
|
2024-05-17 19:08:43 +02:00
|
|
|
|
style: crate::theme::Button::default(),
|
2023-11-15 16:09:17 +01:00
|
|
|
|
variant: Variant::Normal,
|
2025-03-14 11:56:21 -04:00
|
|
|
|
force_enabled: false,
|
2023-11-15 16:09:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn new_image(
|
2024-05-17 19:08:43 +02:00
|
|
|
|
content: impl Into<crate::Element<'a, Message>>,
|
2023-11-15 16:09:17 +01:00
|
|
|
|
on_remove: Option<Message>,
|
|
|
|
|
|
) -> Self {
|
|
|
|
|
|
Self {
|
|
|
|
|
|
id: Id::unique(),
|
|
|
|
|
|
#[cfg(feature = "a11y")]
|
|
|
|
|
|
name: None,
|
|
|
|
|
|
#[cfg(feature = "a11y")]
|
|
|
|
|
|
description: None,
|
2025-03-14 11:56:21 -04:00
|
|
|
|
force_enabled: false,
|
2023-11-15 16:09:17 +01:00
|
|
|
|
#[cfg(feature = "a11y")]
|
|
|
|
|
|
label: None,
|
|
|
|
|
|
content: content.into(),
|
|
|
|
|
|
on_press: None,
|
2024-05-17 20:46:24 -04:00
|
|
|
|
on_press_down: None,
|
2023-11-15 16:09:17 +01:00
|
|
|
|
width: Length::Shrink,
|
|
|
|
|
|
height: Length::Shrink,
|
|
|
|
|
|
padding: Padding::new(5.0),
|
|
|
|
|
|
selected: false,
|
2024-05-17 19:08:43 +02:00
|
|
|
|
style: crate::theme::Button::default(),
|
2023-11-15 16:09:17 +01:00
|
|
|
|
variant: Variant::Image {
|
|
|
|
|
|
on_remove,
|
|
|
|
|
|
close_icon: crate::widget::icon::from_name("window-close-symbolic")
|
|
|
|
|
|
.size(8)
|
|
|
|
|
|
.icon()
|
|
|
|
|
|
.into_svg_handle()
|
|
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
|
|
let bytes: &'static [u8] = &[];
|
|
|
|
|
|
iced_core::svg::Handle::from_memory(bytes)
|
|
|
|
|
|
}),
|
|
|
|
|
|
},
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-30 16:32:10 +01:00
|
|
|
|
/// Sets the [`Id`] of the [`Button`].
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2023-10-30 16:32:10 +01:00
|
|
|
|
pub fn id(mut self, id: Id) -> Self {
|
|
|
|
|
|
self.id = id;
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-01 07:29:19 +02:00
|
|
|
|
/// Sets the width of the [`Button`].
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2023-09-01 07:29:19 +02:00
|
|
|
|
pub fn width(mut self, width: impl Into<Length>) -> Self {
|
|
|
|
|
|
self.width = width.into();
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Sets the height of the [`Button`].
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2023-09-01 07:29:19 +02:00
|
|
|
|
pub fn height(mut self, height: impl Into<Length>) -> Self {
|
|
|
|
|
|
self.height = height.into();
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Sets the [`Padding`] of the [`Button`].
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2023-09-01 07:29:19 +02:00
|
|
|
|
pub fn padding<P: Into<Padding>>(mut self, padding: P) -> Self {
|
|
|
|
|
|
self.padding = padding.into();
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-17 20:46:24 -04:00
|
|
|
|
/// Sets the message that will be produced when the [`Button`] is pressed and released.
|
2023-09-01 07:29:19 +02:00
|
|
|
|
///
|
2024-05-17 20:46:24 -04:00
|
|
|
|
/// Unless `on_press` or `on_press_down` is called, the [`Button`] will be disabled.
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2023-09-01 07:29:19 +02:00
|
|
|
|
pub fn on_press(mut self, on_press: Message) -> Self {
|
2025-05-23 12:40:10 -04:00
|
|
|
|
self.on_press = Some(Box::new(move |_, _| on_press.clone()));
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Sets the message that will be produced when the [`Button`] is pressed and released.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Unless `on_press` or `on_press_down` is called, the [`Button`] will be disabled.
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
pub fn on_press_with_rectangle(
|
|
|
|
|
|
mut self,
|
|
|
|
|
|
on_press: impl Fn(Vector, Rectangle) -> Message + 'a,
|
|
|
|
|
|
) -> Self {
|
|
|
|
|
|
self.on_press = Some(Box::new(on_press));
|
2023-09-01 07:29:19 +02:00
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-17 20:46:24 -04:00
|
|
|
|
/// Sets the message that will be produced when the [`Button`] is pressed,
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Unless `on_press` or `on_press_down` is called, the [`Button`] will be disabled.
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2024-05-17 20:46:24 -04:00
|
|
|
|
pub fn on_press_down(mut self, on_press: Message) -> Self {
|
2025-05-23 12:40:10 -04:00
|
|
|
|
self.on_press_down = Some(Box::new(move |_, _| on_press.clone()));
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Sets the message that will be produced when the [`Button`] is pressed,
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Unless `on_press` or `on_press_down` is called, the [`Button`] will be disabled.
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
pub fn on_press_down_with_rectange(
|
|
|
|
|
|
mut self,
|
|
|
|
|
|
on_press: impl Fn(Vector, Rectangle) -> Message + 'a,
|
|
|
|
|
|
) -> Self {
|
|
|
|
|
|
self.on_press_down = Some(Box::new(on_press));
|
2024-05-17 20:46:24 -04:00
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-01 07:29:19 +02:00
|
|
|
|
/// Sets the message that will be produced when the [`Button`] is pressed,
|
|
|
|
|
|
/// if `Some`.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// If `None`, the [`Button`] will be disabled.
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2023-09-01 07:29:19 +02:00
|
|
|
|
pub fn on_press_maybe(mut self, on_press: Option<Message>) -> Self {
|
2025-05-23 12:40:10 -04:00
|
|
|
|
if let Some(m) = on_press {
|
|
|
|
|
|
self.on_press(m)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
self.on_press = None;
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Sets the message that will be produced when the [`Button`] is pressed and released.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Unless `on_press` or `on_press_down` is called, the [`Button`] will be disabled.
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
pub fn on_press_maybe_with_rectangle(
|
|
|
|
|
|
mut self,
|
|
|
|
|
|
on_press: impl Fn(Vector, Rectangle) -> Message + 'a,
|
|
|
|
|
|
) -> Self {
|
|
|
|
|
|
self.on_press = Some(Box::new(on_press));
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Sets the message that will be produced when the [`Button`] is pressed,
|
|
|
|
|
|
/// if `Some`.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// If `None`, the [`Button`] will be disabled.
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
pub fn on_press_down_maybe(mut self, on_press: Option<Message>) -> Self {
|
|
|
|
|
|
if let Some(m) = on_press {
|
|
|
|
|
|
self.on_press(m)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
self.on_press_down = None;
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Sets the message that will be produced when the [`Button`] is pressed and released.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Unless `on_press` or `on_press_down` is called, the [`Button`] will be disabled.
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
pub fn on_press_down_maybe_with_rectangle(
|
|
|
|
|
|
mut self,
|
|
|
|
|
|
on_press: impl Fn(Vector, Rectangle) -> Message + 'a,
|
|
|
|
|
|
) -> Self {
|
|
|
|
|
|
self.on_press_down = Some(Box::new(on_press));
|
2023-09-01 07:29:19 +02:00
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-14 11:56:21 -04:00
|
|
|
|
/// Sets the the [`Button`] to enabled whether or not it has handlers for on press.
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2025-03-14 11:56:21 -04:00
|
|
|
|
pub fn force_enabled(mut self, enabled: bool) -> Self {
|
|
|
|
|
|
self.force_enabled = enabled;
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-30 16:32:10 +01:00
|
|
|
|
/// Sets the widget to a selected state.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Displays a selection indicator on image buttons.
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2023-11-11 01:10:05 +01:00
|
|
|
|
pub fn selected(mut self, selected: bool) -> Self {
|
2023-11-15 16:09:17 +01:00
|
|
|
|
self.selected = selected;
|
2023-10-30 16:32:10 +01:00
|
|
|
|
|
2023-09-01 07:29:19 +02:00
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-30 16:32:10 +01:00
|
|
|
|
/// Sets the style variant of this [`Button`].
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2024-10-16 20:36:46 -04:00
|
|
|
|
pub fn class(mut self, style: crate::theme::Button) -> Self {
|
2023-10-30 16:32:10 +01:00
|
|
|
|
self.style = style;
|
2023-09-01 07:29:19 +02:00
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "a11y")]
|
|
|
|
|
|
/// Sets the name of the [`Button`].
|
2024-02-08 19:22:18 -05:00
|
|
|
|
pub fn name(mut self, name: impl Into<std::borrow::Cow<'a, str>>) -> Self {
|
2023-09-01 07:29:19 +02:00
|
|
|
|
self.name = Some(name.into());
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "a11y")]
|
|
|
|
|
|
/// Sets the description of the [`Button`].
|
|
|
|
|
|
pub fn description_widget<T: iced_accessibility::Describes>(mut self, description: &T) -> Self {
|
|
|
|
|
|
self.description = Some(iced_accessibility::Description::Id(
|
|
|
|
|
|
description.description(),
|
|
|
|
|
|
));
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "a11y")]
|
|
|
|
|
|
/// Sets the description of the [`Button`].
|
2024-02-08 19:22:18 -05:00
|
|
|
|
pub fn description(mut self, description: impl Into<std::borrow::Cow<'a, str>>) -> Self {
|
2023-09-01 07:29:19 +02:00
|
|
|
|
self.description = Some(iced_accessibility::Description::Text(description.into()));
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "a11y")]
|
|
|
|
|
|
/// Sets the label of the [`Button`].
|
|
|
|
|
|
pub fn label(mut self, label: &dyn iced_accessibility::Labels) -> Self {
|
|
|
|
|
|
self.label = Some(label.label().into_iter().map(|l| l.into()).collect());
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-17 19:08:43 +02:00
|
|
|
|
impl<'a, Message: 'a + Clone> Widget<Message, crate::Theme, crate::Renderer>
|
|
|
|
|
|
for Button<'a, Message>
|
2023-09-01 07:29:19 +02:00
|
|
|
|
{
|
|
|
|
|
|
fn tag(&self) -> tree::Tag {
|
|
|
|
|
|
tree::Tag::of::<State>()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn state(&self) -> tree::State {
|
|
|
|
|
|
tree::State::new(State::new())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn children(&self) -> Vec<Tree> {
|
|
|
|
|
|
vec![Tree::new(&self.content)]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn diff(&mut self, tree: &mut Tree) {
|
|
|
|
|
|
tree.diff_children(std::slice::from_mut(&mut self.content));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-30 22:14:00 -05:00
|
|
|
|
fn size(&self) -> iced_core::Size<Length> {
|
|
|
|
|
|
iced_core::Size::new(self.width, self.height)
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-30 14:01:42 -05:00
|
|
|
|
fn layout(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
tree: &mut Tree,
|
2024-05-17 19:08:43 +02:00
|
|
|
|
renderer: &crate::Renderer,
|
2023-11-30 14:01:42 -05:00
|
|
|
|
limits: &layout::Limits,
|
|
|
|
|
|
) -> layout::Node {
|
2023-09-01 07:29:19 +02:00
|
|
|
|
layout(
|
|
|
|
|
|
renderer,
|
|
|
|
|
|
limits,
|
|
|
|
|
|
self.width,
|
|
|
|
|
|
self.height,
|
|
|
|
|
|
self.padding,
|
2023-11-30 14:01:42 -05:00
|
|
|
|
|renderer, limits| {
|
|
|
|
|
|
self.content
|
|
|
|
|
|
.as_widget()
|
|
|
|
|
|
.layout(&mut tree.children[0], renderer, limits)
|
|
|
|
|
|
},
|
2023-09-01 07:29:19 +02:00
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn operate(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
tree: &mut Tree,
|
|
|
|
|
|
layout: Layout<'_>,
|
2024-05-17 19:08:43 +02:00
|
|
|
|
renderer: &crate::Renderer,
|
2024-10-16 20:36:46 -04:00
|
|
|
|
operation: &mut dyn Operation<()>,
|
2023-09-01 07:29:19 +02:00
|
|
|
|
) {
|
|
|
|
|
|
operation.container(None, layout.bounds(), &mut |operation| {
|
|
|
|
|
|
self.content.as_widget().operate(
|
|
|
|
|
|
&mut tree.children[0],
|
2025-03-20 10:10:02 -04:00
|
|
|
|
layout
|
|
|
|
|
|
.children()
|
|
|
|
|
|
.next()
|
|
|
|
|
|
.unwrap()
|
|
|
|
|
|
.with_virtual_offset(layout.virtual_offset()),
|
2023-09-01 07:29:19 +02:00
|
|
|
|
renderer,
|
|
|
|
|
|
operation,
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
|
|
|
|
|
let state = tree.state.downcast_mut::<State>();
|
|
|
|
|
|
operation.focusable(state, Some(&self.id));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn on_event(
|
|
|
|
|
|
&mut self,
|
|
|
|
|
|
tree: &mut Tree,
|
|
|
|
|
|
event: Event,
|
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
|
cursor: mouse::Cursor,
|
2024-05-17 19:08:43 +02:00
|
|
|
|
renderer: &crate::Renderer,
|
2023-09-01 07:29:19 +02:00
|
|
|
|
clipboard: &mut dyn Clipboard,
|
|
|
|
|
|
shell: &mut Shell<'_, Message>,
|
|
|
|
|
|
viewport: &Rectangle,
|
|
|
|
|
|
) -> event::Status {
|
2023-11-15 16:09:17 +01:00
|
|
|
|
if let Variant::Image {
|
|
|
|
|
|
on_remove: Some(on_remove),
|
|
|
|
|
|
..
|
|
|
|
|
|
} = &self.variant
|
|
|
|
|
|
{
|
|
|
|
|
|
// Capture mouse/touch events on the removal button
|
|
|
|
|
|
match event {
|
|
|
|
|
|
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
|
|
|
|
|
|
| Event::Touch(touch::Event::FingerPressed { .. }) => {
|
|
|
|
|
|
if let Some(position) = cursor.position() {
|
|
|
|
|
|
if removal_bounds(layout.bounds(), 4.0).contains(position) {
|
|
|
|
|
|
shell.publish(on_remove.clone());
|
|
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_ => (),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-28 19:02:08 +00:00
|
|
|
|
if self.content.as_widget_mut().on_event(
|
2023-09-01 07:29:19 +02:00
|
|
|
|
&mut tree.children[0],
|
|
|
|
|
|
event.clone(),
|
2025-03-20 10:10:02 -04:00
|
|
|
|
layout
|
|
|
|
|
|
.children()
|
|
|
|
|
|
.next()
|
|
|
|
|
|
.unwrap()
|
|
|
|
|
|
.with_virtual_offset(layout.virtual_offset()),
|
2023-09-01 07:29:19 +02:00
|
|
|
|
cursor,
|
|
|
|
|
|
renderer,
|
|
|
|
|
|
clipboard,
|
|
|
|
|
|
shell,
|
|
|
|
|
|
viewport,
|
2023-11-28 19:02:08 +00:00
|
|
|
|
) == event::Status::Captured
|
|
|
|
|
|
{
|
2023-09-01 07:29:19 +02:00
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
update(
|
|
|
|
|
|
self.id.clone(),
|
|
|
|
|
|
event,
|
|
|
|
|
|
layout,
|
|
|
|
|
|
cursor,
|
|
|
|
|
|
shell,
|
2025-05-23 12:40:10 -04:00
|
|
|
|
self.on_press.as_deref(),
|
|
|
|
|
|
self.on_press_down.as_deref(),
|
2023-09-01 07:29:19 +02:00
|
|
|
|
|| tree.state.downcast_mut::<State>(),
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-17 19:08:43 +02:00
|
|
|
|
#[allow(clippy::too_many_lines)]
|
2023-09-01 07:29:19 +02:00
|
|
|
|
fn draw(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
tree: &Tree,
|
2024-05-17 19:08:43 +02:00
|
|
|
|
renderer: &mut crate::Renderer,
|
|
|
|
|
|
theme: &crate::Theme,
|
2023-09-01 07:29:19 +02:00
|
|
|
|
renderer_style: &renderer::Style,
|
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
|
cursor: mouse::Cursor,
|
2024-10-22 16:44:29 -04:00
|
|
|
|
viewport: &Rectangle,
|
2023-09-01 07:29:19 +02:00
|
|
|
|
) {
|
|
|
|
|
|
let bounds = layout.bounds();
|
2024-10-22 16:44:29 -04:00
|
|
|
|
if !viewport.intersects(&bounds) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-06-10 16:56:02 -04:00
|
|
|
|
|
|
|
|
|
|
// FIXME: Why is there no content layout
|
|
|
|
|
|
let Some(content_layout) = layout.children().next() else {
|
|
|
|
|
|
return;
|
|
|
|
|
|
};
|
2023-09-01 07:29:19 +02:00
|
|
|
|
|
2024-05-17 19:08:43 +02:00
|
|
|
|
let mut headerbar_alpha = None;
|
|
|
|
|
|
|
2025-03-14 11:56:21 -04:00
|
|
|
|
let is_enabled =
|
|
|
|
|
|
self.on_press.is_some() || self.on_press_down.is_some() || self.force_enabled;
|
2024-05-17 19:08:43 +02:00
|
|
|
|
let is_mouse_over = cursor.position().is_some_and(|p| bounds.contains(p));
|
|
|
|
|
|
|
|
|
|
|
|
let state = tree.state.downcast_ref::<State>();
|
|
|
|
|
|
|
|
|
|
|
|
let styling = if !is_enabled {
|
|
|
|
|
|
theme.disabled(&self.style)
|
|
|
|
|
|
} else if is_mouse_over {
|
|
|
|
|
|
if state.is_pressed {
|
|
|
|
|
|
if !self.selected && matches!(self.style, crate::theme::Button::HeaderBar) {
|
|
|
|
|
|
headerbar_alpha = Some(0.8);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
theme.pressed(state.is_focused, self.selected, &self.style)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if !self.selected && matches!(self.style, crate::theme::Button::HeaderBar) {
|
|
|
|
|
|
headerbar_alpha = Some(0.8);
|
|
|
|
|
|
}
|
|
|
|
|
|
theme.hovered(state.is_focused, self.selected, &self.style)
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if !self.selected && matches!(self.style, crate::theme::Button::HeaderBar) {
|
|
|
|
|
|
headerbar_alpha = Some(0.75);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
theme.active(state.is_focused, self.selected, &self.style)
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let mut icon_color = styling.icon_color.unwrap_or(renderer_style.icon_color);
|
2024-05-21 04:34:35 +02:00
|
|
|
|
|
|
|
|
|
|
// Menu roots should share the accent color that icons get in the header.
|
|
|
|
|
|
let mut text_color = if matches!(self.style, crate::theme::Button::MenuRoot) {
|
|
|
|
|
|
icon_color
|
|
|
|
|
|
} else {
|
|
|
|
|
|
styling.text_color.unwrap_or(renderer_style.text_color)
|
|
|
|
|
|
};
|
2024-05-17 19:08:43 +02:00
|
|
|
|
|
|
|
|
|
|
if let Some(alpha) = headerbar_alpha {
|
|
|
|
|
|
icon_color.a = alpha;
|
|
|
|
|
|
text_color.a = alpha;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-15 16:04:42 -04:00
|
|
|
|
draw::<_, crate::Theme>(
|
|
|
|
|
|
renderer,
|
|
|
|
|
|
bounds,
|
2024-10-22 16:44:29 -04:00
|
|
|
|
*viewport,
|
2024-06-15 16:04:42 -04:00
|
|
|
|
&styling,
|
|
|
|
|
|
|renderer, _styling| {
|
|
|
|
|
|
self.content.as_widget().draw(
|
|
|
|
|
|
&tree.children[0],
|
|
|
|
|
|
renderer,
|
|
|
|
|
|
theme,
|
|
|
|
|
|
&renderer::Style {
|
|
|
|
|
|
icon_color,
|
|
|
|
|
|
text_color,
|
|
|
|
|
|
scale_factor: renderer_style.scale_factor,
|
|
|
|
|
|
},
|
2025-03-20 10:10:02 -04:00
|
|
|
|
content_layout.with_virtual_offset(layout.virtual_offset()),
|
2024-06-15 16:04:42 -04:00
|
|
|
|
cursor,
|
2024-10-22 18:52:43 -04:00
|
|
|
|
&viewport.intersection(&bounds).unwrap_or_default(),
|
2024-06-15 16:04:42 -04:00
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
matches!(self.variant, Variant::Image { .. }),
|
|
|
|
|
|
);
|
2023-10-30 16:32:10 +01:00
|
|
|
|
|
2023-11-15 16:09:17 +01:00
|
|
|
|
if let Variant::Image {
|
|
|
|
|
|
close_icon,
|
|
|
|
|
|
on_remove,
|
|
|
|
|
|
} = &self.variant
|
|
|
|
|
|
{
|
2024-10-22 16:44:29 -04:00
|
|
|
|
renderer.with_layer(*viewport, |renderer| {
|
2024-05-27 22:37:48 +02:00
|
|
|
|
let selection_background = theme.selection_background();
|
|
|
|
|
|
|
2024-08-02 20:00:16 +02:00
|
|
|
|
let c_rad = THEME.lock().unwrap().cosmic().corner_radii;
|
2024-05-27 22:37:48 +02:00
|
|
|
|
|
|
|
|
|
|
if self.selected {
|
|
|
|
|
|
renderer.fill_quad(
|
|
|
|
|
|
Quad {
|
|
|
|
|
|
bounds: Rectangle {
|
|
|
|
|
|
width: 24.0,
|
|
|
|
|
|
height: 20.0,
|
|
|
|
|
|
x: bounds.x + styling.border_width,
|
|
|
|
|
|
y: bounds.y + (bounds.height - 20.0 - styling.border_width),
|
2023-11-15 16:09:17 +01:00
|
|
|
|
},
|
2024-05-27 22:37:48 +02:00
|
|
|
|
border: Border {
|
|
|
|
|
|
radius: [
|
|
|
|
|
|
c_rad.radius_0[0],
|
|
|
|
|
|
c_rad.radius_s[1],
|
|
|
|
|
|
c_rad.radius_0[2],
|
|
|
|
|
|
c_rad.radius_s[3],
|
|
|
|
|
|
]
|
|
|
|
|
|
.into(),
|
|
|
|
|
|
..Default::default()
|
2023-11-15 16:09:17 +01:00
|
|
|
|
},
|
2024-05-27 22:37:48 +02:00
|
|
|
|
shadow: Shadow::default(),
|
|
|
|
|
|
},
|
|
|
|
|
|
selection_background,
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2024-10-16 20:36:46 -04:00
|
|
|
|
let svg_handle = svg::Svg::new(crate::widget::common::object_select().clone())
|
|
|
|
|
|
.color(icon_color);
|
2024-10-22 16:44:29 -04:00
|
|
|
|
let bounds = Rectangle {
|
|
|
|
|
|
width: 16.0,
|
|
|
|
|
|
height: 16.0,
|
|
|
|
|
|
x: bounds.x + 5.0 + styling.border_width,
|
|
|
|
|
|
y: bounds.y + (bounds.height - 18.0 - styling.border_width),
|
|
|
|
|
|
};
|
|
|
|
|
|
if bounds.intersects(viewport) {
|
|
|
|
|
|
iced_core::svg::Renderer::draw_svg(renderer, svg_handle, bounds);
|
|
|
|
|
|
}
|
2024-05-27 22:37:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if on_remove.is_some() {
|
|
|
|
|
|
if let Some(position) = cursor.position() {
|
|
|
|
|
|
if bounds.contains(position) {
|
|
|
|
|
|
let bounds = removal_bounds(layout.bounds(), 4.0);
|
|
|
|
|
|
renderer.fill_quad(
|
|
|
|
|
|
renderer::Quad {
|
|
|
|
|
|
bounds,
|
|
|
|
|
|
shadow: Shadow::default(),
|
|
|
|
|
|
border: Border {
|
|
|
|
|
|
radius: c_rad.radius_m.into(),
|
|
|
|
|
|
..Default::default()
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
selection_background,
|
|
|
|
|
|
);
|
2024-10-16 20:36:46 -04:00
|
|
|
|
let svg_handle = svg::Svg::new(close_icon.clone()).color(icon_color);
|
|
|
|
|
|
iced_core::svg::Renderer::draw_svg(
|
2024-05-27 22:37:48 +02:00
|
|
|
|
renderer,
|
2024-10-16 20:36:46 -04:00
|
|
|
|
svg_handle,
|
2024-05-27 22:37:48 +02:00
|
|
|
|
Rectangle {
|
|
|
|
|
|
width: 16.0,
|
|
|
|
|
|
height: 16.0,
|
|
|
|
|
|
x: bounds.x + 4.0,
|
|
|
|
|
|
y: bounds.y + 4.0,
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2023-11-15 16:09:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-05-27 22:37:48 +02:00
|
|
|
|
});
|
2023-10-30 16:32:10 +01:00
|
|
|
|
}
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn mouse_interaction(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
_tree: &Tree,
|
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
|
cursor: mouse::Cursor,
|
|
|
|
|
|
_viewport: &Rectangle,
|
2024-05-17 19:08:43 +02:00
|
|
|
|
_renderer: &crate::Renderer,
|
2023-09-01 07:29:19 +02:00
|
|
|
|
) -> mouse::Interaction {
|
2025-03-20 10:10:02 -04:00
|
|
|
|
mouse_interaction(
|
|
|
|
|
|
layout.with_virtual_offset(layout.virtual_offset()),
|
|
|
|
|
|
cursor,
|
|
|
|
|
|
self.on_press.is_some(),
|
|
|
|
|
|
)
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn overlay<'b>(
|
|
|
|
|
|
&'b mut self,
|
|
|
|
|
|
tree: &'b mut Tree,
|
|
|
|
|
|
layout: Layout<'_>,
|
2024-05-17 19:08:43 +02:00
|
|
|
|
renderer: &crate::Renderer,
|
2024-10-16 20:36:46 -04:00
|
|
|
|
mut translation: Vector,
|
2024-05-17 19:08:43 +02:00
|
|
|
|
) -> Option<overlay::Element<'b, Message, crate::Theme, crate::Renderer>> {
|
2024-10-22 16:44:29 -04:00
|
|
|
|
let position = layout.bounds().position();
|
2024-10-16 20:36:46 -04:00
|
|
|
|
translation.x += position.x;
|
|
|
|
|
|
translation.y += position.y;
|
2023-09-01 07:29:19 +02:00
|
|
|
|
self.content.as_widget_mut().overlay(
|
|
|
|
|
|
&mut tree.children[0],
|
2025-03-20 10:10:02 -04:00
|
|
|
|
layout
|
|
|
|
|
|
.children()
|
|
|
|
|
|
.next()
|
|
|
|
|
|
.unwrap()
|
|
|
|
|
|
.with_virtual_offset(layout.virtual_offset()),
|
2023-09-01 07:29:19 +02:00
|
|
|
|
renderer,
|
2024-10-16 20:36:46 -04:00
|
|
|
|
translation,
|
2023-09-01 07:29:19 +02:00
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "a11y")]
|
|
|
|
|
|
/// get the a11y nodes for the widget
|
|
|
|
|
|
fn a11y_nodes(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
|
state: &Tree,
|
|
|
|
|
|
p: mouse::Cursor,
|
|
|
|
|
|
) -> iced_accessibility::A11yTree {
|
|
|
|
|
|
use iced_accessibility::{
|
|
|
|
|
|
A11yNode, A11yTree,
|
2025-03-21 03:17:59 +01:00
|
|
|
|
accesskit::{Action, DefaultActionVerb, NodeBuilder, NodeId, Rect, Role},
|
2023-09-01 07:29:19 +02:00
|
|
|
|
};
|
2024-10-22 18:52:43 -04:00
|
|
|
|
// TODO why is state None sometimes?
|
|
|
|
|
|
if matches!(state.state, iced_core::widget::tree::State::None) {
|
|
|
|
|
|
tracing::info!("Button state is missing.");
|
|
|
|
|
|
return A11yTree::default();
|
|
|
|
|
|
}
|
2023-09-01 07:29:19 +02:00
|
|
|
|
|
|
|
|
|
|
let child_layout = layout.children().next().unwrap();
|
2024-10-22 18:52:43 -04:00
|
|
|
|
let child_tree = state.children.first();
|
2023-09-01 07:29:19 +02:00
|
|
|
|
|
|
|
|
|
|
let Rectangle {
|
|
|
|
|
|
x,
|
|
|
|
|
|
y,
|
|
|
|
|
|
width,
|
|
|
|
|
|
height,
|
|
|
|
|
|
} = layout.bounds();
|
|
|
|
|
|
let bounds = Rect::new(x as f64, y as f64, (x + width) as f64, (y + height) as f64);
|
|
|
|
|
|
let is_hovered = state.state.downcast_ref::<State>().is_hovered;
|
|
|
|
|
|
|
|
|
|
|
|
let mut node = NodeBuilder::new(Role::Button);
|
|
|
|
|
|
node.add_action(Action::Focus);
|
|
|
|
|
|
node.add_action(Action::Default);
|
|
|
|
|
|
node.set_bounds(bounds);
|
|
|
|
|
|
if let Some(name) = self.name.as_ref() {
|
|
|
|
|
|
node.set_name(name.clone());
|
|
|
|
|
|
}
|
|
|
|
|
|
match self.description.as_ref() {
|
|
|
|
|
|
Some(iced_accessibility::Description::Id(id)) => {
|
2025-03-14 11:56:21 -04:00
|
|
|
|
node.set_described_by(id.iter().cloned().map(NodeId::from).collect::<Vec<_>>());
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
Some(iced_accessibility::Description::Text(text)) => {
|
|
|
|
|
|
node.set_description(text.clone());
|
|
|
|
|
|
}
|
|
|
|
|
|
None => {}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(label) = self.label.as_ref() {
|
|
|
|
|
|
node.set_labelled_by(label.clone());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if self.on_press.is_none() {
|
2024-03-11 15:21:48 -04:00
|
|
|
|
node.set_disabled();
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
if is_hovered {
|
2024-03-11 15:21:48 -04:00
|
|
|
|
node.set_hovered();
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
node.set_default_action_verb(DefaultActionVerb::Click);
|
|
|
|
|
|
|
2024-10-22 18:52:43 -04:00
|
|
|
|
if let Some(child_tree) = child_tree.map(|child_tree| {
|
2025-03-20 10:10:02 -04:00
|
|
|
|
self.content.as_widget().a11y_nodes(
|
|
|
|
|
|
child_layout.with_virtual_offset(layout.virtual_offset()),
|
|
|
|
|
|
child_tree,
|
|
|
|
|
|
p,
|
|
|
|
|
|
)
|
2024-10-22 18:52:43 -04:00
|
|
|
|
}) {
|
|
|
|
|
|
A11yTree::node_with_child_tree(A11yNode::new(node, self.id.clone()), child_tree)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
A11yTree::leaf(node, self.id.clone())
|
|
|
|
|
|
}
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn id(&self) -> Option<Id> {
|
|
|
|
|
|
Some(self.id.clone())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn set_id(&mut self, id: Id) {
|
|
|
|
|
|
self.id = id;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-17 19:08:43 +02:00
|
|
|
|
impl<'a, Message: Clone + 'a> From<Button<'a, Message>> for crate::Element<'a, Message> {
|
|
|
|
|
|
fn from(button: Button<'a, Message>) -> Self {
|
2023-09-01 07:29:19 +02:00
|
|
|
|
Self::new(button)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// The local state of a [`Button`].
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
2023-11-28 19:02:08 +00:00
|
|
|
|
#[allow(clippy::struct_field_names)]
|
2023-09-01 07:29:19 +02:00
|
|
|
|
pub struct State {
|
|
|
|
|
|
is_hovered: bool,
|
|
|
|
|
|
is_pressed: bool,
|
|
|
|
|
|
is_focused: bool,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
|
|
/// Creates a new [`State`].
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2023-11-28 19:02:08 +00:00
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
|
Self::default()
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns whether the [`Button`] is currently focused or not.
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2023-09-01 07:29:19 +02:00
|
|
|
|
pub fn is_focused(self) -> bool {
|
|
|
|
|
|
self.is_focused
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns whether the [`Button`] is currently hovered or not.
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2023-09-01 07:29:19 +02:00
|
|
|
|
pub fn is_hovered(self) -> bool {
|
|
|
|
|
|
self.is_hovered
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Focuses the [`Button`].
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2023-09-01 07:29:19 +02:00
|
|
|
|
pub fn focus(&mut self) {
|
|
|
|
|
|
self.is_focused = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Unfocuses the [`Button`].
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2023-09-01 07:29:19 +02:00
|
|
|
|
pub fn unfocus(&mut self) {
|
|
|
|
|
|
self.is_focused = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Processes the given [`Event`] and updates the [`State`] of a [`Button`]
|
|
|
|
|
|
/// accordingly.
|
2025-05-23 12:40:10 -04:00
|
|
|
|
#[allow(clippy::needless_pass_by_value, clippy::too_many_arguments)]
|
2023-09-01 07:29:19 +02:00
|
|
|
|
pub fn update<'a, Message: Clone>(
|
|
|
|
|
|
_id: Id,
|
|
|
|
|
|
event: Event,
|
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
|
cursor: mouse::Cursor,
|
|
|
|
|
|
shell: &mut Shell<'_, Message>,
|
2025-05-23 12:40:10 -04:00
|
|
|
|
on_press: Option<&dyn Fn(Vector, Rectangle) -> Message>,
|
|
|
|
|
|
on_press_down: Option<&dyn Fn(Vector, Rectangle) -> Message>,
|
2023-09-01 07:29:19 +02:00
|
|
|
|
state: impl FnOnce() -> &'a mut State,
|
|
|
|
|
|
) -> event::Status {
|
|
|
|
|
|
match event {
|
|
|
|
|
|
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
|
|
|
|
|
| Event::Touch(touch::Event::FingerPressed { .. }) => {
|
2025-03-24 04:27:41 +01:00
|
|
|
|
// Unfocus the button on clicks in case another widget was clicked.
|
|
|
|
|
|
let state = state();
|
|
|
|
|
|
state.unfocus();
|
|
|
|
|
|
|
2024-05-17 20:46:24 -04:00
|
|
|
|
if on_press.is_some() || on_press_down.is_some() {
|
2023-09-01 07:29:19 +02:00
|
|
|
|
let bounds = layout.bounds();
|
|
|
|
|
|
|
|
|
|
|
|
if cursor.is_over(bounds) {
|
|
|
|
|
|
state.is_pressed = true;
|
|
|
|
|
|
|
2024-05-17 20:46:24 -04:00
|
|
|
|
if let Some(on_press_down) = on_press_down {
|
2025-05-23 12:40:10 -04:00
|
|
|
|
let msg = (on_press_down)(layout.virtual_offset(), layout.bounds());
|
|
|
|
|
|
shell.publish(msg);
|
2024-05-17 20:46:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-01 07:29:19 +02:00
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
|
|
|
|
|
|
| Event::Touch(touch::Event::FingerLifted { .. }) => {
|
|
|
|
|
|
if let Some(on_press) = on_press.clone() {
|
|
|
|
|
|
let state = state();
|
|
|
|
|
|
|
|
|
|
|
|
if state.is_pressed {
|
|
|
|
|
|
state.is_pressed = false;
|
|
|
|
|
|
|
|
|
|
|
|
let bounds = layout.bounds();
|
|
|
|
|
|
|
|
|
|
|
|
if cursor.is_over(bounds) {
|
2025-05-23 12:40:10 -04:00
|
|
|
|
let msg = (on_press)(layout.virtual_offset(), layout.bounds());
|
|
|
|
|
|
shell.publish(msg);
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
|
}
|
2024-05-17 20:46:24 -04:00
|
|
|
|
} else if on_press_down.is_some() {
|
|
|
|
|
|
let state = state();
|
|
|
|
|
|
state.is_pressed = false;
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#[cfg(feature = "a11y")]
|
|
|
|
|
|
Event::A11y(event_id, iced_accessibility::accesskit::ActionRequest { action, .. }) => {
|
|
|
|
|
|
let state = state();
|
2024-02-08 19:22:18 -05:00
|
|
|
|
if let Some(Some(on_press)) = (event_id == event_id
|
2023-09-01 07:29:19 +02:00
|
|
|
|
&& matches!(action, iced_accessibility::accesskit::Action::Default))
|
|
|
|
|
|
.then(|| on_press.clone())
|
|
|
|
|
|
{
|
|
|
|
|
|
state.is_pressed = false;
|
2025-05-23 12:40:10 -04:00
|
|
|
|
let msg = (on_press)(layout.virtual_offset(), layout.bounds());
|
|
|
|
|
|
|
|
|
|
|
|
shell.publish(msg);
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
|
}
|
2024-01-30 22:14:00 -05:00
|
|
|
|
Event::Keyboard(keyboard::Event::KeyPressed { key, .. }) => {
|
2023-09-01 07:29:19 +02:00
|
|
|
|
if let Some(on_press) = on_press.clone() {
|
|
|
|
|
|
let state = state();
|
2024-01-30 22:14:00 -05:00
|
|
|
|
if state.is_focused && key == keyboard::Key::Named(keyboard::key::Named::Enter) {
|
2023-09-01 07:29:19 +02:00
|
|
|
|
state.is_pressed = true;
|
2025-05-23 12:40:10 -04:00
|
|
|
|
let msg = (on_press)(layout.virtual_offset(), layout.bounds());
|
|
|
|
|
|
|
|
|
|
|
|
shell.publish(msg);
|
2023-09-01 07:29:19 +02:00
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
Event::Touch(touch::Event::FingerLost { .. }) | Event::Mouse(mouse::Event::CursorLeft) => {
|
|
|
|
|
|
let state = state();
|
|
|
|
|
|
state.is_hovered = false;
|
|
|
|
|
|
state.is_pressed = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => {}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
event::Status::Ignored
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-30 16:32:10 +01:00
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
2024-05-17 19:08:43 +02:00
|
|
|
|
pub fn draw<Renderer: iced_core::Renderer, Theme>(
|
2023-09-01 07:29:19 +02:00
|
|
|
|
renderer: &mut Renderer,
|
|
|
|
|
|
bounds: Rectangle,
|
2024-10-22 16:44:29 -04:00
|
|
|
|
viewport_bounds: Rectangle,
|
2024-10-16 20:36:46 -04:00
|
|
|
|
styling: &super::style::Style,
|
|
|
|
|
|
draw_contents: impl FnOnce(&mut Renderer, &Style),
|
2024-06-15 16:04:42 -04:00
|
|
|
|
is_image: bool,
|
2024-05-17 19:08:43 +02:00
|
|
|
|
) where
|
2024-10-16 20:36:46 -04:00
|
|
|
|
Theme: super::style::Catalog,
|
2023-09-01 07:29:19 +02:00
|
|
|
|
{
|
|
|
|
|
|
let doubled_border_width = styling.border_width * 2.0;
|
|
|
|
|
|
let doubled_outline_width = styling.outline_width * 2.0;
|
|
|
|
|
|
|
|
|
|
|
|
if styling.outline_width > 0.0 {
|
|
|
|
|
|
renderer.fill_quad(
|
|
|
|
|
|
renderer::Quad {
|
|
|
|
|
|
bounds: Rectangle {
|
|
|
|
|
|
x: bounds.x - styling.border_width - styling.outline_width,
|
|
|
|
|
|
y: bounds.y - styling.border_width - styling.outline_width,
|
|
|
|
|
|
width: bounds.width + doubled_border_width + doubled_outline_width,
|
|
|
|
|
|
height: bounds.height + doubled_border_width + doubled_outline_width,
|
|
|
|
|
|
},
|
2024-01-30 22:14:00 -05:00
|
|
|
|
border: Border {
|
|
|
|
|
|
width: styling.outline_width,
|
|
|
|
|
|
color: styling.outline_color,
|
|
|
|
|
|
radius: styling.border_radius,
|
|
|
|
|
|
},
|
2024-02-22 21:05:21 +01:00
|
|
|
|
shadow: Shadow::default(),
|
2023-09-01 07:29:19 +02:00
|
|
|
|
},
|
|
|
|
|
|
Color::TRANSPARENT,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if styling.background.is_some() || styling.border_width > 0.0 {
|
|
|
|
|
|
if styling.shadow_offset != Vector::default() {
|
|
|
|
|
|
// TODO: Implement proper shadow support
|
|
|
|
|
|
renderer.fill_quad(
|
|
|
|
|
|
renderer::Quad {
|
|
|
|
|
|
bounds: Rectangle {
|
|
|
|
|
|
x: bounds.x + styling.shadow_offset.x,
|
|
|
|
|
|
y: bounds.y + styling.shadow_offset.y,
|
2023-10-30 16:32:10 +01:00
|
|
|
|
width: bounds.width,
|
|
|
|
|
|
height: bounds.height,
|
2023-09-01 07:29:19 +02:00
|
|
|
|
},
|
2024-01-30 22:14:00 -05:00
|
|
|
|
border: Border {
|
|
|
|
|
|
radius: styling.border_radius,
|
|
|
|
|
|
..Default::default()
|
|
|
|
|
|
},
|
|
|
|
|
|
shadow: Shadow::default(),
|
2023-09-01 07:29:19 +02:00
|
|
|
|
},
|
|
|
|
|
|
Background::Color([0.0, 0.0, 0.0, 0.5].into()),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-11 01:10:05 +01:00
|
|
|
|
// Draw the button background first.
|
|
|
|
|
|
if let Some(background) = styling.background {
|
|
|
|
|
|
renderer.fill_quad(
|
|
|
|
|
|
renderer::Quad {
|
|
|
|
|
|
bounds,
|
2024-01-30 22:14:00 -05:00
|
|
|
|
border: Border {
|
|
|
|
|
|
radius: styling.border_radius,
|
|
|
|
|
|
..Default::default()
|
|
|
|
|
|
},
|
2024-02-22 21:05:21 +01:00
|
|
|
|
shadow: Shadow::default(),
|
2023-11-11 01:10:05 +01:00
|
|
|
|
},
|
|
|
|
|
|
background,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-17 15:16:43 -04:00
|
|
|
|
// Then button overlay if any.
|
|
|
|
|
|
if let Some(overlay) = styling.overlay {
|
|
|
|
|
|
renderer.fill_quad(
|
|
|
|
|
|
renderer::Quad {
|
|
|
|
|
|
bounds,
|
|
|
|
|
|
border: Border {
|
|
|
|
|
|
radius: styling.border_radius,
|
|
|
|
|
|
..Default::default()
|
|
|
|
|
|
},
|
|
|
|
|
|
shadow: Shadow::default(),
|
|
|
|
|
|
},
|
|
|
|
|
|
overlay,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-11 01:10:05 +01:00
|
|
|
|
// Then draw the button contents onto the background.
|
|
|
|
|
|
draw_contents(renderer, styling);
|
|
|
|
|
|
|
2024-10-22 18:52:43 -04:00
|
|
|
|
let mut clipped_bounds = viewport_bounds.intersection(&bounds).unwrap_or_default();
|
2024-05-27 22:37:48 +02:00
|
|
|
|
clipped_bounds.height += styling.border_width;
|
2025-08-13 21:39:29 +02:00
|
|
|
|
clipped_bounds.width += 1.0;
|
2024-05-27 22:37:48 +02:00
|
|
|
|
|
2025-08-13 21:39:29 +02:00
|
|
|
|
// Finish by drawing the border above the contents.
|
2024-05-27 22:37:48 +02:00
|
|
|
|
renderer.with_layer(clipped_bounds, |renderer| {
|
|
|
|
|
|
renderer.fill_quad(
|
|
|
|
|
|
renderer::Quad {
|
|
|
|
|
|
bounds,
|
|
|
|
|
|
border: Border {
|
|
|
|
|
|
width: styling.border_width,
|
|
|
|
|
|
color: styling.border_color,
|
|
|
|
|
|
radius: styling.border_radius,
|
|
|
|
|
|
},
|
|
|
|
|
|
shadow: Shadow::default(),
|
|
|
|
|
|
},
|
|
|
|
|
|
Color::TRANSPARENT,
|
|
|
|
|
|
);
|
2025-08-13 21:39:29 +02:00
|
|
|
|
})
|
2023-11-11 01:10:05 +01:00
|
|
|
|
} else {
|
|
|
|
|
|
draw_contents(renderer, styling);
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Computes the layout of a [`Button`].
|
|
|
|
|
|
pub fn layout<Renderer>(
|
|
|
|
|
|
renderer: &Renderer,
|
|
|
|
|
|
limits: &layout::Limits,
|
|
|
|
|
|
width: Length,
|
|
|
|
|
|
height: Length,
|
|
|
|
|
|
padding: Padding,
|
|
|
|
|
|
layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node,
|
|
|
|
|
|
) -> layout::Node {
|
|
|
|
|
|
let limits = limits.width(width).height(height);
|
|
|
|
|
|
|
2024-01-30 22:14:00 -05:00
|
|
|
|
let mut content = layout_content(renderer, &limits.shrink(padding));
|
2023-09-01 07:29:19 +02:00
|
|
|
|
let padding = padding.fit(content.size(), limits.max());
|
2024-01-30 22:14:00 -05:00
|
|
|
|
let size = limits
|
|
|
|
|
|
.shrink(padding)
|
|
|
|
|
|
.resolve(width, height, content.size())
|
|
|
|
|
|
.expand(padding);
|
2023-09-01 07:29:19 +02:00
|
|
|
|
|
2024-01-30 22:14:00 -05:00
|
|
|
|
content = content.move_to(Point::new(padding.left, padding.top));
|
2023-09-01 07:29:19 +02:00
|
|
|
|
|
|
|
|
|
|
layout::Node::with_children(size, vec![content])
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns the [`mouse::Interaction`] of a [`Button`].
|
|
|
|
|
|
#[must_use]
|
|
|
|
|
|
pub fn mouse_interaction(
|
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
|
cursor: mouse::Cursor,
|
|
|
|
|
|
is_enabled: bool,
|
|
|
|
|
|
) -> mouse::Interaction {
|
|
|
|
|
|
let is_mouse_over = cursor.is_over(layout.bounds());
|
|
|
|
|
|
|
|
|
|
|
|
if is_mouse_over && is_enabled {
|
|
|
|
|
|
mouse::Interaction::Pointer
|
|
|
|
|
|
} else {
|
|
|
|
|
|
mouse::Interaction::default()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-16 20:36:46 -04:00
|
|
|
|
/// Produces a [`Task`] that focuses the [`Button`] with the given [`Id`].
|
|
|
|
|
|
pub fn focus<Message: 'static>(id: Id) -> Task<Message> {
|
|
|
|
|
|
task::effect(Action::Widget(Box::new(operation::focusable::focus(id))))
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl operation::Focusable for State {
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2023-09-01 07:29:19 +02:00
|
|
|
|
fn is_focused(&self) -> bool {
|
2023-11-28 19:02:08 +00:00
|
|
|
|
Self::is_focused(*self)
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2023-09-01 07:29:19 +02:00
|
|
|
|
fn focus(&mut self) {
|
2023-11-28 19:02:08 +00:00
|
|
|
|
Self::focus(self);
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-21 03:17:59 +01:00
|
|
|
|
#[inline]
|
2023-09-01 07:29:19 +02:00
|
|
|
|
fn unfocus(&mut self) {
|
2023-11-28 19:02:08 +00:00
|
|
|
|
Self::unfocus(self);
|
2023-09-01 07:29:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-11-15 16:09:17 +01:00
|
|
|
|
|
|
|
|
|
|
fn removal_bounds(bounds: Rectangle, offset: f32) -> Rectangle {
|
|
|
|
|
|
Rectangle {
|
|
|
|
|
|
x: bounds.x + bounds.width - 12.0 - offset,
|
|
|
|
|
|
y: bounds.y - 12.0 + offset,
|
|
|
|
|
|
width: 24.0,
|
|
|
|
|
|
height: 24.0,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|