From 16e5802cbac34603a83f2f5a39b9bb8e248af8a1 Mon Sep 17 00:00:00 2001 From: Matthew Ryan Dillon Date: Sat, 24 Aug 2024 16:58:40 -0400 Subject: [PATCH] changed my mind, back to split-up resources --- src/main.rs | 59 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3b59106..a475f8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,20 +11,30 @@ const LINE_ENDING: &str = "\r\n"; const LINE_ENDING: &str = "\n"; #[derive(Resource, Debug)] -struct State { - input_value: String, - output_value: String, - needs_update: bool, +struct InputText { + value: String, +} + +#[derive(Resource, Debug)] +struct OutputText { + value: String, +} + +#[derive(Resource, Debug)] +struct NeedsSort { + value: bool, } fn main() { App::new() .insert_resource(WinitSettings::desktop_app()) - .insert_resource(State { - input_value: "".to_string(), - output_value: "".to_string(), - needs_update: false, + .insert_resource(InputText { + value: "".to_string(), }) + .insert_resource(OutputText { + value: "".to_string(), + }) + .insert_resource(NeedsSort { value: false }) .add_plugins(DefaultPlugins.set(WindowPlugin { primary_window: Some(Window { title: "dsort".to_string(), @@ -38,17 +48,26 @@ fn main() { .run(); } -fn process_text(mut state: ResMut) { - if state.needs_update { - let mut arr = state.input_value.lines().collect::>(); +fn process_text( + mut output_text: ResMut, + mut needs_sort: ResMut, + input_text: Res, +) { + if needs_sort.value { + let mut arr = input_text.value.lines().collect::>(); arr.retain(|&x| !x.is_empty()); arr.sort_by(|&a, &b| vsort::compare(a, b)); - state.output_value = arr.join(LINE_ENDING); - state.needs_update = false; + output_text.value = arr.join(LINE_ENDING); + needs_sort.value = false; } } -fn ui_system(mut contexts: EguiContexts, mut state: ResMut) { +fn ui_system( + mut contexts: EguiContexts, + mut input_text: ResMut, + mut needs_sort: ResMut, + output_text: Res, +) { contexts.ctx_mut().set_visuals(egui::Visuals::light()); egui::CentralPanel::default().show(contexts.ctx_mut(), |ui| { @@ -59,18 +78,18 @@ fn ui_system(mut contexts: EguiContexts, mut state: ResMut) { ui.with_layout(egui::Layout::left_to_right(egui::Align::LEFT), |ui| { let sort_button = ui.button("Sort & Remove Blanks"); if sort_button.clicked() { - state.needs_update = true; + needs_sort.value = true; } let copy_button = ui.button("Copy"); if copy_button.clicked() { - ui.output_mut(|o| o.copied_text = state.output_value.clone()); + ui.output_mut(|o| o.copied_text = output_text.value.clone()); } let clear_button = ui.button("Clear"); if clear_button.clicked() { - state.input_value = "".to_string(); - state.needs_update = true; + input_text.value = "".to_string(); + needs_sort.value = true; } }); @@ -82,7 +101,7 @@ fn ui_system(mut contexts: EguiContexts, mut state: ResMut) { .show(ui, |ui| { ui.add_sized( [ui.available_width() / 2., height], - egui::TextEdit::multiline(&mut state.input_value), + egui::TextEdit::multiline(&mut input_text.value), ); }); @@ -92,7 +111,7 @@ fn ui_system(mut contexts: EguiContexts, mut state: ResMut) { ui.with_layout( egui::Layout::top_down_justified(egui::Align::LEFT), |ui| { - ui.add(egui::Label::new(&state.output_value).selectable(true)); + ui.add(egui::Label::new(&output_text.value).selectable(true)); }, ) });