[fractal/fractal-next] login: use default widget for logging on enter press



commit ca64f47c4dfb9fa28bf51824c84d5af001646ac6
Author: giusdp <depalma gsp gmail com>
Date:   Sun Jun 27 16:06:26 2021 +0200

    login: use default widget for logging on enter press
    
    Expose the login button in login.ui to retrieve it by the default_widget function in login.
    Set the login  button as the default widget when the visible child in main_stack is login,
    and connect the function to the visible_child change signal to update the default widget.
    
    fix: https://gitlab.gnome.org/GNOME/fractal/-/issues/788

 data/resources/ui/login.ui |  2 +-
 src/login.rs               |  6 ++++++
 src/window.rs              | 16 ++++++++++++++++
 3 files changed, 23 insertions(+), 1 deletion(-)
---
diff --git a/data/resources/ui/login.ui b/data/resources/ui/login.ui
index 920de6b6..24290411 100644
--- a/data/resources/ui/login.ui
+++ b/data/resources/ui/login.ui
@@ -12,7 +12,7 @@
               </object>
             </property>
             <child type="end">
-              <object class="GtkButton">
+              <object class="GtkButton" id="next_button">
                 <property name="action_name">login.next</property>
                 <property name="child">
                   <object class="GtkStack" id="next_stack">
diff --git a/src/login.rs b/src/login.rs
index e458bf8a..3c04239e 100644
--- a/src/login.rs
+++ b/src/login.rs
@@ -17,6 +17,8 @@ mod imp {
     #[derive(Debug, Default, CompositeTemplate)]
     #[template(resource = "/org/gnome/FractalNext/login.ui")]
     pub struct Login {
+        #[template_child]
+        pub next_button: TemplateChild<gtk::Button>,
         #[template_child]
         pub next_stack: TemplateChild<gtk::Stack>,
         #[template_child]
@@ -186,6 +188,10 @@ impl Login {
         })
         .unwrap()
     }
+
+    pub fn default_widget(&self) -> gtk::Widget {
+        imp::Login::from_instance(&self).next_button.get().upcast()
+    }
 }
 
 fn build_homeserver_url(server: &str) -> Result<Url, ParseError> {
diff --git a/src/window.rs b/src/window.rs
index 396b0300..8ec431ae 100644
--- a/src/window.rs
+++ b/src/window.rs
@@ -65,6 +65,11 @@ mod imp {
             self.login.connect_new_session(
                 clone!(@weak obj => move |_login, session| obj.add_session(session)),
             );
+
+            self.main_stack.connect_visible_child_notify(
+                clone!(@weak obj => move |_| obj.set_default_by_child()),
+            );
+            obj.set_default_by_child();
         }
     }
 
@@ -148,4 +153,15 @@ impl Window {
         self.set_default_size(width, height);
         self.set_property("maximized", &is_maximized).unwrap();
     }
+
+    /// Change the default widget of the window based on the visible child
+    /// If the login screen is visible, its login button becomes the default widget
+    fn set_default_by_child(&self) {
+        let priv_ = imp::Window::from_instance(self);
+        if priv_.main_stack.visible_child() == Some(priv_.login.get().upcast()) {
+            self.set_default_widget(Some(&priv_.login.default_widget()));
+        } else {
+            self.set_default_widget(gtk::NONE_WIDGET);
+        }
+    }
 }


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