Skip to content

Commit

Permalink
Allow &mut self in overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkah committed Nov 29, 2022
1 parent b4aedce commit 555d5a3
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 83 deletions.
31 changes: 13 additions & 18 deletions lazy/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use iced_native::{
};

use ouroboros::self_referencing;
use std::cell::{Ref, RefCell};
use std::cell::RefCell;
use std::marker::PhantomData;

/// A reusable, custom widget that uses The Elm Architecture.
Expand Down Expand Up @@ -322,25 +322,25 @@ where
}

fn overlay<'b>(
&'b self,
&'b mut self,
tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
) -> Option<overlay::Element<'b, Message, Renderer>> {
let overlay = OverlayBuilder {
instance: self,
instance_ref_builder: |instance| instance.state.borrow(),
tree,
types: PhantomData,
overlay_builder: |instance, tree| {
instance
.as_ref()
.unwrap()
.borrow_element()
.as_ref()
.unwrap()
.as_widget()
.overlay(&mut tree.children[0], layout, renderer)
instance.state.get_mut().as_mut().unwrap().with_element_mut(
move |element| {
element.as_mut().unwrap().as_widget_mut().overlay(
&mut tree.children[0],
layout,
renderer,
)
},
)
},
}
.build();
Expand All @@ -362,15 +362,11 @@ where

#[self_referencing]
struct Overlay<'a, 'b, Message, Renderer, Event, S> {
instance: &'a Instance<'b, Message, Renderer, Event, S>,
instance: &'a mut Instance<'b, Message, Renderer, Event, S>,
tree: &'a mut Tree,
types: PhantomData<(Message, Event, S)>,

#[borrows(instance)]
#[covariant]
instance_ref: Ref<'this, Option<State<'a, Message, Renderer, Event, S>>>,

#[borrows(instance_ref, mut tree)]
#[borrows(mut instance, mut tree)]
#[covariant]
overlay: Option<overlay::Element<'this, Event, Renderer>>,
}
Expand Down Expand Up @@ -514,7 +510,6 @@ where
self.overlay = Some(
OverlayBuilder {
instance: overlay.instance,
instance_ref_builder: |instance| instance.state.borrow(),
tree: overlay.tree,
types: PhantomData,
overlay_builder: |_, _| None,
Expand Down
27 changes: 9 additions & 18 deletions lazy/src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ where
}

fn overlay<'b>(
&'b self,
&'b mut self,
tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
Expand All @@ -216,12 +216,12 @@ where
cached: self,
tree: &mut tree.children[0],
types: PhantomData,
element_ref_builder: |cached| cached.element.borrow(),
element_builder: |element_ref| {
element_ref.as_ref().unwrap().borrow()
},
overlay_builder: |element, tree| {
element.as_widget().overlay(tree, layout, renderer)
overlay_builder: |cached, tree| {
Rc::get_mut(cached.element.get_mut().as_mut().unwrap())
.unwrap()
.get_mut()
.as_widget_mut()
.overlay(tree, layout, renderer)
},
}
.build();
Expand All @@ -237,20 +237,11 @@ where

#[self_referencing]
struct Overlay<'a, 'b, Message, Renderer, Dependency, View> {
cached: &'a Lazy<'b, Message, Renderer, Dependency, View>,
cached: &'a mut Lazy<'b, Message, Renderer, Dependency, View>,
tree: &'a mut Tree,
types: PhantomData<(Message, Dependency, View)>,

#[borrows(cached)]
#[covariant]
element_ref:
Ref<'this, Option<Rc<RefCell<Element<'static, Message, Renderer>>>>>,

#[borrows(element_ref)]
#[covariant]
element: Ref<'this, Element<'static, Message, Renderer>>,

#[borrows(element, mut tree)]
#[borrows(mut cached, mut tree)]
#[covariant]
overlay: Option<overlay::Element<'this, Message, Renderer>>,
}
Expand Down
22 changes: 13 additions & 9 deletions lazy/src/responsive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,35 +235,39 @@ where
}

fn overlay<'b>(
&'b self,
&'b mut self,
tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
) -> Option<overlay::Element<'b, Message, Renderer>> {
use std::ops::DerefMut;

let state = tree.state.downcast_ref::<State>();

let overlay = OverlayBuilder {
content: self.content.borrow_mut(),
tree: state.tree.borrow_mut(),
types: PhantomData,
overlay_builder: |content, tree| {
overlay_builder: |content: &mut RefMut<Content<_, _>>, tree| {
content.update(
tree,
renderer,
layout.bounds().size(),
&self.view,
);

let Content {
element, layout, ..
} = content.deref_mut();

let content_layout = Layout::with_offset(
layout.position() - Point::ORIGIN,
&content.layout,
layout.bounds().position() - Point::ORIGIN,
layout,
);

content.element.as_widget().overlay(
tree,
content_layout,
renderer,
)
element
.as_widget_mut()
.overlay(tree, content_layout, renderer)
},
}
.build();
Expand Down
4 changes: 2 additions & 2 deletions native/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ where
}

fn overlay<'b>(
&'b self,
&'b mut self,
tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
Expand Down Expand Up @@ -560,7 +560,7 @@ where
}

fn overlay<'b>(
&'b self,
&'b mut self,
state: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
Expand Down
6 changes: 3 additions & 3 deletions native/src/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ where
/// This method will generally only be used by advanced users that are
/// implementing the [`Widget`](crate::Widget) trait.
pub fn from_children<'a, Message, Renderer>(
children: &'a [crate::Element<'_, Message, Renderer>],
children: &'a mut [crate::Element<'_, Message, Renderer>],
tree: &'a mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
Expand All @@ -102,11 +102,11 @@ where
Renderer: crate::Renderer,
{
children
.iter()
.iter_mut()
.zip(&mut tree.children)
.zip(layout.children())
.filter_map(|((child, state), layout)| {
child.as_widget().overlay(state, layout, renderer)
child.as_widget_mut().overlay(state, layout, renderer)
})
.next()
}
18 changes: 9 additions & 9 deletions native/src/user_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ where

let mut state = State::Updated;
let mut manual_overlay =
ManuallyDrop::new(self.root.as_widget().overlay(
ManuallyDrop::new(self.root.as_widget_mut().overlay(
&mut self.state,
Layout::new(&self.base),
renderer,
Expand Down Expand Up @@ -226,7 +226,7 @@ where
);

manual_overlay =
ManuallyDrop::new(self.root.as_widget().overlay(
ManuallyDrop::new(self.root.as_widget_mut().overlay(
&mut self.state,
Layout::new(&self.base),
renderer,
Expand Down Expand Up @@ -395,11 +395,11 @@ where

let viewport = Rectangle::with_size(self.bounds);

let base_cursor = if let Some(overlay) = self.root.as_widget().overlay(
&mut self.state,
Layout::new(&self.base),
renderer,
) {
let base_cursor = if let Some(overlay) = self
.root
.as_widget_mut()
.overlay(&mut self.state, Layout::new(&self.base), renderer)
{
let overlay_layout = self
.overlay
.take()
Expand Down Expand Up @@ -452,7 +452,7 @@ where
overlay
.as_ref()
.and_then(|layout| {
root.as_widget()
root.as_widget_mut()
.overlay(&mut self.state, Layout::new(base), renderer)
.map(|overlay| {
let overlay_interaction = overlay.mouse_interaction(
Expand Down Expand Up @@ -497,7 +497,7 @@ where
);

if let Some(layout) = self.overlay.as_ref() {
if let Some(overlay) = self.root.as_widget().overlay(
if let Some(overlay) = self.root.as_widget_mut().overlay(
&mut self.state,
Layout::new(&self.base),
renderer,
Expand Down
2 changes: 1 addition & 1 deletion native/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ where

/// Returns the overlay of the [`Widget`], if there is any.
fn overlay<'a>(
&'a self,
&'a mut self,
_state: &'a mut Tree,
_layout: Layout<'_>,
_renderer: &Renderer,
Expand Down
4 changes: 2 additions & 2 deletions native/src/widget/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,12 @@ where
}

fn overlay<'b>(
&'b self,
&'b mut self,
tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
) -> Option<overlay::Element<'b, Message, Renderer>> {
self.content.as_widget().overlay(
self.content.as_widget_mut().overlay(
&mut tree.children[0],
layout.children().next().unwrap(),
renderer,
Expand Down
4 changes: 2 additions & 2 deletions native/src/widget/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,12 @@ where
}

fn overlay<'b>(
&'b self,
&'b mut self,
tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
) -> Option<overlay::Element<'b, Message, Renderer>> {
overlay::from_children(&self.children, tree, layout, renderer)
overlay::from_children(&mut self.children, tree, layout, renderer)
}
}

Expand Down
4 changes: 2 additions & 2 deletions native/src/widget/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ where
}

fn overlay<'b>(
&'b self,
&'b mut self,
tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
) -> Option<overlay::Element<'b, Message, Renderer>> {
self.content.as_widget().overlay(
self.content.as_widget_mut().overlay(
&mut tree.children[0],
layout.children().next().unwrap(),
renderer,
Expand Down
4 changes: 2 additions & 2 deletions native/src/widget/pane_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,13 +444,13 @@ where
}

fn overlay<'b>(
&'b self,
&'b mut self,
tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
) -> Option<overlay::Element<'_, Message, Renderer>> {
self.contents
.iter()
.iter_mut()
.zip(&mut tree.children)
.zip(layout.children())
.filter_map(|(((_, pane), tree), layout)| {
Expand Down
8 changes: 4 additions & 4 deletions native/src/widget/pane_grid/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,12 @@ where
}

pub(crate) fn overlay<'b>(
&'b self,
&'b mut self,
tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
) -> Option<overlay::Element<'b, Message, Renderer>> {
if let Some(title_bar) = self.title_bar.as_ref() {
if let Some(title_bar) = self.title_bar.as_mut() {
let mut children = layout.children();
let title_bar_layout = children.next()?;

Expand All @@ -321,14 +321,14 @@ where
match title_bar.overlay(title_bar_state, title_bar_layout, renderer)
{
Some(overlay) => Some(overlay),
None => self.body.as_widget().overlay(
None => self.body.as_widget_mut().overlay(
body_state,
children.next()?,
renderer,
),
}
} else {
self.body.as_widget().overlay(
self.body.as_widget_mut().overlay(
&mut tree.children[0],
layout,
renderer,
Expand Down
8 changes: 4 additions & 4 deletions native/src/widget/pane_grid/title_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ where
}

pub(crate) fn overlay<'b>(
&'b self,
&'b mut self,
tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
Expand All @@ -415,13 +415,13 @@ where
let controls_state = states.next().unwrap();

content
.as_widget()
.as_widget_mut()
.overlay(title_state, title_layout, renderer)
.or_else(move || {
controls.as_ref().and_then(|controls| {
controls.as_mut().and_then(|controls| {
let controls_layout = children.next()?;

controls.as_widget().overlay(
controls.as_widget_mut().overlay(
controls_state,
controls_layout,
renderer,
Expand Down
Loading

0 comments on commit 555d5a3

Please sign in to comment.