Skip to content

Commit

Permalink
Make widget::Tree mutable in Widget::layout
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Aug 30, 2023
1 parent 301e6e5 commit a026e91
Show file tree
Hide file tree
Showing 45 changed files with 135 additions and 135 deletions.
4 changes: 2 additions & 2 deletions core/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ where

fn layout(
&self,
tree: &Tree,
tree: &mut Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
Expand Down Expand Up @@ -492,7 +492,7 @@ where

fn layout(
&self,
tree: &Tree,
tree: &mut Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
Expand Down
4 changes: 2 additions & 2 deletions core/src/layout/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn resolve<Message, Renderer>(
spacing: f32,
align_items: Alignment,
items: &[Element<'_, Message, Renderer>],
trees: &[widget::Tree],
trees: &mut [widget::Tree],
) -> Node
where
Renderer: crate::Renderer,
Expand All @@ -83,7 +83,7 @@ where
let mut nodes: Vec<Node> = Vec::with_capacity(items.len());
nodes.resize(items.len(), Node::default());

for (i, (child, tree)) in items.iter().zip(trees).enumerate() {
for (i, (child, tree)) in items.iter().zip(trees.iter_mut()).enumerate() {
let fill_factor = match axis {
Axis::Horizontal => child.as_widget().width(),
Axis::Vertical => child.as_widget().height(),
Expand Down
2 changes: 1 addition & 1 deletion core/src/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where
///
/// [`Node`]: layout::Node
fn layout(
&self,
&mut self,
renderer: &Renderer,
bounds: Size,
position: Point,
Expand Down
4 changes: 2 additions & 2 deletions core/src/overlay/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ where

/// Computes the layout of the [`Element`] in the given bounds.
pub fn layout(
&self,
&mut self,
renderer: &Renderer,
bounds: Size,
translation: Vector,
Expand Down Expand Up @@ -150,7 +150,7 @@ where
Renderer: crate::Renderer,
{
fn layout(
&self,
&mut self,
renderer: &Renderer,
bounds: Size,
position: Point,
Expand Down
4 changes: 2 additions & 2 deletions core/src/overlay/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ where
Renderer: crate::Renderer,
{
fn layout(
&self,
&mut self,
renderer: &Renderer,
bounds: Size,
position: Point,
Expand All @@ -71,7 +71,7 @@ where
layout::Node::with_children(
bounds,
self.children
.iter()
.iter_mut()
.map(|child| child.layout(renderer, bounds, translation))
.collect(),
)
Expand Down
2 changes: 1 addition & 1 deletion core/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ where
/// user interface.
fn layout(
&self,
tree: &Tree,
tree: &mut Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node;
Expand Down
19 changes: 9 additions & 10 deletions core/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ use crate::alignment;
use crate::layout;
use crate::mouse;
use crate::renderer;
use crate::text::{self, Paragraph as _};
use crate::text::{self, Paragraph};
use crate::widget::tree::{self, Tree};
use crate::{Color, Element, Layout, Length, Pixels, Point, Rectangle, Widget};

use std::borrow::Cow;
use std::cell::RefCell;

pub use text::{LineHeight, Shaping};

Expand Down Expand Up @@ -120,7 +119,7 @@ where

/// The internal state of a [`Text`] widget.
#[derive(Debug, Default)]
pub struct State<T>(RefCell<T>);
pub struct State<P: Paragraph>(P);

impl<'a, Message, Renderer> Widget<Message, Renderer> for Text<'a, Renderer>
where
Expand All @@ -132,7 +131,7 @@ where
}

fn state(&self) -> tree::State {
tree::State::new(State(RefCell::new(Renderer::Paragraph::default())))
tree::State::new(State(Renderer::Paragraph::default()))
}

fn width(&self) -> Length {
Expand All @@ -145,12 +144,12 @@ where

fn layout(
&self,
tree: &Tree,
tree: &mut Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
layout(
tree.state.downcast_ref::<State<Renderer::Paragraph>>(),
tree.state.downcast_mut::<State<Renderer::Paragraph>>(),
renderer,
limits,
self.width,
Expand Down Expand Up @@ -189,7 +188,7 @@ where

/// Produces the [`layout::Node`] of a [`Text`] widget.
pub fn layout<Renderer>(
state: &State<Renderer::Paragraph>,
state: &mut State<Renderer::Paragraph>,
renderer: &Renderer,
limits: &layout::Limits,
width: Length,
Expand All @@ -211,10 +210,10 @@ where
let size = size.unwrap_or_else(|| renderer.default_size());
let font = font.unwrap_or_else(|| renderer.default_font());

let mut paragraph = state.0.borrow_mut();
let State(ref mut paragraph) = state;

renderer.update_paragraph(
&mut paragraph,
paragraph,
text::Text {
content,
bounds,
Expand Down Expand Up @@ -251,7 +250,7 @@ pub fn draw<Renderer>(
) where
Renderer: text::Renderer,
{
let paragraph = state.0.borrow();
let State(ref paragraph) = state;
let bounds = layout.bounds();

let x = match paragraph.horizontal_alignment() {
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_quad/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod quad {

fn layout(
&self,
_tree: &widget::Tree,
_tree: &mut widget::Tree,
_renderer: &Renderer,
_limits: &layout::Limits,
) -> layout::Node {
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_widget/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ mod circle {

fn layout(
&self,
_tree: &widget::Tree,
_tree: &mut widget::Tree,
_renderer: &Renderer,
_limits: &layout::Limits,
) -> layout::Node {
Expand Down
2 changes: 1 addition & 1 deletion examples/geometry/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod rainbow {

fn layout(
&self,
_tree: &widget::Tree,
_tree: &mut widget::Tree,
_renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
Expand Down
2 changes: 1 addition & 1 deletion examples/loading_spinners/src/circular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ where

fn layout(
&self,
_tree: &Tree,
_tree: &mut Tree,
_renderer: &iced::Renderer<Theme>,
limits: &layout::Limits,
) -> layout::Node {
Expand Down
2 changes: 1 addition & 1 deletion examples/loading_spinners/src/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ where

fn layout(
&self,
_tree: &Tree,
_tree: &mut Tree,
_renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
Expand Down
12 changes: 7 additions & 5 deletions examples/modal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,15 @@ mod modal {

fn layout(
&self,
tree: &widget::Tree,
tree: &mut widget::Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
self.base
.as_widget()
.layout(&tree.children[0], renderer, limits)
self.base.as_widget().layout(
&mut tree.children[0],
renderer,
limits,
)
}

fn on_event(
Expand Down Expand Up @@ -402,7 +404,7 @@ mod modal {
Message: Clone,
{
fn layout(
&self,
&mut self,
renderer: &Renderer,
_bounds: Size,
position: Point,
Expand Down
12 changes: 7 additions & 5 deletions examples/toast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,15 @@ mod toast {

fn layout(
&self,
tree: &Tree,
tree: &mut Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
self.content
.as_widget()
.layout(&tree.children[0], renderer, limits)
self.content.as_widget().layout(
&mut tree.children[0],
renderer,
limits,
)
}

fn tag(&self) -> widget::tree::Tag {
Expand Down Expand Up @@ -503,7 +505,7 @@ mod toast {
for Overlay<'a, 'b, Message>
{
fn layout(
&self,
&mut self,
renderer: &Renderer,
bounds: Size,
position: Point,
Expand Down
6 changes: 3 additions & 3 deletions runtime/src/user_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ where
state.diff(root.as_widget());

let base = root.as_widget().layout(
&state,
&mut state,
renderer,
&layout::Limits::new(Size::ZERO, bounds),
);
Expand Down Expand Up @@ -230,7 +230,7 @@ where
let _ = ManuallyDrop::into_inner(manual_overlay);

self.base = self.root.as_widget().layout(
&self.state,
&mut self.state,
renderer,
&layout::Limits::new(Size::ZERO, self.bounds),
);
Expand Down Expand Up @@ -330,7 +330,7 @@ where

shell.revalidate_layout(|| {
self.base = self.root.as_widget().layout(
&self.state,
&mut self.state,
renderer,
&layout::Limits::new(Size::ZERO, self.bounds),
);
Expand Down
10 changes: 6 additions & 4 deletions widget/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,16 @@ where

fn layout(
&self,
tree: &Tree,
tree: &mut Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
layout(limits, self.width, self.height, self.padding, |limits| {
self.content
.as_widget()
.layout(&tree.children[0], renderer, limits)
self.content.as_widget().layout(
&mut tree.children[0],
renderer,
limits,
)
})
}

Expand Down
2 changes: 1 addition & 1 deletion widget/src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ where

fn layout(
&self,
_tree: &Tree,
_tree: &mut Tree,
_renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
Expand Down
4 changes: 2 additions & 2 deletions widget/src/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ where

fn layout(
&self,
tree: &Tree,
tree: &mut Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
Expand All @@ -195,7 +195,7 @@ where
|limits| {
let state = tree
.state
.downcast_ref::<widget::text::State<Renderer::Paragraph>>();
.downcast_mut::<widget::text::State<Renderer::Paragraph>>();

widget::text::layout(
state,
Expand Down
4 changes: 2 additions & 2 deletions widget/src/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ where

fn layout(
&self,
tree: &Tree,
tree: &mut Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
Expand All @@ -139,7 +139,7 @@ where
self.spacing,
self.align_items,
&self.children,
&tree.children,
&mut tree.children,
)
}

Expand Down
2 changes: 1 addition & 1 deletion widget/src/combo_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ where

fn layout(
&self,
tree: &widget::Tree,
tree: &mut widget::Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
Expand Down
2 changes: 1 addition & 1 deletion widget/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ where

fn layout(
&self,
tree: &Tree,
tree: &mut Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
Expand Down
2 changes: 1 addition & 1 deletion widget/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ where

fn layout(
&self,
_tree: &Tree,
_tree: &mut Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
Expand Down
2 changes: 1 addition & 1 deletion widget/src/image/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ where

fn layout(
&self,
_tree: &Tree,
_tree: &mut Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
Expand Down
Loading

0 comments on commit a026e91

Please sign in to comment.