[polari/wip/fmuellner/nickserv: 9/20] chatView: Delegate state tracking to the app



commit 09b6fcfdbdd2b1f70e8bacec2d5177d3ed41c411
Author: Florian Müllner <fmuellner gnome org>
Date:   Wed Aug 3 20:35:42 2016 +0200

    chatView: Delegate state tracking to the app
    
    In order to only mark messages as read when we can assume that the
    user has seen it, we track both whether the view's toplevel window
    has focus and whether the view's room is currently active. While
    the existing code to do this isn't particularly bad, it is actually
    simpler to delegate the state tracking to the application, with the
    added benefit that it will be available to other modules that want
    to apply the same check.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=769656

 src/application.js |   13 ++++++++++++-
 src/chatView.js    |   32 +++++---------------------------
 2 files changed, 17 insertions(+), 28 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index 052a4cb..67517d6 100644
--- a/src/application.js
+++ b/src/application.js
@@ -22,7 +22,8 @@ const IRC_SCHEMA_REGEX = /^(irc?:\/\/)([\da-z\.-]+):?(\d+)?\/(?:%23)?([\w\.\+-]+
 const Application = new Lang.Class({
     Name: 'Application',
     Extends: Gtk.Application,
-    Signals: { 'prepare-shutdown': {} },
+    Signals: { 'prepare-shutdown': {},
+               'room-focus-changed': {} },
 
     _init: function() {
         this.parent({ application_id: 'org.gnome.Polari',
@@ -33,6 +34,12 @@ const Application = new Lang.Class({
         this._retryData = new Map();
     },
 
+    isRoomFocused: function(room) {
+        return this.active_window &&
+               this.active_window.is_active &&
+               this.active_window.active_room == room;
+    },
+
     vfunc_startup: function() {
         this.parent();
 
@@ -144,6 +151,10 @@ const Application = new Lang.Class({
             this._window = new MainWindow.MainWindow({ application: this });
             this._window.connect('destroy',
                                  () => { this.emit('prepare-shutdown'); });
+            this._window.connect('notify::active-room',
+                                 () => { this.emit('room-focus-changed'); });
+            this._window.connect('notify::is-active',
+                                 () => { this.emit('room-focus-changed'); });
             this._window.show_all();
         }
         this._window.present();
diff --git a/src/chatView.js b/src/chatView.js
index ad59b33..65af13c 100644
--- a/src/chatView.js
+++ b/src/chatView.js
@@ -258,12 +258,6 @@ const ChatView = new Lang.Class({
 
         this.connect('screen-changed',
                      Lang.bind(this, this._updateIndent));
-        this.connect('map', Lang.bind(this, this._updateActive));
-        this.connect('unmap', Lang.bind(this, this._updateActive));
-        this.connect('parent-set',
-                     Lang.bind(this, this._updateToplevel));
-        this.connect('state-flags-changed',
-                     Lang.bind(this, this._updateToplevel));
         this.connect('scroll-event', Lang.bind(this, this._onScroll));
         this.connect('edge-reached', (w, pos) => {
             if (pos == Gtk.PositionType.BOTTOM)
@@ -290,8 +284,6 @@ const ChatView = new Lang.Class({
 
         this._room = room;
         this._state = { lastNick: null, lastTimestamp: 0, lastStatusGroup: 0 };
-        this._active = false;
-        this._toplevelFocus = false;
         this._joinTime = 0;
         this._maxNickChars = MAX_NICK_CHARS;
         this._hoveredButtonTags = [];
@@ -325,6 +317,9 @@ const ChatView = new Lang.Class({
         this._app = Gio.Application.get_default();
         PasteManager.DropTargetIface.addTargets(this, this._view);
 
+        this._app.connect('room-focus-changed',
+                          Lang.bind(this, this._checkMessages));
+
         this._hoverCursor = Gdk.Cursor.new(Gdk.CursorType.HAND1);
 
         this._channelSignals = [];
@@ -603,23 +598,6 @@ const ChatView = new Lang.Class({
         this._view.left_margin = MARGIN + totalWidth;
     },
 
-    _updateActive: function() {
-        let active = this.get_mapped();
-        if (this._active == active)
-            return;
-        this._active = active;
-        this._checkMessages();
-    },
-
-    _updateToplevel: function() {
-        let flags = this.get_state_flags();
-        let toplevelFocus = !(flags & Gtk.StateFlags.BACKDROP);
-        if (this._toplevelFocus == toplevelFocus)
-            return;
-        this._toplevelFocus = toplevelFocus;
-        this._checkMessages();
-    },
-
     _updateScroll: function() {
         if (!this._autoscroll)
             return;
@@ -834,7 +812,7 @@ const ChatView = new Lang.Class({
     },
 
     _checkMessages: function() {
-        if (!this._active || !this._toplevelFocus || !this._channel)
+        if (!this._app.isRoomFocused(this._room) || !this._channel)
             return;
 
         this._needsIndicator = true;
@@ -1243,7 +1221,7 @@ const ChatView = new Lang.Class({
         }
 
         if (message.pendingId == undefined /* outgoing */ ||
-            (this._active && this._toplevelFocus && this._pending.size == 0))
+            (this._app.isRoomFocused(this._room) && this._pending.size == 0))
             this._channel.ack_message_async(tpMessage, null);
         else if (this._needsIndicator)
             this._setIndicatorMark(this._view.buffer.get_end_iter());


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