From cc0ce1e89abeddc8cfa7907559a05806866fb672 Mon Sep 17 00:00:00 2001 From: Beinsezii Date: Wed, 31 Jan 2024 15:39:33 -0800 Subject: [PATCH] Save extra values to preset file so you don't have to memorize the 10 different vim groups... --- examples/bsz.toml | 17 +++++++++ src/gui/mod.rs | 97 ++++++++++++++++++++++++++++------------------- src/main.rs | 7 +++- 3 files changed, 79 insertions(+), 42 deletions(-) diff --git a/examples/bsz.toml b/examples/bsz.toml index 94fb932..026932a 100644 --- a/examples/bsz.toml +++ b/examples/bsz.toml @@ -5,3 +5,20 @@ background = [0.0, 0.0, 60.0] spectrum = [50.0, 50.0, 20.0] spectrum_bright = [50.0, 50.0, 355.0] accent = 3 + +[extras.bsz-i3-blocklets] +COLOR_ALT = 2 +COLOR_LO = 4 +COLOR_HI = 3 +COLOR_ERR = 1 + +[extras.Vim] +SPECIAL = 10 +STATEMENT = 6 +TYPE = 5 +UNDERLINED = 11 +IDENTIFIER = 12 +PREPROC = 2 +ERROR = 1 +TODO = 9 +CONSTANT = 3 diff --git a/src/gui/mod.rs b/src/gui/mod.rs index a13b6f6..e9bc196 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -193,7 +193,6 @@ impl Display for Output { pub struct CollurgyUI { data: Collurgy, exporters: HashMap, - extras: Option>, output: Output, scale: f32, } @@ -202,20 +201,27 @@ impl CollurgyUI { // {{{ pub fn new( _cc: &CreationContext, - data: Collurgy, + mut data: Collurgy, exporters: HashMap, ) -> Self { + // Fill missing extras into data + for (k, v) in exporters.iter() { + if let Some(extras) = &v.extras { + if !data.extras.contains_key(k) { + data.extras.insert(k.to_string(), extras.clone()); + } + } + } Self { data, output: Output::TOML, exporters, - extras: None, scale: scale_factor(), } } - fn output(&self) -> String { + fn process_output(&self) -> String { match &self.output { - Output::Exporter(s) => self.exporters[s].export(&self.data, &self.extras), + Output::Exporter(s) => self.exporters[s].export(&self.data), Output::JSON => serde_json::to_string(&self.data).unwrap(), Output::TOML => toml::to_string(&self.data).unwrap(), } @@ -227,18 +233,13 @@ impl CollurgyUI { self.data = collurgy } } - fn set(&mut self, output: Output) { - self.extras = match &output { - Output::Exporter(e) => self.exporters[e].extras.clone(), - _ => None, - }; - self.output = output; - } // }}} } impl App for CollurgyUI { fn update(&mut self, ctx: &Context, _frame: &mut eframe::Frame) { + // {{{ + // DnD ctx.input(|input| { for f in &input.raw.dropped_files { if let Some(bytes) = &f.bytes { @@ -260,29 +261,29 @@ impl App for CollurgyUI { SidePanel::right("ExportPan") .min_width(200.0) .show(ctx, |ui| { + // EXPORTER HEADER {{{ ui.horizontal(|ui| { - // {{{ ui.menu_button(self.output.to_string(), |ui| { let mut vals: Vec = self.exporters.keys().cloned().collect(); vals.sort(); for exp in vals.into_iter() { if ui.button(format!("Export/{}", &exp)).clicked() { - self.set(Output::Exporter(exp)); + self.output = Output::Exporter(exp); ui.close_menu(); } } if ui.button("Save/JSON").clicked() { - self.set(Output::JSON); + self.output = Output::JSON; ui.close_menu(); } if ui.button("Save/TOML").clicked() { - self.set(Output::TOML); + self.output = Output::TOML; ui.close_menu(); } }); if ui.button("Copy").clicked() { ui.output_mut(|o| { - o.copied_text = self.output(); + o.copied_text = self.process_output(); }); } if ui.button("Save").clicked() { @@ -306,7 +307,7 @@ impl App for CollurgyUI { } // on Wayland this has like a 75% chance of making egui go poof if let Some(file) = dialog.save_file() { - let _ = fs::write(file, self.output()); + let _ = fs::write(file, self.process_output()); } } if ui.button("Load").clicked() { @@ -319,40 +320,50 @@ impl App for CollurgyUI { } } } - // }}} }); + // EXPORTER HEADER }}} + // EXPORTER {{{ ScrollArea::both().show(ui, |ui| { - if let Some(extras) = self.extras.as_mut() { - let mut sorted: Vec<(&String, &mut usize)> = extras.iter_mut().collect(); - sorted.sort_unstable_by(|a, b| a.0.cmp(b.0)); - for (id, n) in sorted.into_iter() { - if *n < 16 { - ui.horizontal(|ui| { - ui.label( - RichText::new(id) - .background_color(if *n != 0 { - colors[0] - } else { - colors[15] - }) - .color(colors[*n]), - ); - ui.add(DragValue::new(n).clamp_range(0..=15)); - }); + if let Output::Exporter(e) = &self.output { + if let Some(extras) = self.data.extras.get_mut(&self.exporters[e].name) { + let mut sorted: Vec<(&String, &mut usize)> = + extras.iter_mut().collect(); + sorted.sort_unstable_by(|a, b| a.0.cmp(b.0)); + for (id, n) in sorted.into_iter() { + if *n < 16 { + ui.horizontal(|ui| { + ui.label( + RichText::new(id) + .background_color(if *n != 0 { + colors[0] + } else { + colors[15] + }) + .color(colors[*n]), + ); + ui.add(DragValue::new(n).clamp_range(0..=15)); + }); + } + } + if ui.button("Reset All").clicked() { + if let Some(new_extras) = &self.exporters[e].extras { + *extras = new_extras.clone() + } } } } // sneaky immutable textedit hack? // ui.code_editor(&mut self.output().as_str()); // textedit always wraps??? - ui.add(Label::new(self.output()).wrap(false)) + ui.add(Label::new(self.process_output()).wrap(false)) }); + // EXPORTER }}} }); let fill = colcon::str2space("oklab 0.5 0 0", Space::SRGB).unwrap(); CentralPanel::default() .frame(Frame::none().fill(Rgba::from_rgb(fill[0], fill[1], fill[2]).into())) .show(&ctx, |ui| { - // {{{ + // HEADER {{{ ui.horizontal(|ui| { ui.add_sized( (150.0, 20.0), @@ -397,8 +408,10 @@ impl App for CollurgyUI { ) }); }); + // HEADER }}} ScrollArea::both().show(ui, |ui| { ui.spacing_mut().item_spacing = (4.0 * s, 4.0 * s).into(); + // LCH PICKERS {{{ ui.horizontal(|ui| { ui.spacing_mut().item_spacing = (4.0 * s, 1.0 * s).into(); ui.add(LCH::new( @@ -438,6 +451,8 @@ impl App for CollurgyUI { self.data.high2023, )); }); + // LCH PICKERS }}} + // COLOR BUTTONS {{{ Grid::new("color_buttons") .spacing((4.0 * s, 4.0 * s)) .show(ui, |ui| { @@ -461,6 +476,8 @@ impl App for CollurgyUI { } } }); + // COLOR BUTTONS }}} + // LOREM IPSUM {{{ for (fg, bg) in [ (colors[15], colors[0]), (colors[7], colors[0]), @@ -471,10 +488,10 @@ impl App for CollurgyUI { ui.label(RichText::from(LI).color(fg).size(10.0 * s)) }); } + // }}} }); - // }}} }); - } + } // }}} fn clear_color(&self, _visuals: &egui::Visuals) -> [f32; 4] { [0.0, 0.0, 0.0, 0.0] } diff --git a/src/main.rs b/src/main.rs index a363d9e..1f34c89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,6 +71,8 @@ pub struct Collurgy { spectrum_bright: [f32; 3], /// Which # should be accent accent: usize, + #[serde(default)] + extras: HashMap>, } impl Default for Collurgy { @@ -83,6 +85,7 @@ impl Default for Collurgy { spectrum: [50.0, 50.0, 30.0], spectrum_bright: [70.0, 50.0, 30.0], accent: 11, // Bright Yellow + extras: HashMap::new() } } } @@ -155,7 +158,7 @@ pub struct Exporter { } impl Exporter { - fn export(&self, data: &Collurgy, extras: &Option>) -> String { + fn export(&self, data: &Collurgy) -> String { let frgb = data.compute(); let irgb = frgb.map(|pixel| srgb_to_irgb(pixel)); let hex = irgb.map(|pixel| irgb_to_hex(pixel)); @@ -192,7 +195,7 @@ impl Exporter { ("{ACCHEX}".to_string(), hex[data.accent].clone()), ]); - if let Some(ext) = extras.as_ref().or(self.extras.as_ref()) { + if let Some(ext) = data.extras.get(&String::from(&self.name)) { for (id, n) in ext { if let (Some(iv), Some(fv), Some(hv)) = (irgb.get(*n), frgb.get(*n), hex.get(*n)) { swaps.append(&mut vec![