-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement mutate pass from pass spec RFC (#510)
This is part of the "Pass Specification" RFC: linebender/rfcs#7 Rename WidgetCtx to MutateCtx. Add a mutate pass. Add a `mutate_later` context method to trigger that pass. Refactor `edit_root_widget` to use a version of that pass. Add a separate constructor for the synthetic WidgetState created in RenderRoot.
- Loading branch information
1 parent
0c80c0f
commit dc2a433
Showing
14 changed files
with
280 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2024 the Xilem Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use tracing::info_span; | ||
|
||
use crate::passes::merge_state_up; | ||
use crate::render_root::RenderRoot; | ||
use crate::widget::WidgetMut; | ||
use crate::{MutateCtx, Widget, WidgetId, WidgetState}; | ||
|
||
pub(crate) fn mutate_widget( | ||
root: &mut RenderRoot, | ||
id: WidgetId, | ||
mutate_fn: impl FnOnce(WidgetMut<'_, Box<dyn Widget>>), | ||
) { | ||
let mut dummy_state = WidgetState::root(root.root.id(), root.get_kurbo_size()); | ||
let (widget_mut, state_mut) = root.widget_arena.get_pair_mut(id); | ||
|
||
let _span = info_span!("mutate_widget").entered(); | ||
let root_widget = WidgetMut { | ||
ctx: MutateCtx { | ||
global_state: &mut root.state, | ||
// FIXME - This works in practice, because the loop below will merge the | ||
// state up to the root. But it's semantically awkward. | ||
// parent_widget_state should probably be an Option. | ||
parent_widget_state: &mut dummy_state, | ||
widget_state: state_mut.item, | ||
widget_state_children: state_mut.children, | ||
widget_children: widget_mut.children, | ||
}, | ||
widget: widget_mut.item, | ||
is_reborrow: false, | ||
}; | ||
|
||
mutate_fn(root_widget); | ||
|
||
let mut current_id = Some(id); | ||
while let Some(id) = current_id { | ||
let parent_id = root.widget_arena.parent_of(id); | ||
merge_state_up(&mut root.widget_arena, id); | ||
current_id = parent_id; | ||
} | ||
} | ||
|
||
pub(crate) fn run_mutate_pass(root: &mut RenderRoot, root_state: &mut WidgetState) { | ||
// TODO - Factor out into a "pre-event" function? | ||
// root.state.next_focused_widget = root.state.focused_widget; | ||
|
||
let callbacks = std::mem::take(&mut root.state.mutate_callbacks); | ||
for callback in callbacks { | ||
mutate_widget(root, callback.id, callback.callback); | ||
} | ||
|
||
root_state.merge_up(root.widget_arena.get_state_mut(root.root.id()).item); | ||
// root.post_event_processing(&mut root_state); | ||
} |
Oops, something went wrong.