[ghex/libadwaita] Port to libadwaita: Initial commit




commit a21c2c13057005152da10fea86de02d350d0b82b
Author: Logan Rathbone <poprocks gmail com>
Date:   Wed Jun 15 15:27:35 2022 -0400

    Port to libadwaita: Initial commit
    
    Next step is to port the custom widgets such as the GHex notebook tab to
    standard libadwaita equivalents.

 meson.build                       |   3 +-
 src/configuration.c               | 151 ++++----------------------------------
 src/configuration.h               |   6 +-
 src/ghex-application-window.c     |  37 +---------
 src/ghex-application-window.h     |   5 +-
 src/ghex-application-window.ui.in |  91 ++++++++++++-----------
 src/main.c                        |  12 +--
 src/meson.build                   |   3 +-
 src/preferences.c                 |   7 +-
 src/preferences.h                 |   1 +
 10 files changed, 85 insertions(+), 231 deletions(-)
---
diff --git a/meson.build b/meson.build
index 63d6c15..2f247a6 100644
--- a/meson.build
+++ b/meson.build
@@ -147,7 +147,8 @@ glib_ver = '>= 2.68.0'
 gmodule_dep = dependency('gmodule-2.0')
 glib_dep = dependency('glib-2.0', version: glib_ver)
 gio_dep = dependency('gio-2.0', version: glib_ver)
-gtk_dep = dependency('gtk4', version: '>= 4.0.0')
+gtk_dep = dependency('gtk4', version: '>= 4.4.0')
+adw_dep = dependency('libadwaita-1')
 
 configure_file(
   output: 'config.h',
diff --git a/src/configuration.c b/src/configuration.c
index 959853b..032fa86 100644
--- a/src/configuration.c
+++ b/src/configuration.c
@@ -45,9 +45,6 @@ char *data_font_name;
 guint shaded_box_size;
 gboolean show_offsets_column;
 int def_dark_mode;
-/* Will default to false here. We can't set it until we get a 'screen', so
- * we'll save calling get_sys_default_is_dark() until GHex launches. */
-gboolean sys_default_is_dark;
 
 static void
 offsets_column_changed_cb (GSettings   *settings,
@@ -72,147 +69,27 @@ dark_mode_changed_cb (GSettings   *settings,
                   const gchar *key,
                   gpointer     user_data)
 {
-    def_dark_mode = g_settings_get_enum (settings, key);
-}
-
-
-/* NOTE: for GTK 4.0 backwards compatibility, we manually define these
- * here. If we wish to add an additional dependency on
- * gsettings-desktop-schemas >= 42, this can be eliminated.
- */
-enum system_color_scheme {
-       SYSTEM_DEFAULT,
-       SYSTEM_PREFER_DARK,
-       SYSTEM_PREFER_LIGHT,
-};
-
-static gboolean
-try_dark_from_gsettings (void)
-{
-       int color_scheme;
-       GSettingsSchemaSource *source;
-       GSettingsSchema *schema;
-       char *schema_id = NULL;
-
-       source = g_settings_schema_source_get_default ();
-       schema = g_settings_schema_source_lookup (source, "org.freedesktop.appearance", TRUE);
-
-       if (schema && g_settings_schema_has_key (schema, "color-scheme"))
-       {
-               schema_id = "org.freedesktop.appearance";
-       }
-       else
-       {
-               g_clear_pointer (&schema, g_settings_schema_unref);
-               schema = g_settings_schema_source_lookup (source, "org.gnome.desktop.interface", TRUE);
-               if (schema && g_settings_schema_has_key (schema, "color-scheme"))
-               {
-                       schema_id = "org.gnome.desktop.interface";
-               }
-               g_clear_pointer (&schema, g_settings_schema_unref);
-       }
-
-       if (schema_id)
-       {
-               GSettings *set = g_settings_new (schema_id);
-               color_scheme = g_settings_get_enum (set, "color-scheme");
-               if (color_scheme == SYSTEM_PREFER_DARK)
-                       sys_default_is_dark = TRUE;
-               g_object_unref (set);
+       AdwStyleManager *manager = adw_style_manager_get_default ();
 
-               return TRUE;
-       }
-       return FALSE;
-}
+    def_dark_mode = g_settings_get_enum (settings, key);
 
-static gboolean
-try_dark_from_portal (void)
-{
-       int color_scheme;
-       char *color_scheme_str = NULL;
-       GDBusProxy *settings_portal = NULL;
-       GVariant *gvar = NULL, *gvar2 = NULL;
-       gboolean retval = FALSE;
-
-       settings_portal = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
-                       G_DBUS_PROXY_FLAGS_NONE,
-                       NULL,
-                       "org.freedesktop.portal.Desktop",
-                       "/org/freedesktop/portal/desktop",
-                       "org.freedesktop.portal.Settings",
-                       NULL,
-                       NULL);
-
-       if (! settings_portal)
-               goto out;
-
-       gvar = g_dbus_proxy_call_sync (settings_portal,
-                       "Read",
-                       g_variant_new ("(ss)", "org.freedesktop.appearance", "color-scheme"),
-                       G_DBUS_CALL_FLAGS_NONE,
-                       G_MAXINT,
-                       NULL,
-                       NULL);
-
-       if (gvar)
+       switch (def_dark_mode)
        {
-               g_variant_get (gvar, "(v)", &gvar2);
-               g_variant_get (g_variant_get_variant (gvar2), "u", &color_scheme);
-               retval = TRUE;
-               goto out;
-       }
-
-       gvar = g_dbus_proxy_call_sync (settings_portal,
-                       "Read",
-                       g_variant_new ("(ss)", "org.gnome.desktop.interface", "color-scheme"),
-                       G_DBUS_CALL_FLAGS_NONE,
-                       G_MAXINT,
-                       NULL,
-                       NULL);
+               case DARK_MODE_OFF:
+                       adw_style_manager_set_color_scheme (manager, ADW_COLOR_SCHEME_FORCE_LIGHT);
+                       break;
 
-       if (! gvar)
-               goto out;
+               case DARK_MODE_ON:
+                       adw_style_manager_set_color_scheme (manager, ADW_COLOR_SCHEME_FORCE_DARK);
+                       break;
 
-       g_variant_get (gvar, "(v)", &gvar2);
-       g_variant_get (g_variant_get_variant (gvar2), "s", &color_scheme_str);
+               case DARK_MODE_SYSTEM:
+                       adw_style_manager_set_color_scheme (manager, ADW_COLOR_SCHEME_DEFAULT);
+                       break;
 
-       if (color_scheme_str)
-       {
-               retval = TRUE;
-               if (g_strcmp0 (color_scheme_str, "prefer-dark") == 0)
-                       color_scheme = SYSTEM_PREFER_DARK;
+               default:
+                       break;
        }
-
-out:
-       if (color_scheme == SYSTEM_PREFER_DARK)
-               sys_default_is_dark = TRUE;
-
-       g_clear_object (&settings_portal);
-       g_clear_pointer (&gvar, g_variant_unref);
-       g_clear_pointer (&gvar2, g_variant_unref);
-
-       return retval;
-}
-
-void
-get_sys_default_is_dark (void)
-{
-       GtkSettings *gtk_settings;
-       gboolean got_dark_pref;
-
-       got_dark_pref = try_dark_from_portal ();
-       if (got_dark_pref)
-               return;
-
-       got_dark_pref = try_dark_from_gsettings ();
-       if (got_dark_pref)
-               return;
-
-       /* If all else fails, try to fetch from GtkSettings. */
-       gtk_settings = gtk_settings_get_default ();
-       g_object_get (gtk_settings,
-                       "gtk-application-prefer-dark-theme", &sys_default_is_dark,
-                       NULL);
 }
 
 static void
diff --git a/src/configuration.h b/src/configuration.h
index af17e1a..ffea2a8 100644
--- a/src/configuration.h
+++ b/src/configuration.h
@@ -34,6 +34,7 @@
 #define GHEX_CONFIGURATION_H
 
 #include <gtk/gtk.h>
+#include <adwaita.h>
 
 G_BEGIN_DECLS
 
@@ -62,7 +63,6 @@ extern gboolean               show_offsets_column;
 extern guint           shaded_box_size;
 extern int                     def_group_type;
 extern int                     def_dark_mode;
-extern gboolean                sys_default_is_dark;
 
 extern GSettings       *settings;
 extern GtkCssProvider *provider;
@@ -70,10 +70,6 @@ extern GtkCssProvider *provider;
 /* Initializes the gsettings client */
 void ghex_init_configuration (void);
 
-/* Cache the system default of prefer-dark-theme as gtk does not do this for
- * us. */
-void get_sys_default_is_dark (void);
-
 G_END_DECLS
 
 #endif /* GHEX_CONFIGURATION_H */
diff --git a/src/ghex-application-window.c b/src/ghex-application-window.c
index 65e591a..73572e4 100644
--- a/src/ghex-application-window.c
+++ b/src/ghex-application-window.c
@@ -48,7 +48,7 @@ static gpointer extra_user_data;
 
 struct _GHexApplicationWindow
 {
-       GtkApplicationWindow parent_instance;
+       AdwApplicationWindow parent_instance;
 
        HexWidget *gh;
        HexDialog *dialog;
@@ -120,8 +120,7 @@ static const char *main_actions[] = {
        NULL                            /* last action */
 };
 
-G_DEFINE_TYPE (GHexApplicationWindow, ghex_application_window,
-               GTK_TYPE_APPLICATION_WINDOW)
+G_DEFINE_TYPE (GHexApplicationWindow, ghex_application_window, ADW_TYPE_APPLICATION_WINDOW)
 
 /* ---- */
 
@@ -196,27 +195,6 @@ set_gtkhex_group_type_from_settings (HexWidget *gh)
        hex_widget_set_group_type (gh, def_group_type);
 }
 
-static void
-set_dark_mode_from_settings (GHexApplicationWindow *self)
-{
-       GtkSettings *gtk_settings;
-
-       gtk_settings = gtk_settings_get_default ();
-
-       if (def_dark_mode == DARK_MODE_SYSTEM) {
-               g_object_set (G_OBJECT(gtk_settings),
-                               "gtk-application-prefer-dark-theme",
-                               sys_default_is_dark,
-                               NULL);
-       } else {
-               g_object_set (G_OBJECT(gtk_settings),
-                               "gtk-application-prefer-dark-theme",
-                               def_dark_mode == DARK_MODE_ON ? TRUE : FALSE,
-                               NULL);
-       }
-}
-       
-
 static void
 settings_font_changed_cb (GSettings   *settings,
                const gchar     *key,
@@ -1542,12 +1520,6 @@ ghex_application_window_init (GHexApplicationWindow *self)
 
        gtk_widget_init_template (widget);
 
-       /* Cache system default of prefer-dark-mode; gtk does not do this. This
-        * is run here as it cannot be done until we have a 'screen'. */
-       get_sys_default_is_dark ();
-       /* Do dark mode if requested */
-       set_dark_mode_from_settings (self);
-
        /* Setup conversions box and pane */
        self->dialog = hex_dialog_new ();
        self->dialog_widget = hex_dialog_getview (self->dialog);
@@ -1585,9 +1557,6 @@ ghex_application_window_init (GHexApplicationWindow *self)
     g_signal_connect (settings, "changed::" GHEX_PREF_GROUP,
                       G_CALLBACK (settings_group_type_changed_cb), self);
 
-    g_signal_connect_swapped (settings, "changed::" GHEX_PREF_DARK_MODE,
-                      G_CALLBACK (set_dark_mode_from_settings), self);
-
        /* Actions - SETTINGS */
 
        /* for the 'group data by' stuff. There isn't a function to do this from
@@ -1945,7 +1914,7 @@ ghex_application_window_class_init(GHexApplicationWindowClass *klass)
 }
 
 GtkWidget *
-ghex_application_window_new (GtkApplication *app)
+ghex_application_window_new (AdwApplication *app)
 {
        return g_object_new (GHEX_TYPE_APPLICATION_WINDOW,
                        "application", app,
diff --git a/src/ghex-application-window.h b/src/ghex-application-window.h
index ff27ee2..5be91ea 100644
--- a/src/ghex-application-window.h
+++ b/src/ghex-application-window.h
@@ -26,6 +26,7 @@
 #define GHEX_APPLICATION_WINDOW_H
 
 #include <gtk/gtk.h>
+#include <adwaita.h>
 #include <glib/gi18n.h>
 
 #include "gtkhex.h"
@@ -45,9 +46,9 @@ G_BEGIN_DECLS
 #define GHEX_TYPE_APPLICATION_WINDOW (ghex_application_window_get_type ())
 G_DECLARE_FINAL_TYPE (GHexApplicationWindow, ghex_application_window,
                                GHEX, APPLICATION_WINDOW,
-                               GtkApplicationWindow)
+                               AdwApplicationWindow)
 
-GtkWidget *    ghex_application_window_new (GtkApplication *app);
+GtkWidget *    ghex_application_window_new (AdwApplication *app);
 void           ghex_application_window_add_hex (GHexApplicationWindow *self,
                                HexWidget *gh);
 void           ghex_application_window_set_hex (GHexApplicationWindow *self,
diff --git a/src/ghex-application-window.ui.in b/src/ghex-application-window.ui.in
index 6d8eef8..abaf522 100644
--- a/src/ghex-application-window.ui.in
+++ b/src/ghex-application-window.ui.in
@@ -145,59 +145,66 @@
                </section>
        </menu>
 
-       <template class="GHexApplicationWindow" parent="GtkApplicationWindow">
+       <template class="GHexApplicationWindow" parent="AdwApplicationWindow">
                <property name="title" translatable="yes">GHex</property>
                <property name="default-width">800</property>
                <property name="default-height">600</property>
                <property name="icon-name">@app_id@</property>
 
-               <child type="titlebar">
-                       <object class="GtkHeaderBar" id="headerbar">
-                               <property name="show-title-buttons">true</property>
+               <child> <!-- main vert gtkbox -->
+                       <object class="GtkBox"> <!-- main vert gtkbox -->
+                               <property name="orientation">vertical</property>
+
 
                                <child>
-                                       <object class="GtkButton">
-                                               <property name="valign">center</property>
-                                               <property name="label">Open</property>
-                                               <property name="action-name">ghex.open</property>
-                                               <property name="tooltip-text" translatable="yes">Open a file 
for hex editing</property>
-                                       </object>
-                               </child>
+                                       <object class="GtkHeaderBar" id="headerbar">
+                                               <property name="show-title-buttons">true</property>
+
+                                               <child>
+                                                       <object class="GtkButton">
+                                                               <property name="valign">center</property>
+                                                               <property name="label">Open</property>
+                                                               <property 
name="action-name">ghex.open</property>
+                                                               <property name="tooltip-text" 
translatable="yes">Open a file for hex editing</property>
+                                                       </object>
+                                               </child>
+
+                                               <child type="end">
+                                                       <object class="GtkMenuButton" 
id="hamburger_menu_button">
+                                                               <property name="valign">center</property>
+                                                               <property 
name="menu-model">hamburger_menu</property>
+                                                               <property 
name="icon-name">open-menu-symbolic</property>
+                                                               <accessibility>
+                                                                       <property name="label" 
translatable="yes">Main menu</property>
+                                                               </accessibility>
+                                                       </object>
+                                               </child>
+
+                                               <child type="end">
+                                                       <object class="GtkToggleButton" id="find_button">
+                                                               <property name="valign">center</property>
+                                                               <property 
name="icon-name">edit-find-symbolic</property>
+                                                               <property 
name="action-name">ghex.find</property>
+                                                               <property name="tooltip-text" 
translatable="yes">Find a string in the hex document</property>
+                                                       </object>
+                                               </child>
+
+                                               <child type="end">
+                                                       <object class="GtkButton">
+                                                               <property name="valign">center</property>
+                                                               <property 
name="icon-name">document-save-symbolic</property>
+                                                               <property 
name="action-name">ghex.save</property>
+                                                               <property name="tooltip-text" 
translatable="yes">Save document</property>
+                                                       </object>
+                                               </child>
+                                       </object> <!-- headerbar -->
+                               </child> <!-- titlebar -->
+
+
 
-                               <child type="end">
-                                       <object class="GtkMenuButton" id="hamburger_menu_button">
-                                               <property name="valign">center</property>
-                                               <property name="menu-model">hamburger_menu</property>
-                                               <property name="icon-name">open-menu-symbolic</property>
-                                               <accessibility>
-                                                       <property name="label" translatable="yes">Main 
menu</property>
-                                               </accessibility>
-                                       </object>
-                               </child>
 
-                               <child type="end">
-                                       <object class="GtkToggleButton" id="find_button">
-                                               <property name="valign">center</property>
-                                               <property name="icon-name">edit-find-symbolic</property>
-                                               <property name="action-name">ghex.find</property>
-                                               <property name="tooltip-text" translatable="yes">Find a 
string in the hex document</property>
-                                       </object>
-                               </child>
 
-                               <child type="end">
-                                       <object class="GtkButton">
-                                               <property name="valign">center</property>
-                                               <property name="icon-name">document-save-symbolic</property>
-                                               <property name="action-name">ghex.save</property>
-                                               <property name="tooltip-text" translatable="yes">Save 
document</property>
-                                       </object>
-                               </child>
-                       </object> <!-- headerbar -->
-               </child> <!-- titlebar -->
 
-               <child> <!-- main vert gtkbox -->
-                       <object class="GtkBox"> <!-- main vert gtkbox -->
-                               <property name="orientation">vertical</property>
 
                                <child> <!-- child_box for notebook & content area -->
                                        <object class="GtkBox" id="child_box">
diff --git a/src/main.c b/src/main.c
index 14750b9..ec6539c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -86,7 +86,7 @@ ghex_locale_dir (void)
 }
 
 static void
-do_app_window (GtkApplication *app)
+do_app_window (AdwApplication *app)
 {
        if (! window)
                window = GTK_WINDOW(ghex_application_window_new (app));
@@ -110,7 +110,7 @@ handle_local_options (GApplication *application,
 }
 
 static void
-activate (GtkApplication *app,
+activate (AdwApplication *app,
        gpointer user_data)
 {
        /* WORKAROUND https://gitlab.gnome.org/GNOME/gtk/-/issues/4880 */
@@ -119,7 +119,7 @@ activate (GtkApplication *app,
 
        do_app_window (app);
 
-       gtk_window_set_application (window, app);
+       gtk_window_set_application (window, GTK_APPLICATION(app));
        gtk_window_present (window);
 }
 
@@ -132,7 +132,7 @@ open (GApplication *application,
 {
        GHexApplicationWindow *app_win;
 
-       activate (GTK_APPLICATION(application), NULL);
+       activate (ADW_APPLICATION(application), NULL);
        app_win = GHEX_APPLICATION_WINDOW(window);
 
        for (int i = 0; i < n_files; ++i)
@@ -142,7 +142,7 @@ open (GApplication *application,
 int
 main (int argc, char *argv[])
 {
-       GtkApplication *app;
+       AdwApplication *app;
        char *locale_dir;
        int status;
 
@@ -158,7 +158,7 @@ main (int argc, char *argv[])
 
        ghex_init_configuration ();
 
-       app = gtk_application_new (APP_ID, G_APPLICATION_HANDLES_OPEN);
+       app = adw_application_new (APP_ID, G_APPLICATION_HANDLES_OPEN);
 
        g_application_add_main_option_entries (G_APPLICATION(app), entries);
 
diff --git a/src/meson.build b/src/meson.build
index 80a37b4..db275a2 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -137,7 +137,8 @@ ghex = executable(
   meson.project_name(),
   ghex_sources + res,
   include_directories: ghex_root_dir,
-  dependencies: libgtkhex_dep,
+  dependencies: [adw_dep, libgtkhex_dep],
+  #  dependencies: [libgtkhex_dep, adw_dep],
   c_args: ghex_c_args,
   install: true
 )
diff --git a/src/preferences.c b/src/preferences.c
index 29f188a..7e4a4ef 100644
--- a/src/preferences.c
+++ b/src/preferences.c
@@ -373,7 +373,7 @@ setup_signals (void)
 static void
 grab_widget_values_from_settings (void)
 {
-       GtkSettings *gtk_settings;
+       AdwStyleManager *manager = adw_style_manager_get_default ();
 
        /* font_button */
        gtk_font_chooser_set_font (GTK_FONT_CHOOSER(font_button),
@@ -388,8 +388,9 @@ grab_widget_values_from_settings (void)
                                TRUE);
                gtk_widget_set_sensitive (dark_mode_switch, FALSE);
                gtk_switch_set_state (GTK_SWITCH(dark_mode_switch),
-                               sys_default_is_dark);
-       } else
+                               adw_style_manager_get_dark (manager));
+       }
+       else
        {
                gtk_check_button_set_active (GTK_CHECK_BUTTON(system_default_chkbtn),
                                FALSE);
diff --git a/src/preferences.h b/src/preferences.h
index 8510a64..aa80418 100644
--- a/src/preferences.h
+++ b/src/preferences.h
@@ -34,6 +34,7 @@
 #define GHEX_PREFERENCES_H
 
 #include <gtk/gtk.h>
+#include <adwaita.h>
 #include <glib/gi18n.h>
 #include <string.h>
 


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