[gnome-robots/gtk4] Migrate to GTK4




commit b52c318c6cd41c08150a57d69475fef9c57a72ed
Author: Andrey Kutejko <andy128k gmail com>
Date:   Sun Feb 13 13:51:33 2022 +0100

    Migrate to GTK4

 meson.build                |  5 ++---
 src/application.vala       | 14 ++++--------
 src/assets.vala            |  3 +--
 src/controls.vala          | 16 +++++++++-----
 src/cursors.vala           | 25 +++++++++++----------
 src/game-area.vala         | 20 ++++++++---------
 src/graphics.vala          | 20 ++++++++---------
 src/meson.build            |  1 -
 src/properties-dialog.vala |  6 ++---
 src/window-size.vala       | 37 +++++++++++--------------------
 src/window.vala            | 55 +++++++++++++++++++++++++++++++---------------
 11 files changed, 103 insertions(+), 99 deletions(-)
---
diff --git a/meson.build b/meson.build
index db8b9b1..eefc77d 100644
--- a/meson.build
+++ b/meson.build
@@ -16,10 +16,9 @@ add_project_arguments([
 gee_dependency = dependency('gee-0.8')
 gio_dependency = dependency('gio-2.0', version: '>= 2.32')
 glib_dependency = dependency('glib-2.0', version: '>= 2.32')
-gnome_games_dependency = dependency('libgnome-games-support-1', version: '>= 1.7.1')
+gnome_games_dependency = dependency('libgnome-games-support-2', version: '>= 2.0')
 gsound_dependency = dependency('gsound', version: '>= 1.0.3')
-gtk_dependency = dependency('gtk+-3.0', version: '>= 3.24.0')
-gdk_dependency = dependency('gdk-3.0', version: '>= 3.24.0')
+gtk_dependency = dependency('gtk4', version: '>= 4.2.0')
 rsvg_dependency = dependency('librsvg-2.0', version: '>= 2.36.2')
 
 bindir      = join_paths(get_option('prefix'), get_option('bindir'))
diff --git a/src/application.vala b/src/application.vala
index fa385cb..62dcad4 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -18,6 +18,7 @@
  */
 
 using Gtk;
+using Gdk;
 
 class RobotsApplication : Gtk.Application {
 
@@ -72,7 +73,7 @@ class RobotsApplication : Gtk.Application {
     protected override void activate () {
         var window = get_active_window () as RobotsWindow;
         if (window != null) {
-            window.present_with_time (get_current_event_time ());
+            window.present_with_time (CURRENT_TIME);
             return;
         }
 
@@ -87,8 +88,7 @@ class RobotsApplication : Gtk.Application {
             // TODO message box
             quit ();
         }
-
-        window.show_all ();
+        window.show ();
     }
 
     private void new_game_cb () {
@@ -130,13 +130,7 @@ class RobotsApplication : Gtk.Application {
     }
 
     private void help_cb () {
-        try {
-            show_uri_on_window (get_active_window (),
-                                "help:gnome-robots",
-                                get_current_event_time ());
-        } catch (Error error) {
-            warning ("Failed to show help: %s", error.message);
-        }
+        show_uri (get_active_window (), "help:gnome-robots", CURRENT_TIME);
     }
 
     private void about_cb () {
diff --git a/src/assets.vala b/src/assets.vala
index b3ecf03..496978c 100644
--- a/src/assets.vala
+++ b/src/assets.vala
@@ -52,8 +52,7 @@ public class DirectoryAssets : Object, Assets {
         _splat_bubble = new Bubble.from_file (
             Path.build_filename (directory, "pixmaps", "splat.png"));
 
-        var display = Gdk.Display.get_default ();
-        _cursors = make_cursors_for_display (display);
+        _cursors = make_cursors ();
     }
 }
 
diff --git a/src/controls.vala b/src/controls.vala
index 3c0c2b6..f4199f9 100644
--- a/src/controls.vala
+++ b/src/controls.vala
@@ -19,7 +19,7 @@
 
 using Gtk;
 
-public class GamesControlsList : Bin {
+public class GamesControlsList : Widget {
 
     private Gtk.ListStore store;
     private TreeView view;
@@ -33,6 +33,11 @@ public class GamesControlsList : Bin {
         COUNT
     }
 
+    construct {
+        var layout = new Gtk.BinLayout ();
+        this.set_layout_manager (layout);
+    }
+
     public GamesControlsList(Properties properties) {
         store = new Gtk.ListStore (Column.COUNT,
                                    Type.STRING,
@@ -72,13 +77,12 @@ public class GamesControlsList : Bin {
 
         view.append_column (column2);
 
-        var sw = new ScrolledWindow (null, null);
+        var sw = new ScrolledWindow ();
         sw.hscrollbar_policy = PolicyType.NEVER;
         sw.vscrollbar_policy = PolicyType.AUTOMATIC;
-        sw.shadow_type = ShadowType.IN;
-        sw.add (view);
-
-        add (sw);
+        sw.has_frame = true;
+        sw.set_child (view);
+        sw.set_parent (this);
 
         this.properties = properties;
         properties.changed.connect (properties_changed_cb);
diff --git a/src/cursors.vala b/src/cursors.vala
index f90d112..4e8e17f 100644
--- a/src/cursors.vala
+++ b/src/cursors.vala
@@ -19,23 +19,24 @@
 
 using Gdk;
 
-public Array<Cursor> make_cursors_for_display (Display display) {
+public Array<Cursor> make_cursors () {
     Array<Cursor> cursors = new Array<Cursor> ();
-    cursors.append_val (make_cursor (display, cursor_up_left, 3, 3));
-    cursors.append_val (make_cursor (display, cursor_up, 10, 3));
-    cursors.append_val (make_cursor (display, cursor_up_right, 17, 3));
-    cursors.append_val (make_cursor (display, cursor_left, 3, 10));
-    cursors.append_val (make_cursor (display, cursor_hold, 10, 10));
-    cursors.append_val (make_cursor (display, cursor_right, 17, 10));
-    cursors.append_val (make_cursor (display, cursor_down_left, 3, 17));
-    cursors.append_val (make_cursor (display, cursor_down, 10, 17));
-    cursors.append_val (make_cursor (display, cursor_down_right, 17, 17));
+    cursors.append_val (make_cursor (cursor_up_left, 3, 3));
+    cursors.append_val (make_cursor (cursor_up, 10, 3));
+    cursors.append_val (make_cursor (cursor_up_right, 17, 3));
+    cursors.append_val (make_cursor (cursor_left, 3, 10));
+    cursors.append_val (make_cursor (cursor_hold, 10, 10));
+    cursors.append_val (make_cursor (cursor_right, 17, 10));
+    cursors.append_val (make_cursor (cursor_down_left, 3, 17));
+    cursors.append_val (make_cursor (cursor_down, 10, 17));
+    cursors.append_val (make_cursor (cursor_down_right, 17, 17));
     return cursors;
 }
 
-private Cursor make_cursor (Display display, string[] xpm, int hsx, int hsy) {
+private Cursor make_cursor (string[] xpm, int hsx, int hsy) {
     var pixbuf = new Pixbuf.from_xpm_data (xpm);
-    return new Cursor.from_pixbuf (display, pixbuf, hsx, hsy);
+    var texture = Texture.for_pixbuf (pixbuf);
+    return new Cursor.from_texture (texture, hsx, hsy, null);
 }
 
 const string cursor_up_left[] = {
diff --git a/src/game-area.vala b/src/game-area.vala
index 7127267..c64ea66 100644
--- a/src/game-area.vala
+++ b/src/game-area.vala
@@ -27,7 +27,7 @@ public class GameArea : DrawingArea {
     const int MINIMUM_TILE_HEIGHT = 8;
     const int ANIMATION_DELAY = 100;
 
-    private GestureMultiPress click_controller;
+    private GestureClick click_controller;
     private EventControllerMotion motion_controller;
     private Game game;
     private GameConfigs game_configs;
@@ -95,14 +95,15 @@ public class GameArea : DrawingArea {
 
         game.config = game_configs.find_by_name (properties.selected_config) ?? game_configs[0];
 
-        add_events (Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK | 
Gdk.EventMask.POINTER_MOTION_MASK);
-        draw.connect ((cr) => draw_cb (cr));
+        set_draw_func ((_area, cr, width, height) => draw_cb (cr, width, height));
 
-        click_controller = new GestureMultiPress (this);
+        click_controller = new GestureClick ();
         click_controller.pressed.connect ((n_pressed, x, y) => mouse_cb (n_pressed, x, y));
+        add_controller (click_controller);
 
-        motion_controller = new EventControllerMotion (this);
+        motion_controller = new EventControllerMotion ();
         motion_controller.motion.connect ((x, y) => move_cb (x, y));
+        add_controller (motion_controller);
 
         set_size_request (MINIMUM_TILE_WIDTH * game.arena.width,
                           MINIMUM_TILE_HEIGHT * game.arena.height);
@@ -158,7 +159,7 @@ public class GameArea : DrawingArea {
         };
     }
 
-    private bool draw_cb (Context cr) {
+    private bool draw_cb (Context cr, int _width, int _height) {
         Size tile_size = tile_size ();
 
         for (int j = 0; j < game.arena.height; j++) {
@@ -259,16 +260,15 @@ public class GameArea : DrawingArea {
     }
 
     private void move_cb (double x, double y) {
-        var window = get_window ();
         if (game.state != Game.State.PLAYING) {
-            window.set_cursor (null);
+            set_cursor (null);
         } else {
             int dx, dy;
             get_dir (x, y, out dx, out dy);
 
             var cursor_index = 3 * dy + dx + 4;
             var cursor = assets.cursors.index(cursor_index);
-            window.set_cursor (cursor);
+            set_cursor (cursor);
         }
     }
 
@@ -415,7 +415,7 @@ public class GameArea : DrawingArea {
      * Displays a modal dialog box with a given message
      **/
     private void message_box (string msg) {
-        var window = get_toplevel () as Gtk.Window;
+        var window = get_root () as Gtk.Window;
         if (window != null) {
             var dlg = new Gtk.MessageDialog (window,
                                              Gtk.DialogFlags.MODAL,
diff --git a/src/graphics.vala b/src/graphics.vala
index 828156b..83dd8e3 100644
--- a/src/graphics.vala
+++ b/src/graphics.vala
@@ -41,21 +41,21 @@ public RGBA calculate_light_color (RGBA color) {
     double brightness = color.red + color.green + color.blue;
     if (brightness > (1.0 / 1.1)) {
         /* Darken light colours. */
-        light.red = 0.9 * color.red;
-        light.green = 0.9 * color.green;
-        light.blue = 0.9 * color.blue;
+        light.red = 0.9f * color.red;
+        light.green = 0.9f * color.green;
+        light.blue = 0.9f * color.blue;
     } else if (brightness > 0.04) {
         /* Lighten darker colours. */
-        light.red = 1.1 * color.red;
-        light.green = 1.1 * color.green;
-        light.blue = 1.1 * color.blue;
+        light.red = 1.1f * color.red;
+        light.green = 1.1f * color.green;
+        light.blue = 1.1f * color.blue;
     } else {
         /* Very dark colours, add rather than multiply. */
-        light.red = 0.04 + color.red;
-        light.green = 0.04 + color.green;
-        light.blue = 0.04 + color.blue;
+        light.red = 0.04f + color.red;
+        light.green = 0.04f + color.green;
+        light.blue = 0.04f + color.blue;
     }
-    light.alpha = 1.0;
+    light.alpha = 1.0f;
     return light;
 }
 
diff --git a/src/meson.build b/src/meson.build
index 1787353..6e341da 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -52,7 +52,6 @@ executable(
         gnome_games_dependency,
         gsound_dependency,
         gtk_dependency,
-        gdk_dependency,
         rsvg_dependency
     ],
     install_dir: get_option('bindir'),
diff --git a/src/properties-dialog.vala b/src/properties-dialog.vala
index 501aea9..5415576 100644
--- a/src/properties-dialog.vala
+++ b/src/properties-dialog.vala
@@ -76,7 +76,7 @@ public class PropertiesDialog : Dialog {
         notebook.margin_bottom = 10;
         notebook.margin_start = 10;
         notebook.margin_end = 10;
-        get_content_area ().pack_start (notebook, true, true, 0);
+        get_content_area ().append (notebook);
 
         /* The configuration page */
         var cpage = form_grid ();
@@ -161,7 +161,7 @@ public class PropertiesDialog : Dialog {
 
         var dbut = new Button.with_mnemonic (_("_Restore Defaults"));
         dbut.clicked.connect (reset_keys);
-        hbox.pack_start (dbut, false, false, 0);
+        hbox.append (dbut);
 
         label = new Label.with_mnemonic (_("Keyboard"));
         notebook.append_page (kpage, label);
@@ -190,7 +190,7 @@ public class PropertiesDialog : Dialog {
                                         themes,
                                         properties);
         dlg.response.connect (() => dlg.destroy ());
-        dlg.show_all ();
+        dlg.present ();
     }
 }
 
diff --git a/src/window-size.vala b/src/window-size.vala
index 8e259c6..4d7fede 100644
--- a/src/window-size.vala
+++ b/src/window-size.vala
@@ -48,31 +48,20 @@ public class WindowSizeSettings : Object, WindowSize {
     }
 }
 
-private bool window_configure_event_cb (Gtk.Window window,
-                                        WindowSize size
-) {
-    if (!size.is_maximized) {
-        int width, height;
-        window.get_size (out width, out height);
-        size.width = width;
-        size.height = height;
-    }
-    return false;
-}
-
-private bool window_state_event_cb (Gtk.Window window,
-                                    WindowSize size,
-                                    Gdk.EventWindowState event
-) {
-    if ((event.changed_mask & Gdk.WindowState.MAXIMIZED) != 0) {
-        size.is_maximized = (event.new_window_state & Gdk.WindowState.MAXIMIZED) != 0;
-    }
-    return false;
-}
-
 public void remember_window_size (Gtk.Window window, WindowSize size) {
-    window.configure_event.connect (() => window_configure_event_cb (window, size));
-    window.window_state_event.connect (event => window_state_event_cb (window, size, event));
+    window.notify["default-width"].connect (() => {
+        if (!window.maximized) {
+            size.width = window.default_width;
+        }
+    });
+    window.notify["default-height"].connect (() => {
+        if (!window.maximized) {
+            size.height = window.default_height;
+        }
+    });
+    window.notify["maximized"].connect (() => {
+        size.is_maximized = window.maximized;
+    });
     window.set_default_size (size.width, size.height);
     if (size.is_maximized) {
         window.maximize ();
diff --git a/src/window.vala b/src/window.vala
index 9e0b2cc..80cdc87 100644
--- a/src/window.vala
+++ b/src/window.vala
@@ -22,6 +22,7 @@ using Gtk;
 public class RobotsWindow : ApplicationWindow {
 
     private HeaderBar headerbar;
+    private Label header_subtitle;
     private Label safe_teleports_label;
     private GameArea game_area;
     private EventControllerKey key_controller;
@@ -38,17 +39,28 @@ public class RobotsWindow : ApplicationWindow {
         remember_window_size (this, new WindowSizeSettings ("org.gnome.Robots"));
         this.properties = properties;
 
+        var header_title = new Label.with_mnemonic (_("Robots"));
+        header_title.single_line_mode = true;
+        header_title.get_style_context ().add_class ("title");
+
+        header_subtitle = new Label.with_mnemonic ("");
+        header_subtitle.single_line_mode = true;
+        header_subtitle.get_style_context ().add_class ("subtitle");
+
+        var header_box = new Box (Orientation.VERTICAL, 0);
+        header_box.valign = Align.CENTER;
+        header_box.append (header_title);
+        header_box.append (header_subtitle);
+
         headerbar = new HeaderBar ();
-        headerbar.set_title (_("Robots"));
-        headerbar.set_show_close_button (true);
+        headerbar.set_title_widget (header_box);
+        headerbar.show_title_buttons = true;
         set_titlebar (headerbar);
 
         var appmenu = app.get_menu_by_id ("primary-menu");
         var menu_button = new MenuButton ();
-        var icon = new Image.from_icon_name ("open-menu-symbolic", IconSize.BUTTON);
-        menu_button.set_image (icon);
+        menu_button.set_icon_name ("open-menu-symbolic");
         menu_button.set_menu_model (appmenu);
-        menu_button.show ();
         headerbar.pack_end (menu_button);
 
         GLib.ActionEntry[] win_entries = {
@@ -67,18 +79,22 @@ public class RobotsWindow : ApplicationWindow {
         game_area.updated.connect (game => update_game_status (game));
 
         var gridframe = new Games.GridFrame (game.width, game.height);
-        gridframe.add (game_area);
+        gridframe.child = game_area;
 
         var hbox = button_box ();
 
         var vbox = new Box (Orientation.VERTICAL, 0);
-        vbox.pack_start (gridframe, true, true, 0);
-        vbox.pack_start (hbox, false, false, 0);
+        gridframe.vexpand = true;
+        gridframe.hexpand = true;
+        vbox.append (gridframe);
+        hbox.hexpand = true;
+        vbox.append (hbox);
 
-        add (vbox);
+        set_child (vbox);
 
-        key_controller = new EventControllerKey (this);
+        key_controller = new EventControllerKey ();
         key_controller.key_pressed.connect (keyboard_cb);
+        ((Widget) this).add_controller (key_controller);
 
         highscores = new RobotsScoresContext (this);
         game_area.add_score.connect ((game_type, score) => {
@@ -98,10 +114,11 @@ public class RobotsWindow : ApplicationWindow {
             label.margin_top = 15;
             label.margin_bottom = 15;
             var button = new Button ();
-            button.add (label);
+            button.set_child (label);
             button.set_action_name ("win.random-teleport");
             size_group.add_widget (button);
-            hbox.pack_start (button, true, true, 0);
+            button.hexpand = true;
+            hbox.append (button);
         }
 
         {
@@ -110,10 +127,11 @@ public class RobotsWindow : ApplicationWindow {
             safe_teleports_label.margin_top = 15;
             safe_teleports_label.margin_bottom = 15;
             var button = new Button ();
-            button.add (safe_teleports_label);
+            button.set_child (safe_teleports_label);
             button.set_action_name ("win.safe-teleport");
             size_group.add_widget (button);
-            hbox.pack_start (button, true, true, 0);
+            button.hexpand = true;
+            hbox.append (button);
         }
 
         {
@@ -121,17 +139,18 @@ public class RobotsWindow : ApplicationWindow {
             label.margin_top = 15;
             label.margin_bottom = 15;
             var button = new Button ();
-            button.add (label);
+            button.set_child (label);
             button.set_action_name ("win.wait");
             size_group.add_widget (button);
-            hbox.pack_start (button, true, true, 0);
+            button.hexpand = true;
+            hbox.append (button);
         }
 
         return hbox;
     }
 
     private void update_game_status (Game game) {
-        headerbar.set_subtitle (
+        header_subtitle.set_label (
             _("Level: %d\tScore: %d").printf (game.status.current_level, game.status.score));
 
         /* Second line of safe teleports button label. %d is the number of teleports remaining. */
@@ -169,7 +188,7 @@ public class RobotsWindow : ApplicationWindow {
          * if N is used as a key, then Ctrl-N is never picked up. The cleaner
          * option, making the signal a connect_after signal skims the arrow keys
          * before we can get to them which is a bigger problem. */
-        if ((state & (Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.MOD1_MASK)) != 0) {
+        if ((state & (Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.ALT_MASK)) != 0) {
             return false;
         }
 


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