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";
|
const LINE_ENDING: &str = "\n";
|
||||||
|
|
||||||
#[derive(Resource, Debug)]
|
#[derive(Resource, Debug)]
|
||||||
struct State {
|
struct InputText {
|
||||||
input_value: String,
|
value: String,
|
||||||
output_value: String,
|
}
|
||||||
needs_update: bool,
|
|
||||||
|
#[derive(Resource, Debug)]
|
||||||
|
struct OutputText {
|
||||||
|
value: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Resource, Debug)]
|
||||||
|
struct NeedsSort {
|
||||||
|
value: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.insert_resource(WinitSettings::desktop_app())
|
.insert_resource(WinitSettings::desktop_app())
|
||||||
.insert_resource(State {
|
.insert_resource(InputText {
|
||||||
input_value: "".to_string(),
|
value: "".to_string(),
|
||||||
output_value: "".to_string(),
|
|
||||||
needs_update: false,
|
|
||||||
})
|
})
|
||||||
|
.insert_resource(OutputText {
|
||||||
|
value: "".to_string(),
|
||||||
|
})
|
||||||
|
.insert_resource(NeedsSort { value: false })
|
||||||
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
||||||
primary_window: Some(Window {
|
primary_window: Some(Window {
|
||||||
title: "dsort".to_string(),
|
title: "dsort".to_string(),
|
||||||
|
@ -38,17 +48,26 @@ fn main() {
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_text(mut state: ResMut<State>) {
|
fn process_text(
|
||||||
if state.needs_update {
|
mut output_text: ResMut<OutputText>,
|
||||||
let mut arr = state.input_value.lines().collect::<Vec<_>>();
|
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.retain(|&x| !x.is_empty());
|
||||||
arr.sort_by(|&a, &b| vsort::compare(a, b));
|
arr.sort_by(|&a, &b| vsort::compare(a, b));
|
||||||
state.output_value = arr.join(LINE_ENDING);
|
output_text.value = arr.join(LINE_ENDING);
|
||||||
state.needs_update = false;
|
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());
|
contexts.ctx_mut().set_visuals(egui::Visuals::light());
|
||||||
|
|
||||||
egui::CentralPanel::default().show(contexts.ctx_mut(), |ui| {
|
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| {
|
ui.with_layout(egui::Layout::left_to_right(egui::Align::LEFT), |ui| {
|
||||||
let sort_button = ui.button("Sort & Remove Blanks");
|
let sort_button = ui.button("Sort & Remove Blanks");
|
||||||
if sort_button.clicked() {
|
if sort_button.clicked() {
|
||||||
state.needs_update = true;
|
needs_sort.value = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
let copy_button = ui.button("Copy");
|
let copy_button = ui.button("Copy");
|
||||||
if copy_button.clicked() {
|
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");
|
let clear_button = ui.button("Clear");
|
||||||
if clear_button.clicked() {
|
if clear_button.clicked() {
|
||||||
state.input_value = "".to_string();
|
input_text.value = "".to_string();
|
||||||
state.needs_update = true;
|
needs_sort.value = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -82,7 +101,7 @@ fn ui_system(mut contexts: EguiContexts, mut state: ResMut<State>) {
|
||||||
.show(ui, |ui| {
|
.show(ui, |ui| {
|
||||||
ui.add_sized(
|
ui.add_sized(
|
||||||
[ui.available_width() / 2., height],
|
[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(
|
ui.with_layout(
|
||||||
egui::Layout::top_down_justified(egui::Align::LEFT),
|
egui::Layout::top_down_justified(egui::Align::LEFT),
|
||||||
|ui| {
|
|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