Merge branch 'master' into feature/time-travel

This commit is contained in:
Héctor Ramón Jiménez 2025-04-29 02:06:34 +02:00
commit 4334923add
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
35 changed files with 583 additions and 171 deletions

View file

@ -368,12 +368,13 @@ where
tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
viewport: &Rectangle,
translation: Vector,
) -> Option<overlay::Element<'b, B, Theme, Renderer>> {
let mapper = &self.mapper;
self.widget
.overlay(tree, layout, renderer, translation)
.overlay(tree, layout, renderer, viewport, translation)
.map(move |overlay| overlay.map(mapper))
}
}
@ -519,10 +520,15 @@ where
state: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
viewport: &Rectangle,
translation: Vector,
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
self.element
.widget
.overlay(state, layout, renderer, translation)
self.element.widget.overlay(
state,
layout,
renderer,
viewport,
translation,
)
}
}

View file

@ -101,6 +101,16 @@ where
) -> Option<Element<'a, Message, Theme, Renderer>> {
None
}
/// The index of the overlay.
///
/// Overlays with a higher index will be rendered on top of overlays with
/// a lower index.
///
/// By default, it returns `1.0`.
fn index(&self) -> f32 {
1.0
}
}
/// Returns a [`Group`] of overlay [`Element`] children.
@ -112,6 +122,7 @@ pub fn from_children<'a, Message, Theme, Renderer>(
tree: &'a mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
viewport: &Rectangle,
translation: Vector,
) -> Option<Element<'a, Message, Theme, Renderer>>
where
@ -122,9 +133,13 @@ where
.zip(&mut tree.children)
.zip(layout.children())
.filter_map(|((child, state), layout)| {
child
.as_widget_mut()
.overlay(state, layout, renderer, translation)
child.as_widget_mut().overlay(
state,
layout,
renderer,
viewport,
translation,
)
})
.collect::<Vec<_>>();

View file

@ -23,6 +23,18 @@ where
Self { overlay }
}
/// Returns a reference to the [`Overlay`] of the [`Element`],
pub fn as_overlay(&self) -> &dyn Overlay<Message, Theme, Renderer> {
self.overlay.as_ref()
}
/// Returns a mutable reference to the [`Overlay`] of the [`Element`],
pub fn as_overlay_mut(
&mut self,
) -> &mut dyn Overlay<Message, Theme, Renderer> {
self.overlay.as_mut()
}
/// Applies a transformation to the produced message of the [`Element`].
pub fn map<B>(
self,
@ -38,82 +50,6 @@ where
overlay: Box::new(Map::new(self.overlay, f)),
}
}
/// Computes the layout of the [`Element`] in the given bounds.
pub fn layout(
&mut self,
renderer: &Renderer,
bounds: Size,
) -> layout::Node {
self.overlay.layout(renderer, bounds)
}
/// Processes a runtime [`Event`].
pub fn update(
&mut self,
event: &Event,
layout: Layout<'_>,
cursor: mouse::Cursor,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
) {
self.overlay
.update(event, layout, cursor, renderer, clipboard, shell);
}
/// Returns the current [`mouse::Interaction`] of the [`Element`].
pub fn mouse_interaction(
&self,
layout: Layout<'_>,
cursor: mouse::Cursor,
viewport: &Rectangle,
renderer: &Renderer,
) -> mouse::Interaction {
self.overlay
.mouse_interaction(layout, cursor, viewport, renderer)
}
/// Draws the [`Element`] and its children using the given [`Layout`].
pub fn draw(
&self,
renderer: &mut Renderer,
theme: &Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor: mouse::Cursor,
) {
self.overlay.draw(renderer, theme, style, layout, cursor);
}
/// Applies a [`widget::Operation`] to the [`Element`].
pub fn operate(
&mut self,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation,
) {
self.overlay.operate(layout, renderer, operation);
}
/// Returns true if the cursor is over the [`Element`].
pub fn is_over(
&self,
layout: Layout<'_>,
renderer: &Renderer,
cursor_position: Point,
) -> bool {
self.overlay.is_over(layout, renderer, cursor_position)
}
/// Returns the nested overlay of the [`Element`], if there is any.
pub fn overlay<'b>(
&'b mut self,
layout: Layout<'_>,
renderer: &Renderer,
) -> Option<Element<'b, Message, Theme, Renderer>> {
self.overlay.overlay(layout, renderer)
}
}
struct Map<'a, A, B, Theme, Renderer> {

View file

@ -25,18 +25,18 @@ where
/// Creates a [`Group`] with the given elements.
pub fn with_children(
children: Vec<overlay::Element<'a, Message, Theme, Renderer>>,
mut children: Vec<overlay::Element<'a, Message, Theme, Renderer>>,
) -> Self {
Group { children }
}
use std::cmp;
/// Adds an [`overlay::Element`] to the [`Group`].
pub fn push(
mut self,
child: impl Into<overlay::Element<'a, Message, Theme, Renderer>>,
) -> Self {
self.children.push(child.into());
self
children.sort_unstable_by(|a, b| {
a.as_overlay()
.index()
.partial_cmp(&b.as_overlay().index())
.unwrap_or(cmp::Ordering::Equal)
});
Group { children }
}
/// Turns the [`Group`] into an overlay [`overlay::Element`].
@ -67,7 +67,7 @@ where
bounds,
self.children
.iter_mut()
.map(|child| child.layout(renderer, bounds))
.map(|child| child.as_overlay_mut().layout(renderer, bounds))
.collect(),
)
}
@ -82,7 +82,9 @@ where
shell: &mut Shell<'_, Message>,
) {
for (child, layout) in self.children.iter_mut().zip(layout.children()) {
child.update(event, layout, cursor, renderer, clipboard, shell);
child
.as_overlay_mut()
.update(event, layout, cursor, renderer, clipboard, shell);
}
}
@ -95,7 +97,9 @@ where
cursor: mouse::Cursor,
) {
for (child, layout) in self.children.iter().zip(layout.children()) {
child.draw(renderer, theme, style, layout, cursor);
child
.as_overlay()
.draw(renderer, theme, style, layout, cursor);
}
}
@ -110,7 +114,9 @@ where
.iter()
.zip(layout.children())
.map(|(child, layout)| {
child.mouse_interaction(layout, cursor, viewport, renderer)
child
.as_overlay()
.mouse_interaction(layout, cursor, viewport, renderer)
})
.max()
.unwrap_or_default()
@ -125,7 +131,7 @@ where
operation.container(None, layout.bounds(), &mut |operation| {
self.children.iter_mut().zip(layout.children()).for_each(
|(child, layout)| {
child.operate(layout, renderer, operation);
child.as_overlay_mut().operate(layout, renderer, operation);
},
);
});
@ -141,7 +147,9 @@ where
.iter()
.zip(layout.children())
.any(|(child, layout)| {
child.is_over(layout, renderer, cursor_position)
child
.as_overlay()
.is_over(layout, renderer, cursor_position)
})
}
@ -154,11 +162,20 @@ where
.children
.iter_mut()
.zip(layout.children())
.filter_map(|(child, layout)| child.overlay(layout, renderer))
.filter_map(|(child, layout)| {
child.as_overlay_mut().overlay(layout, renderer)
})
.collect::<Vec<_>>();
(!children.is_empty()).then(|| Group::with_children(children).overlay())
}
fn index(&self) -> f32 {
self.children
.first()
.map(|child| child.as_overlay().index())
.unwrap_or(1.0)
}
}
impl<'a, Message, Theme, Renderer> From<Group<'a, Message, Theme, Renderer>>

View file

@ -157,6 +157,30 @@ impl Rectangle<f32> {
distance_x.hypot(distance_y)
}
/// Computes the offset that must be applied to the [`Rectangle`] to be placed
/// inside the given `container`.
pub fn offset(&self, container: &Rectangle) -> Vector {
let Some(intersection) = self.intersection(container) else {
return Vector::ZERO;
};
let left = intersection.x - self.x;
let top = intersection.y - self.y;
Vector::new(
if left > 0.0 {
left
} else {
intersection.x + intersection.width - self.x - self.width
},
if top > 0.0 {
top
} else {
intersection.y + intersection.height - self.y - self.height
},
)
}
/// Returns true if the current [`Rectangle`] is completely within the given
/// `container`.
pub fn is_within(&self, container: &Rectangle) -> bool {
@ -268,6 +292,17 @@ impl Rectangle<f32> {
Self::new(position, size)
}
/// Scales the [`Rectangle`] without changing its position, effectively
/// "zooming" it.
pub fn zoom(self, zoom: f32) -> Self {
Self {
x: self.x - (self.width * (zoom - 1.0)) / 2.0,
y: self.y - (self.height * (zoom - 1.0)) / 2.0,
width: self.width * zoom,
height: self.height * zoom,
}
}
}
impl std::ops::Mul<f32> for Rectangle<f32> {

View file

@ -106,6 +106,10 @@ pub enum Edit {
Paste(Arc<String>),
/// Break the current line.
Enter,
/// Indent the current line.
Indent,
/// Unindent the current line.
Unindent,
/// Delete the previous character.
Backspace,
/// Delete the next character.

View file

@ -96,7 +96,9 @@ where
}
/// Reconciles the [`Widget`] with the provided [`Tree`].
fn diff(&self, _tree: &mut Tree) {}
fn diff(&self, tree: &mut Tree) {
tree.children.clear();
}
/// Applies an [`Operation`] to the [`Widget`].
fn operate(
@ -144,6 +146,7 @@ where
_state: &'a mut Tree,
_layout: Layout<'_>,
_renderer: &Renderer,
_viewport: &Rectangle,
_translation: Vector,
) -> Option<overlay::Element<'a, Message, Theme, Renderer>> {
None