Skip to content

Commit

Permalink
Implement update_anim pass
Browse files Browse the repository at this point in the history
  • Loading branch information
PoignardAzur committed Aug 24, 2024
1 parent c77c6ec commit 89e2a17
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 15 deletions.
1 change: 1 addition & 0 deletions masonry/src/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ impl_context_method!(MutateCtx<'_>, EventCtx<'_>, LifeCycleCtx<'_>, {
/// Request an animation frame.
pub fn request_anim_frame(&mut self) {
trace!("request_anim_frame");
self.widget_state.needs_anim = true;
self.widget_state.request_anim = true;
}

Expand Down
58 changes: 54 additions & 4 deletions masonry/src/passes/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
use std::collections::HashSet;

use cursor_icon::CursorIcon;
use tracing::trace;
use tracing::{info_span, trace};

use crate::passes::merge_state_up;
use crate::render_root::{RenderRoot, RenderRootSignal};
use crate::{LifeCycleCtx, StatusChange, Widget, WidgetId, WidgetState};
use crate::passes::{merge_state_up, recurse_on_children};
use crate::render_root::{RenderRoot, RenderRootSignal, RenderRootState};
use crate::tree_arena::ArenaMut;
use crate::{LifeCycle, LifeCycleCtx, StatusChange, Widget, WidgetId, WidgetState};

fn get_id_path(root: &RenderRoot, widget_id: Option<WidgetId>) -> Vec<WidgetId> {
let Some(widget_id) = widget_id else {
Expand Down Expand Up @@ -145,3 +146,52 @@ pub(crate) fn run_update_pointer_pass(root: &mut RenderRoot, root_state: &mut Wi
// Pass root widget state to synthetic state create at beginning of pass
root_state.merge_up(root.widget_arena.get_state_mut(root.root.id()).item);
}

// ----------------

fn update_anim_for_widget(
global_state: &mut RenderRootState,
mut widget: ArenaMut<'_, Box<dyn Widget>>,
mut state: ArenaMut<'_, WidgetState>,
elapsed_ns: u64,
) {
let _span = widget.item.make_trace_span().entered();

if !state.item.needs_anim {
return;
}

if state.item.request_anim {
let mut ctx = LifeCycleCtx {
global_state,
widget_state: state.item,
widget_state_children: state.children.reborrow_mut(),
widget_children: widget.children.reborrow_mut(),
};
widget
.item
.lifecycle(&mut ctx, &LifeCycle::AnimFrame(elapsed_ns));
}

state.item.needs_anim = false;
state.item.request_anim = false;

let id = state.item.id;
let parent_state = state.item;
recurse_on_children(
id,
widget.reborrow_mut(),
state.children,
|widget, mut state| {
update_anim_for_widget(global_state, widget, state.reborrow_mut(), elapsed_ns);
parent_state.merge_up(state.item);
},
);
}

pub(crate) fn run_update_anim_pass(root: &mut RenderRoot, elapsed_ns: u64) {
let _span = info_span!("update_anim").entered();

let (root_widget, root_state) = root.widget_arena.get_pair_mut(root.root.id());
update_anim_for_widget(&mut root.state, root_widget, root_state, elapsed_ns);
}
12 changes: 8 additions & 4 deletions masonry/src/render_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::passes::compose::root_compose;
use crate::passes::event::{root_on_access_event, root_on_pointer_event, root_on_text_event};
use crate::passes::mutate::{mutate_widget, run_mutate_pass};
use crate::passes::paint::root_paint;
use crate::passes::update::run_update_pointer_pass;
use crate::passes::update::{run_update_anim_pass, run_update_pointer_pass};
use crate::text::TextBrush;
use crate::tree_arena::TreeArena;
use crate::widget::WidgetArena;
Expand Down Expand Up @@ -203,9 +203,13 @@ impl RenderRoot {
let last = self.last_anim.take();
let elapsed_ns = last.map(|t| now.duration_since(t).as_nanos()).unwrap_or(0) as u64;
let root_state = self.root_state();
if root_state.request_anim {
root_state.request_anim = false;
self.root_lifecycle(LifeCycle::AnimFrame(elapsed_ns));
if root_state.needs_anim {
run_update_anim_pass(self, elapsed_ns);

let mut root_state =
self.widget_arena.get_state_mut(self.root.id()).item.clone();
self.post_event_processing(&mut root_state);

self.last_anim = Some(now);
}
Handled::Yes
Expand Down
6 changes: 2 additions & 4 deletions masonry/src/widget/widget_pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,8 @@ impl<W: Widget> WidgetPod<W> {

true
}
LifeCycle::AnimFrame(_) => {
state.request_anim = false;
true
}
// Animations have been moved to the update_anim pass
LifeCycle::AnimFrame(_) => false,
LifeCycle::DisabledChanged(ancestors_disabled) => {
state.update_focus_chain = true;

Expand Down
10 changes: 7 additions & 3 deletions masonry/src/widget/widget_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ pub struct WidgetState {
/// The accessibility method must be called on this widget or a descendant
pub(crate) needs_accessibility: bool,

/// Any descendant has requested an animation frame.
/// An animation must run on this widget
pub(crate) request_anim: bool,
/// An animation must run on this widget or a descendant
pub(crate) needs_anim: bool,

pub(crate) update_focus_chain: bool,

Expand Down Expand Up @@ -183,6 +185,7 @@ impl WidgetState {
request_accessibility: true,
needs_accessibility: true,
has_focus: false,
needs_anim: true,
request_anim: true,
focus_chain: Vec::new(),
children: Bloom::new(),
Expand All @@ -208,11 +211,12 @@ impl WidgetState {
needs_layout: false,
request_compose: false,
needs_compose: false,
needs_paint: false,
request_paint: false,
needs_paint: false,
request_accessibility: false,
needs_accessibility: false,
request_anim: false,
needs_anim: false,
children_changed: false,
update_focus_chain: false,
..WidgetState::new(id, "<root>")
Expand Down Expand Up @@ -250,7 +254,7 @@ impl WidgetState {
self.needs_layout |= child_state.needs_layout;
self.needs_compose |= child_state.needs_compose;
self.needs_paint |= child_state.needs_paint;
self.request_anim |= child_state.request_anim;
self.needs_anim |= child_state.needs_anim;
self.needs_accessibility |= child_state.needs_accessibility;
self.children_disabled_changed |= child_state.children_disabled_changed;
self.children_disabled_changed |=
Expand Down

0 comments on commit 89e2a17

Please sign in to comment.