[gnome-software: 5/7] repos-dialog: Make it a HdyWindow




commit e9d54cad11632530f53471a75730771278245945
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Fri Jul 30 10:23:10 2021 +0200

    repos-dialog: Make it a HdyWindow
    
    We don't need it to be a GtkDialog as we don't use it as a dialog, so
    let's instead make it a HdyWindow to have nice rounded corners, to
    simplify the code a bit, and to avoid the annoying 2px padding that
    GtkDialog adds and having to listen it its response just to destroy it.

 src/gs-repos-dialog.c  | 44 +++++++++++++++++++++++++++++++++-----------
 src/gs-repos-dialog.h  |  3 ++-
 src/gs-repos-dialog.ui | 38 +++++++++++++++++---------------------
 src/gs-shell.c         |  4 ----
 4 files changed, 52 insertions(+), 37 deletions(-)
---
diff --git a/src/gs-repos-dialog.c b/src/gs-repos-dialog.c
index bbb8bd5af..2662a70af 100644
--- a/src/gs-repos-dialog.c
+++ b/src/gs-repos-dialog.c
@@ -23,7 +23,7 @@
 
 struct _GsReposDialog
 {
-       GtkDialog        parent_instance;
+       HdyWindow        parent_instance;
        GSettings       *settings;
        GsApp           *third_party_repo;
        GHashTable      *sections; /* gchar * ~> GsReposSection * */
@@ -31,13 +31,12 @@ struct _GsReposDialog
        GCancellable    *cancellable;
        GsPluginLoader  *plugin_loader;
        GtkWidget       *status_empty;
-       GtkWidget       *label_header;
        GtkWidget       *content_page;
        GtkWidget       *spinner;
        GtkWidget       *stack;
 };
 
-G_DEFINE_TYPE (GsReposDialog, gs_repos_dialog, GTK_TYPE_DIALOG)
+G_DEFINE_TYPE (GsReposDialog, gs_repos_dialog, HDY_TYPE_WINDOW)
 
 typedef struct {
        GsReposDialog   *dialog;
@@ -57,6 +56,35 @@ install_remove_data_free (InstallRemoveData *data)
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(InstallRemoveData, install_remove_data_free);
 
+static gboolean
+key_press_event_cb (GtkWidget            *sender,
+                    GdkEvent             *event,
+                    HdyPreferencesWindow *self)
+{
+       guint keyval;
+       GdkModifierType state;
+       GdkKeymap *keymap;
+       GdkEventKey *key_event = (GdkEventKey *) event;
+
+       gdk_event_get_state (event, &state);
+
+       keymap = gdk_keymap_get_for_display (gtk_widget_get_display (sender));
+
+       gdk_keymap_translate_keyboard_state (keymap,
+                                            key_event->hardware_keycode,
+                                            state,
+                                            key_event->group,
+                                            &keyval, NULL, NULL, NULL);
+
+       if (keyval == GDK_KEY_Escape) {
+               gtk_window_close (GTK_WINDOW (self));
+
+               return GDK_EVENT_STOP;
+       }
+
+       return GDK_EVENT_PROPAGATE;
+}
+
 static void
 repo_enabled_cb (GObject *source,
                  GAsyncResult *res,
@@ -411,8 +439,6 @@ get_sources_cb (GsPluginLoader *plugin_loader,
                        g_warning ("failed to get sources: %s", error->message);
                }
                gtk_stack_set_visible_child_name (GTK_STACK (dialog->stack), "empty");
-               gtk_style_context_add_class (gtk_widget_get_style_context (dialog->label_header),
-                                            "dim-label");
                return;
        }
 
@@ -427,13 +453,9 @@ get_sources_cb (GsPluginLoader *plugin_loader,
        if (gs_app_list_length (list) == 0) {
                g_debug ("no sources to show");
                gtk_stack_set_visible_child_name (GTK_STACK (dialog->stack), "empty");
-               gtk_style_context_add_class (gtk_widget_get_style_context (dialog->label_header), 
"dim-label");
                return;
        }
 
-       gtk_style_context_remove_class (gtk_widget_get_style_context (dialog->label_header),
-                                       "dim-label");
-
        /* add each */
        gtk_stack_set_visible_child_name (GTK_STACK (dialog->stack), "sources");
        for (guint i = 0; i < gs_app_list_length (list); i++) {
@@ -679,10 +701,11 @@ gs_repos_dialog_class_init (GsReposDialogClass *klass)
        gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Software/gs-repos-dialog.ui");
 
        gtk_widget_class_bind_template_child (widget_class, GsReposDialog, status_empty);
-       gtk_widget_class_bind_template_child (widget_class, GsReposDialog, label_header);
        gtk_widget_class_bind_template_child (widget_class, GsReposDialog, content_page);
        gtk_widget_class_bind_template_child (widget_class, GsReposDialog, spinner);
        gtk_widget_class_bind_template_child (widget_class, GsReposDialog, stack);
+
+       gtk_widget_class_bind_template_callback (widget_class, key_press_event_cb);
 }
 
 GtkWidget *
@@ -691,7 +714,6 @@ gs_repos_dialog_new (GtkWindow *parent, GsPluginLoader *plugin_loader)
        GsReposDialog *dialog;
 
        dialog = g_object_new (GS_TYPE_REPOS_DIALOG,
-                              "use-header-bar", TRUE,
                               "transient-for", parent,
                               "modal", TRUE,
                               NULL);
diff --git a/src/gs-repos-dialog.h b/src/gs-repos-dialog.h
index ff82350c3..1349830e8 100644
--- a/src/gs-repos-dialog.h
+++ b/src/gs-repos-dialog.h
@@ -10,6 +10,7 @@
 #pragma once
 
 #include <gtk/gtk.h>
+#include <handy.h>
 
 #include "gnome-software-private.h"
 
@@ -17,7 +18,7 @@ G_BEGIN_DECLS
 
 #define GS_TYPE_REPOS_DIALOG (gs_repos_dialog_get_type ())
 
-G_DECLARE_FINAL_TYPE (GsReposDialog, gs_repos_dialog, GS, REPOS_DIALOG, GtkDialog)
+G_DECLARE_FINAL_TYPE (GsReposDialog, gs_repos_dialog, GS, REPOS_DIALOG, HdyWindow)
 
 GtkWidget      *gs_repos_dialog_new            (GtkWindow      *parent,
                                                 GsPluginLoader *plugin_loader);
diff --git a/src/gs-repos-dialog.ui b/src/gs-repos-dialog.ui
index 6857b7ade..c32dbb213 100644
--- a/src/gs-repos-dialog.ui
+++ b/src/gs-repos-dialog.ui
@@ -1,33 +1,29 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <requires lib="gtk+" version="3.10"/>
-  <template class="GsReposDialog" parent="GtkDialog">
-    <property name="title" translatable="yes">Software Repositories</property>
+  <template class="GsReposDialog" parent="HdyWindow">
     <property name="modal">True</property>
-    <property name="default_width">600</property>
-    <property name="default_height">600</property>
+    <property name="window_position">center</property>
     <property name="destroy_with_parent">True</property>
-    <property name="type_hint">dialog</property>
+    <property name="icon_name">dialog-information</property>
     <property name="skip_taskbar_hint">True</property>
-    <property name="use_header_bar">1</property>
-    <child internal-child="headerbar">
-      <object class="GtkHeaderBar">
-        <child type="title">
-          <object class="GtkLabel" id="label_header">
+    <property name="title" translatable="yes">Software Repositories</property>
+    <property name="type_hint">dialog</property>
+    <property name="default-width">640</property>
+    <property name="default-height">576</property>
+    <signal name="key-press-event" handler="key_press_event_cb" after="yes" swapped="no"/>
+
+    <child>
+      <object class="GtkBox">
+        <property name="visible">True</property>
+        <property name="orientation">vertical</property>
+        <child>
+          <object class="HdyHeaderBar">
+            <property name="show_close_button">True</property>
             <property name="visible">True</property>
-            <property name="label" translatable="yes">Software Repositories</property>
-            <property name="selectable">False</property>
-            <style>
-              <class name="title"/>
-            </style>
+            <property name="title" bind-source="GsReposDialog" bind-property="title" 
bind-flags="sync-create"/>
           </object>
         </child>
-      </object>
-    </child>
-    <child internal-child="vbox">
-      <object class="GtkBox" id="dialog-vbox1">
-        <property name="visible">True</property>
-        <property name="orientation">vertical</property>
         <child>
           <object class="GtkStack" id="stack">
             <property name="visible">True</property>
diff --git a/src/gs-shell.c b/src/gs-shell.c
index 36ae64542..acddd7669 100644
--- a/src/gs-shell.c
+++ b/src/gs-shell.c
@@ -2313,10 +2313,6 @@ gs_shell_show_sources (GsShell *shell)
 
        dialog = gs_repos_dialog_new (GTK_WINDOW (shell), shell->plugin_loader);
        gs_shell_modal_dialog_present (shell, GTK_WINDOW (dialog));
-
-       /* just destroy */
-       g_signal_connect_swapped (dialog, "response",
-                                 G_CALLBACK (gtk_widget_destroy), dialog);
 }
 
 void


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