Skip to content

Commit 10d98d2

Browse files
committed
support automatic reset of expanded arrays/objects when DefaultExpand setting changes
1 parent 22e5d5f commit 10d98d2

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/default_expand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::HashSet;
22

33
use egui::Id;
44

5-
#[derive(Debug, Clone, Copy, Default)]
5+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
66
/// Configuration for how a [`JsonTree`](crate::JsonTree) should expand arrays and objects by default.
77
pub enum DefaultExpand<'a> {
88
/// Expand all arrays and objects.
@@ -25,7 +25,7 @@ pub enum DefaultExpand<'a> {
2525
SearchResultsOrAll(&'a str),
2626
}
2727

28-
#[derive(Debug, Clone)]
28+
#[derive(Debug)]
2929
/// Internal representation for the [`DefaultExpand`] setting.
3030
pub(crate) enum InnerDefaultExpand {
3131
All,

src/node.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,30 @@ impl<'a, 'b, T: ToJsonTreeValue> JsonTreeNode<'a, 'b, T> {
8585
node.show_impl(ui, &mut vec![], &mut reset_path_ids, &mut renderer);
8686
});
8787

88-
JsonTreeResponse {
88+
let should_reset_expanded = if tree.config.auto_reset_expanded {
89+
let default_expand_hash_id = ResetExpandedHashId(egui::util::hash(default_expand));
90+
let default_expand_changed = ui.ctx().data_mut(|d| {
91+
let default_expand_changed =
92+
d.get_temp::<ResetExpandedHashId>(tree.id) != Some(default_expand_hash_id);
93+
if default_expand_changed {
94+
d.insert_temp(tree.id, default_expand_hash_id);
95+
}
96+
default_expand_changed
97+
});
98+
default_expand_changed
99+
} else {
100+
false
101+
};
102+
103+
let response = JsonTreeResponse {
89104
collapsing_state_ids: reset_path_ids,
105+
};
106+
107+
if should_reset_expanded {
108+
response.reset_expanded(ui);
90109
}
110+
111+
response
91112
}
92113

93114
fn show_impl(
@@ -458,3 +479,8 @@ struct JsonTreeNodeConfig {
458479
style: JsonTreeStyle,
459480
search_term: Option<SearchTerm>,
460481
}
482+
483+
/// Stored in `egui`'s `IdTypeMap` to represent a hashed value to indicate whether to reset expanded arrays/objects when this changes for a particular tree Id.
484+
/// Avoids potential conflicts in case a `u64` happened to be stored against the same tree Id.
485+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
486+
struct ResetExpandedHashId(u64);

src/tree.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::hash::Hash;
1010
pub(crate) struct JsonTreeConfig<'a, T: ToJsonTreeValue> {
1111
pub(crate) style: Option<JsonTreeStyle>,
1212
pub(crate) default_expand: Option<DefaultExpand<'a>>,
13+
pub(crate) auto_reset_expanded: bool,
1314
pub(crate) renderer: JsonTreeRenderer<'a, T>,
1415
}
1516

@@ -18,6 +19,7 @@ impl<T: ToJsonTreeValue> Default for JsonTreeConfig<'_, T> {
1819
Self {
1920
style: Default::default(),
2021
default_expand: Default::default(),
22+
auto_reset_expanded: false,
2123
renderer: Default::default(),
2224
}
2325
}
@@ -54,6 +56,12 @@ impl<'a, T: ToJsonTreeValue> JsonTree<'a, T> {
5456
self
5557
}
5658

59+
/// Automatically reset expanded arrays/objects to respect the [`DefaultExpand`] setting when it changes.
60+
pub fn auto_reset_expanded(mut self, auto_reset_expanded: bool) -> Self {
61+
self.config.auto_reset_expanded = auto_reset_expanded;
62+
self
63+
}
64+
5765
/// A convenience method for conditionally registering a custom rendering hook.
5866
/// See [`JsonTree::on_render`].
5967
pub fn on_render_if(

0 commit comments

Comments
 (0)