2020-07-10 02:39:12 +02:00
|
|
|
//! Display interactive elements on top of other widgets.
|
2020-07-10 01:31:56 +02:00
|
|
|
mod element;
|
2023-01-17 10:01:17 -08:00
|
|
|
mod group;
|
2020-05-23 01:07:59 +02:00
|
|
|
|
2020-07-10 01:31:56 +02:00
|
|
|
pub use element::Element;
|
2023-01-17 10:01:17 -08:00
|
|
|
pub use group::Group;
|
2020-05-23 01:07:59 +02:00
|
|
|
|
2020-11-12 01:29:11 +01:00
|
|
|
use crate::event::{self, Event};
|
|
|
|
|
use crate::layout;
|
2021-10-18 16:43:18 +07:00
|
|
|
use crate::mouse;
|
2021-10-18 15:19:04 +07:00
|
|
|
use crate::renderer;
|
2022-07-28 02:46:51 +02:00
|
|
|
use crate::widget;
|
2022-11-10 00:10:53 +01:00
|
|
|
use crate::widget::Tree;
|
2022-02-22 14:10:49 +07:00
|
|
|
use crate::{Clipboard, Layout, Point, Rectangle, Shell, Size};
|
2020-04-14 12:11:10 +02:00
|
|
|
|
2020-07-10 02:39:12 +02:00
|
|
|
/// An interactive component that can be displayed on top of other widgets.
|
2020-07-10 01:31:56 +02:00
|
|
|
pub trait Overlay<Message, Renderer>
|
2020-04-18 14:42:48 +02:00
|
|
|
where
|
|
|
|
|
Renderer: crate::Renderer,
|
|
|
|
|
{
|
2020-07-10 02:39:12 +02:00
|
|
|
/// Returns the layout [`Node`] of the [`Overlay`].
|
|
|
|
|
///
|
|
|
|
|
/// This [`Node`] is used by the runtime to compute the [`Layout`] of the
|
|
|
|
|
/// user interface.
|
|
|
|
|
///
|
2020-11-25 07:11:27 +01:00
|
|
|
/// [`Node`]: layout::Node
|
2020-04-18 14:42:48 +02:00
|
|
|
fn layout(
|
|
|
|
|
&self,
|
|
|
|
|
renderer: &Renderer,
|
|
|
|
|
bounds: Size,
|
|
|
|
|
position: Point,
|
2020-07-10 01:31:56 +02:00
|
|
|
) -> layout::Node;
|
2020-04-18 14:42:48 +02:00
|
|
|
|
2020-07-10 02:39:12 +02:00
|
|
|
/// Draws the [`Overlay`] using the associated `Renderer`.
|
2020-04-18 14:42:48 +02:00
|
|
|
fn draw(
|
|
|
|
|
&self,
|
|
|
|
|
renderer: &mut Renderer,
|
2022-05-14 01:47:55 +02:00
|
|
|
theme: &Renderer::Theme,
|
2021-10-18 15:19:04 +07:00
|
|
|
style: &renderer::Style,
|
2020-04-18 14:42:48 +02:00
|
|
|
layout: Layout<'_>,
|
|
|
|
|
cursor_position: Point,
|
2021-10-14 16:07:22 +07:00
|
|
|
);
|
2020-07-10 01:31:56 +02:00
|
|
|
|
2022-11-10 00:10:53 +01:00
|
|
|
/// Applies a [`widget::Operation`] to the [`Overlay`].
|
2022-07-28 02:46:51 +02:00
|
|
|
fn operate(
|
2022-11-18 12:09:18 -08:00
|
|
|
&mut self,
|
2022-07-28 02:46:51 +02:00
|
|
|
_layout: Layout<'_>,
|
2022-12-22 14:29:24 +01:00
|
|
|
_renderer: &Renderer,
|
2022-07-28 02:46:51 +02:00
|
|
|
_operation: &mut dyn widget::Operation<Message>,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 02:39:12 +02:00
|
|
|
/// Processes a runtime [`Event`].
|
|
|
|
|
///
|
|
|
|
|
/// It receives:
|
|
|
|
|
/// * an [`Event`] describing user interaction
|
|
|
|
|
/// * the computed [`Layout`] of the [`Overlay`]
|
|
|
|
|
/// * the current cursor position
|
|
|
|
|
/// * a mutable `Message` list, allowing the [`Overlay`] to produce
|
|
|
|
|
/// new messages based on user interaction.
|
|
|
|
|
/// * the `Renderer`
|
|
|
|
|
/// * a [`Clipboard`], if available
|
|
|
|
|
///
|
|
|
|
|
/// By default, it does nothing.
|
2020-07-10 01:31:56 +02:00
|
|
|
fn on_event(
|
|
|
|
|
&mut self,
|
|
|
|
|
_event: Event,
|
|
|
|
|
_layout: Layout<'_>,
|
|
|
|
|
_cursor_position: Point,
|
|
|
|
|
_renderer: &Renderer,
|
2021-03-10 01:59:02 +01:00
|
|
|
_clipboard: &mut dyn Clipboard,
|
2021-11-29 16:22:01 +07:00
|
|
|
_shell: &mut Shell<'_, Message>,
|
2020-11-12 01:29:11 +01:00
|
|
|
) -> event::Status {
|
|
|
|
|
event::Status::Ignored
|
2020-04-14 12:11:10 +02:00
|
|
|
}
|
2021-10-18 16:43:18 +07:00
|
|
|
|
2022-04-30 14:20:52 +02:00
|
|
|
/// Returns the current [`mouse::Interaction`] of the [`Overlay`].
|
2021-10-18 16:43:18 +07:00
|
|
|
///
|
|
|
|
|
/// By default, it returns [`mouse::Interaction::Idle`].
|
|
|
|
|
fn mouse_interaction(
|
|
|
|
|
&self,
|
|
|
|
|
_layout: Layout<'_>,
|
|
|
|
|
_cursor_position: Point,
|
2021-11-02 15:03:29 +07:00
|
|
|
_viewport: &Rectangle,
|
2022-01-11 14:12:28 +07:00
|
|
|
_renderer: &Renderer,
|
2021-10-18 16:43:18 +07:00
|
|
|
) -> mouse::Interaction {
|
|
|
|
|
mouse::Interaction::Idle
|
|
|
|
|
}
|
2023-01-17 11:12:10 -08:00
|
|
|
|
2023-01-24 01:59:34 +01:00
|
|
|
/// Returns true if the cursor is over the [`Overlay`].
|
|
|
|
|
///
|
|
|
|
|
/// By default, it returns true if the bounds of the `layout` contain
|
|
|
|
|
/// the `cursor_position`.
|
2023-01-17 17:20:53 -08:00
|
|
|
fn is_over(&self, layout: Layout<'_>, cursor_position: Point) -> bool {
|
2023-01-17 11:12:10 -08:00
|
|
|
layout.bounds().contains(cursor_position)
|
|
|
|
|
}
|
2020-04-14 12:11:10 +02:00
|
|
|
}
|
2022-07-27 06:49:20 +02:00
|
|
|
|
2023-01-17 10:12:51 -08:00
|
|
|
/// Returns a [`Group`] of overlay [`Element`] children.
|
2022-07-27 06:49:20 +02:00
|
|
|
///
|
|
|
|
|
/// This method will generally only be used by advanced users that are
|
|
|
|
|
/// implementing the [`Widget`](crate::Widget) trait.
|
|
|
|
|
pub fn from_children<'a, Message, Renderer>(
|
2022-11-19 12:25:59 -08:00
|
|
|
children: &'a mut [crate::Element<'_, Message, Renderer>],
|
2022-07-27 06:49:20 +02:00
|
|
|
tree: &'a mut Tree,
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
renderer: &Renderer,
|
|
|
|
|
) -> Option<Element<'a, Message, Renderer>>
|
|
|
|
|
where
|
|
|
|
|
Renderer: crate::Renderer,
|
|
|
|
|
{
|
2023-01-17 10:12:51 -08:00
|
|
|
let children = children
|
2022-11-19 12:25:59 -08:00
|
|
|
.iter_mut()
|
2022-07-27 06:49:20 +02:00
|
|
|
.zip(&mut tree.children)
|
|
|
|
|
.zip(layout.children())
|
|
|
|
|
.filter_map(|((child, state), layout)| {
|
2022-11-19 12:25:59 -08:00
|
|
|
child.as_widget_mut().overlay(state, layout, renderer)
|
2022-07-27 06:49:20 +02:00
|
|
|
})
|
2023-01-17 10:12:51 -08:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
|
|
(!children.is_empty()).then(|| Group::with_children(children).overlay())
|
2022-07-27 06:49:20 +02:00
|
|
|
}
|