[polari/wip/bastianilso/status-hiding] kind-of-review



commit 6a24040d31cf2136fa8ff6d4520edbd88c8a8fe7
Author: Florian Müllner <fmuellner gnome org>
Date:   Thu Dec 3 18:45:08 2015 +0100

    kind-of-review

 src/chatView.js |  160 +++++++++++++++++++++---------------------------------
 1 files changed, 62 insertions(+), 98 deletions(-)
---
diff --git a/src/chatView.js b/src/chatView.js
index 9680078..27418b4 100644
--- a/src/chatView.js
+++ b/src/chatView.js
@@ -20,7 +20,7 @@ const SCROLL_TIMEOUT = 100; // ms
 const TIMESTAMP_INTERVAL = 300; // seconds of inactivity after which to
                                 // insert a timestamp
 
-const INACTIVITY_THRESHOLD = 300; // a threshold in seconds used to control
+const INACTIVITY_THRESHOLD = 4; // a threshold in seconds used to control
                                   // the visibility of status messages
 const STATUS_NOISE_MAXIMUM = 4;
 
@@ -694,11 +694,7 @@ const ChatView = new Lang.Class({
     _onMemberRenamed: function(room, oldMember, newMember) {
         let time = GLib.DateTime.new_now_utc().to_unix();
         let text = _("%s is now known as %s").format(oldMember.alias, newMember.alias);
-        if (this._shouldShowStatus(oldMember.alias)) {
-            this._insertStatus(text);
-        } else if (time - this._state.lastTimestamp > INACTIVITY_THRESHOLD) {
-            this._insertStatusCompressed(text, 'renamed');
-        }
+        this._insertStatus(text, oldMember.alias, 'renamed');
         this._setNickStatus(oldMember.alias, Tp.ConnectionPresenceType.OFFLINE);
         this._setNickStatus(newMember.alias, Tp.ConnectionPresenceType.AVAILABLE);
     },
@@ -708,11 +704,7 @@ const ChatView = new Lang.Class({
         let text = _("%s has disconnected").format(member.alias);
         if (message)
             text += ' (%s)'.format(message);
-        if (this._shouldShowStatus(member.alias)) {
-            this._insertStatus(text);
-        } else if (time - this._state.lastTimestamp > INACTIVITY_THRESHOLD) {
-            this._insertStatusCompressed(text, 'left');
-        }
+        this._insertStatus(text, member.alias, 'left');
         this._setNickStatus(member.alias, Tp.ConnectionPresenceType.OFFLINE);
     },
 
@@ -722,11 +714,7 @@ const ChatView = new Lang.Class({
             actor ? _("%s has been kicked by %s").format(member.alias,
                                                          actor.alias)
                   : _("%s has been kicked").format(member.alias);
-        if (this._shouldShowStatus(member.alias)) {
-            this._insertStatus(message);
-        } else if (time - this._state.lastTimestamp > INACTIVITY_THRESHOLD) {
-            this._insertStatusCompressed(message, 'left');
-        }
+        this._insertStatus(message, member.alias, 'left');
         this._setNickStatus(member.alias, Tp.ConnectionPresenceType.OFFLINE);
     },
 
@@ -736,22 +724,14 @@ const ChatView = new Lang.Class({
             actor ? _("%s has been banned by %s").format(member.alias,
                                                          actor.alias)
                   : _("%s has been banned").format(member.alias)
-        if (this._shouldShowStatus(member.alias)) {
-            this._insertStatus(message);
-        } else if (time - this._state.lastTimestamp > INACTIVITY_THRESHOLD) {
-            this._insertStatusCompressed(message, 'left');
-        }
+        this._insertStatus(message, member.alias, 'left');
         this._setNickStatus(member.alias, Tp.ConnectionPresenceType.OFFLINE);
     },
 
     _onMemberJoined: function(room, member) {
         let time = GLib.DateTime.new_now_utc().to_unix();
         let text = _("%s joined").format(member.alias);
-        if (this._shouldShowStatus(member.alias)) {
-            this._insertStatus(text);
-        } else if (time - this._state.lastTimestamp > INACTIVITY_THRESHOLD) {
-            this._insertStatusCompressed(text, 'joined');
-        }
+        this._insertStatus(text, member.alias, 'joined');
         this._setNickStatus(member.alias, Tp.ConnectionPresenceType.AVAILABLE);
     },
 
@@ -762,11 +742,7 @@ const ChatView = new Lang.Class({
         if (message)
             text += ' (%s)'.format(message);
 
-        if (this._shouldShowStatus(member.alias))
-            this._insertStatus(text);
-        else if (time - this._state.lastTimestamp > INACTIVITY_THRESHOLD)
-            this._insertStatusCompressed(text, 'left');
-
+        this._insertStatus(text, member.alias, 'left');
         this._setNickStatus(member.alias, Tp.ConnectionPresenceType.OFFLINE);
     },
 
@@ -805,85 +781,69 @@ const ChatView = new Lang.Class({
         return time - nickTag._lastActivity < INACTIVITY_THRESHOLD;
     },
 
-/*
-    _insertStatus: function( The status message, time ? ) {
+    _updateStatusHeader: function() {
+        let buffer = this._view.buffer;
+        let headerMark = buffer.get_mark('idle-status-start');
+        let tagName = 'status-compressed' + this._state.lastStatusGroup;
+        let headerTag;
+        if (!headerMark) {
+            headerMark = buffer.create_mark('idle-status-start', buffer.get_end_iter(), true);
+            headerTag = new Gtk.TextTag({ name: tagName });
+            buffer.tag_table.add(headerTag);
+            this._ensureNewLine([headerTag]);
+        } else {
+            let start = buffer.get_iter_at_mark(headerMark);
+            let end = start.copy();
+            end.forward_line();
+            buffer.delete(start, end);
 
-        // Should the status be grouped?
-            // YES: Is there a group which exists already?
-                // YES: Then insert into the group
-                // NO: Then create a new group
+            headerTag = this._lookupTag(tagName);
+        }
 
-        // Should the status be shown at all? If not, then we return
+        headerTag.invisible = this._statusCount.total < STATUS_NOISE_MAXIMUM;
+
+        let stats = [];
+        if (this._statusCount.joined > 0)
+            stats.push(ngettext("%d user joined",
+                                "%d users joined", 
this._statusCount.joined).format(this._statusCount.joined));
+        if (this._statusCount.left > 0)
+            stats.push(ngettext("%d user left",
+                                "%d users left", this._statusCount.left).format(this._statusCount.left));
+        this._insertWithTags(buffer.get_iter_at_mark(headerMark),
+                             stats.join(", ") + ' (\u2026)\n',
+                             [this._lookupTag('status'), headerTag]);
     },
 
-*/
-
-
-
-
-    _insertStatusCompressed: function(statusText, status) {
+    _insertStatus: function(text, member, type) {
         let time = GLib.DateTime.new_now_utc().to_unix();
         if (time - this._joinTime < IGNORE_STATUS_TIME)
             return;
 
-        this._state.lastNick = null;
-
-        if (this._statusCount.total == 0)
-            this._ensureNewLine();
-
-        if (status == 'left')
-            this._statusCount.left++;
-        else if (status == 'joined')
-            this._statusCount.joined++;
-        this._statusCount.total++;
-
-        let buffer = this._view.buffer;
-        let markStart = buffer.get_mark('idle-status-start');
-        if (!markStart) {
-            let iter = buffer.get_end_iter();
-            markStart = buffer.create_mark('idle-status-start', iter, true);
-        }
+        let grouped = time - this._state.lastTimestamp > INACTIVITY_THRESHOLD;
+        if (!grouped && !this._shouldShowStatus(member))
+            return;
 
-        let statusGroupNumber = this._state.lastStatusGroup;
-        let statusGroupTag = this._lookupTag('status' + statusGroupNumber);
-        if (!statusGroupTag) {
-            statusGroupTag = new Gtk.TextTag({ name: 'status' + statusGroupNumber });
-            buffer.tag_table.add(statusGroupTag);
-        }
+        this._state.lastNick = null;
 
-        let statusTag = this._lookupTag('status');
-        let endIter = buffer.get_end_iter();
-        this._insertWithTags(endIter, statusText + '\n', [statusGroupTag, statusTag]);
+        let tags = [this._lookupTag('status')];
+        if (grouped) {
+            if (this._statusCount.hasOwnProperty(type))
+                this._statusCount[type]++;
+            this._updateStatusHeader();
 
-        if (this._statusCount.total > STATUS_NOISE_MAXIMUM) {
-            let statusCompressedTag = this._lookupTag('status-compressed' + statusGroupNumber);
-            if (!statusCompressedTag) {
-                statusCompressedTag = new Gtk.TextTag({ name: 'status-compressed' + statusGroupNumber });
-                buffer.tag_table.add(statusCompressedTag);
-                statusGroupTag.invisible = true;
+            let tag = this._lookupTag('status' + this._state.lastStatusGroup);
+            if (!tag) {
+                tag = new Gtk.TextTag({ name: 'status' + this._state.lastStatusGroup })
+                this._view.buffer.tag_table.add(tag);
             }
-
-            let compressedText = this._statusCount.joined + " users joined, "
-                                 + this._statusCount.left + " users left. ";
-
-            let markStartIter = buffer.get_iter_at_mark(markStart);
-            let markEndIter = markStartIter.copy();
-            markEndIter.forward_line();
-            buffer.delete(markStartIter, markEndIter);
-
-            markStartIter = buffer.get_iter_at_mark(markStart);
-            this._insertWithTags(markStartIter, compressedText + ' (\u2026) \n' , [statusTag, 
statusCompressedTag]);
+            tags.push(tag);
+        } else {
+            this._resetStatusCompressed();
         }
-    },
 
-    _insertStatus: function(text) {
-        let time = GLib.DateTime.new_now_utc().to_unix();
-        if (time - this._joinTime < IGNORE_STATUS_TIME)
-            return;
-        this._state.lastNick = null;
-        this._ensureNewLine();
+        this._ensureNewLine(tags);
         let iter = this._view.buffer.get_end_iter();
-        this._insertWithTagName(iter, text, 'status');
+        this._insertWithTags(iter, text, tags);
     },
 
     _formatTimestamp: function(timestamp) {
@@ -1104,11 +1064,15 @@ const ChatView = new Lang.Class({
         this._insertWithTags(iter, text.substr(pos), tags);
     },
 
-    _ensureNewLine: function() {
+    _ensureNewLine: function(tags) {
         let buffer = this._view.get_buffer();
         let iter = buffer.get_end_iter();
-        if (iter.get_line_offset() != 0)
-            buffer.insert(iter, '\n', -1);
+        if (iter.get_line_offset() != 0) {
+            if (tags)
+                this._insertWithTags(iter, '\n', tags);
+            else
+                buffer.insert(iter, '\n', -1);
+        }
     },
 
     _getLineIters: function(iter) {


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