Skip to main content

cosmic/widget/button/
widget.rs

1// Copyright 2019 H�ctor Ram�n, Iced contributors
2// Copyright 2023 System76 <info@system76.com>
3// SPDX-License-Identifier: MIT
4
5//! Allow your users to perform actions by pressing a button.
6//!
7//! A [`Button`] has some local [`State`].
8
9use iced::Alignment;
10use iced_runtime::core::widget::Id;
11use iced_runtime::{Action, Task, keyboard, task};
12
13use iced_core::event::Event;
14use iced_core::renderer::{self, Quad, Renderer};
15use iced_core::widget::Operation;
16use iced_core::widget::tree::{self, Tree};
17use iced_core::{
18    Background, Border, Clipboard, Color, Layout, Length, Padding, Point, Rectangle, Shadow, Shell,
19    Vector, Widget, layout, mouse, overlay, svg, touch,
20};
21use iced_renderer::core::widget::operation;
22
23use crate::theme::THEME;
24
25pub use super::style::{Catalog, Style};
26
27/// Internally defines different button widget variants.
28enum Variant<Message> {
29    Normal,
30    Image {
31        close_icon: svg::Handle,
32        on_remove: Option<Message>,
33    },
34}
35
36/// A generic button which emits a message when pressed.
37#[allow(missing_debug_implementations)]
38#[must_use]
39pub struct Button<'a, Message> {
40    id: Id,
41    #[cfg(feature = "a11y")]
42    name: Option<std::borrow::Cow<'a, str>>,
43    #[cfg(feature = "a11y")]
44    description: Option<iced_accessibility::Description<'a>>,
45    #[cfg(feature = "a11y")]
46    label: Option<Vec<iced_accessibility::accesskit::NodeId>>,
47    content: crate::Element<'a, Message>,
48    on_press: Option<Box<dyn Fn(Vector, Rectangle) -> Message + 'a>>,
49    on_press_down: Option<Box<dyn Fn(Vector, Rectangle) -> Message + 'a>>,
50    width: Length,
51    height: Length,
52    padding: Padding,
53    selected: bool,
54    style: crate::theme::Button,
55    variant: Variant<Message>,
56    force_enabled: bool,
57}
58
59impl<'a, Message: Clone + 'a> Button<'a, Message> {
60    /// Creates a new [`Button`] with the given content.
61    pub(super) fn new(content: impl Into<crate::Element<'a, Message>>) -> Self {
62        Self {
63            id: Id::unique(),
64            #[cfg(feature = "a11y")]
65            name: None,
66            #[cfg(feature = "a11y")]
67            description: None,
68            #[cfg(feature = "a11y")]
69            label: None,
70            content: content.into(),
71            on_press: None,
72            on_press_down: None,
73            width: Length::Shrink,
74            height: Length::Shrink,
75            padding: Padding::new(5.0),
76            selected: false,
77            style: crate::theme::Button::default(),
78            variant: Variant::Normal,
79            force_enabled: false,
80        }
81    }
82
83    pub fn new_image(
84        content: impl Into<crate::Element<'a, Message>>,
85        on_remove: Option<Message>,
86    ) -> Self {
87        Self {
88            id: Id::unique(),
89            #[cfg(feature = "a11y")]
90            name: None,
91            #[cfg(feature = "a11y")]
92            description: None,
93            force_enabled: false,
94            #[cfg(feature = "a11y")]
95            label: None,
96            content: content.into(),
97            on_press: None,
98            on_press_down: None,
99            width: Length::Shrink,
100            height: Length::Shrink,
101            padding: Padding::new(5.0),
102            selected: false,
103            style: crate::theme::Button::default(),
104            variant: Variant::Image {
105                on_remove,
106                close_icon: crate::widget::icon::from_name("window-close-symbolic")
107                    .size(8)
108                    .icon()
109                    .into_svg_handle()
110                    .unwrap_or_else(|| {
111                        let bytes: &'static [u8] = &[];
112                        iced_core::svg::Handle::from_memory(bytes)
113                    }),
114            },
115        }
116    }
117
118    /// Sets the [`Id`] of the [`Button`].
119    #[inline]
120    pub fn id(mut self, id: Id) -> Self {
121        self.id = id;
122        self
123    }
124
125    /// Sets the width of the [`Button`].
126    #[inline]
127    pub fn width(mut self, width: impl Into<Length>) -> Self {
128        self.width = width.into();
129        self
130    }
131
132    /// Sets the height of the [`Button`].
133    #[inline]
134    pub fn height(mut self, height: impl Into<Length>) -> Self {
135        self.height = height.into();
136        self
137    }
138
139    /// Sets the [`Padding`] of the [`Button`].
140    #[inline]
141    pub fn padding<P: Into<Padding>>(mut self, padding: P) -> Self {
142        self.padding = padding.into();
143        self
144    }
145
146    /// Sets the message that will be produced when the [`Button`] is pressed and released.
147    ///
148    /// Unless `on_press` or `on_press_down` is called, the [`Button`] will be disabled.
149    #[inline]
150    pub fn on_press(mut self, on_press: Message) -> Self {
151        self.on_press = Some(Box::new(move |_, _| on_press.clone()));
152        self
153    }
154
155    /// Sets the message that will be produced when the [`Button`] is pressed and released.
156    ///
157    /// Unless `on_press` or `on_press_down` is called, the [`Button`] will be disabled.
158    #[inline]
159    pub fn on_press_with_rectangle(
160        mut self,
161        on_press: impl Fn(Vector, Rectangle) -> Message + 'a,
162    ) -> Self {
163        self.on_press = Some(Box::new(on_press));
164        self
165    }
166
167    /// Sets the message that will be produced when the [`Button`] is pressed,
168    ///
169    /// Unless `on_press` or `on_press_down` is called, the [`Button`] will be disabled.
170    #[inline]
171    pub fn on_press_down(mut self, on_press: Message) -> Self {
172        self.on_press_down = Some(Box::new(move |_, _| on_press.clone()));
173        self
174    }
175
176    /// Sets the message that will be produced when the [`Button`] is pressed,
177    ///
178    /// Unless `on_press` or `on_press_down` is called, the [`Button`] will be disabled.
179    #[inline]
180    pub fn on_press_down_with_rectangle(
181        mut self,
182        on_press: impl Fn(Vector, Rectangle) -> Message + 'a,
183    ) -> Self {
184        self.on_press_down = Some(Box::new(on_press));
185        self
186    }
187
188    /// Sets the message that will be produced when the [`Button`] is pressed,
189    /// if `Some`.
190    ///
191    /// If `None`, the [`Button`] will be disabled.
192    #[inline]
193    pub fn on_press_maybe(mut self, on_press: Option<Message>) -> Self {
194        if let Some(m) = on_press {
195            self.on_press(m)
196        } else {
197            self.on_press = None;
198            self
199        }
200    }
201
202    /// Sets the message that will be produced when the [`Button`] is pressed and released.
203    ///
204    /// Unless `on_press` or `on_press_down` is called, the [`Button`] will be disabled.
205    #[inline]
206    pub fn on_press_maybe_with_rectangle(
207        mut self,
208        on_press: impl Fn(Vector, Rectangle) -> Message + 'a,
209    ) -> Self {
210        self.on_press = Some(Box::new(on_press));
211        self
212    }
213
214    /// Sets the message that will be produced when the [`Button`] is pressed,
215    /// if `Some`.
216    ///
217    /// If `None`, the [`Button`] will be disabled.
218    #[inline]
219    pub fn on_press_down_maybe(mut self, on_press: Option<Message>) -> Self {
220        if let Some(m) = on_press {
221            self.on_press(m)
222        } else {
223            self.on_press_down = None;
224            self
225        }
226    }
227
228    /// Sets the message that will be produced when the [`Button`] is pressed and released.
229    ///
230    /// Unless `on_press` or `on_press_down` is called, the [`Button`] will be disabled.
231    #[inline]
232    pub fn on_press_down_maybe_with_rectangle(
233        mut self,
234        on_press: impl Fn(Vector, Rectangle) -> Message + 'a,
235    ) -> Self {
236        self.on_press_down = Some(Box::new(on_press));
237        self
238    }
239
240    /// Sets the the [`Button`] to enabled whether or not it has handlers for on press.
241    #[inline]
242    pub fn force_enabled(mut self, enabled: bool) -> Self {
243        self.force_enabled = enabled;
244        self
245    }
246
247    /// Sets the widget to a selected state.
248    ///
249    /// Displays a selection indicator on image buttons.
250    #[inline]
251    pub fn selected(mut self, selected: bool) -> Self {
252        self.selected = selected;
253
254        self
255    }
256
257    /// Sets the style variant of this [`Button`].
258    #[inline]
259    pub fn class(mut self, style: crate::theme::Button) -> Self {
260        self.style = style;
261        self
262    }
263
264    #[cfg(feature = "a11y")]
265    /// Sets the name of the [`Button`].
266    pub fn name(mut self, name: impl Into<std::borrow::Cow<'a, str>>) -> Self {
267        self.name = Some(name.into());
268        self
269    }
270
271    #[cfg(feature = "a11y")]
272    /// Sets the description of the [`Button`].
273    pub fn description_widget<T: iced_accessibility::Describes>(mut self, description: &T) -> Self {
274        self.description = Some(iced_accessibility::Description::Id(
275            description.description(),
276        ));
277        self
278    }
279
280    #[cfg(feature = "a11y")]
281    /// Sets the description of the [`Button`].
282    pub fn description(mut self, description: impl Into<std::borrow::Cow<'a, str>>) -> Self {
283        self.description = Some(iced_accessibility::Description::Text(description.into()));
284        self
285    }
286
287    #[cfg(feature = "a11y")]
288    /// Sets the label of the [`Button`].
289    pub fn label(mut self, label: &dyn iced_accessibility::Labels) -> Self {
290        self.label = Some(label.label().into_iter().map(|l| l.into()).collect());
291        self
292    }
293}
294
295impl<'a, Message: 'a + Clone> Widget<Message, crate::Theme, crate::Renderer>
296    for Button<'a, Message>
297{
298    fn tag(&self) -> tree::Tag {
299        tree::Tag::of::<State>()
300    }
301
302    fn state(&self) -> tree::State {
303        tree::State::new(State::new())
304    }
305
306    fn children(&self) -> Vec<Tree> {
307        vec![Tree::new(&self.content)]
308    }
309
310    fn diff(&mut self, tree: &mut Tree) {
311        tree.diff_children(std::slice::from_mut(&mut self.content));
312    }
313
314    fn size(&self) -> iced_core::Size<Length> {
315        iced_core::Size::new(self.width, self.height)
316    }
317
318    fn layout(
319        &mut self,
320        tree: &mut Tree,
321        renderer: &crate::Renderer,
322        limits: &layout::Limits,
323    ) -> layout::Node {
324        layout(
325            renderer,
326            limits,
327            self.width,
328            self.height,
329            self.padding,
330            |renderer, limits| {
331                self.content
332                    .as_widget_mut()
333                    .layout(&mut tree.children[0], renderer, limits)
334            },
335        )
336    }
337
338    fn operate(
339        &mut self,
340        tree: &mut Tree,
341        layout: Layout<'_>,
342        renderer: &crate::Renderer,
343        operation: &mut dyn Operation<()>,
344    ) {
345        operation.container(None, layout.bounds());
346        operation.traverse(&mut |operation| {
347            self.content.as_widget_mut().operate(
348                &mut tree.children[0],
349                layout
350                    .children()
351                    .next()
352                    .unwrap()
353                    .with_virtual_offset(layout.virtual_offset()),
354                renderer,
355                operation,
356            );
357        });
358        let state = tree.state.downcast_mut::<State>();
359        operation.focusable(Some(&self.id), layout.bounds(), state);
360    }
361
362    fn update(
363        &mut self,
364        tree: &mut Tree,
365        event: &Event,
366        layout: Layout<'_>,
367        cursor: mouse::Cursor,
368        renderer: &crate::Renderer,
369        clipboard: &mut dyn Clipboard,
370        shell: &mut Shell<'_, Message>,
371        viewport: &Rectangle,
372    ) {
373        if let Variant::Image {
374            on_remove: Some(on_remove),
375            ..
376        } = &self.variant
377        {
378            // Capture mouse/touch events on the removal button
379            match event {
380                Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
381                | Event::Touch(touch::Event::FingerPressed { .. }) => {
382                    if let Some(position) = cursor.position()
383                        && removal_bounds(layout.bounds(), 4.0).contains(position)
384                    {
385                        shell.publish(on_remove.clone());
386                        shell.capture_event();
387                        return;
388                    }
389                }
390
391                _ => (),
392            }
393        }
394        self.content.as_widget_mut().update(
395            &mut tree.children[0],
396            event,
397            layout
398                .children()
399                .next()
400                .unwrap()
401                .with_virtual_offset(layout.virtual_offset()),
402            cursor,
403            renderer,
404            clipboard,
405            shell,
406            viewport,
407        );
408        if shell.is_event_captured() {
409            return;
410        }
411
412        update(
413            self.id.clone(),
414            event,
415            layout,
416            cursor,
417            shell,
418            self.on_press.as_deref(),
419            self.on_press_down.as_deref(),
420            || tree.state.downcast_mut::<State>(),
421        )
422    }
423
424    #[allow(clippy::too_many_lines)]
425    fn draw(
426        &self,
427        tree: &Tree,
428        renderer: &mut crate::Renderer,
429        theme: &crate::Theme,
430        renderer_style: &renderer::Style,
431        layout: Layout<'_>,
432        cursor: mouse::Cursor,
433        viewport: &Rectangle,
434    ) {
435        let bounds = layout.bounds();
436        if !viewport.intersects(&bounds) {
437            return;
438        }
439
440        // FIXME: Why is there no content layout
441        let Some(content_layout) = layout.children().next() else {
442            return;
443        };
444
445        let mut headerbar_alpha = None;
446
447        let is_enabled =
448            self.on_press.is_some() || self.on_press_down.is_some() || self.force_enabled;
449        let is_mouse_over = cursor.position().is_some_and(|p| bounds.contains(p));
450
451        let state = tree.state.downcast_ref::<State>();
452
453        let mut styling = if !is_enabled {
454            theme.disabled(&self.style)
455        } else if is_mouse_over {
456            if state.is_pressed {
457                if !self.selected && matches!(self.style, crate::theme::Button::HeaderBar) {
458                    headerbar_alpha = Some(0.8);
459                }
460
461                theme.pressed(state.is_focused, self.selected, &self.style)
462            } else {
463                if !self.selected && matches!(self.style, crate::theme::Button::HeaderBar) {
464                    headerbar_alpha = Some(0.8);
465                }
466                theme.hovered(state.is_focused, self.selected, &self.style)
467            }
468        } else {
469            if !self.selected && matches!(self.style, crate::theme::Button::HeaderBar) {
470                headerbar_alpha = Some(0.75);
471            }
472
473            theme.active(state.is_focused, self.selected, &self.style)
474        };
475        if matches!(
476            self.style,
477            crate::theme::Button::MenuItem | crate::theme::Button::MenuFolder
478        ) {
479            match theme.list_item_position {
480                Some((Alignment::Start, _)) => {
481                    styling.border_radius =
482                        styling.border_radius.bottom(theme.cosmic().radius_0()[3]);
483                }
484                Some((Alignment::End, _)) => {
485                    styling.border_radius = styling.border_radius.top(theme.cosmic().radius_0()[0]);
486                }
487                Some((Alignment::Center, _)) => {}
488                None => {
489                    styling.border_radius = theme.cosmic().radius_0().into();
490                }
491            }
492        }
493
494        let mut icon_color = styling.icon_color.unwrap_or(renderer_style.icon_color);
495
496        // Menu roots should share the accent color that icons get in the header.
497        let mut text_color = if matches!(self.style, crate::theme::Button::MenuRoot) {
498            icon_color
499        } else {
500            styling.text_color.unwrap_or(renderer_style.text_color)
501        };
502
503        if let Some(alpha) = headerbar_alpha {
504            icon_color.a = alpha;
505            text_color.a = alpha;
506        }
507
508        draw::<_, crate::Theme>(
509            renderer,
510            bounds,
511            *viewport,
512            &styling,
513            |renderer, _styling| {
514                self.content.as_widget().draw(
515                    &tree.children[0],
516                    renderer,
517                    theme,
518                    &renderer::Style {
519                        icon_color,
520                        text_color,
521                        scale_factor: renderer_style.scale_factor,
522                    },
523                    content_layout.with_virtual_offset(layout.virtual_offset()),
524                    cursor,
525                    &viewport.intersection(&bounds).unwrap_or_default(),
526                );
527            },
528            matches!(self.variant, Variant::Image { .. }),
529        );
530
531        if let Variant::Image {
532            close_icon,
533            on_remove,
534        } = &self.variant
535        {
536            renderer.with_layer(*viewport, |renderer| {
537                let selection_background = theme.selection_background();
538
539                let c_rad = THEME.lock().unwrap().cosmic().corner_radii;
540
541                if self.selected {
542                    renderer.fill_quad(
543                        Quad {
544                            bounds: Rectangle {
545                                width: 24.0,
546                                height: 20.0,
547                                x: bounds.x + styling.border_width,
548                                y: bounds.y + (bounds.height - 20.0 - styling.border_width),
549                            },
550                            border: Border {
551                                radius: [
552                                    c_rad.radius_0[0],
553                                    c_rad.radius_s[1],
554                                    c_rad.radius_0[2],
555                                    c_rad.radius_s[3],
556                                ]
557                                .into(),
558                                ..Default::default()
559                            },
560                            shadow: Shadow::default(),
561                            snap: true,
562                        },
563                        selection_background,
564                    );
565
566                    let svg_handle = svg::Svg::new(crate::widget::common::object_select().clone())
567                        .color(icon_color);
568                    let bounds = Rectangle {
569                        width: 16.0,
570                        height: 16.0,
571                        x: bounds.x + 5.0 + styling.border_width,
572                        y: bounds.y + (bounds.height - 18.0 - styling.border_width),
573                    };
574                    if bounds.intersects(viewport) {
575                        iced_core::svg::Renderer::draw_svg(renderer, svg_handle, bounds, bounds);
576                    }
577                }
578
579                if on_remove.is_some()
580                    && let Some(position) = cursor.position()
581                    && bounds.contains(position)
582                {
583                    let bounds = removal_bounds(layout.bounds(), 4.0);
584                    renderer.fill_quad(
585                        renderer::Quad {
586                            bounds,
587                            shadow: Shadow::default(),
588                            border: Border {
589                                radius: c_rad.radius_m.into(),
590                                ..Default::default()
591                            },
592                            snap: true,
593                        },
594                        selection_background,
595                    );
596                    let svg_handle = svg::Svg::new(close_icon.clone()).color(icon_color);
597                    iced_core::svg::Renderer::draw_svg(
598                        renderer,
599                        svg_handle,
600                        Rectangle {
601                            width: 16.0,
602                            height: 16.0,
603                            x: bounds.x + 4.0,
604                            y: bounds.y + 4.0,
605                        },
606                        Rectangle {
607                            width: 16.0,
608                            height: 16.0,
609                            x: bounds.x + 4.0,
610                            y: bounds.y + 4.0,
611                        },
612                    );
613                }
614            });
615        }
616    }
617
618    fn mouse_interaction(
619        &self,
620        _tree: &Tree,
621        layout: Layout<'_>,
622        cursor: mouse::Cursor,
623        _viewport: &Rectangle,
624        _renderer: &crate::Renderer,
625    ) -> mouse::Interaction {
626        mouse_interaction(
627            layout.with_virtual_offset(layout.virtual_offset()),
628            cursor,
629            self.on_press.is_some(),
630        )
631    }
632
633    fn overlay<'b>(
634        &'b mut self,
635        tree: &'b mut Tree,
636        layout: Layout<'b>,
637        renderer: &crate::Renderer,
638        viewport: &Rectangle,
639        mut translation: Vector,
640    ) -> Option<overlay::Element<'b, Message, crate::Theme, crate::Renderer>> {
641        let position = layout.bounds().position();
642        translation.x += position.x;
643        translation.y += position.y;
644        self.content.as_widget_mut().overlay(
645            &mut tree.children[0],
646            layout
647                .children()
648                .next()
649                .unwrap()
650                .with_virtual_offset(layout.virtual_offset()),
651            renderer,
652            viewport,
653            translation,
654        )
655    }
656
657    #[cfg(feature = "a11y")]
658    /// get the a11y nodes for the widget
659    fn a11y_nodes(
660        &self,
661        layout: Layout<'_>,
662        state: &Tree,
663        p: mouse::Cursor,
664    ) -> iced_accessibility::A11yTree {
665        use iced_accessibility::accesskit::{Action, Node, NodeId, Rect, Role};
666        use iced_accessibility::{A11yNode, A11yTree};
667        // TODO why is state None sometimes?
668        if matches!(state.state, iced_core::widget::tree::State::None) {
669            tracing::info!("Button state is missing.");
670            return A11yTree::default();
671        }
672
673        let child_layout = layout.children().next().unwrap();
674        let child_tree = state.children.first();
675
676        let Rectangle {
677            x,
678            y,
679            width,
680            height,
681        } = layout.bounds();
682        let bounds = Rect::new(x as f64, y as f64, (x + width) as f64, (y + height) as f64);
683        let _is_hovered = state.state.downcast_ref::<State>().is_hovered;
684
685        let mut node = Node::new(Role::Button);
686        node.add_action(Action::Focus);
687        node.add_action(Action::Click);
688        node.set_bounds(bounds);
689        if let Some(name) = self.name.as_ref() {
690            node.set_label(name.clone());
691        }
692        match self.description.as_ref() {
693            Some(iced_accessibility::Description::Id(id)) => {
694                node.set_described_by(id.iter().cloned().map(NodeId::from).collect::<Vec<_>>());
695            }
696            Some(iced_accessibility::Description::Text(text)) => {
697                node.set_description(text.clone());
698            }
699            None => {}
700        }
701
702        if let Some(label) = self.label.as_ref() {
703            node.set_labelled_by(label.clone());
704        }
705
706        if self.on_press.is_none() {
707            node.set_disabled();
708        }
709        // TODO hover
710        // if is_hovered {
711        //     node.set_hovered();
712        // }
713
714        if let Some(child_tree) = child_tree.map(|child_tree| {
715            self.content.as_widget().a11y_nodes(
716                child_layout.with_virtual_offset(layout.virtual_offset()),
717                child_tree,
718                p,
719            )
720        }) {
721            A11yTree::node_with_child_tree(A11yNode::new(node, self.id.clone()), child_tree)
722        } else {
723            A11yTree::leaf(node, self.id.clone())
724        }
725    }
726
727    fn id(&self) -> Option<Id> {
728        Some(self.id.clone())
729    }
730
731    fn set_id(&mut self, id: Id) {
732        self.id = id;
733    }
734}
735
736impl<'a, Message: Clone + 'a> From<Button<'a, Message>> for crate::Element<'a, Message> {
737    fn from(button: Button<'a, Message>) -> Self {
738        Self::new(button)
739    }
740}
741
742/// The local state of a [`Button`].
743#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
744#[allow(clippy::struct_field_names)]
745pub struct State {
746    is_hovered: bool,
747    is_pressed: bool,
748    is_focused: bool,
749}
750
751impl State {
752    /// Creates a new [`State`].
753    #[inline]
754    pub fn new() -> Self {
755        Self::default()
756    }
757
758    /// Returns whether the [`Button`] is currently focused or not.
759    #[inline]
760    pub fn is_focused(self) -> bool {
761        self.is_focused
762    }
763
764    /// Returns whether the [`Button`] is currently hovered or not.
765    #[inline]
766    pub fn is_hovered(self) -> bool {
767        self.is_hovered
768    }
769
770    /// Focuses the [`Button`].
771    #[inline]
772    pub fn focus(&mut self) {
773        self.is_focused = true;
774    }
775
776    /// Unfocuses the [`Button`].
777    #[inline]
778    pub fn unfocus(&mut self) {
779        self.is_focused = false;
780    }
781}
782
783/// Processes the given [`Event`] and updates the [`State`] of a [`Button`]
784/// accordingly.
785#[allow(clippy::needless_pass_by_value, clippy::too_many_arguments)]
786pub fn update<'a, Message: Clone>(
787    _id: Id,
788    event: &Event,
789    layout: Layout<'_>,
790    cursor: mouse::Cursor,
791    shell: &mut Shell<'_, Message>,
792    on_press: Option<&dyn Fn(Vector, Rectangle) -> Message>,
793    on_press_down: Option<&dyn Fn(Vector, Rectangle) -> Message>,
794    state: impl FnOnce() -> &'a mut State,
795) {
796    match event {
797        Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
798        | Event::Touch(touch::Event::FingerPressed { .. }) => {
799            // Unfocus the button on clicks in case another widget was clicked.
800            let state = state();
801            state.unfocus();
802
803            if on_press.is_some() || on_press_down.is_some() {
804                let bounds = layout.bounds();
805
806                if cursor.is_over(bounds) {
807                    state.is_pressed = true;
808                    shell.request_redraw();
809
810                    if let Some(on_press_down) = on_press_down {
811                        let msg = (on_press_down)(layout.virtual_offset(), layout.bounds());
812                        shell.publish(msg);
813                    }
814
815                    shell.capture_event();
816                }
817            }
818        }
819        Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
820        | Event::Touch(touch::Event::FingerLifted { .. }) => {
821            if let Some(on_press) = on_press {
822                let state = state();
823
824                if state.is_pressed {
825                    state.is_pressed = false;
826
827                    let bounds = layout.bounds();
828
829                    if cursor.is_over(bounds) {
830                        let msg = (on_press)(layout.virtual_offset(), layout.bounds());
831                        shell.publish(msg);
832                    }
833
834                    shell.capture_event();
835                }
836            } else if on_press_down.is_some() {
837                let state = state();
838                if state.is_pressed {
839                    state.is_pressed = false;
840                    shell.request_redraw();
841                }
842            }
843        }
844        #[cfg(feature = "a11y")]
845        Event::A11y(_event_id, iced_accessibility::accesskit::ActionRequest { action, .. }) => {
846            let state = state();
847            if let Some(on_press) = matches!(action, iced_accessibility::accesskit::Action::Click)
848                .then_some(on_press)
849                .flatten()
850            {
851                state.is_pressed = false;
852                let msg = (on_press)(layout.virtual_offset(), layout.bounds());
853
854                shell.publish(msg);
855            }
856            shell.capture_event();
857        }
858        Event::Keyboard(keyboard::Event::KeyPressed { key, .. }) => {
859            if let Some(on_press) = on_press {
860                let state = state();
861                if state.is_focused && *key == keyboard::Key::Named(keyboard::key::Named::Enter) {
862                    state.is_pressed = true;
863                    let msg = (on_press)(layout.virtual_offset(), layout.bounds());
864
865                    shell.publish(msg);
866                    shell.capture_event();
867                }
868            }
869        }
870        Event::Mouse(mouse::Event::CursorMoved { .. })
871        | Event::Touch(touch::Event::FingerMoved { .. }) => {
872            let state = state();
873            let is_hovered = cursor.is_over(layout.bounds());
874            if state.is_hovered != is_hovered {
875                state.is_hovered = is_hovered;
876                shell.request_redraw();
877            }
878        }
879        Event::Touch(touch::Event::FingerLost { .. }) | Event::Mouse(mouse::Event::CursorLeft) => {
880            let state = state();
881
882            if state.is_hovered || state.is_pressed {
883                shell.request_redraw();
884            }
885            state.is_hovered = false;
886            state.is_pressed = false;
887        }
888        _ => {}
889    }
890}
891
892#[allow(clippy::too_many_arguments)]
893pub fn draw<Renderer: iced_core::Renderer, Theme>(
894    renderer: &mut Renderer,
895    bounds: Rectangle,
896    viewport_bounds: Rectangle,
897    styling: &super::style::Style,
898    draw_contents: impl FnOnce(&mut Renderer, &Style),
899    _is_image: bool,
900) where
901    Theme: super::style::Catalog,
902{
903    let doubled_border_width = styling.border_width * 2.0;
904    let doubled_outline_width = styling.outline_width * 2.0;
905
906    if styling.outline_width > 0.0 {
907        renderer.fill_quad(
908            renderer::Quad {
909                bounds: Rectangle {
910                    x: bounds.x - styling.border_width - styling.outline_width,
911                    y: bounds.y - styling.border_width - styling.outline_width,
912                    width: bounds.width + doubled_border_width + doubled_outline_width,
913                    height: bounds.height + doubled_border_width + doubled_outline_width,
914                },
915                border: Border {
916                    width: styling.outline_width,
917                    color: styling.outline_color,
918                    radius: styling.border_radius,
919                },
920                shadow: Shadow::default(),
921                snap: true,
922            },
923            Color::TRANSPARENT,
924        );
925    }
926
927    if styling.background.is_some() || styling.border_width > 0.0 {
928        if styling.shadow_offset != Vector::default() {
929            // TODO: Implement proper shadow support
930            renderer.fill_quad(
931                renderer::Quad {
932                    bounds: Rectangle {
933                        x: bounds.x + styling.shadow_offset.x,
934                        y: bounds.y + styling.shadow_offset.y,
935                        width: bounds.width,
936                        height: bounds.height,
937                    },
938                    border: Border {
939                        radius: styling.border_radius,
940                        ..Default::default()
941                    },
942                    shadow: Shadow::default(),
943                    snap: true,
944                },
945                Background::Color([0.0, 0.0, 0.0, 0.5].into()),
946            );
947        }
948
949        // Draw the button background first.
950        if let Some(background) = styling.background {
951            renderer.fill_quad(
952                renderer::Quad {
953                    bounds,
954                    border: Border {
955                        radius: styling.border_radius,
956                        ..Default::default()
957                    },
958                    shadow: Shadow::default(),
959                    snap: true,
960                },
961                background,
962            );
963        }
964
965        // Then button overlay if any.
966        if let Some(overlay) = styling.overlay {
967            renderer.fill_quad(
968                renderer::Quad {
969                    bounds,
970                    border: Border {
971                        radius: styling.border_radius,
972                        ..Default::default()
973                    },
974                    shadow: Shadow::default(),
975                    snap: true,
976                },
977                overlay,
978            );
979        }
980
981        // Then draw the button contents onto the background.
982        draw_contents(renderer, styling);
983
984        let mut clipped_bounds = viewport_bounds.intersection(&bounds).unwrap_or_default();
985        clipped_bounds.height += styling.border_width;
986        clipped_bounds.width += 1.0;
987
988        // Finish by drawing the border above the contents.
989        renderer.with_layer(clipped_bounds, |renderer| {
990            renderer.fill_quad(
991                renderer::Quad {
992                    bounds,
993                    border: Border {
994                        width: styling.border_width,
995                        color: styling.border_color,
996                        radius: styling.border_radius,
997                    },
998                    shadow: Shadow::default(),
999                    snap: true,
1000                },
1001                Color::TRANSPARENT,
1002            );
1003        })
1004    } else {
1005        draw_contents(renderer, styling);
1006    }
1007}
1008
1009/// Computes the layout of a [`Button`].
1010pub fn layout<Renderer>(
1011    renderer: &Renderer,
1012    limits: &layout::Limits,
1013    width: Length,
1014    height: Length,
1015    padding: Padding,
1016    layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node,
1017) -> layout::Node {
1018    let limits = limits.width(width).height(height);
1019
1020    let mut content = layout_content(renderer, &limits.shrink(padding));
1021    let padding = padding.fit(content.size(), limits.max());
1022    let size = limits
1023        .shrink(padding)
1024        .resolve(width, height, content.size())
1025        .expand(padding);
1026
1027    content = content.move_to(Point::new(padding.left, padding.top));
1028
1029    layout::Node::with_children(size, vec![content])
1030}
1031
1032/// Returns the [`mouse::Interaction`] of a [`Button`].
1033#[must_use]
1034pub fn mouse_interaction(
1035    layout: Layout<'_>,
1036    cursor: mouse::Cursor,
1037    is_enabled: bool,
1038) -> mouse::Interaction {
1039    let is_mouse_over = cursor.is_over(layout.bounds());
1040
1041    if is_mouse_over && is_enabled {
1042        mouse::Interaction::Pointer
1043    } else {
1044        mouse::Interaction::default()
1045    }
1046}
1047
1048/// Produces a [`Task`] that focuses the [`Button`] with the given [`Id`].
1049pub fn focus<Message: 'static>(id: Id) -> Task<Message> {
1050    task::effect(Action::Widget(Box::new(operation::focusable::focus(id))))
1051}
1052
1053impl operation::Focusable for State {
1054    #[inline]
1055    fn is_focused(&self) -> bool {
1056        Self::is_focused(*self)
1057    }
1058
1059    #[inline]
1060    fn focus(&mut self) {
1061        Self::focus(self);
1062    }
1063
1064    #[inline]
1065    fn unfocus(&mut self) {
1066        Self::unfocus(self);
1067    }
1068}
1069
1070fn removal_bounds(bounds: Rectangle, offset: f32) -> Rectangle {
1071    Rectangle {
1072        x: bounds.x + bounds.width - 12.0 - offset,
1073        y: bounds.y - 12.0 + offset,
1074        width: 24.0,
1075        height: 24.0,
1076    }
1077}