[polari/wip/fmuellner/room-list: 2/7] serverRoomManager: Add ServerRoomList widget



commit da8cac1e2fd4ad938d3b214e1d02f2ce5786b96b
Author: Isabella Ribeiro <belinhacbr gmail com>
Date:   Wed Mar 23 21:54:00 2016 -0400

    serverRoomManager: Add ServerRoomList widget
    
    https://bugzilla.gnome.org/show_bug.cgi?id=763200
    
    Tweak server room rows
    
    add separators
    
    style nit

 src/serverRoomManager.js |  146 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 146 insertions(+), 0 deletions(-)
---
diff --git a/src/serverRoomManager.js b/src/serverRoomManager.js
index 78a4c30..a9d601a 100644
--- a/src/serverRoomManager.js
+++ b/src/serverRoomManager.js
@@ -1,4 +1,6 @@
 const GLib = imports.gi.GLib;
+const GObject = imports.gi.GObject;
+const Gtk = imports.gi.Gtk;
 const Tp = imports.gi.TelepathyGLib;
 
 const AccountsMonitor = imports.accountsMonitor;
@@ -88,3 +90,147 @@ const _ServerRoomManager = new Lang.Class({
     }
 });
 Signals.addSignalMethods(_ServerRoomManager.prototype);
+
+const ServerRoomList = new Lang.Class({
+    Name: 'ServerRoomList',
+    Extends: Gtk.ScrolledWindow,
+    Properties: { 'can-join': GObject.ParamSpec.boolean('can-join',
+                                                        'can-join',
+                                                        'can-join',
+                                                        GObject.ParamFlags.READABLE,
+                                                        false),
+                  'loading': GObject.ParamSpec.boolean('loading',
+                                                       'loading',
+                                                       'loading',
+                                                       GObject.ParamFlags.READABLE,
+                                                       false)
+    },
+
+    _init: function(params) {
+        this._account = null;
+
+        this.parent(params);
+
+        this.connect('destroy', () => {
+            this.setAccount(null);
+        });
+
+        this._list = new Gtk.ListBox({ visible: true });
+        this._list.set_sort_func(Lang.bind(this, this._sort));
+        this._list.set_header_func(Lang.bind(this, this._updateHeader));
+        this._list.connect('row-activated',
+                               Lang.bind(this, this._onRowActivated));
+        this.add(this._list);
+
+        this._manager = getDefault();
+        this._manager.connect('loaded', Lang.bind(this, this._onLoaded));
+    },
+
+    get can_join() {
+        return this._list.get_children().some(r => r.checked);
+    },
+
+    get loading() {
+        return this._manager.isLoading(this._account);
+    },
+
+    _onRowActivated: function(list, row) {
+        row.activate();
+    },
+
+    _sort: function(row1, row2) {
+        return row2.info.get_members_count(null) -
+               row1.info.get_members_count(null);
+    },
+
+    get selectedRooms() {
+        let selectedRows = this._list.get_children().filter(r => r.checked);
+        return selectedRows.map(r => r.info.get_name());
+    },
+
+    setAccount: function(account) {
+        if (this._account == account)
+            return;
+
+        this._account = account;
+        this._onLoaded(this._manager, account);
+    },
+
+    _updateHeader: function(row, before) {
+        if (!before)
+            row.set_header(null);
+        else if (!row.get_header())
+            row.set_header(new Gtk.Separator());
+    },
+
+    _onLoaded: function(mgr, account) {
+        if (account != this._account)
+            return;
+
+        this._list.foreach(function(w) { w.destroy(); });
+        this._manager.getRoomInfos(account).forEach(roomInfo => {
+            let row = new ServerRoomRow({ info: roomInfo });
+            row.connect('notify::checked', () => { this.notify('can-join'); });
+            this._list.add(row);
+        });
+    }
+});
+
+const ServerRoomRow = new Lang.Class({
+    Name: 'ServerRoomRow',
+    Extends: Gtk.ListBoxRow,
+    Properties: { 'checked': GObject.ParamSpec.boolean('checked',
+                                                       'checked',
+                                                       'checked',
+                                                       GObject.ParamFlags.READABLE,
+                                                       false),
+    },
+
+    _init: function(params) {
+        if (!params || !params.info)
+            throw new Error('No info in parameters');
+
+        this._info = params.info;
+        delete params.info;
+
+        this.parent(params);
+
+        let name = this._info.get_name();
+        if (name[0] == '#')
+           name = name.substr(1, name.length);
+
+        let box = new Gtk.Box({ spacing: 12, margin: 12 });
+        this.add(box);
+
+        this._checkbox = new Gtk.CheckButton();
+        this._checkbox.connect('toggled', Lang.bind(this,
+            function() {
+                this.notify('checked');
+            }));
+
+        box.add(this._checkbox);
+
+        box.add(new Gtk.Label({ label: name,
+                                hexpand: true,
+                                halign: Gtk.Align.START }));
+
+        let count = this._info.get_members_count(null);
+        let label = new Gtk.Label({ label: "%d".format(count) });
+        label.get_style_context().add_class('dim-label');
+        box.add(label);
+
+        this.show_all();
+    },
+
+    get info() {
+        return this._info;
+    },
+
+    get checked() {
+        return this._checkbox.active;
+    },
+
+    vfunc_activate: function() {
+        this._checkbox.activate();
+    }
+});


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