Skip to content

Commit

Permalink
Merge pull request #3 from Philipp-M/xilem_core-rebuild-return-element
Browse files Browse the repository at this point in the history
xilem_core: return element in `View::rebuild`
  • Loading branch information
Philipp-M authored Jun 4, 2024
2 parents e3adff4 + c763e09 commit 6fa0c55
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 134 deletions.
15 changes: 7 additions & 8 deletions xilem/src/view/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

use crate::{core::View, Pod};
use masonry::{
widget::{self, WidgetMut},
ArcStr,
};
use masonry::{widget, ArcStr};
use xilem_core::Mut;

use crate::{MessageResult, ViewCtx, ViewId};

Expand Down Expand Up @@ -35,24 +33,25 @@ where
ctx.with_leaf_action_widget(|_| Pod::new(widget::Button::new(self.label.clone())))
}

fn rebuild(
fn rebuild<'el>(
&self,
prev: &Self,
_: &mut Self::ViewState,
ctx: &mut ViewCtx,
mut element: WidgetMut<widget::Button>,
) {
mut element: Mut<'el, Self::Element>,
) -> Mut<'el, Self::Element> {
if prev.label != self.label {
element.set_text(self.label.clone());
ctx.mark_changed();
}
element
}

fn teardown(
&self,
_: &mut Self::ViewState,
ctx: &mut ViewCtx,
element: <Self::Element as xilem_core::ViewElement>::Mut<'_>,
element: Mut<'_, Self::Element>,
) {
ctx.teardown_leaf(element);
}
Expand Down
15 changes: 7 additions & 8 deletions xilem/src/view/checkbox.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Copyright 2024 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0

use masonry::{
widget::{self, WidgetMut},
ArcStr,
};
use masonry::{widget, ArcStr};
use xilem_core::Mut;

use crate::{MessageResult, Pod, View, ViewCtx, ViewId};

Expand Down Expand Up @@ -45,13 +43,13 @@ where
})
}

fn rebuild(
fn rebuild<'el>(
&self,
prev: &Self,
(): &mut Self::ViewState,
ctx: &mut ViewCtx,
mut element: WidgetMut<'_, widget::Checkbox>,
) {
mut element: Mut<'el, Self::Element>,
) -> Mut<'el, Self::Element> {
if prev.label != self.label {
element.set_text(self.label.clone());
ctx.mark_changed();
Expand All @@ -60,13 +58,14 @@ where
element.set_checked(self.checked);
ctx.mark_changed();
}
element
}

fn teardown(
&self,
(): &mut Self::ViewState,
ctx: &mut ViewCtx,
element: WidgetMut<'_, widget::Checkbox>,
element: Mut<'_, Self::Element>,
) {
ctx.teardown_leaf(element);
}
Expand Down
21 changes: 11 additions & 10 deletions xilem/src/view/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use masonry::{
widget::{self, Axis, CrossAxisAlignment, MainAxisAlignment, WidgetMut},
Widget,
};
use xilem_core::{AppendVec, ElementSplice, View, ViewSequence};
use xilem_core::{AppendVec, ElementSplice, Mut, View, ViewSequence};

use crate::{Pod, ViewCtx};

Expand Down Expand Up @@ -75,13 +75,13 @@ where
(Pod::new(widget), seq_state)
}

fn rebuild(
fn rebuild<'el>(
&self,
prev: &Self,
view_state: &mut Self::ViewState,
ctx: &mut ViewCtx,
mut element: WidgetMut<widget::Flex>,
) {
mut element: Mut<'el, Self::Element>,
) -> Mut<'el, Self::Element> {
if prev.axis != self.axis {
element.set_direction(self.axis);
ctx.mark_changed();
Expand All @@ -102,24 +102,25 @@ where
let mut splice = FlexSplice {
// Skip the initial spacer which is always present
ix: 1,
element,
element: &mut element,
scratch: AppendVec::default(),
};
self.sequence
.seq_rebuild(&prev.sequence, view_state, ctx, &mut splice);
debug_assert!(splice.scratch.into_inner().is_empty());
element
}

fn teardown(
&self,
view_state: &mut Self::ViewState,
ctx: &mut ViewCtx,
element: <Self::Element as xilem_core::ViewElement>::Mut<'_>,
mut element: Mut<'_, Self::Element>,
) {
let mut splice = FlexSplice {
// Skip the initial spacer which is always present
ix: 1,
element,
element: &mut element,
scratch: AppendVec::default(),
};
self.sequence.seq_teardown(view_state, ctx, &mut splice);
Expand All @@ -138,13 +139,13 @@ where
}
}

struct FlexSplice<'w> {
struct FlexSplice<'f, 'w> {
ix: usize,
element: WidgetMut<'w, widget::Flex>,
element: &'f mut WidgetMut<'w, widget::Flex>,
scratch: AppendVec<Pod<Box<dyn Widget>>>,
}

impl ElementSplice<Pod<Box<dyn Widget>>> for FlexSplice<'_> {
impl<'f> ElementSplice<Pod<Box<dyn Widget>>> for FlexSplice<'f, '_> {
fn insert(&mut self, element: Pod<Box<dyn masonry::Widget>>) {
self.element.insert_child_pod(self.ix, element.inner);
// Insert a spacer after the child
Expand Down
16 changes: 7 additions & 9 deletions xilem/src/view/label.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Copyright 2024 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0

use masonry::{
widget::{self, WidgetMut},
ArcStr,
};
use masonry::{widget, ArcStr};
use xilem_core::Mut;

use crate::{Color, MessageResult, Pod, TextAlignment, View, ViewCtx, ViewId};

Expand Down Expand Up @@ -55,13 +53,13 @@ impl<State, Action> View<State, Action, ViewCtx> for Label {
(widget_pod, ())
}

fn rebuild(
fn rebuild<'el>(
&self,
prev: &Self,
(): &mut Self::ViewState,
ctx: &mut ViewCtx,
mut element: WidgetMut<'_, widget::Label>,
) {
mut element: Mut<'el, Self::Element>,
) -> Mut<'el, Self::Element> {
if prev.label != self.label {
element.set_text(self.label.clone());
ctx.mark_changed();
Expand All @@ -78,10 +76,10 @@ impl<State, Action> View<State, Action, ViewCtx> for Label {
element.set_alignment(self.alignment);
ctx.mark_changed();
}
element
}

fn teardown(&self, (): &mut Self::ViewState, _: &mut ViewCtx, _: WidgetMut<'_, widget::Label>) {
}
fn teardown(&self, (): &mut Self::ViewState, _: &mut ViewCtx, _: Mut<'_, Self::Element>) {}

fn message(
&self,
Expand Down
17 changes: 7 additions & 10 deletions xilem/src/view/prose.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Copyright 2024 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0

use masonry::{
text2::TextBrush,
widget::{self, WidgetMut},
ArcStr,
};
use masonry::{text2::TextBrush, widget, ArcStr};
use xilem_core::Mut;

use crate::{Color, MessageResult, Pod, TextAlignment, View, ViewCtx, ViewId};

Expand Down Expand Up @@ -57,13 +54,13 @@ impl<State, Action> View<State, Action, ViewCtx> for Prose {
(widget_pod, ())
}

fn rebuild(
fn rebuild<'el>(
&self,
prev: &Self,
(): &mut Self::ViewState,
ctx: &mut ViewCtx,
mut element: WidgetMut<'_, widget::Prose>,
) {
mut element: Mut<'el, Self::Element>,
) -> Mut<'el, Self::Element> {
if prev.label != self.label {
element.set_text(self.label.clone());
ctx.mark_changed();
Expand All @@ -76,10 +73,10 @@ impl<State, Action> View<State, Action, ViewCtx> for Prose {
element.set_alignment(self.alignment);
ctx.mark_changed();
}
element
}

fn teardown(&self, (): &mut Self::ViewState, _: &mut ViewCtx, _: WidgetMut<'_, widget::Prose>) {
}
fn teardown(&self, (): &mut Self::ViewState, _: &mut ViewCtx, _: Mut<'_, Self::Element>) {}

fn message(
&self,
Expand Down
16 changes: 7 additions & 9 deletions xilem/src/view/textbox.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Copyright 2024 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0

use masonry::{
text2::TextBrush,
widget::{self, WidgetMut},
};
use xilem_core::View;
use masonry::{text2::TextBrush, widget};
use xilem_core::{Mut, View};

use crate::{Color, MessageResult, Pod, TextAlignment, ViewCtx, ViewId};

Expand Down Expand Up @@ -80,13 +77,13 @@ impl<State: 'static, Action: 'static> View<State, Action, ViewCtx> for Textbox<S
})
}

fn rebuild(
fn rebuild<'el>(
&self,
prev: &Self,
_: &mut Self::ViewState,
ctx: &mut ViewCtx,
mut element: WidgetMut<widget::Textbox>,
) {
mut element: Mut<'el, Self::Element>,
) -> Mut<'el, Self::Element> {
// Unlike the other properties, we don't compare to the previous value;
// instead, we compare directly to the element's text. This is to handle
// cases like "Previous data says contents is 'fooba', user presses 'r',
Expand All @@ -107,13 +104,14 @@ impl<State: 'static, Action: 'static> View<State, Action, ViewCtx> for Textbox<S
element.set_alignment(self.alignment);
ctx.mark_changed();
}
element
}

fn teardown(
&self,
_: &mut Self::ViewState,
ctx: &mut ViewCtx,
element: <Self::Element as xilem_core::ViewElement>::Mut<'_>,
element: Mut<'_, Self::Element>,
) {
ctx.teardown_leaf(element);
}
Expand Down
17 changes: 10 additions & 7 deletions xilem_core/examples/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

use std::{io::stdin, path::PathBuf};

use xilem_core::{AnyElement, AnyView, SuperElement, View, ViewElement, ViewId, ViewPathTracker};
use xilem_core::{
AnyElement, AnyView, Mut, SuperElement, View, ViewElement, ViewId, ViewPathTracker,
};

#[derive(Debug)]
enum State {
Expand Down Expand Up @@ -103,7 +105,7 @@ impl SuperElement<FsPath> for FsPath {

fn with_downcast_val<R>(
this: Self::Mut<'_>,
f: impl FnOnce(<FsPath as ViewElement>::Mut<'_>) -> R,
f: impl FnOnce(Mut<'_, FsPath>) -> R,
) -> (Self::Mut<'_>, R) {
let ret = f(this);
(this, ret)
Expand Down Expand Up @@ -155,28 +157,29 @@ impl<State, Action> View<State, Action, ViewCtx> for File {
(path.into(), ())
}

fn rebuild(
fn rebuild<'el>(
&self,
prev: &Self,
_view_state: &mut Self::ViewState,
ctx: &mut ViewCtx,
element: <Self::Element as xilem_core::ViewElement>::Mut<'_>,
) {
element: Mut<'el, Self::Element>,
) -> Mut<'el, Self::Element> {
if prev.name != self.name {
let new_path = ctx.current_folder_path.join(&*self.name);
let _ = std::fs::rename(&element, &new_path);
*element = new_path;
}
if self.contents != prev.contents {
let _ = std::fs::write(element, self.contents.as_bytes());
let _ = std::fs::write(&element, self.contents.as_bytes());
}
element
}

fn teardown(
&self,
_view_state: &mut Self::ViewState,
_ctx: &mut ViewCtx,
element: <Self::Element as xilem_core::ViewElement>::Mut<'_>,
element: Mut<'_, Self::Element>,
) {
let _ = std::fs::remove_file(element);
}
Expand Down
11 changes: 6 additions & 5 deletions xilem_core/examples/user_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use core::any::Any;

use xilem_core::{
DynMessage, MessageResult, SuperElement, View, ViewElement, ViewId, ViewPathTracker,
DynMessage, MessageResult, Mut, SuperElement, View, ViewElement, ViewId, ViewPathTracker,
};

pub fn app_logic(_: &mut u32) -> impl WidgetView<u32> {
Expand Down Expand Up @@ -57,21 +57,22 @@ impl<State, Action> View<State, Action, ViewCtx> for Button {
)
}

fn rebuild(
fn rebuild<'el>(
&self,
_prev: &Self,
_view_state: &mut Self::ViewState,
_ctx: &mut ViewCtx,
_element: <Self::Element as ViewElement>::Mut<'_>,
) {
element: Mut<'el, Self::Element>,
) -> Mut<'el, Self::Element> {
// Nothing to do
element
}

fn teardown(
&self,
_view_state: &mut Self::ViewState,
_ctx: &mut ViewCtx,
_element: <Self::Element as ViewElement>::Mut<'_>,
_element: Mut<'_, Self::Element>,
) {
// Nothing to do
}
Expand Down
Loading

0 comments on commit 6fa0c55

Please sign in to comment.