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

Update overlay code to compile with iced master #201

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/grid/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ impl Sandbox for App {
let col_spacing_slider =
slider(0.0..=100.0, self.column_spacing, Message::ColumnSpacing).width(Length::Fill);

let debug_mode_check = checkbox("", self.debug_layout, Message::DebugToggled);
let debug_mode_check = checkbox("", self.debug_layout).on_toggle(Message::DebugToggled);

let fill_checkboxes = row![
checkbox("Width", self.fill_width, Message::FillWidth),
checkbox("Height", self.fill_height, Message::FillHeight)
checkbox("Width", self.fill_width).on_toggle(Message::FillWidth),
checkbox("Height", self.fill_height).on_toggle(Message::FillHeight)
]
.spacing(10);

Expand Down
7 changes: 5 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,9 @@ 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
13 changes: 8 additions & 5 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,17 @@ 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);
return self.underlay.as_widget_mut().overlay(
&mut state.children[0],
layout,
renderer,
translation,
);
}

let bounds = layout.bounds();
Expand Down
24 changes: 16 additions & 8 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,31 @@ 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);
return self.underlay.as_widget_mut().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
13 changes: 8 additions & 5 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,17 @@ 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);
return self.underlay.as_widget_mut().overlay(
&mut state.children[0],
layout,
renderer,
translation,
);
}

let bounds = layout.bounds();
Expand Down
23 changes: 13 additions & 10 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,27 +211,30 @@ 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);
return self.underlay.as_widget_mut().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(
Some(overlay::Element::new(Box::new(
FloatingElementOverlay::new(
layout.position() + translation,
&mut state.children[1],
&mut self.element,
&self.anchor,
&self.offset,
bounds,
)),
))
),
)))
} else {
None
}
Expand Down
7 changes: 5 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,16 @@ 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
35 changes: 17 additions & 18 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,28 +232,27 @@ 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,
self.backdrop.clone(),
self.esc.clone(),
self.style.clone(),
self.horizontal_alignment,
self.vertical_alignment,
)),
))
Some(overlay::Element::new(Box::new(ModalOverlay::new(
&mut state.children[1],
overlay,
self.backdrop.clone(),
self.esc.clone(),
self.style.clone(),
self.horizontal_alignment,
self.vertical_alignment,
))))
} else {
self.underlay
.as_widget_mut()
.overlay(&mut state.children[0], layout, renderer)
self.underlay.as_widget_mut().overlay(
&mut state.children[0],
layout,
renderer,
translation,
)
}
}

Expand Down
16 changes: 5 additions & 11 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 @@ -552,13 +552,7 @@ where
Message: 'static + Clone,
Theme: 'a + StyleSheet + button::StyleSheet + widget::text::StyleSheet,
{
fn layout(
&mut self,
renderer: &Renderer,
bounds: Size,
position: Point,
_translation: Vector,
) -> Node {
fn layout(&mut self, renderer: &Renderer, bounds: Size) -> Node {
let (max_width, max_height) = if bounds.width > bounds.height {
(600.0, 300.0)
} else {
Expand Down Expand Up @@ -598,10 +592,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 +612,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
20 changes: 9 additions & 11 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 @@ -75,13 +79,7 @@ where
Renderer: 'a + core::Renderer,
Theme: StyleSheet,
{
fn layout(
&mut self,
renderer: &Renderer,
bounds: Size,
position: Point,
_translation: Vector,
) -> Node {
fn layout(&mut self, renderer: &Renderer, bounds: Size) -> Node {
let limits = Limits::new(Size::ZERO, bounds);
let max_size = limits.max();

Expand All @@ -91,7 +89,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
Loading
Loading