| 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 super::editor::ControlEditor; |
| 21 | use crate::{ |
| 22 | properties::{Properties, *}, |
| 23 | utils::settings_aware_widget, |
| 24 | }; |
| 25 | use adw::prelude::*; |
| 26 | use gettextrs::gettext; |
| 27 | use gtk::{gio, glib}; |
| 28 | |
| 29 | pub fn create_controls_list(settings: &gio::Settings) -> adw::PreferencesGroup { |
| 30 | let pg = adw::PreferencesGroup::builder() |
| 31 | .title(gettext("Keyboard")) |
| 32 | .build(); |
| 33 | |
| 34 | for (key, label) in [ |
| 35 | (CONTROL_KEY_NW, gettext("Key to move NW")), |
| 36 | (CONTROL_KEY_N, gettext("Key to move N")), |
| 37 | (CONTROL_KEY_NE, gettext("Key to move NE")), |
| 38 | (CONTROL_KEY_W, gettext("Key to move W")), |
| 39 | (CONTROL_KEY_HOLD, gettext("Key to hold")), |
| 40 | (CONTROL_KEY_E, gettext("Key to move E")), |
| 41 | (CONTROL_KEY_SW, gettext("Key to move SW")), |
| 42 | (CONTROL_KEY_S, gettext("Key to move S")), |
| 43 | (CONTROL_KEY_SE, gettext("Key to move SE")), |
| 44 | ] { |
| 45 | let editor = new_editor(settings, key); |
| 46 | |
| 47 | let action_row = adw::ActionRow::builder() |
| 48 | .title(&label) |
| 49 | .activatable_widget(&editor) |
| 50 | .build(); |
| 51 | action_row.add_suffix(&editor); |
| 52 | pg.add(&action_row); |
| 53 | } |
| 54 | |
| 55 | let dbut = gtk::Button::builder() |
| 56 | .label(gettext("_Restore Defaults")) |
| 57 | .use_underline(true) |
| 58 | .build(); |
| 59 | dbut.connect_clicked(glib::clone!( |
| 60 | #[weak] |
| 61 | settings, |
| 62 | move |_| { |
| 63 | settings.reset_all_control_keys(); |
| 64 | } |
| 65 | )); |
| 66 | pg.set_header_suffix(Some(&dbut)); |
| 67 | |
| 68 | pg |
| 69 | } |
| 70 | |
| 71 | fn new_editor(settings: &gio::Settings, control_key: ControlKey) -> ControlEditor { |
| 72 | let editor = ControlEditor::default(); |
| 73 | |
| 74 | editor.set_key(settings.get_control_key(&control_key)); |
| 75 | |
| 76 | editor.connect_edited(glib::clone!( |
| 77 | #[strong] |
| 78 | settings, |
| 79 | move |keyval| { |
| 80 | settings.set_control_key(&control_key, keyval); |
| 81 | } |
| 82 | )); |
| 83 | editor.connect_cleared(glib::clone!( |
| 84 | #[strong] |
| 85 | settings, |
| 86 | move || { |
| 87 | settings.reset_control_key(&control_key); |
| 88 | settings.get_control_key(&control_key) |
| 89 | } |
| 90 | )); |
| 91 | settings_aware_widget( |
| 92 | &editor, |
| 93 | settings, |
| 94 | Some(control_key.settings_key), |
| 95 | move |e, s| { |
| 96 | e.set_key(s.get_control_key(&control_key)); |
| 97 | }, |
| 98 | ); |
| 99 | editor |
| 100 | } |