| 1 | /* |
| 2 | * Copyright 2020-2024 Andrey Kutejko <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * For more details see the file COPYING. |
| 18 | */ |
| 19 | |
| 20 | use gettextrs::gettext; |
| 21 | use gtk::{gdk, glib, prelude::*, subclass::prelude::*}; |
| 22 | |
| 23 | mod imp { |
| 24 | use super::*; |
| 25 | use std::{cell::Cell, sync::OnceLock}; |
| 26 | |
| 27 | pub struct ControlEditor { |
| 28 | pub stack: gtk::Stack, |
| 29 | pub shortcut_label: adw::ShortcutLabel, |
| 30 | pub label: gtk::Label, |
| 31 | pub key: Cell<Option<gdk::Key>>, |
| 32 | pub editing: Cell<bool>, |
| 33 | } |
| 34 | |
| 35 | #[glib::object_subclass] |
| 36 | impl ObjectSubclass for ControlEditor { |
| 37 | const NAME: &'static str = "RobotsControlEditor"; |
| 38 | type Type = super::ControlEditor; |
| 39 | type ParentType = gtk::Widget; |
| 40 | |
| 41 | fn new() -> Self { |
| 42 | Self { |
| 43 | stack: gtk::Stack::new(), |
| 44 | shortcut_label: adw::ShortcutLabel::new(""), |
| 45 | label: gtk::Label::builder() |
| 46 | .halign(gtk::Align::Start) |
| 47 | .valign(gtk::Align::Center) |
| 48 | .build(), |
| 49 | key: Cell::new(None), |
| 50 | editing: Cell::new(false), |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | impl ObjectImpl for ControlEditor { |
| 56 | fn constructed(&self) { |
| 57 | self.parent_constructed(); |
| 58 | |
| 59 | let obj = self.obj(); |
| 60 | obj.set_layout_manager(Some(gtk::BinLayout::new())); |
| 61 | obj.set_focusable(true); |
| 62 | |
| 63 | self.stack.set_parent(&*obj); |
| 64 | self.stack.add_child(&self.shortcut_label); |
| 65 | self.stack.add_child(&self.label); |
| 66 | |
| 67 | self.shortcut_label.set_halign(gtk::Align::End); |
| 68 | |
| 69 | let click_controller = gtk::GestureClick::new(); |
| 70 | click_controller.connect_pressed(glib::clone!( |
| 71 | #[weak(rename_to = imp)] |
| 72 | self, |
| 73 | move |_, _, _, _| imp.set_editing(true) |
| 74 | )); |
| 75 | obj.add_controller(click_controller); |
| 76 | |
| 77 | let key_controller = gtk::EventControllerKey::new(); |
| 78 | key_controller.connect_key_pressed(glib::clone!( |
| 79 | #[weak(rename_to = imp)] |
| 80 | self, |
| 81 | #[upgrade_or] |
| 82 | glib::Propagation::Proceed, |
| 83 | move |_, keyval, _keycode, mods| imp.key_controller_key_pressed(keyval, mods) |
| 84 | )); |
| 85 | obj.add_controller(key_controller); |
| 86 | |
| 87 | let focus_controller = gtk::EventControllerFocus::new(); |
| 88 | focus_controller.connect_leave(glib::clone!( |
| 89 | #[weak(rename_to = imp)] |
| 90 | self, |
| 91 | move |_| imp.set_editing(false) |
| 92 | )); |
| 93 | obj.add_controller(focus_controller); |
| 94 | |
| 95 | obj.connect_mnemonic_activate(glib::clone!( |
| 96 | #[weak(rename_to = imp)] |
| 97 | self, |
| 98 | #[upgrade_or] |
| 99 | glib::Propagation::Proceed, |
| 100 | move |_, _| { |
| 101 | imp.set_editing(true); |
| 102 | glib::Propagation::Proceed |
| 103 | } |
| 104 | )); |
| 105 | obj.connect_move_focus(glib::clone!( |
| 106 | #[weak(rename_to = imp)] |
| 107 | self, |
| 108 | move |_, _| imp.set_editing(false) |
| 109 | )); |
| 110 | } |
| 111 | |
| 112 | fn dispose(&self) { |
| 113 | while let Some(child) = self.obj().first_child() { |
| 114 | child.unparent(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | fn signals() -> &'static [glib::subclass::Signal] { |
| 119 | static SIGNALS: OnceLock<Vec<glib::subclass::Signal>> = OnceLock::new(); |
| 120 | SIGNALS.get_or_init(|| { |
| 121 | vec![ |
| 122 | glib::subclass::Signal::builder("edited") |
| 123 | .param_types([gdk::Key::static_type()]) |
| 124 | .return_type::<()>() |
| 125 | .build(), |
| 126 | glib::subclass::Signal::builder("cleared") |
| 127 | .return_type::<gdk::Key>() |
| 128 | .build(), |
| 129 | ] |
| 130 | }) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | impl WidgetImpl for ControlEditor {} |
| 135 | |
| 136 | impl ControlEditor { |
| 137 | pub fn set_editing(&self, editing: bool) { |
| 138 | self.editing.set(editing); |
| 139 | if editing { |
| 140 | self.obj().grab_focus(); |
| 141 | } |
| 142 | self.update_label(); |
| 143 | } |
| 144 | |
| 145 | pub fn update_label(&self) { |
| 146 | if self.editing.get() { |
| 147 | self.stack.set_visible_child(&self.label); |
| 148 | self.label.set_label(&gettext("New accelerator…")); |
| 149 | } else if let Some(key) = self.key.get() { |
| 150 | self.stack.set_visible_child(&self.shortcut_label); |
| 151 | self.shortcut_label |
| 152 | .set_accelerator(>k::accelerator_name(key, gdk::ModifierType::empty())); |
| 153 | } else { |
| 154 | self.stack.set_visible_child(&self.label); |
| 155 | self.label.set_label(&gettext("Disabled")); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | fn key_controller_key_pressed( |
| 160 | &self, |
| 161 | keyval: gdk::Key, |
| 162 | mods: gdk::ModifierType, |
| 163 | ) -> glib::Propagation { |
| 164 | if self.editing.get() { |
| 165 | self.set_editing(false); |
| 166 | if mods == gdk::ModifierType::empty() { |
| 167 | return match keyval { |
| 168 | gdk::Key::Tab => glib::Propagation::Proceed, |
| 169 | gdk::Key::BackSpace => { |
| 170 | self.key.set(Some(self.obj().emit_cleared())); |
| 171 | glib::Propagation::Stop |
| 172 | } |
| 173 | gdk::Key::Escape => glib::Propagation::Stop, |
| 174 | _ => { |
| 175 | self.key.set(Some(keyval)); |
| 176 | self.obj().emit_edited(keyval); |
| 177 | glib::Propagation::Stop |
| 178 | } |
| 179 | }; |
| 180 | } |
| 181 | } else { |
| 182 | if mods == gdk::ModifierType::empty() { |
| 183 | match keyval { |
| 184 | gdk::Key::Return | gdk::Key::KP_Enter => { |
| 185 | self.set_editing(true); |
| 186 | return glib::Propagation::Stop; |
| 187 | } |
| 188 | _ => return glib::Propagation::Proceed, |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | glib::Propagation::Proceed |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | glib::wrapper! { |
| 198 | pub struct ControlEditor(ObjectSubclass<imp::ControlEditor>) |
| 199 | @extends gtk::Widget, |
| 200 | @implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget; |
| 201 | } |
| 202 | |
| 203 | impl Default for ControlEditor { |
| 204 | fn default() -> Self { |
| 205 | glib::Object::builder().build() |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | impl ControlEditor { |
| 210 | pub fn key(&self) -> Option<gdk::Key> { |
| 211 | self.imp().key.get() |
| 212 | } |
| 213 | |
| 214 | pub fn set_key(&self, key: gdk::Key) { |
| 215 | self.imp().key.set(Some(key)); |
| 216 | self.imp().update_label(); |
| 217 | } |
| 218 | |
| 219 | fn emit_edited(&self, key: gdk::Key) { |
| 220 | self.emit_by_name::<()>("edited", &[&key]); |
| 221 | } |
| 222 | |
| 223 | pub fn connect_edited<F>(&self, f: F) -> glib::signal::SignalHandlerId |
| 224 | where |
| 225 | F: Fn(gdk::Key) + 'static, |
| 226 | { |
| 227 | self.connect_closure( |
| 228 | "edited", |
| 229 | false, |
| 230 | glib::closure_local!(move |_self: &Self, key: gdk::Key| (f)(key)), |
| 231 | ) |
| 232 | } |
| 233 | |
| 234 | fn emit_cleared(&self) -> gdk::Key { |
| 235 | self.emit_by_name::<gdk::Key>("cleared", &[]) |
| 236 | } |
| 237 | |
| 238 | pub fn connect_cleared<F>(&self, f: F) -> glib::signal::SignalHandlerId |
| 239 | where |
| 240 | F: Fn() -> gdk::Key + 'static, |
| 241 | { |
| 242 | self.connect_closure( |
| 243 | "cleared", |
| 244 | false, |
| 245 | glib::closure_local!(move |_self: &Self| (f)()), |
| 246 | ) |
| 247 | } |
| 248 | } |