[gnome-games] application-window: Make running a game cancellable



commit b3d8d301327c35ca1b9c6310b235fc8617cf350c
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Mon Mar 7 20:38:03 2016 +0100

    application-window: Make running a game cancellable
    
    Allow to cancel trying to run a game.
    
    This will be used in next commit when asking to quit when a game is
    being started, this is paticularly useful when trying to quit but the
    user is prompted if he sould resume the game or not.

 src/ui/application-window.vala |   23 +++++++++++++++++++++++
 1 files changed, 23 insertions(+), 0 deletions(-)
---
diff --git a/src/ui/application-window.vala b/src/ui/application-window.vala
index 1a62cd4..8adf9e6 100644
--- a/src/ui/application-window.vala
+++ b/src/ui/application-window.vala
@@ -34,6 +34,8 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
 
        private HashTable<Game, Runner> runners;
 
+       private Cancellable run_game_cancellable;
+
        public ApplicationWindow (ListModel collection) {
                content_box.collection = collection;
        }
@@ -56,6 +58,15 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
                });
        }
 
+       public void run_game (Game game) {
+               if (run_game_cancellable != null)
+                       run_game_cancellable.cancel ();
+
+               run_game_cancellable = new Cancellable ();
+
+               run_game_with_cancellable (game, run_game_cancellable);
+       }
+
        [GtkCallback]
        public bool on_key_pressed (Gdk.EventKey event) {
                var default_modifiers = Gtk.accelerator_get_default_mod_mask ();
@@ -83,6 +94,10 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
 
        [GtkCallback]
        private void on_game_activated (Game game) {
+               run_game (game);
+       }
+
+       private void run_game_with_cancellable (Game game, Cancellable cancellable) {
                Runner runner = null;
                try {
                        runner = get_runner_for_game (game);
@@ -103,9 +118,17 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
                if (runner.can_resume) {
                        var dialog = new ResumeDialog ();
                        dialog.set_transient_for (this);
+
+                       cancellable.cancelled.connect (() => {
+                               dialog.destroy ();
+                       });
+
                        var response = dialog.run ();
                        dialog.destroy ();
 
+                       if (cancellable.is_cancelled ())
+                               response = Gtk.ResponseType.CANCEL;
+
                        switch (response) {
                        case Gtk.ResponseType.CANCEL:
                                content_box.runner = null;


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