diff options
author | metamuffin <yvchraiqi@protonmail.com> | 2022-08-17 07:45:49 +0200 |
---|---|---|
committer | metamuffin <yvchraiqi@protonmail.com> | 2022-08-17 07:45:49 +0200 |
commit | aca12bc6d0194364d9e36c455fbcd2390e00d8be (patch) | |
tree | 5d449cfb50fe4ec4537a96e0c76f035f2f6961e7 | |
parent | 32bfbe158ab695c9258c13e6f32a619b780d8930 (diff) | |
download | karlender-aca12bc6d0194364d9e36c455fbcd2390e00d8be.tar karlender-aca12bc6d0194364d9e36c455fbcd2390e00d8be.tar.bz2 karlender-aca12bc6d0194364d9e36c455fbcd2390e00d8be.tar.zst |
better edit menus
-rw-r--r-- | karld/src/main.rs | 2 | ||||
-rw-r--r-- | karlgui/src/edit.rs | 230 | ||||
-rw-r--r-- | karlgui/src/helper.rs | 93 | ||||
-rw-r--r-- | karlgui/src/main.rs | 1 |
4 files changed, 246 insertions, 80 deletions
diff --git a/karld/src/main.rs b/karld/src/main.rs index 10130d7..22a12f7 100644 --- a/karld/src/main.rs +++ b/karld/src/main.rs @@ -127,7 +127,7 @@ lazy_static::lazy_static! { } pub fn handle_packet(client: u32, packet: ServerboundPacket, responder: Sender<ClientboundPacket>) { - std::thread::sleep(std::time::Duration::from_millis(75)); + // std::thread::sleep(std::time::Duration::from_millis(75)); // for testing clients with latency match packet { ServerboundPacket::Sync => { let _ = responder.send(ClientboundPacket::Sync); diff --git a/karlgui/src/edit.rs b/karlgui/src/edit.rs index d2f7150..b65feb2 100644 --- a/karlgui/src/edit.rs +++ b/karlgui/src/edit.rs @@ -1,8 +1,10 @@ use std::ops::Range; -use egui::{Color32, DragValue, RichText, Ui}; +use egui::{Color32, DragValue, TextEdit, Ui}; use karlcommon::{Condition, Property, Schedule, Task}; +use crate::helper::{edit_value, format_value}; + pub struct ShowOrEdit<T> { pub inner: T, pub edit: bool, @@ -12,6 +14,9 @@ impl<T: EditableWidget> ShowOrEdit<T> { pub fn new(inner: T, edit: bool) -> ShowOrEdit<T> { Self { inner, edit } } + pub fn show_only(&mut self, ui: &mut Ui) { + self.inner.ui(ui, false); + } pub fn changed(&mut self, ui: &mut Ui) -> bool { let changed = match self.edit { true => { @@ -49,12 +54,30 @@ impl EditableWidget for Task { ui.label(&*d); } } - ui.horizontal(|ui| { - ui.label("Tags:"); - for t in &self.tags { - ui.colored_label(Color32::LIGHT_GREEN, t); - } - }); + if self.tags.len() != 0 || edit { + ui.horizontal(|ui| { + ui.label("Tags:"); + let mut remove = None; + for (i, t) in self.tags.iter_mut().enumerate() { + if edit { + TextEdit::singleline(t).desired_width(50.0).show(ui); + if ui.button("π").clicked() { + remove = Some(i); + } + } else { + ui.colored_label(Color32::LIGHT_GREEN, t); + } + } + if edit { + if ui.button("β").clicked() { + self.tags.push(String::from("blub")) + } + } + if let Some(remove) = remove { + self.tags.remove(remove); + } + }); + } self.schedule.ui(ui, edit); }); } @@ -74,7 +97,22 @@ impl EditableWidget for Schedule { } => { ui.horizontal(|ui| { ui.label("Dynamic with priority"); - ui.label(&format!(" {} ", priority)); + match edit { + true => ui.add(DragValue::new(priority)), + false => ui.label(&format!("{}", priority)), + }; + ui.label("and duration"); + match edit { + true => ui.add(DragValue::new(duration)), + false => ui.label(&format!("{}", duration)), + }; + ui.label("seconds"); + if let Some(scheduled) = scheduled { + ui.label("at "); + scheduled.ui(ui, false) + } + ui.label("during"); + condition.ui(ui, edit); }); } Schedule::Condition(c) => c.ui(ui, edit), @@ -85,81 +123,108 @@ impl EditableWidget for Schedule { impl EditableWidget for Condition { fn ui(&mut self, ui: &mut Ui, edit: bool) { - ui.group(|ui| match self { - Condition::Never => { - ui.label("never"); - } - Condition::From(c) => { - ui.horizontal(|ui| { - ui.label("Starting from"); - c.ui(ui, edit); - if edit { - ui.menu_button("β Change type", |ui| { - add_condition(ui, |d| *c = Box::new(d)) - }); - } - }); - } - Condition::Invert(c) => { - ui.horizontal(|ui| { - ui.label("not when"); - c.ui(ui, edit); - if edit { - ui.menu_button("β Change type", |ui| { - add_condition(ui, |d| *c = Box::new(d)) - }); - } - }); - } - Condition::Or(cs) => combine_condition(ui, edit, "or", cs), - Condition::And(cs) => combine_condition(ui, edit, "and", cs), - Condition::Equal { - prop, - value, - modulus: _, - } => { - ui.horizontal(|ui| { - ui.label("when"); - if edit { - egui::ComboBox::from_id_source(ui.id()) - .selected_text(prop.to_str()) - .show_ui(ui, |ui| { - for v in Property::VALUES { - ui.selectable_value(prop, *v, v.to_str()); - } + let res = ui + .group(|ui| match self { + Condition::Never => { + ui.label("never"); + } + Condition::From(c) => { + ui.horizontal(|ui| { + ui.label("Starting from"); + c.ui(ui, edit); + if edit { + ui.menu_button("β Change type", |ui| { + add_condition(ui, |d| *c = Box::new(d)) }); - } else { - ui.label(prop.to_str()); - } - ui.label("="); - if edit { - ui.add(DragValue::new(value)); - } else { - ui.label(&format!("{}", value)); - } - }); - } - Condition::Range { - prop, - min, - max, - modulus: _, - } => { - ui.horizontal(|ui| { - ui.label("when "); - ui.label(&format!("{}", min)); - ui.label("β€"); - ui.label(&format!("{:?}", prop)); - ui.label("<"); - ui.label(&format!("{}", max)) + } + }); + } + Condition::Invert(c) => { + ui.horizontal(|ui| { + ui.label("not when"); + c.ui(ui, edit); + if edit { + ui.menu_button("β Change type", |ui| { + add_condition(ui, |d| *c = Box::new(d)) + }); + } + }); + } + Condition::Or(cs) => combine_condition(ui, edit, "or", cs), + Condition::And(cs) => combine_condition(ui, edit, "and", cs), + Condition::Equal { + prop, + value, + modulus: _, + } => { + ui.horizontal(|ui| { + ui.label("when"); + if edit { + egui::ComboBox::from_id_source(ui.id()) + .selected_text(prop.to_str()) + .show_ui(ui, |ui| { + for v in Property::VALUES { + ui.selectable_value(prop, *v, v.to_str()); + } + }); + } else { + ui.label(prop.to_str()); + } + ui.label("="); + if edit { + ui.push_id(0, |ui| edit_value(ui, *prop, value)); + } else { + ui.label(&format!("{}", format_value(*prop, *value))); + } + }); + } + Condition::Range { + prop, + min, + max, + modulus: _, + } => { + ui.horizontal(|ui| { + ui.label("when"); + match edit { + true => drop(ui.push_id(0, |ui| edit_value(ui, *prop, min))), + false => drop(ui.label(&format!("{}", format_value(*prop, *min)))), + } + ui.label("β€"); + if edit { + egui::ComboBox::from_id_source(ui.id()) + .selected_text(prop.to_str()) + .show_ui(ui, |ui| { + for v in Property::VALUES { + ui.selectable_value(prop, *v, v.to_str()); + } + }); + } else { + ui.label(prop.to_str()); + } + ui.label("<"); + match edit { + true => drop(ui.push_id(1, |ui| edit_value(ui, *prop, max))), + false => drop(ui.label(&format!("{}", format_value(*prop, *max)))), + } + }); + } + }) + .response; + if edit { + res.context_menu(|ui| { + ui.menu_button("Replace withβ¦", |ui| { + add_condition(ui, |c| { + *self = c; + }) }); - } - }); + }); + } } } impl EditableWidget for Range<i64> { - fn ui(&mut self, ui: &mut Ui, edit: bool) { + fn ui(&mut self, ui: &mut Ui, _edit: bool) { ui.label("todo"); } } @@ -173,7 +238,7 @@ fn combine_condition(ui: &mut Ui, edit: bool, combinator: &str, cs: &mut Vec<Con ui.label(if i != 0 { combinator } else { "" }); c.ui(ui, edit); if edit { - if ui.button(RichText::from("π").color(Color32::RED)).clicked() { + if ui.button("π").clicked() { remove = Some(i); } } @@ -196,14 +261,17 @@ fn combine_condition(ui: &mut Ui, edit: bool, combinator: &str, cs: &mut Vec<Con fn add_condition(ui: &mut Ui, mut add: impl FnMut(Condition) -> ()) { ui.menu_button("Combinators", |ui| { if ui.button("And").clicked() { + ui.close_menu(); add(Condition::And(vec![])) } if ui.button("Or").clicked() { + ui.close_menu(); add(Condition::Or(vec![])) } }); ui.menu_button("Constraints", |ui| { if ui.button("Equal").clicked() { + ui.close_menu(); add(Condition::Equal { modulus: None, prop: Property::Unix, @@ -211,6 +279,7 @@ fn add_condition(ui: &mut Ui, mut add: impl FnMut(Condition) -> ()) { }) } if ui.button("Range").clicked() { + ui.close_menu(); add(Condition::Range { prop: Property::Unix, min: 0, @@ -219,15 +288,18 @@ fn add_condition(ui: &mut Ui, mut add: impl FnMut(Condition) -> ()) { }) } if ui.button("Never").clicked() { + ui.close_menu(); add(Condition::Never) } }); ui.menu_button("Modifier", |ui| { if ui.button("Invert").clicked() { + ui.close_menu(); add(Condition::Invert(Box::new(Condition::Never))) } if ui.button("Starting from").clicked() { - add(Condition::From(Box::new(Condition::Never))) + ui.close_menu(); + add(Condition::From (Box::new(Condition::Never))) } }); } diff --git a/karlgui/src/helper.rs b/karlgui/src/helper.rs new file mode 100644 index 0000000..1ae29c8 --- /dev/null +++ b/karlgui/src/helper.rs @@ -0,0 +1,93 @@ +use egui::{DragValue, Ui}; +use karlcommon::Property; + +pub fn format_value(prop: Property, value: i64) -> String { + match prop { + Property::Year => format!("{value}"), + Property::Monthofyear => format!( + "{}", + match value { + 0 => "January", + 1 => "February", + 2 => "March", + 3 => "April", + 4 => "May", + 5 => "June", + 6 => "July", + 7 => "August", + 8 => "September", + 9 => "October", + 10 => "November", + 11 => "December", + _ => "(invalid)", + } + ), + Property::Weekofmonth => format!("{value}"), + Property::Dayofyear => format!("{value}"), + Property::Dayofmonth => format!("{value}"), + Property::Dayofweek => format!( + "{}", + match value { + 0 => "Monday", + 1 => "Thuesday", + 2 => "Wednesday", + 3 => "Thursday", + 4 => "Friday", + 5 => "Saturday", + 6 => "Sunday", + _ => "(invalid)", + } + ), + Property::Hour => format!("{value}h"), + Property::Minute => format!("{value}min"), + Property::Second => format!("{value}s"), + Property::Unix => format!("{value}s"), + } +} + +pub fn edit_value(ui: &mut Ui, prop: Property, value: &mut i64) { + match prop { + Property::Year => { + ui.add(DragValue::new(value)); + } + Property::Monthofyear => { + egui::ComboBox::from_id_source(ui.id()) + .selected_text(format_value(prop, *value)) + .show_ui(ui, |ui| { + for v in 0..12 { + ui.selectable_value(value, v, format_value(prop, v)); + } + }); + } + Property::Dayofweek => { + egui::ComboBox::from_id_source(ui.id()) + .selected_text(format_value(prop, *value)) + .show_ui(ui, |ui| { + for v in 0..7 { + ui.selectable_value(value, v, format_value(prop, v)); + } + }); + } + Property::Weekofmonth => { + ui.add(DragValue::new(value).clamp_range(0..=5)); + } + Property::Dayofyear => { + ui.add(DragValue::new(value).clamp_range(0..=366)); + } + Property::Dayofmonth => { + ui.add(DragValue::new(value).clamp_range(0..=31)); + } + Property::Hour => { + ui.add(DragValue::new(value).clamp_range(0..=23).suffix("h")); + } + Property::Minute => { + ui.add(DragValue::new(value).clamp_range(0..=59).suffix("m")); + } + Property::Second => { + ui.add(DragValue::new(value).clamp_range(0..=59).suffix("s")); + } + Property::Unix => { + ui.add(DragValue::new(value).suffix("s")); + } + } +} diff --git a/karlgui/src/main.rs b/karlgui/src/main.rs index 3832044..a85459e 100644 --- a/karlgui/src/main.rs +++ b/karlgui/src/main.rs @@ -1,5 +1,6 @@ pub mod client; pub mod edit; +pub mod helper; use crate::client::Client; use edit::ShowOrEdit; |