[gnome-games] ui: Delay pausing the game on focus change



commit 2a925ff7b2c6ea1594e13e91cdde9ee5108697eb
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Fri Sep 2 05:14:43 2016 +0200

    ui: Delay pausing the game on focus change
    
    Add a 500 milliseconds delay before pausing the game when the
    application lost the focus.
    
    This avoids annoying pauses when the shell briefly takes the focus (when
    changing the sound volume for example) while avoiding to loose control
    of the game for too long.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=770426

 src/ui/application-window.vala |   42 +++++++++++++++++++++++++++++++++++++--
 1 files changed, 39 insertions(+), 3 deletions(-)
---
diff --git a/src/ui/application-window.vala b/src/ui/application-window.vala
index 969f9ed..ddfde36 100644
--- a/src/ui/application-window.vala
+++ b/src/ui/application-window.vala
@@ -2,6 +2,8 @@
 
 [GtkTemplate (ui = "/org/gnome/Games/ui/application-window.ui")]
 private class Games.ApplicationWindow : Gtk.ApplicationWindow {
+       private const uint FOCUS_OUT_DELAY_MILLISECONDS = 500;
+
        private UiState _ui_state;
        public UiState ui_state {
                set {
@@ -76,6 +78,8 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
        private Cancellable run_game_cancellable;
        private Cancellable quit_game_cancellable;
 
+       private long focus_out_timeout_id;
+
        public ApplicationWindow (ListModel collection) {
                collection_box.collection = collection;
        }
@@ -90,6 +94,8 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
                                                        BindingFlags.BIDIRECTIONAL);
                header_bar_fullscreen_binding = bind_property ("is-fullscreen", display_header_bar, 
"is-fullscreen",
                                                               BindingFlags.BIDIRECTIONAL);
+
+               focus_out_timeout_id = -1;
        }
 
        public void run_game (Game game) {
@@ -303,10 +309,16 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
 
        [GtkCallback]
        private void on_toplevel_focus () {
-               if (ui_state != UiState.DISPLAY)
-                       return;
+               update_pause (true);
+       }
 
-               if (display_box.runner == null)
+       private void update_pause (bool with_delay) {
+               if (focus_out_timeout_id != -1) {
+                       Source.remove ((uint) focus_out_timeout_id);
+                       focus_out_timeout_id = -1;
+               }
+
+               if (!can_update_pause ())
                        return;
 
                if (has_toplevel_focus)
@@ -316,10 +328,34 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
                        catch (Error e) {
                                warning (e.message);
                        }
+               else if (with_delay)
+                       focus_out_timeout_id = Timeout.add (FOCUS_OUT_DELAY_MILLISECONDS, 
on_focus_out_delay_elapsed);
                else
                        display_box.runner.pause ();
        }
 
+       private bool on_focus_out_delay_elapsed () {
+               focus_out_timeout_id = -1;
+
+               if (!can_update_pause ())
+                       return false;
+
+               if (!has_toplevel_focus)
+                       display_box.runner.pause ();
+
+               return false;
+       }
+
+       private bool can_update_pause () {
+               if (ui_state != UiState.DISPLAY)
+                       return false;
+
+               if (display_box.runner == null)
+                       return false;
+
+               return true;
+       }
+
        private bool handle_collection_key_event (Gdk.EventKey event) {
                if (ui_state != UiState.COLLECTION)
                        return false;


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