[fractal/fractal-next] session: Fix keyboard shortcuts



commit 17532d7ba229bea739fc1dcfb562b4a2f0bcaac4
Author: Julian Sparber <julian sparber net>
Date:   Thu Sep 9 11:32:33 2021 +0200

    session: Fix keyboard shortcuts

 data/resources/ui/session.ui   |  1 +
 data/resources/ui/shortcuts.ui |  4 ++--
 data/resources/ui/sidebar.ui   |  2 +-
 src/application.rs             |  2 --
 src/session/mod.rs             | 29 ++++++++++++++++++++++++++---
 src/window.rs                  | 22 ++--------------------
 6 files changed, 32 insertions(+), 28 deletions(-)
---
diff --git a/data/resources/ui/session.ui b/data/resources/ui/session.ui
index 3330f2a2..6efb6e53 100644
--- a/data/resources/ui/session.ui
+++ b/data/resources/ui/session.ui
@@ -4,6 +4,7 @@
     <property name="item-type">Error</property>
   </object>
   <template class="Session" parent="AdwBin">
+    <property name="focusable">true</property>
     <property name="child">
       <object class="GtkOverlay">
         <child type="overlay">
diff --git a/data/resources/ui/shortcuts.ui b/data/resources/ui/shortcuts.ui
index 33a33378..b46f2922 100644
--- a/data/resources/ui/shortcuts.ui
+++ b/data/resources/ui/shortcuts.ui
@@ -24,13 +24,13 @@
             <child>
               <object class="GtkShortcutsShortcut">
                 <property name="title" translatable="yes" context="shortcut window">Search Room 
List</property>
-                <property name="action-name">win.toggle-room-search</property>
+                <property name="action-name">session.toggle-room-search</property>
               </object>
             </child>
             <child>
               <object class="GtkShortcutsShortcut">
                 <property name="title" translatable="yes" context="shortcut window">Close Room</property>
-                <property name="action-name">win.close-room</property>
+                <property name="action-name">session.close-room</property>
               </object>
             </child>
           </object>
diff --git a/data/resources/ui/sidebar.ui b/data/resources/ui/sidebar.ui
index 06b3c2f9..f86a2f57 100644
--- a/data/resources/ui/sidebar.ui
+++ b/data/resources/ui/sidebar.ui
@@ -47,7 +47,7 @@
               <object class="GtkToggleButton" id="search_button">
                 <property name="icon-name">system-search-symbolic</property>
                 <property name="active" bind-source="room_search" bind-property="search-mode-enabled" 
bind-flags="sync-create"/>
-                <property name="action-name">win.toggle-room-search</property>
+                <property name="action-name">session.toggle-room-search</property>
               </object>
             </child>
           </object>
diff --git a/src/application.rs b/src/application.rs
index d85414d5..0ccac8cb 100644
--- a/src/application.rs
+++ b/src/application.rs
@@ -142,8 +142,6 @@ impl Application {
     fn setup_accels(&self) {
         self.set_accels_for_action("app.quit", &["<Control>q"]);
         self.set_accels_for_action("win.show-help-overlay", &["<Control>question"]);
-        self.set_accels_for_action("win.toggle-room-search", &["<Control>k"]);
-        self.set_accels_for_action("win.close-room", &["Escape"]);
     }
 
     fn setup_css(&self) {
diff --git a/src/session/mod.rs b/src/session/mod.rs
index b888ccf0..a52f87ee 100644
--- a/src/session/mod.rs
+++ b/src/session/mod.rs
@@ -25,7 +25,7 @@ use crate::session::content::ContentType;
 use adw::subclass::prelude::BinImpl;
 use gtk::subclass::prelude::*;
 use gtk::{self, prelude::*};
-use gtk::{gio, glib, glib::clone, glib::SyncSender, CompositeTemplate, SelectionModel};
+use gtk::{gdk, gio, glib, glib::clone, glib::SyncSender, CompositeTemplate, SelectionModel};
 use gtk_macros::send;
 use log::error;
 use matrix_sdk::ruma::{
@@ -76,6 +76,28 @@ mod imp {
 
         fn class_init(klass: &mut Self::Class) {
             Self::bind_template(klass);
+
+            klass.install_action("session.close-room", None, move |session, _, _| {
+                session.set_selected_room(None);
+            });
+
+            klass.add_binding_action(
+                gdk::keys::constants::Escape,
+                gdk::ModifierType::empty(),
+                "session.close-room",
+                None,
+            );
+
+            klass.install_action("session.toggle-room-search", None, move |session, _, _| {
+                session.toggle_room_search();
+            });
+
+            klass.add_binding_action(
+                gdk::keys::constants::k,
+                gdk::ModifierType::CONTROL_MASK,
+                "session.toggle-room-search",
+                None,
+            );
         }
 
         fn instance_init(obj: &InitializingObject<Self>) {
@@ -267,9 +289,10 @@ impl Session {
         );
     }
 
-    pub fn room_search_bar(&self) -> gtk::SearchBar {
+    fn toggle_room_search(&self) {
         let priv_ = imp::Session::from_instance(self);
-        priv_.sidebar.room_search_bar()
+        let room_search = priv_.sidebar.room_search_bar();
+        room_search.set_search_mode(!room_search.is_search_mode());
     }
 
     pub fn login_with_previous_session(&self, session: StoredSession) {
diff --git a/src/window.rs b/src/window.rs
index 7e4e590f..cbcecc60 100644
--- a/src/window.rs
+++ b/src/window.rs
@@ -1,11 +1,9 @@
 use crate::config::{APP_ID, PROFILE};
-use crate::gio::SimpleAction;
 use crate::secret;
 use crate::Application;
 use crate::Login;
 use crate::Session;
 use adw::subclass::prelude::AdwApplicationWindowImpl;
-use gio::PropertyAction;
 use glib::signal::Inhibit;
 use gtk::subclass::prelude::*;
 use gtk::{self, prelude::*};
@@ -104,24 +102,8 @@ impl Window {
         session.set_logged_in_users(&priv_.sessions.pages());
         priv_.sessions.add_child(session);
         priv_.sessions.set_visible_child(session);
-        self.install_session_actions(session);
-    }
-
-    /// Installs session-related actions to the Window.
-    fn install_session_actions(&self, session: &Session) {
-        let room_search_bar = session.room_search_bar();
-        let room_search_toggle_action = PropertyAction::new(
-            "toggle-room-search",
-            &room_search_bar,
-            "search-mode-enabled",
-        );
-        self.add_action(&room_search_toggle_action);
-
-        let close_room_action = SimpleAction::new("close-room", None);
-        close_room_action.connect_activate(clone!(@weak session => move |_, _| {
-            session.set_selected_room(None);
-        }));
-        self.add_action(&close_room_action);
+        // We need to grab the focus so that keyboard shortcuts work
+        session.grab_focus();
     }
 
     fn restore_sessions(&self) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]