103 lines
3.4 KiB
Rust
103 lines
3.4 KiB
Rust
#![windows_subsystem = "windows"]
|
|
use bevy::{prelude::*, winit::WinitSettings};
|
|
use bevy_egui::{
|
|
egui::{self},
|
|
EguiContexts, EguiPlugin,
|
|
};
|
|
|
|
#[cfg(windows)]
|
|
const LINE_ENDING: &str = "\r\n";
|
|
#[cfg(not(windows))]
|
|
const LINE_ENDING: &str = "\n";
|
|
|
|
#[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(State {
|
|
input_value: "".to_string(),
|
|
output_value: "".to_string(),
|
|
needs_update: false,
|
|
})
|
|
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
|
primary_window: Some(Window {
|
|
title: "dsort".to_string(),
|
|
..Default::default()
|
|
}),
|
|
..Default::default()
|
|
}))
|
|
.add_plugins(EguiPlugin)
|
|
.add_systems(Update, ui_system)
|
|
.add_systems(Update, process_text)
|
|
.run();
|
|
}
|
|
|
|
fn process_text(mut state: ResMut<State>) {
|
|
if state.needs_update {
|
|
let mut arr = state.input_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;
|
|
}
|
|
}
|
|
|
|
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::top_down(egui::Align::Center), |ui| {
|
|
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;
|
|
}
|
|
|
|
let copy_button = ui.button("Copy");
|
|
if copy_button.clicked() {
|
|
ui.output_mut(|o| o.copied_text = state.output_value.clone());
|
|
}
|
|
|
|
let clear_button = ui.button("Clear");
|
|
if clear_button.clicked() {
|
|
state.input_value = "".to_string();
|
|
state.needs_update = true;
|
|
}
|
|
});
|
|
|
|
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(
|
|
[ui.available_width() / 2., height],
|
|
egui::TextEdit::multiline(&mut state.input_value),
|
|
);
|
|
});
|
|
|
|
egui::ScrollArea::vertical()
|
|
.id_source("right")
|
|
.show(ui, |ui| {
|
|
ui.with_layout(
|
|
egui::Layout::top_down_justified(egui::Align::LEFT),
|
|
|ui| {
|
|
ui.add(egui::Label::new(&state.output_value).selectable(true));
|
|
},
|
|
)
|
|
});
|
|
},
|
|
);
|
|
});
|
|
});
|
|
}
|