From 7d02e442e366e9e4380e3e18197adc5a081b878e Mon Sep 17 00:00:00 2001 From: PixelDots Date: Thu, 6 Jun 2024 12:35:31 -0500 Subject: [PATCH] Added Graphs toggle Keybinding and CheckBox --- i18n/en/cosmic_color_picker.ftl | 1 + src/app.rs | 32 +++++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/i18n/en/cosmic_color_picker.ftl b/i18n/en/cosmic_color_picker.ftl index 9b265ae..f9dee89 100644 --- a/i18n/en/cosmic_color_picker.ftl +++ b/i18n/en/cosmic_color_picker.ftl @@ -2,6 +2,7 @@ app-title = COSMIC Color Picker ## Menu view = View +graphs = Graphs menu-about = About ## About diff --git a/src/app.rs b/src/app.rs index d372648..e44cc51 100644 --- a/src/app.rs +++ b/src/app.rs @@ -21,6 +21,7 @@ pub struct ColorPicker { show_graphs: bool, colorspace_combo: widget::combo_box::State, + keybinds: HashMap, core: Core, } @@ -37,6 +38,7 @@ pub enum Message { AddSpace, RemoveSpace(usize), + ToggleGraphs, ToggleAboutPage, LaunchUrl(String), @@ -46,6 +48,7 @@ pub enum Message { #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum Action { + ToggleGraphs, About, } @@ -54,6 +57,7 @@ impl MenuAction for Action { fn message(&self) -> Message { match self { + Action::ToggleGraphs => Message::ToggleGraphs, Action::About => Message::ToggleAboutPage, } } @@ -80,8 +84,11 @@ impl Application for ColorPicker { vec![MenuBar::new(vec![menu::Tree::with_children( menu::root(fl!("view")), menu::items( - &HashMap::new(), - vec![menu::Item::Button(fl!("menu-about"), Action::About)], + &self.keybinds, + vec![ + menu::Item::CheckBox(fl!("graphs"), self.show_graphs, Action::ToggleGraphs), + menu::Item::Button(fl!("menu-about"), Action::About), + ], ), )]) .into()] @@ -92,10 +99,19 @@ impl Application for ColorPicker { } fn init(core: Core, _flags: Self::Flags) -> (Self, Command) { + let mut keybinds = HashMap::new(); + keybinds.insert( + menu::KeyBind { + modifiers: vec![menu::key_bind::Modifier::Ctrl], + key: Key::Character("g".into()), + }, + Action::ToggleGraphs, + ); + let mut app = ColorPicker { spaces: vec![ColorSpace::default()], last_edited: 0, - show_graphs: true, + show_graphs: false, colorspace_combo: widget::combo_box::State::new(vec![ ColorSpaceCombo::Rgb, @@ -104,6 +120,7 @@ impl Application for ColorPicker { ColorSpaceCombo::Oklch, ColorSpaceCombo::Cmyk, ]), + keybinds, core, }; @@ -137,6 +154,9 @@ impl Application for ColorPicker { self.spaces.remove(index); } + Message::ToggleGraphs => { + self.show_graphs = !self.show_graphs; + } Message::ToggleAboutPage => { self.core.window.show_context = !self.core.window.show_context; } @@ -151,6 +171,12 @@ impl Application for ColorPicker { return self.copy_to_clipboard(index); } Message::Key(key, modifiers) => { + for (key_bind, action) in self.keybinds.iter() { + if key_bind.matches(modifiers, &key) { + return self.update(action.message()); + } + } + if modifiers.control() && key == Key::Character("c".into()) { return self.copy_to_clipboard(self.last_edited); }