Skip to content

Commit

Permalink
Update overlay code to compile with iced master
Browse files Browse the repository at this point in the history
The change in iced: iced-rs/iced#2226
  • Loading branch information
brianch authored and Davidster committed Feb 4, 2024
1 parent 0e40564 commit c9b905e
Show file tree
Hide file tree
Showing 17 changed files with 81 additions and 73 deletions.
5 changes: 3 additions & 2 deletions src/native/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use iced_widget::{
renderer, touch,
widget::{Operation, Tree},
Alignment, Border, Clipboard, Color, Element, Event, Layout, Length, Padding, Pixels,
Point, Rectangle, Shadow, Shell, Size, Widget,
Point, Rectangle, Shadow, Shell, Size, Vector, Widget
},
text::LineHeight,
};
Expand Down Expand Up @@ -583,6 +583,7 @@ where
tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
translation: Vector,
) -> Option<core::overlay::Element<'b, Message, Theme, Renderer>> {
let mut children = vec![&mut self.head, &mut self.body];
if let Some(foot) = &mut self.foot {
Expand All @@ -594,7 +595,7 @@ where
.zip(layout.children())
.filter_map(|((child, state), layout)| {
layout.children().next().and_then(|child_layout| {
child.as_widget_mut().overlay(state, child_layout, renderer)
child.as_widget_mut().overlay(state, child_layout, renderer, translation)
})
})
.collect::<Vec<_>>();
Expand Down
5 changes: 3 additions & 2 deletions src/native/color_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use iced_widget::{
self,
tree::{self, Tag, Tree},
},
Clipboard, Color, Element, Event, Layout, Length, Point, Rectangle, Shell, Widget,
Clipboard, Color, Element, Event, Layout, Length, Point, Rectangle, Shell, Vector, Widget
},
renderer::Renderer,
};
Expand Down Expand Up @@ -242,14 +242,15 @@ where
state: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
translation: Vector,
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
let picker_state: &mut State = state.state.downcast_mut();

if !self.show_picker {
return self
.underlay
.as_widget_mut()
.overlay(&mut state.children[0], layout, renderer);
.overlay(&mut state.children[0], layout, renderer, translation);
}

let bounds = layout.bounds();
Expand Down
15 changes: 10 additions & 5 deletions src/native/context_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use iced_widget::core::{
mouse::{self, Button, Cursor},
overlay, renderer,
widget::{tree, Operation, Tree},
Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Widget,
Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Vector, Widget
};

use crate::native::overlay::ContextMenuOverlay;
Expand Down Expand Up @@ -218,23 +218,28 @@ where
state: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
translation: Vector,
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
let s: &mut State = state.state.downcast_mut();

if !s.show {
return self
.underlay
.as_widget_mut()
.overlay(&mut state.children[0], layout, renderer);
.overlay(&mut state.children[0], layout, renderer, translation);
}

let position = s.cursor_position;
let content = (self.overlay)();
content.as_widget().diff(&mut state.children[1]);

Some(
ContextMenuOverlay::new(&mut state.children[1], content, self.style.clone(), s)
.overlay(position),
ContextMenuOverlay::new(
position + translation,
&mut state.children[1],
content,
self.style.clone(),
s
).overlay(),
)
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/native/date_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use iced_widget::{
self,
tree::{Tag, Tree},
},
Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Widget,
Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Vector, Widget
},
renderer::Renderer,
text,
Expand Down Expand Up @@ -249,14 +249,15 @@ where
state: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
translation: Vector,
) -> Option<core::overlay::Element<'b, Message, Theme, Renderer>> {
let picker_state: &mut State = state.state.downcast_mut();

if !self.show_picker {
return self
.underlay
.as_widget_mut()
.overlay(&mut state.children[0], layout, renderer);
.overlay(&mut state.children[0], layout, renderer, translation);
}

let bounds = layout.bounds();
Expand Down
7 changes: 4 additions & 3 deletions src/native/floating_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use iced_widget::core::{
mouse::{self, Cursor},
overlay, renderer,
widget::{Operation, Tree},
Clipboard, Element, Event, Layout, Length, Rectangle, Shell, Widget,
Clipboard, Element, Event, Layout, Length, Rectangle, Shell, Vector, Widget
};

pub mod anchor;
Expand Down Expand Up @@ -211,20 +211,21 @@ where
state: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
translation: Vector,
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
if self.hidden {
return self
.underlay
.as_widget_mut()
.overlay(&mut state.children[0], layout, renderer);
.overlay(&mut state.children[0], layout, renderer, translation);
}

if state.children.len() == 2 {
let bounds = layout.bounds();

Some(overlay::Element::new(
bounds.position(),
Box::new(FloatingElementOverlay::new(
layout.position() + translation,
&mut state.children[1],
&mut self.element,
&self.anchor,
Expand Down
5 changes: 3 additions & 2 deletions src/native/grid/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use iced_widget::core::{
overlay::Group,
renderer::Style,
widget::{Operation, Tree},
Clipboard, Element, Event, Layout, Length, Rectangle, Shell, Size, Widget,
Clipboard, Element, Event, Layout, Length, Rectangle, Shell, Size, Vector, Widget
};

use super::{layout::layout, types::Grid};
Expand Down Expand Up @@ -159,13 +159,14 @@ where
tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
translation: Vector,
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
let children = self
.elements_iter_mut()
.zip(&mut tree.children)
.zip(layout.children())
.filter_map(|((child, state), layout)| {
child.as_widget_mut().overlay(state, layout, renderer)
child.as_widget_mut().overlay(state, layout, renderer, translation)
})
.collect::<Vec<_>>();

Expand Down
8 changes: 3 additions & 5 deletions src/native/modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use iced_widget::core::{
mouse::{self, Cursor},
overlay, renderer,
widget::{Operation, Tree},
Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Widget,
Clipboard, Element, Event, Layout, Length, Rectangle, Shell, Vector, Widget
};

pub use crate::style::modal::StyleSheet;
Expand Down Expand Up @@ -232,14 +232,12 @@ where
state: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
translation: Vector,
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
if let Some(overlay) = &mut self.overlay {
let bounds = layout.bounds();
let position = Point::new(bounds.x, bounds.y);
overlay.as_widget().diff(&mut state.children[1]);

Some(overlay::Element::new(
position,
Box::new(ModalOverlay::new(
&mut state.children[1],
overlay,
Expand All @@ -253,7 +251,7 @@ where
} else {
self.underlay
.as_widget_mut()
.overlay(&mut state.children[0], layout, renderer)
.overlay(&mut state.children[0], layout, renderer, translation)
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/native/overlay/color_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ where
/// Turn this [`ColorPickerOverlay`] into an overlay [`Element`](overlay::Element).
#[must_use]
pub fn overlay(self) -> overlay::Element<'a, Message, Theme, Renderer> {
overlay::Element::new(self.position, Box::new(self))
overlay::Element::new(Box::new(self))
}

/// The event handling for the HSV color area.
Expand Down Expand Up @@ -556,8 +556,6 @@ where
&mut self,
renderer: &Renderer,
bounds: Size,
position: Point,
_translation: Vector,
) -> Node {
let (max_width, max_height) = if bounds.width > bounds.height {
(600.0, 300.0)
Expand Down Expand Up @@ -598,10 +596,10 @@ where
.bounds();

// ----------- Block 1 ----------------------
let block1_node = block1_layout(self, renderer, block1_bounds, position);
let block1_node = block1_layout(self, renderer, block1_bounds, self.position);

// ----------- Block 2 ----------------------
let block2_node = block2_layout(self, renderer, block2_bounds, position);
let block2_node = block2_layout(self, renderer, block2_bounds, self.position);

let (width, height) = if bounds.width > bounds.height {
(
Expand All @@ -618,7 +616,7 @@ where
let mut node =
Node::with_children(Size::new(width, height), vec![block1_node, block2_node]);

node.center_and_bounce(position, bounds);
node.center_and_bounce(self.position, bounds);
node
}

Expand Down
14 changes: 8 additions & 6 deletions src/native/overlay/context_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use iced_widget::core::{
overlay, renderer, touch,
widget::tree::Tree,
window, Border, Clipboard, Color, Element, Event, Layout, Point, Rectangle, Shadow, Shell,
Size, Vector,
Size,
};

/// The overlay of the [`ContextMenu`](crate::native::ContextMenu).
Expand All @@ -28,6 +28,8 @@ pub struct ContextMenuOverlay<
Renderer: 'a + core::Renderer,
Theme: StyleSheet,
{
// The position of the element
position: Point,
/// The state of the [`ContextMenuOverlay`].
tree: &'a mut Tree,
/// The content of the [`ContextMenuOverlay`].
Expand All @@ -46,6 +48,7 @@ where
{
/// Creates a new [`ContextMenuOverlay`].
pub(crate) fn new<C>(
position: Point,
tree: &'a mut Tree,
content: C,
style: <Theme as StyleSheet>::Style,
Expand All @@ -55,6 +58,7 @@ where
C: Into<Element<'a, Message, Theme, Renderer>>,
{
ContextMenuOverlay {
position,
tree,
content: content.into(),
style,
Expand All @@ -63,8 +67,8 @@ where
}

/// Turn this [`ContextMenuOverlay`] into an overlay [`Element`](overlay::Element).
pub fn overlay(self, position: Point) -> overlay::Element<'a, Message, Theme, Renderer> {
overlay::Element::new(position, Box::new(self))
pub fn overlay(self) -> overlay::Element<'a, Message, Theme, Renderer> {
overlay::Element::new(Box::new(self))
}
}

Expand All @@ -79,8 +83,6 @@ where
&mut self,
renderer: &Renderer,
bounds: Size,
position: Point,
_translation: Vector,
) -> Node {
let limits = Limits::new(Size::ZERO, bounds);
let max_size = limits.max();
Expand All @@ -91,7 +93,7 @@ where
.layout(self.tree, renderer, &limits);

// Try to stay inside borders
let mut position = position;
let mut position = self.position;
if position.x + content.size().width > bounds.width {
position.x = f32::max(0.0, position.x - content.size().width);
}
Expand Down
8 changes: 3 additions & 5 deletions src/native/overlay/date_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use iced_widget::{
touch,
widget::tree::Tree,
Alignment, Border, Clipboard, Color, Element, Event, Layout, Length, Overlay, Padding,
Point, Rectangle, Renderer as _, Shadow, Shell, Size, Vector, Widget,
Point, Rectangle, Renderer as _, Shadow, Shell, Size, Widget,
},
renderer::Renderer,
text, Button, Column, Container, Row, Text,
Expand Down Expand Up @@ -113,7 +113,7 @@ where
/// Turn this [`DatePickerOverlay`] into an overlay [`Element`](overlay::Element).
#[must_use]
pub fn overlay(self) -> overlay::Element<'a, Message, Theme, Renderer> {
overlay::Element::new(self.position, Box::new(self))
overlay::Element::new(Box::new(self))
}

/// String representation of the current year.
Expand Down Expand Up @@ -373,8 +373,6 @@ where
&mut self,
renderer: &Renderer,
bounds: Size,
position: Point,
_translation: Vector,
) -> Node {
let limits = Limits::new(Size::ZERO, bounds)
.shrink(Padding::from(PADDING))
Expand Down Expand Up @@ -528,7 +526,7 @@ where
),
vec![col, cancel_button, submit_button],
);
node.center_and_bounce(position, bounds);
node.center_and_bounce(self.position, bounds);
node
}

Expand Down
Loading

0 comments on commit c9b905e

Please sign in to comment.