2023-10-12 13:23:21 +02:00
|
|
|
// Copyright 2023 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
|
|
use crate::Element;
|
|
|
|
|
|
|
|
|
|
use iced::advanced::layout::{self, Layout};
|
2024-10-16 20:36:46 -04:00
|
|
|
use iced::advanced::widget::{self, Operation};
|
2023-10-12 13:23:21 +02:00
|
|
|
use iced::advanced::{Clipboard, Shell};
|
2025-03-21 13:33:07 +01:00
|
|
|
use iced::advanced::{overlay, renderer};
|
|
|
|
|
use iced::{Event, Point, Rectangle, Size, event, mouse};
|
2024-07-22 07:23:17 +02:00
|
|
|
use iced_core::Renderer;
|
2023-10-12 13:23:21 +02:00
|
|
|
|
|
|
|
|
pub(super) struct Overlay<'a, 'b, Message> {
|
2024-10-16 20:36:46 -04:00
|
|
|
pub(crate) position: Point,
|
2023-10-12 13:23:21 +02:00
|
|
|
pub(super) content: &'b mut Element<'a, Message>,
|
|
|
|
|
pub(super) tree: &'b mut widget::Tree,
|
|
|
|
|
pub(super) width: f32,
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-14 11:56:21 -04:00
|
|
|
impl<Message> overlay::Overlay<Message, crate::Theme, crate::Renderer> for Overlay<'_, '_, Message>
|
2023-10-12 13:23:21 +02:00
|
|
|
where
|
|
|
|
|
Message: Clone,
|
|
|
|
|
{
|
2024-10-16 20:36:46 -04:00
|
|
|
fn layout(&mut self, renderer: &crate::Renderer, bounds: Size) -> layout::Node {
|
|
|
|
|
let position = self.position;
|
2023-10-12 13:23:21 +02:00
|
|
|
let limits = layout::Limits::new(Size::ZERO, bounds)
|
|
|
|
|
.width(self.width)
|
|
|
|
|
.height(bounds.height - 8.0 - position.y);
|
|
|
|
|
|
2024-01-30 22:14:00 -05:00
|
|
|
let node = self
|
2023-12-07 15:27:52 -05:00
|
|
|
.content
|
|
|
|
|
.as_widget()
|
2023-12-12 19:53:17 -05:00
|
|
|
.layout(self.tree, renderer, &limits);
|
2023-10-12 13:23:21 +02:00
|
|
|
let node_size = node.size();
|
|
|
|
|
|
2025-10-11 16:24:38 +10:00
|
|
|
node.move_to(Point {
|
2023-10-12 13:23:21 +02:00
|
|
|
x: if bounds.width > node_size.width - 8.0 {
|
|
|
|
|
bounds.width - node_size.width - 8.0
|
|
|
|
|
} else {
|
|
|
|
|
0.0
|
|
|
|
|
},
|
|
|
|
|
y: if bounds.height > node_size.height - 8.0 {
|
|
|
|
|
bounds.height - node_size.height - 8.0
|
|
|
|
|
} else {
|
|
|
|
|
0.0
|
|
|
|
|
},
|
2024-01-30 22:14:00 -05:00
|
|
|
})
|
2023-10-12 13:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_event(
|
|
|
|
|
&mut self,
|
|
|
|
|
event: Event,
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
cursor: mouse::Cursor,
|
|
|
|
|
renderer: &crate::Renderer,
|
|
|
|
|
clipboard: &mut dyn Clipboard,
|
|
|
|
|
shell: &mut Shell<'_, Message>,
|
|
|
|
|
) -> event::Status {
|
|
|
|
|
self.content.as_widget_mut().on_event(
|
|
|
|
|
self.tree,
|
|
|
|
|
event,
|
|
|
|
|
layout,
|
|
|
|
|
cursor,
|
|
|
|
|
renderer,
|
|
|
|
|
clipboard,
|
|
|
|
|
shell,
|
|
|
|
|
&layout.bounds(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw(
|
|
|
|
|
&self,
|
|
|
|
|
renderer: &mut crate::Renderer,
|
|
|
|
|
theme: &crate::Theme,
|
|
|
|
|
style: &renderer::Style,
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
cursor: mouse::Cursor,
|
|
|
|
|
) {
|
2024-07-22 07:23:17 +02:00
|
|
|
renderer.with_layer(layout.bounds(), |renderer| {
|
|
|
|
|
self.content.as_widget().draw(
|
|
|
|
|
self.tree,
|
|
|
|
|
renderer,
|
|
|
|
|
theme,
|
|
|
|
|
style,
|
|
|
|
|
layout,
|
|
|
|
|
cursor,
|
|
|
|
|
&layout.bounds(),
|
|
|
|
|
);
|
|
|
|
|
})
|
2023-10-12 13:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn operate(
|
|
|
|
|
&mut self,
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
renderer: &crate::Renderer,
|
2024-10-16 20:36:46 -04:00
|
|
|
operation: &mut dyn Operation<()>,
|
2023-10-12 13:23:21 +02:00
|
|
|
) {
|
|
|
|
|
self.content
|
|
|
|
|
.as_widget_mut()
|
|
|
|
|
.operate(self.tree, layout, renderer, operation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn mouse_interaction(
|
|
|
|
|
&self,
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
cursor: mouse::Cursor,
|
|
|
|
|
viewport: &Rectangle,
|
|
|
|
|
renderer: &crate::Renderer,
|
|
|
|
|
) -> mouse::Interaction {
|
|
|
|
|
self.content
|
|
|
|
|
.as_widget()
|
|
|
|
|
.mouse_interaction(self.tree, layout, cursor, viewport, renderer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn overlay<'c>(
|
|
|
|
|
&'c mut self,
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
renderer: &crate::Renderer,
|
2024-01-30 22:14:00 -05:00
|
|
|
) -> Option<overlay::Element<'c, Message, crate::Theme, crate::Renderer>> {
|
2023-10-12 13:23:21 +02:00
|
|
|
self.content
|
|
|
|
|
.as_widget_mut()
|
2024-11-19 19:45:11 -05:00
|
|
|
.overlay(self.tree, layout, renderer, iced::Vector::default())
|
2023-10-12 13:23:21 +02:00
|
|
|
}
|
|
|
|
|
}
|