Skip to main content

iced_core/
overlay.rs

1//! Display interactive elements on top of other widgets.
2mod element;
3mod group;
4
5pub use element::Element;
6pub use group::Group;
7
8use crate::mouse;
9use crate::renderer;
10use crate::widget;
11use crate::widget::Tree;
12use crate::{Event, Layout, Rectangle, Shell, Size, Vector, Widget};
13
14/// An interactive component that can be displayed on top of other widgets.
15pub trait Overlay<Message, Theme, Renderer>
16where
17    Renderer: crate::Renderer,
18{
19    /// Draws the [`Overlay`] using the associated `Renderer`.
20    ///
21    /// Implementors are expected to call
22    /// [`Renderer::with_layer`](renderer::Renderer::with_layer) themselves,
23    /// so that their contents are properly clipped.
24    fn draw(
25        &self,
26        renderer: &mut Renderer,
27        theme: &Theme,
28        style: &renderer::Style,
29        cursor: mouse::Cursor,
30    );
31
32    /// Applies a [`widget::Operation`] to the [`Overlay`].
33    fn operate(&mut self, _renderer: &Renderer, _operation: &mut dyn widget::Operation) {}
34
35    /// Processes a runtime [`Event`].
36    ///
37    /// By default, it does nothing.
38    fn update(
39        &mut self,
40        _event: &Event,
41        _cursor: mouse::Cursor,
42        _renderer: &Renderer,
43        _shell: &mut Shell<'_, Message>,
44    ) {
45    }
46
47    /// Returns the current [`mouse::Interaction`] of the [`Overlay`].
48    ///
49    /// By default, it returns [`mouse::Interaction::None`].
50    fn mouse_interaction(
51        &self,
52        _cursor: mouse::Cursor,
53        _renderer: &Renderer,
54    ) -> mouse::Interaction {
55        mouse::Interaction::None
56    }
57
58    /// Returns the nested overlays of the [`Overlay`].
59    fn overlay<'a>(
60        &'a mut self,
61        _renderer: &Renderer,
62    ) -> Vec<Element<'a, Message, Theme, Renderer>> {
63        Vec::new()
64    }
65
66    /// The index of the overlay.
67    ///
68    /// Overlays with a higher index will be rendered on top of overlays with
69    /// a lower index.
70    ///
71    /// By default, it returns `1.0`.
72    fn index(&self) -> f32 {
73        1.0
74    }
75}
76
77/// Returns the overlays of the given [`Widget`] children.
78///
79/// This method will generally only be used by advanced users that are
80/// implementing the [`Widget`] trait.
81pub fn from_children<'a, Message, Theme, Renderer>(
82    children: &'a mut [impl Widget<Message, Theme, Renderer>],
83    tree: &'a mut Tree,
84    layout: Layout,
85    renderer: &Renderer,
86    viewport: &Rectangle,
87    translation: Vector,
88    window: Size,
89) -> Vec<Element<'a, Message, Theme, Renderer>>
90where
91    Renderer: crate::Renderer,
92{
93    children
94        .iter_mut()
95        .zip(layout.iter_mut(&mut tree.children))
96        .flat_map(|(child, (layout, state))| {
97            child.overlay(state, layout, renderer, viewport, translation, window)
98        })
99        .collect()
100}