[gnome-games] display-view: Make run and quit async



commit eceaea0ef09bb501e297a912e76209920221337f
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Fri Oct 16 20:46:52 2020 +0500

    display-view: Make run and quit async
    
    Avoid another gtk_dialog_run(), two to go.

 src/ui/application-window.vala | 43 ++++++++++++++++++++++++++++++------------
 src/ui/application.vala        | 13 ++++++++++---
 src/ui/display-view.vala       | 24 ++++++++++++++++-------
 3 files changed, 58 insertions(+), 22 deletions(-)
---
diff --git a/src/ui/application-window.vala b/src/ui/application-window.vala
index 779c0d65..96adb05c 100644
--- a/src/ui/application-window.vala
+++ b/src/ui/application-window.vala
@@ -60,6 +60,8 @@ private class Games.ApplicationWindow : Hdy.ApplicationWindow {
        public GameModel game_model { get; construct; }
        public CollectionModel collection_model { get; construct; }
 
+       private bool confirm_exit;
+
        public ApplicationWindow (Application application, GameModel game_model, CollectionModel 
collection_model) {
                Object (application: application, game_model: game_model, collection_model: collection_model);
 
@@ -105,23 +107,23 @@ private class Games.ApplicationWindow : Hdy.ApplicationWindow {
                collection_view.show_error (error_message);
        }
 
-       public void run_game (Game game) {
+       public async void run_game (Game game) {
                if (current_view != collection_view)
                        return;
 
                current_view = display_view;
-               display_view.run_game (game);
+               yield display_view.run_game (game);
 
                inhibit (Gtk.ApplicationInhibitFlags.IDLE | Gtk.ApplicationInhibitFlags.LOGOUT);
        }
 
-       public bool quit_game () {
+       public async bool quit_game () {
                // If the window have been deleted/hidden we probably don't want to
                // prompt the user.
                if (!visible)
                        return true;
 
-               return display_view.quit_game ();
+               return yield display_view.quit_game ();
        }
 
        public override void size_allocate (Gtk.Allocation allocation) {
@@ -133,7 +135,19 @@ private class Games.ApplicationWindow : Hdy.ApplicationWindow {
 
        [GtkCallback]
        public bool on_delete_event () {
-               return !quit_game ();
+               if (confirm_exit)
+                       return true;
+
+               quit_game.begin ((obj, res) => {
+                       if (!quit_game.end (res))
+                               return;
+
+                       confirm_exit = true;
+
+                       close ();
+               });
+
+               return false;
        }
 
        [GtkCallback]
@@ -147,10 +161,13 @@ private class Games.ApplicationWindow : Hdy.ApplicationWindow {
 
                if ((keyval == Gdk.Key.q || keyval == Gdk.Key.Q) &&
                    (event.state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK) {
-                       if (!quit_game ())
-                               return false;
 
-                       destroy ();
+                       quit_game.begin ((obj, res) => {
+                               if (!quit_game.end (res))
+                                       return;
+
+                               destroy ();
+                       });
 
                        return true;
                }
@@ -207,15 +224,17 @@ private class Games.ApplicationWindow : Hdy.ApplicationWindow {
 
        [GtkCallback]
        private void on_game_activated (Game game) {
-               run_game (game);
+               run_game.begin (game);
        }
 
        [GtkCallback]
        private void on_display_back () {
-               if (quit_game ())
-                       current_view = collection_view;
+               quit_game.begin ((obj, res) => {
+                       if (quit_game.end (res))
+                               current_view = collection_view;
 
-               uninhibit (Gtk.ApplicationInhibitFlags.IDLE | Gtk.ApplicationInhibitFlags.LOGOUT);
+                       uninhibit (Gtk.ApplicationInhibitFlags.IDLE | Gtk.ApplicationInhibitFlags.LOGOUT);
+               });
        }
 
        [GtkCallback]
diff --git a/src/ui/application.vala b/src/ui/application.vala
index ab738c4b..9c58e40f 100644
--- a/src/ui/application.vala
+++ b/src/ui/application.vala
@@ -219,7 +219,7 @@ public class Games.Application : Gtk.Application {
                        return;
                }
 
-               window.run_game (game);
+               window.run_game.begin (game);
        }
 
        protected override void startup () {
@@ -246,7 +246,7 @@ public class Games.Application : Gtk.Application {
                        return;
                }
 
-               window.run_game (game);
+               window.run_game.begin (game);
        }
 
        protected override int command_line (ApplicationCommandLine command_line) {
@@ -519,7 +519,14 @@ public class Games.Application : Gtk.Application {
        }
 
        private void quit_application () {
-               if (window != null && !window.quit_game ())
+               quit_application_internal.begin ();
+       }
+
+       private async void quit_application_internal () {
+               if (window != null)
+                       return;
+
+               if (window != null && !yield window.quit_game ())
                        return;
 
                quit ();
diff --git a/src/ui/display-view.vala b/src/ui/display-view.vala
index ef6cd5a8..e79f6bfd 100644
--- a/src/ui/display-view.vala
+++ b/src/ui/display-view.vala
@@ -338,9 +338,9 @@ private class Games.DisplayView : Gtk.Box, UiView {
                back ();
        }
 
-       public void run_game (Game game) {
+       public async void run_game (Game game) {
                // If there is a game already running we have to quit it first
-               if (runner != null && !quit_game ())
+               if (runner != null && !yield quit_game ())
                        return;
 
                this.game = game;
@@ -518,7 +518,7 @@ private class Games.DisplayView : Gtk.Box, UiView {
                }
        }
 
-       public bool quit_game () {
+       public async bool quit_game () {
                if (run_game_cancellable != null)
                        run_game_cancellable.cancel ();
 
@@ -528,7 +528,7 @@ private class Games.DisplayView : Gtk.Box, UiView {
                var cancellable = new Cancellable ();
                quit_game_cancellable = cancellable;
 
-               var result = quit_game_with_cancellable (cancellable);
+               var result = yield quit_game_with_cancellable (cancellable);
 
                // Only reset the cancellable if another one didn't replace it.
                if (quit_game_cancellable == cancellable)
@@ -537,7 +537,7 @@ private class Games.DisplayView : Gtk.Box, UiView {
                return result;
        }
 
-       public bool quit_game_with_cancellable (Cancellable cancellable) {
+       private async bool quit_game_with_cancellable (Cancellable cancellable) {
                if (runner == null)
                        return true;
 
@@ -580,7 +580,17 @@ private class Games.DisplayView : Gtk.Box, UiView {
                        quit_dialog = null;
                });
 
-               var response = quit_dialog.run ();
+               var response = Gtk.ResponseType.CANCEL;
+
+               quit_dialog.response.connect (r => {
+                       response = (Gtk.ResponseType) r;
+
+                       quit_game_with_cancellable.callback ();
+               });
+
+               quit_dialog.present ();
+
+               yield;
 
                // The null check is necessary because the dialog could already
                // be canceled by this point
@@ -679,7 +689,7 @@ private class Games.DisplayView : Gtk.Box, UiView {
 
        private async void restart_internal () {
                if (runner == null || !runner.is_integrated) {
-                       run_game (game);
+                       yield run_game (game);
 
                        return;
                }


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