singular state object && label
This commit is contained in:
parent
4df863db06
commit
bdf0cb5caa
1 changed files with 29 additions and 28 deletions
57
src/main.rs
57
src/main.rs
|
@ -10,24 +10,20 @@ const LINE_ENDING: &str = "\r\n";
|
|||
#[cfg(not(windows))]
|
||||
const LINE_ENDING: &str = "\n";
|
||||
|
||||
#[derive(Resource)]
|
||||
struct InputText {
|
||||
value: String,
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
struct OutputText {
|
||||
value: String,
|
||||
#[derive(Resource, Debug)]
|
||||
struct State {
|
||||
input_value: String,
|
||||
output_value: String,
|
||||
needs_update: bool,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.insert_resource(WinitSettings::desktop_app())
|
||||
.insert_resource(InputText {
|
||||
value: "".to_string(),
|
||||
})
|
||||
.insert_resource(OutputText {
|
||||
value: "".to_string(),
|
||||
.insert_resource(State {
|
||||
input_value: "".to_string(),
|
||||
output_value: "".to_string(),
|
||||
needs_update: false,
|
||||
})
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_plugins(EguiPlugin)
|
||||
|
@ -37,46 +33,51 @@ fn main() {
|
|||
.run();
|
||||
}
|
||||
|
||||
// TODO: move this into init
|
||||
fn set_window_title(mut window_query: Query<&mut Window, With<PrimaryWindow>>) {
|
||||
if let Ok(mut window) = window_query.get_single_mut() {
|
||||
window.title = "dsort".to_string();
|
||||
}
|
||||
}
|
||||
|
||||
fn process_text(input_text: Res<InputText>, mut output_text: ResMut<OutputText>) {
|
||||
let mut arr = input_text.value.lines().collect::<Vec<_>>();
|
||||
arr.sort_by(|&a, &b| vsort::compare(a, b));
|
||||
output_text.value = arr.join(LINE_ENDING);
|
||||
fn process_text(mut state: ResMut<State>) {
|
||||
if state.needs_update {
|
||||
println!("updating sort");
|
||||
let mut arr = state.input_value.lines().collect::<Vec<_>>();
|
||||
arr.sort_by(|&a, &b| vsort::compare(a, b));
|
||||
state.output_value = arr.join(LINE_ENDING);
|
||||
state.needs_update = false;
|
||||
}
|
||||
}
|
||||
|
||||
fn ui_system(
|
||||
mut contexts: EguiContexts,
|
||||
mut input_text: ResMut<InputText>,
|
||||
mut output_text: ResMut<OutputText>,
|
||||
) {
|
||||
fn ui_system(mut contexts: EguiContexts, mut state: ResMut<State>) {
|
||||
contexts.ctx_mut().set_visuals(egui::Visuals::light());
|
||||
|
||||
egui::CentralPanel::default().show(contexts.ctx_mut(), |ui| {
|
||||
let window_size = ui.available_size();
|
||||
let height = window_size[1];
|
||||
|
||||
ui.with_layout(
|
||||
egui::Layout::left_to_right(egui::Align::Center).with_cross_justify(true),
|
||||
|ui| {
|
||||
egui::ScrollArea::vertical()
|
||||
.id_source("left")
|
||||
.show(ui, |ui| {
|
||||
ui.add_sized(
|
||||
let response = ui.add_sized(
|
||||
[ui.available_width() / 2., height],
|
||||
egui::TextEdit::multiline(&mut input_text.value),
|
||||
egui::TextEdit::multiline(&mut state.input_value),
|
||||
);
|
||||
if response.changed() {
|
||||
state.needs_update = true;
|
||||
}
|
||||
});
|
||||
|
||||
egui::ScrollArea::vertical()
|
||||
.id_source("right")
|
||||
.show(ui, |ui| {
|
||||
ui.add_sized(
|
||||
[ui.available_width(), height],
|
||||
egui::TextEdit::multiline(&mut output_text.value),
|
||||
);
|
||||
ui.with_layout(egui::Layout::top_down_justified(egui::Align::LEFT), |ui| {
|
||||
ui.add(egui::Label::new(&state.output_value).selectable(true));
|
||||
})
|
||||
});
|
||||
},
|
||||
);
|
||||
|
|
Loading…
Add table
Reference in a new issue