Skip to content

Commit

Permalink
Add update_state_deferred and simplify update_state
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Jan 19, 2024
1 parent 9f71e60 commit ccfe8f2
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 66 deletions.
22 changes: 11 additions & 11 deletions src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,17 @@ impl Id {
});
}

pub fn update_state(&self, state: impl Any, deferred: bool) {
if !deferred {
self.add_update_message(UpdateMessage::State {
id: *self,
state: Box::new(state),
});
} else {
CENTRAL_DEFERRED_UPDATE_MESSAGES.with(|msgs| {
msgs.borrow_mut().push((*self, Box::new(state)));
});
}
pub fn update_state(&self, state: impl Any) {
self.add_update_message(UpdateMessage::State {
id: *self,
state: Box::new(state),
});
}

pub fn update_state_deferred(&self, state: impl Any) {
CENTRAL_DEFERRED_UPDATE_MESSAGES.with(|msgs| {
msgs.borrow_mut().push((*self, Box::new(state)));
});
}

pub(crate) fn update_style(&self, style: Style, offset: StackOffset<Style>) {
Expand Down
4 changes: 1 addition & 3 deletions src/views/dyn_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ pub fn dyn_container<CF: Fn(T) -> Box<dyn View> + 'static, T: 'static>(
) -> DynamicContainer<T> {
let id = Id::next();

let initial = create_updater(update_view, move |new_state| {
id.update_state(new_state, false)
});
let initial = create_updater(update_view, move |new_state| id.update_state(new_state));

let child_fn = Box::new(as_child_of_current_scope(child_fn));
let (child, child_scope) = child_fn(initial);
Expand Down
2 changes: 1 addition & 1 deletion src/views/dyn_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where
}
diff
};
id.update_state(diff, false);
id.update_state(diff);
HashRun(hashed_items)
});
let view_fn = Box::new(as_child_of_current_scope(view_fn));
Expand Down
2 changes: 1 addition & 1 deletion src/views/img.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn img(image: impl Fn() -> Vec<u8> + 'static) -> Img {
pub(crate) fn img_dynamic(image: impl Fn() -> Option<Rc<DynamicImage>> + 'static) -> Img {
let id = Id::next();
create_effect(move |_| {
id.update_state(image(), false);
id.update_state(image());
});
Img {
data: ViewData::new(id),
Expand Down
2 changes: 1 addition & 1 deletion src/views/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub fn label<S: Display + 'static>(label: impl Fn() -> S + 'static) -> Label {
let id = Id::next();
let initial_label = create_updater(
move || label().to_string(),
move |new_label| id.update_state(new_label, false),
move |new_label| id.update_state(new_label),
);
Label::new(id, initial_label)
}
Expand Down
14 changes: 7 additions & 7 deletions src/views/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ where
let selection = create_rw_signal(None);
create_effect(move |_| {
selection.track();
id.update_state(ListUpdate::SelectionChanged, false);
id.update_state(ListUpdate::SelectionChanged);
});
let stack = v_stack_from_iter(iterator.into_iter().enumerate().map(move |(index, v)| {
Item {
Expand Down Expand Up @@ -80,14 +80,14 @@ where
Key::Named(NamedKey::Home) => {
if length > 0 {
selection.set(Some(0));
id.update_state(ListUpdate::ScrollToSelected, false);
id.update_state(ListUpdate::ScrollToSelected);
}
EventPropagation::Stop
}
Key::Named(NamedKey::End) => {
if length > 0 {
selection.set(Some(length - 1));
id.update_state(ListUpdate::ScrollToSelected, false);
id.update_state(ListUpdate::ScrollToSelected);
}
EventPropagation::Stop
}
Expand All @@ -97,13 +97,13 @@ where
Some(i) => {
if i > 0 {
selection.set(Some(i - 1));
id.update_state(ListUpdate::ScrollToSelected, false);
id.update_state(ListUpdate::ScrollToSelected);
}
}
None => {
if length > 0 {
selection.set(Some(length - 1));
id.update_state(ListUpdate::ScrollToSelected, false);
id.update_state(ListUpdate::ScrollToSelected);
}
}
}
Expand All @@ -115,13 +115,13 @@ where
Some(i) => {
if i < length - 1 {
selection.set(Some(i + 1));
id.update_state(ListUpdate::ScrollToSelected, false);
id.update_state(ListUpdate::ScrollToSelected);
}
}
None => {
if length > 0 {
selection.set(Some(0));
id.update_state(ListUpdate::ScrollToSelected, false);
id.update_state(ListUpdate::ScrollToSelected);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/views/rich_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn rich_text(text_layout: impl Fn() -> TextLayout + 'static) -> RichText {
let text = text_layout();
create_effect(move |_| {
let new_text_layout = text_layout();
id.update_state(new_text_layout, false);
id.update_state(new_text_layout);
});
RichText {
data: ViewData::new(id),
Expand Down
36 changes: 18 additions & 18 deletions src/views/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,31 +127,31 @@ impl Scroll {
self
}

pub fn on_ensure_visible(self, to: impl Fn() -> Rect + 'static) -> Self {
pub fn ensure_visible(self, to: impl Fn() -> Rect + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
let rect = to();
id.update_state(ScrollState::EnsureVisible(rect), true);
id.update_state_deferred(ScrollState::EnsureVisible(rect));
});

self
}

pub fn on_scroll_delta(self, delta: impl Fn() -> Vec2 + 'static) -> Self {
pub fn scroll_delta(self, delta: impl Fn() -> Vec2 + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
let delta = delta();
id.update_state(ScrollState::ScrollDelta(delta), false);
id.update_state(ScrollState::ScrollDelta(delta));
});

self
}

pub fn on_scroll_to(self, origin: impl Fn() -> Option<Point> + 'static) -> Self {
pub fn scroll_to(self, origin: impl Fn() -> Option<Point> + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
if let Some(origin) = origin() {
id.update_state(ScrollState::ScrollTo(origin), true);
id.update_state_deferred(ScrollState::ScrollTo(origin));
}
});

Expand All @@ -163,7 +163,7 @@ impl Scroll {
let id = self.id();
create_effect(move |_| {
let percent = percent() / 100.;
id.update_state(ScrollState::ScrollToPercent(percent), true);
id.update_state_deferred(ScrollState::ScrollToPercent(percent));
});
self
}
Expand All @@ -172,7 +172,7 @@ impl Scroll {
let id = self.id();
create_effect(move |_| {
if let Some(view) = view() {
id.update_state(ScrollState::ScrollToView(view), true);
id.update_state_deferred(ScrollState::ScrollToView(view));
}
});

Expand All @@ -182,33 +182,33 @@ impl Scroll {
pub fn hide_bar(self, hide: impl Fn() -> bool + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
id.update_state(ScrollState::HiddenBar(hide()), false);
id.update_state(ScrollState::HiddenBar(hide()));
});
self
}

pub fn propagate_pointer_wheel(self, value: impl Fn() -> bool + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
id.update_state(ScrollState::PropagatePointerWheel(value()), false);
id.update_state(ScrollState::PropagatePointerWheel(value()));
});
self
}

pub fn vertical_scroll_as_horizontal(self, value: impl Fn() -> bool + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
id.update_state(ScrollState::VerticalScrollAsHorizontal(value()), false);
id.update_state(ScrollState::VerticalScrollAsHorizontal(value()));
});
self
}

fn scroll_delta(&mut self, app_state: &mut AppState, delta: Vec2) {
fn do_scroll_delta(&mut self, app_state: &mut AppState, delta: Vec2) {
let new_origin = self.child_viewport.origin() + delta;
self.clamp_child_viewport(app_state, self.child_viewport.with_origin(new_origin));
}

fn scroll_to(&mut self, app_state: &mut AppState, origin: Point) {
fn do_scroll_to(&mut self, app_state: &mut AppState, origin: Point) {
self.clamp_child_viewport(app_state, self.child_viewport.with_origin(origin));
}

Expand Down Expand Up @@ -487,15 +487,15 @@ impl Scroll {
- self.actual_rect.height() / 2.0;
let mut new_origin = self.child_viewport.origin();
new_origin.y = new_y;
self.scroll_to(app_state, new_origin);
self.do_scroll_to(app_state, new_origin);
}

fn click_horizontal_bar_area(&mut self, app_state: &mut AppState, pos: Point) {
let new_x = (pos.x / self.actual_rect.width()) * self.child_size.width
- self.actual_rect.width() / 2.0;
let mut new_origin = self.child_viewport.origin();
new_origin.x = new_x;
self.scroll_to(app_state, new_origin);
self.do_scroll_to(app_state, new_origin);
}

fn point_within_vertical_bar(&self, app_state: &mut AppState, pos: Point) -> bool {
Expand Down Expand Up @@ -655,16 +655,16 @@ impl View for Scroll {
self.pan_to_visible(cx.app_state, rect);
}
ScrollState::ScrollDelta(delta) => {
self.scroll_delta(cx.app_state, delta);
self.do_scroll_delta(cx.app_state, delta);
}
ScrollState::ScrollTo(origin) => {
self.scroll_to(cx.app_state, origin);
self.do_scroll_to(cx.app_state, origin);
}
ScrollState::ScrollToPercent(percent) => {
let mut child_size = self.child_size;
child_size *= percent as f64;
let point = child_size.to_vec2().to_point();
self.scroll_to(cx.app_state, point);
self.do_scroll_to(cx.app_state, point);
}
ScrollState::ScrollToView(id) => {
self.do_scroll_to_view(cx.app_state, id, None);
Expand Down
2 changes: 1 addition & 1 deletion src/views/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn svg(svg_str: impl Fn() -> String + 'static) -> Svg {
let id = Id::next();
create_effect(move |_| {
let new_svg_str = svg_str();
id.update_state(new_svg_str, false);
id.update_state(new_svg_str);
});
Svg {
data: ViewData::new(id),
Expand Down
4 changes: 2 additions & 2 deletions src/views/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ where
}
diff
};
id.update_state(TabState::Diff(Box::new(diff)), false);
id.update_state(TabState::Diff(Box::new(diff)));
HashRun(hashed_items)
});

create_effect(move |_| {
let active = active_fn();
id.update_state(TabState::Active::<T>(active), false);
id.update_state(TabState::Active::<T>(active));
});

let view_fn = Box::new(as_child_of_current_scope(view_fn));
Expand Down
2 changes: 1 addition & 1 deletion src/views/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub fn text_input(buffer: RwSignal<String>) -> TextInput {
{
create_effect(move |_| {
let text = buffer.get();
id.update_state((text, is_focused.get()), false);
id.update_state((text, is_focused.get()));
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/views/tooltip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl View for Tooltip {
let id = self.id();
let token =
exec_after(Duration::from_secs_f64(self.style.delay()), move |token| {
id.update_state(token, false);
id.update_state(token);
});
self.hover = Some((e.pos, token));
}
Expand Down
14 changes: 7 additions & 7 deletions src/views/virtual_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where
let offsets = create_rw_signal(Vec::new());
create_effect(move |_| {
selection.track();
id.update_state(ListUpdate::SelectionChanged, false);
id.update_state(ListUpdate::SelectionChanged);
});

let shared = Rc::new((each_fn, item_size));
Expand Down Expand Up @@ -152,15 +152,15 @@ where
Key::Named(NamedKey::Home) => {
if length.get_untracked() > 0 {
selection.set(Some(0));
id.update_state(ListUpdate::ScrollToSelected, false);
id.update_state(ListUpdate::ScrollToSelected);
}
EventPropagation::Stop
}
Key::Named(NamedKey::End) => {
let length = length.get_untracked();
if length > 0 {
selection.set(Some(length - 1));
id.update_state(ListUpdate::ScrollToSelected, false);
id.update_state(ListUpdate::ScrollToSelected);
}
EventPropagation::Stop
}
Expand All @@ -170,14 +170,14 @@ where
Some(i) => {
if i > 0 {
selection.set(Some(i - 1));
id.update_state(ListUpdate::ScrollToSelected, false);
id.update_state(ListUpdate::ScrollToSelected);
}
}
None => {
let length = length.get_untracked();
if length > 0 {
selection.set(Some(length - 1));
id.update_state(ListUpdate::ScrollToSelected, false);
id.update_state(ListUpdate::ScrollToSelected);
}
}
}
Expand All @@ -189,13 +189,13 @@ where
Some(i) => {
if i < length.get_untracked() - 1 {
selection.set(Some(i + 1));
id.update_state(ListUpdate::ScrollToSelected, false);
id.update_state(ListUpdate::ScrollToSelected);
}
}
None => {
if length.get_untracked() > 0 {
selection.set(Some(0));
id.update_state(ListUpdate::ScrollToSelected, false);
id.update_state(ListUpdate::ScrollToSelected);
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/views/virtual_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,11 @@ where

if !diff.is_empty() || prev_before_size != before_size || prev_content_size != content_size
{
id.update_state(
VirtualStackState {
diff,
before_size,
content_size,
},
false,
);
id.update_state(VirtualStackState {
diff,
before_size,
content_size,
});
}
(before_size, content_size, HashRun(hashed_items))
});
Expand Down
Loading

0 comments on commit ccfe8f2

Please sign in to comment.