Skip to content

Commit

Permalink
Merge pull request #6 from Zoxc/update-flags
Browse files Browse the repository at this point in the history
Introduce ChangeFlags and use it instead of bools to track changes in views
  • Loading branch information
Zoxc authored Dec 1, 2022
2 parents 2102bec + 01d8417 commit 3d99fdf
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 21 deletions.
6 changes: 2 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,14 @@ where
let state =
if let Some(element) = self.root_pod.as_mut().and_then(|pod| pod.downcast_mut()) {
let mut state = response.state.unwrap();
let changed = response.view.rebuild(
let changes = response.view.rebuild(
&mut self.cx,
response.prev.as_ref().unwrap(),
self.id.as_mut().unwrap(),
&mut state,
element,
);
if changed {
self.root_pod.as_mut().unwrap().request_update();
}
self.root_pod.as_mut().unwrap().mark(changes);
assert!(self.cx.is_empty(), "id path imbalance on rebuild");
state
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use futures_task::{ArcWake, Waker};
use crate::{
event::EventResult,
id::{Id, IdPath},
widget::Widget,
widget::{ChangeFlags, Widget},
};

/// A view object representing a node in the UI.
Expand Down Expand Up @@ -73,7 +73,7 @@ pub trait View<T, A = ()>: Send {
id: &mut Id,
state: &mut Self::State,
element: &mut Self::Element,
) -> bool;
) -> ChangeFlags;

/// Propagate an event.
///
Expand Down
9 changes: 4 additions & 5 deletions src/view/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::any::Any;

use crate::{event::EventResult, id::Id};
use crate::{event::EventResult, id::Id, widget::ChangeFlags};

use super::{Cx, View};

Expand Down Expand Up @@ -58,12 +58,11 @@ impl<T, A> View<T, A> for Button<T, A> {
_id: &mut crate::id::Id,
_state: &mut Self::State,
element: &mut Self::Element,
) -> bool {
) -> ChangeFlags {
if prev.label != self.label {
element.set_label(self.label.clone());
true
element.set_label(self.label.clone())
} else {
false
ChangeFlags::empty()
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/view/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::any::Any;

use crate::{event::EventResult, id::Id};
use crate::{event::EventResult, id::Id, widget::ChangeFlags};

use super::{Cx, View};

Expand All @@ -35,12 +35,11 @@ impl<T, A> View<T, A> for String {
_id: &mut crate::id::Id,
_state: &mut Self::State,
element: &mut Self::Element,
) -> bool {
) -> ChangeFlags {
if prev != self {
element.set_text(self.clone());
true
element.set_text(self.clone())
} else {
false
ChangeFlags::empty()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use piet_scene::SceneBuilder;
use self::contexts::LifeCycleCx;
pub use self::contexts::{AlignCx, CxState, EventCx, LayoutCx, PaintCx, PreparePaintCx, UpdateCx};
pub use self::core::Pod;
pub(crate) use self::core::{PodFlags, WidgetState};
pub(crate) use self::core::{ChangeFlags, PodFlags, WidgetState};
pub use self::raw_event::{LifeCycle, RawEvent};

use self::align::SingleAlignment;
Expand Down
5 changes: 3 additions & 2 deletions src/widget/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use super::{
align::{FirstBaseline, LastBaseline, SingleAlignment},
contexts::LifeCycleCx,
piet_scene_helpers::{self, UnitPoint},
AlignCx, EventCx, LayoutCx, LifeCycle, PaintCx, RawEvent, UpdateCx, Widget,
AlignCx, ChangeFlags, EventCx, LayoutCx, LifeCycle, PaintCx, RawEvent, UpdateCx, Widget,
};

pub struct Button {
Expand All @@ -40,9 +40,10 @@ impl Button {
}
}

pub fn set_label(&mut self, label: String) {
pub fn set_label(&mut self, label: String) -> ChangeFlags {
self.label = label;
self.layout = None;
ChangeFlags::LAYOUT | ChangeFlags::PAINT
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/widget/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ bitflags! {
}
}

bitflags! {
#[derive(Default)]
#[must_use]
pub struct ChangeFlags: u8 {
const UPDATE = 1;
const LAYOUT = 2;
const PAINT = 4;
}
}

/// A pod that contains a widget (in a container).
pub struct Pod {
pub(crate) state: WidgetState,
Expand Down Expand Up @@ -127,6 +137,11 @@ impl Pod {
(*self.widget).as_any_mut().downcast_mut()
}

pub fn mark(&mut self, flags: ChangeFlags) {
self.state
.request(PodFlags::from_bits(flags.bits().into()).unwrap());
}

pub fn request_update(&mut self) {
self.state.request(PodFlags::REQUEST_UPDATE);
}
Expand Down
5 changes: 3 additions & 2 deletions src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::text::ParleyBrush;
use super::{
align::{FirstBaseline, LastBaseline, SingleAlignment, VertAlignment},
contexts::LifeCycleCx,
AlignCx, EventCx, LayoutCx, LifeCycle, PaintCx, RawEvent, UpdateCx, Widget,
AlignCx, ChangeFlags, EventCx, LayoutCx, LifeCycle, PaintCx, RawEvent, UpdateCx, Widget,
};

pub struct TextWidget {
Expand All @@ -39,8 +39,9 @@ impl TextWidget {
}
}

pub fn set_text(&mut self, text: String) {
pub fn set_text(&mut self, text: String) -> ChangeFlags {
self.text = text;
ChangeFlags::LAYOUT | ChangeFlags::PAINT
}
}

Expand Down

0 comments on commit 3d99fdf

Please sign in to comment.