Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MERGE UPSTREAM #23

Merged
merged 11 commits into from
Nov 24, 2023
6 changes: 4 additions & 2 deletions core/src/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use crate::mouse;
use crate::renderer;
use crate::widget;
use crate::widget::Tree;
use crate::{layout, IME};
use crate::{Clipboard, Layout, Point, Rectangle, Shell, Size};
use crate::{
layout, Clipboard, Layout, Point, Rectangle, Shell, Size, Vector, IME,
};

/// An interactive component that can be displayed on top of other widgets.
pub trait Overlay<Message, Renderer>
Expand All @@ -29,6 +30,7 @@ where
renderer: &Renderer,
bounds: Size,
position: Point,
translation: Vector,
) -> layout::Node;

/// Draws the [`Overlay`] using the associated `Renderer`.
Expand Down
20 changes: 16 additions & 4 deletions core/src/overlay/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::any::Any;
#[allow(missing_debug_implementations)]
pub struct Element<'a, Message, Renderer> {
position: Point,
translation: Vector,
overlay: Box<dyn Overlay<Message, Renderer> + 'a>,
}

Expand All @@ -25,7 +26,11 @@ where
position: Point,
overlay: Box<dyn Overlay<Message, Renderer> + 'a>,
) -> Self {
Self { position, overlay }
Self {
position,
overlay,
translation: Vector::ZERO,
}
}

/// Returns the position of the [`Element`].
Expand All @@ -36,6 +41,7 @@ where
/// Translates the [`Element`].
pub fn translate(mut self, translation: Vector) -> Self {
self.position = self.position + translation;
self.translation = self.translation + translation;
self
}

Expand All @@ -48,6 +54,7 @@ where
{
Element {
position: self.position,
translation: self.translation,
overlay: Box::new(Map::new(self.overlay, f)),
}
}
Expand All @@ -59,8 +66,12 @@ where
bounds: Size,
translation: Vector,
) -> layout::Node {
self.overlay
.layout(renderer, bounds, self.position + translation)
self.overlay.layout(
renderer,
bounds,
self.position + translation,
self.translation + translation,
)
}

/// Processes a runtime [`Event`].
Expand Down Expand Up @@ -155,8 +166,9 @@ where
renderer: &Renderer,
bounds: Size,
position: Point,
translation: Vector,
) -> layout::Node {
self.content.layout(renderer, bounds, position)
self.content.layout(renderer, bounds, position, translation)
}

fn operate(
Expand Down
12 changes: 7 additions & 5 deletions core/src/overlay/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::widget;
use crate::IME;
use crate::{Clipboard, Event, Layout, Overlay, Point, Rectangle, Shell, Size};

use crate::{
Clipboard, Event, Layout, Overlay, Point, Rectangle, Shell, Size, Vector,
IME,
};

/// An [`Overlay`] container that displays multiple overlay [`overlay::Element`]
/// children.
Expand Down Expand Up @@ -65,10 +68,9 @@ where
&mut self,
renderer: &Renderer,
bounds: Size,
position: Point,
_position: Point,
translation: Vector,
) -> layout::Node {
let translation = position - Point::ORIGIN;

layout::Node::with_children(
bounds,
self.children
Expand Down
2 changes: 2 additions & 0 deletions examples/modal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ mod modal {
use iced::mouse;
use iced::{
BorderRadius, Color, Element, Event, Length, Point, Rectangle, Size,
Vector,
};

/// A widget that centers a modal element over some base element
Expand Down Expand Up @@ -415,6 +416,7 @@ mod modal {
renderer: &Renderer,
_bounds: Size,
position: Point,
_translation: Vector,
) -> layout::Node {
let limits = layout::Limits::new(Size::ZERO, self.size)
.width(Length::Fill)
Expand Down
2 changes: 1 addition & 1 deletion examples/progress_bar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A simple progress bar that can be filled by using a slider.
The __[`main`]__ file contains all the code of the example.

<div align="center">
<img src="https://iced.rs/examples/pokedex.gif">
<img src="https://iced.rs/examples/progress_bar.gif">
</div>

You can run it with `cargo run`:
Expand Down
1 change: 1 addition & 0 deletions examples/toast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ mod toast {
renderer: &Renderer,
bounds: Size,
position: Point,
_translation: Vector,
) -> layout::Node {
let limits = layout::Limits::new(Size::ZERO, bounds)
.width(Length::Fill)
Expand Down
15 changes: 8 additions & 7 deletions runtime/src/overlay/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use crate::core::mouse;
use crate::core::overlay;
use crate::core::renderer;
use crate::core::widget;
use crate::core::{Clipboard, Event, Layout, Point, Rectangle, Shell, Size};
use crate::core::{
Clipboard, Event, Layout, Point, Rectangle, Shell, Size, Vector,
};

/// An overlay container that displays nested overlays
#[allow(missing_debug_implementations)]
Expand Down Expand Up @@ -35,19 +37,18 @@ where
&mut self,
renderer: &Renderer,
bounds: Size,
position: Point,
_position: Point,
translation: Vector,
) -> layout::Node {
fn recurse<Message, Renderer>(
element: &mut overlay::Element<'_, Message, Renderer>,
renderer: &Renderer,
bounds: Size,
position: Point,
translation: Vector,
) -> layout::Node
where
Renderer: renderer::Renderer,
{
let translation = position - Point::ORIGIN;

let node = element.layout(renderer, bounds, translation);

if let Some(mut nested) =
Expand All @@ -57,15 +58,15 @@ where
node.size(),
vec![
node,
recurse(&mut nested, renderer, bounds, position),
recurse(&mut nested, renderer, bounds, translation),
],
)
} else {
layout::Node::with_children(node.size(), vec![node])
}
}

recurse(&mut self.overlay, renderer, bounds, position)
recurse(&mut self.overlay, renderer, bounds, translation)
}

/// Draws the [`Nested`] overlay using the associated `Renderer`.
Expand Down
30 changes: 23 additions & 7 deletions runtime/src/user_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use crate::core::mouse;
use crate::core::renderer;
use crate::core::widget;
use crate::core::window;
use crate::core::{Clipboard, Element, Layout, Point, Rectangle, Shell, Size};
use crate::core::{
Clipboard, Element, Layout, Point, Rectangle, Shell, Size, Vector,
};
use crate::overlay;

/// A set of interactive graphical elements with a specific [`Layout`].
Expand Down Expand Up @@ -202,7 +204,8 @@ where
let bounds = self.bounds;

let mut overlay = manual_overlay.as_mut().unwrap();
let mut layout = overlay.layout(renderer, bounds, Point::ORIGIN);
let mut layout =
overlay.layout(renderer, bounds, Point::ORIGIN, Vector::ZERO);
let mut event_statuses = Vec::new();

for event in events.iter().cloned() {
Expand Down Expand Up @@ -257,8 +260,12 @@ where
overlay = manual_overlay.as_mut().unwrap();

shell.revalidate_layout(|| {
layout =
overlay.layout(renderer, bounds, Point::ORIGIN);
layout = overlay.layout(
renderer,
bounds,
Point::ORIGIN,
Vector::ZERO,
);
});
}

Expand Down Expand Up @@ -456,7 +463,12 @@ where
.map(overlay::Nested::new)
{
let overlay_layout = self.overlay.take().unwrap_or_else(|| {
overlay.layout(renderer, self.bounds, Point::ORIGIN)
overlay.layout(
renderer,
self.bounds,
Point::ORIGIN,
Vector::ZERO,
)
});

let cursor = if cursor
Expand Down Expand Up @@ -574,8 +586,12 @@ where
.map(overlay::Nested::new)
{
if self.overlay.is_none() {
self.overlay =
Some(overlay.layout(renderer, self.bounds, Point::ORIGIN));
self.overlay = Some(overlay.layout(
renderer,
self.bounds,
Point::ORIGIN,
Vector::ZERO,
));
}

overlay.operate(
Expand Down
5 changes: 3 additions & 2 deletions widget/src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::core::widget::tree::{self, Tree};
use crate::core::widget::{self, Widget};
use crate::core::Element;
use crate::core::{
self, Clipboard, Hasher, Length, Point, Rectangle, Shell, Size, IME,
self, Clipboard, Hasher, Length, Point, Rectangle, Shell, Size, Vector, IME,
};
use crate::runtime::overlay::Nested;

Expand Down Expand Up @@ -335,9 +335,10 @@ where
renderer: &Renderer,
bounds: Size,
position: Point,
translation: Vector,
) -> layout::Node {
self.with_overlay_maybe(|overlay| {
overlay.layout(renderer, bounds, position)
overlay.layout(renderer, bounds, position, translation)
})
.unwrap_or_default()
}
Expand Down
3 changes: 2 additions & 1 deletion widget/src/lazy/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,9 +579,10 @@ where
renderer: &Renderer,
bounds: Size,
position: Point,
translation: Vector,
) -> layout::Node {
self.with_overlay_maybe(|overlay| {
overlay.layout(renderer, bounds, position)
overlay.layout(renderer, bounds, position, translation)
})
.unwrap_or_default()
}
Expand Down
7 changes: 4 additions & 3 deletions widget/src/lazy/responsive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::core::renderer;
use crate::core::widget;
use crate::core::widget::tree::{self, Tree};
use crate::core::{
self, Clipboard, Element, Length, Point, Rectangle, Shell, Size, Widget,
IME,
self, Clipboard, Element, Length, Point, Rectangle, Shell, Size, Vector,
Widget, IME,
};
use crate::horizontal_space;
use crate::runtime::overlay::Nested;
Expand Down Expand Up @@ -370,9 +370,10 @@ where
renderer: &Renderer,
bounds: Size,
position: Point,
translation: Vector,
) -> layout::Node {
self.with_overlay_maybe(|overlay| {
overlay.layout(renderer, bounds, position)
overlay.layout(renderer, bounds, position, translation)
})
.unwrap_or_default()
}
Expand Down
1 change: 1 addition & 0 deletions widget/src/overlay/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ where
renderer: &Renderer,
bounds: Size,
position: Point,
_translation: Vector,
) -> layout::Node {
let space_below = bounds.height - (position.y + self.target_height);
let space_above = position.y;
Expand Down
11 changes: 10 additions & 1 deletion widget/src/tooltip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,19 @@ where
) -> event::Status {
let state = tree.state.downcast_mut::<State>();

let was_idle = *state == State::Idle;

*state = cursor
.position_over(layout.bounds())
.map(|cursor_position| State::Hovered { cursor_position })
.unwrap_or_default();

let is_idle = *state == State::Idle;

if was_idle != is_idle {
shell.invalidate_layout();
}

self.content.as_widget_mut().on_event(
&mut tree.children[0],
event,
Expand Down Expand Up @@ -293,7 +301,7 @@ pub enum Position {
Right,
}

#[derive(Debug, Clone, Copy, Default)]
#[derive(Debug, Clone, Copy, PartialEq, Default)]
enum State {
#[default]
Idle,
Expand Down Expand Up @@ -329,6 +337,7 @@ where
renderer: &Renderer,
bounds: Size,
position: Point,
_translation: Vector,
) -> layout::Node {
let viewport = Rectangle::with_size(bounds);

Expand Down
Loading