Skip to content

Commit 4215043

Browse files
authored
Merge pull request #47 from dmackdev/auto-reset-expanded
Support automatic reset of expanded arrays/objects when `DefaultExpand` setting changes
2 parents 90fd190 + 3fa358e commit 4215043

File tree

10 files changed

+239
-105
lines changed

10 files changed

+239
-105
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
use egui::{Slider, Ui};
2+
use egui_json_tree::{DefaultExpand, JsonTree};
3+
use serde_json::Value;
4+
5+
use super::Show;
6+
7+
#[derive(Default)]
8+
enum StateDefaultExpand {
9+
All,
10+
#[default]
11+
None,
12+
ToLevel(u8),
13+
SearchResults(String),
14+
SearchResultsOrAll(String),
15+
}
16+
17+
impl<'a> From<&'a StateDefaultExpand> for DefaultExpand<'a> {
18+
fn from(value: &'a StateDefaultExpand) -> Self {
19+
match value {
20+
StateDefaultExpand::All => DefaultExpand::All,
21+
StateDefaultExpand::None => DefaultExpand::None,
22+
StateDefaultExpand::ToLevel(l) => DefaultExpand::ToLevel(*l),
23+
StateDefaultExpand::SearchResults(search_term) => {
24+
DefaultExpand::SearchResults(search_term)
25+
}
26+
StateDefaultExpand::SearchResultsOrAll(search_term) => {
27+
DefaultExpand::SearchResultsOrAll(search_term)
28+
}
29+
}
30+
}
31+
}
32+
33+
pub struct DefaultExpandExample {
34+
value: Value,
35+
state_default_expand: StateDefaultExpand,
36+
}
37+
38+
impl DefaultExpandExample {
39+
pub fn new(value: Value) -> Self {
40+
Self {
41+
value,
42+
state_default_expand: Default::default(),
43+
}
44+
}
45+
}
46+
47+
impl Show for DefaultExpandExample {
48+
fn title(&self) -> &'static str {
49+
"Default Expand settings"
50+
}
51+
52+
fn show(&mut self, ui: &mut Ui) {
53+
ui.hyperlink_to("Source", "https://github.com/dmackdev/egui_json_tree/blob/master/examples/demo/src/apps/default_expand.rs");
54+
ui.label("A showcase of the different options to configure how the tree expands arrays and objects by default.");
55+
ui.add_space(10.0);
56+
57+
if ui
58+
.radio(
59+
matches!(self.state_default_expand, StateDefaultExpand::None),
60+
"None",
61+
)
62+
.clicked()
63+
{
64+
self.state_default_expand = StateDefaultExpand::None;
65+
}
66+
if ui
67+
.radio(
68+
matches!(self.state_default_expand, StateDefaultExpand::All),
69+
"All",
70+
)
71+
.clicked()
72+
{
73+
self.state_default_expand = StateDefaultExpand::All;
74+
}
75+
if ui
76+
.radio(
77+
matches!(self.state_default_expand, StateDefaultExpand::ToLevel(_)),
78+
"To level",
79+
)
80+
.clicked()
81+
{
82+
self.state_default_expand = StateDefaultExpand::ToLevel(0);
83+
}
84+
if ui
85+
.radio(
86+
matches!(
87+
self.state_default_expand,
88+
StateDefaultExpand::SearchResults(_)
89+
),
90+
"Search results",
91+
)
92+
.clicked()
93+
{
94+
self.state_default_expand = StateDefaultExpand::SearchResults("".to_string());
95+
}
96+
if ui
97+
.radio(
98+
matches!(
99+
self.state_default_expand,
100+
StateDefaultExpand::SearchResultsOrAll(_)
101+
),
102+
"Search results or all",
103+
)
104+
.clicked()
105+
{
106+
self.state_default_expand = StateDefaultExpand::SearchResultsOrAll("".to_string());
107+
}
108+
109+
match &mut self.state_default_expand {
110+
StateDefaultExpand::All => {}
111+
StateDefaultExpand::None => {}
112+
StateDefaultExpand::ToLevel(level) => {
113+
ui.add(Slider::new(level, 0..=4));
114+
}
115+
StateDefaultExpand::SearchResults(search_term)
116+
| StateDefaultExpand::SearchResultsOrAll(search_term) => {
117+
ui.label("Search:");
118+
ui.horizontal(|ui| {
119+
ui.text_edit_singleline(search_term);
120+
if ui.button("Clear").clicked() {
121+
search_term.clear();
122+
}
123+
});
124+
}
125+
};
126+
127+
let response = JsonTree::new(self.title(), &self.value)
128+
.default_expand((&self.state_default_expand).into())
129+
.show(ui);
130+
131+
if ui.button("Reset expanded").clicked() {
132+
response.reset_expanded(ui);
133+
}
134+
}
135+
}

examples/demo/src/apps/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use serde_json::Value;
44

55
pub mod copy_to_clipboard;
66
pub mod custom_input;
7+
pub mod default_expand;
78
pub mod editor;
89
pub mod search;
910
pub mod toggle_buttons;

examples/demo/src/apps/search.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,17 @@ impl Show for SearchExample {
2929
ui.add_space(10.0);
3030

3131
ui.label("Search:");
32-
let (text_edit_response, clear_button_response) = ui
33-
.horizontal(|ui| {
34-
let text_edit_response = ui.text_edit_singleline(&mut self.search_input);
35-
let clear_button_response = ui.button("Clear");
36-
(text_edit_response, clear_button_response)
37-
})
38-
.inner;
32+
ui.horizontal(|ui| {
33+
ui.text_edit_singleline(&mut self.search_input);
34+
if ui.button("Clear").clicked() {
35+
self.search_input.clear();
36+
}
37+
});
3938

4039
let response = JsonTree::new(self.title(), &self.value)
4140
.default_expand(DefaultExpand::SearchResults(&self.search_input))
4241
.show(ui);
4342

44-
if text_edit_response.changed() {
45-
response.reset_expanded(ui);
46-
}
47-
48-
if clear_button_response.clicked() {
49-
self.search_input.clear();
50-
response.reset_expanded(ui);
51-
}
52-
5343
if ui.button("Reset expanded").clicked() {
5444
response.reset_expanded(ui);
5545
}

examples/demo/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use apps::{
55
};
66
use serde_json::json;
77

8+
use crate::apps::default_expand::DefaultExpandExample;
9+
810
mod apps;
911

1012
struct DemoApp {
@@ -15,7 +17,7 @@ struct DemoApp {
1517

1618
impl Default for DemoApp {
1719
fn default() -> Self {
18-
let complex_object = json!({"foo": [1, 2, [3]], "bar": { "qux" : false, "thud": { "a/b": [4, 5, { "m~n": "Greetings!" }]}, "grep": 21}, "baz": null});
20+
let complex_object = json!({"foo": [1, 2, ["grep"]], "bar": { "qux" : false, "thud": { "a/b": [4, 5, { "m~n": "Greetings!" }]}, "grep": 21}, "baz": null});
1921
let long_strings_object = json!({
2022
"baz": "Ullamco ipsum proident occaecat eiusmod ea aute ex non cupidatat laboris duis amet cupidatat. Ullamco sint do enim consectetur Lorem occaecat mollit. Aliquip voluptate ullamco consectetur adipisicing elit fugiat labore laboris. Occaecat non incididunt duis consectetur aliquip dolore cillum eiusmod. Qui sunt est excepteur laborum.",
2123
"bar": [
@@ -53,6 +55,7 @@ impl Default for DemoApp {
5355
Box::new(SearchExample::new(complex_object.clone())),
5456
Box::new(CopyToClipboardExample::new(complex_object.clone())),
5557
Box::new(JsonEditorExample::new(complex_object.clone())),
58+
Box::new(DefaultExpandExample::new(complex_object.clone())),
5659
Box::new(ToggleButtonsCustomisationDemo::new(complex_object)),
5760
Box::new(WrappingExample::new(long_strings_object)),
5861
],

src/default_expand.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
#[derive(Debug, Clone, Copy, Default)]
1+
use std::collections::HashSet;
2+
3+
use egui::Id;
4+
5+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
26
/// Configuration for how a [`JsonTree`](crate::JsonTree) should expand arrays and objects by default.
37
pub enum DefaultExpand<'a> {
48
/// Expand all arrays and objects.
@@ -20,3 +24,13 @@ pub enum DefaultExpand<'a> {
2024
/// Similar to `SearchResults`, but expands all arrays and objects if the search term is empty.
2125
SearchResultsOrAll(&'a str),
2226
}
27+
28+
#[derive(Debug)]
29+
/// Internal representation for the [`DefaultExpand`] setting.
30+
pub(crate) enum InnerDefaultExpand {
31+
All,
32+
None,
33+
ToLevel(u8),
34+
/// Specifies which arrays/objects should be expanded by default, based on its hashed JSON pointer.
35+
Paths(HashSet<Id>),
36+
}

0 commit comments

Comments
 (0)