changed my mind, back to split-up resources
This commit is contained in:
parent
ebb41089ea
commit
16e5802cba
1 changed files with 39 additions and 20 deletions
59
src/main.rs
59
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<State>) {
|
||||
if state.needs_update {
|
||||
let mut arr = state.input_value.lines().collect::<Vec<_>>();
|
||||
fn process_text(
|
||||
mut output_text: ResMut<OutputText>,
|
||||
mut needs_sort: ResMut<NeedsSort>,
|
||||
input_text: Res<InputText>,
|
||||
) {
|
||||
if needs_sort.value {
|
||||
let mut arr = input_text.value.lines().collect::<Vec<_>>();
|
||||
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<State>) {
|
||||
fn ui_system(
|
||||
mut contexts: EguiContexts,
|
||||
mut input_text: ResMut<InputText>,
|
||||
mut needs_sort: ResMut<NeedsSort>,
|
||||
output_text: Res<OutputText>,
|
||||
) {
|
||||
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<State>) {
|
|||
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<State>) {
|
|||
.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<State>) {
|
|||
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));
|
||||
},
|
||||
)
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue