[polari] roomStack: Use inheritance instead of delegation



commit f9be062c3ceadec8c5aa993fc16574f9843ae7f1
Author: Florian Müllner <fmuellner gnome org>
Date:   Sat Feb 6 21:18:10 2016 +0100

    roomStack: Use inheritance instead of delegation
    
    We used to use delegation almost exclusively, but since we started to apply
    templates, inheritance has been creeping in more and more. By now, from all
    our classes that provide a UI, about half inherit from Gtk.Widget while the
    other half uses a .widget delegate. The resulting inconsistency is getting
    rather annoying, so say good-bye to delegation and embrace inheritance ...

 data/resources/main-window.ui |   11 +++---
 src/mainWindow.js             |   23 ++++--------
 src/roomStack.js              |   77 +++++++++++++++++++++++++---------------
 3 files changed, 61 insertions(+), 50 deletions(-)
---
diff --git a/data/resources/main-window.ui b/data/resources/main-window.ui
index 3263302..48f4cd7 100644
--- a/data/resources/main-window.ui
+++ b/data/resources/main-window.ui
@@ -218,7 +218,11 @@
             <property name="visible">True</property>
             <property name="vexpand">True</property>
             <child>
-              <placeholder/>
+              <object class="Gjs_RoomStack" id="room_stack">
+                <property name="visible">True</property>
+                <property name="homogeneous">True</property>
+                <property name="transition-type">crossfade</property>
+              </object>
             </child>
           </object>
         </child>
@@ -232,9 +236,4 @@
         <widget name="room_sidebar"/>
       </widgets>
   </object>
-  <object class="GtkSizeGroup" id="bottom_size_group">
-      <property name="mode">vertical</property>
-      <widgets>
-      </widgets>
-  </object>
 </interface>
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 239068b..66926e1 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -245,24 +245,9 @@ const MainWindow = new Lang.Class({
         this.window.application = app;
 
         let overlay = builder.get_object('overlay');
-        let sizeGroup = builder.get_object('bottom_size_group');
-        this._roomStack = new RoomStack.RoomStack(sizeGroup);
-        overlay.add(this._roomStack.widget);
-
         overlay.add_overlay(app.notificationQueue.widget);
         overlay.add_overlay(app.commandOutputQueue.widget);
 
-        // command output notifications should not pop up over
-        // the input area, but appear to emerge from it, so
-        // set up an appropriate margin - this relies on the
-        // last widget added to the size group at this point
-        // is the room stack's placeholder entry, which will
-        // never be destroyed
-        sizeGroup.get_widgets()[0].connect('size-allocate',
-            function(w, rect) {
-                app.commandOutputQueue.widget.margin_bottom = rect.height - 1;
-            });
-
         this._titlebarRight = builder.get_object('titlebar_right');
         this._titlebarLeft = builder.get_object('titlebar_left');
 
@@ -274,6 +259,14 @@ const MainWindow = new Lang.Class({
         this._userListPopover = builder.get_object('user_list_popover');
         this._revealer = builder.get_object('room_list_revealer');
 
+        // command output notifications should not pop up over
+        // the input area, but appear to emerge from it, so
+        // set up an appropriate margin
+        let roomStack = builder.get_object('room_stack');
+        roomStack.bind_property('entry-area-height',
+                                app.commandOutputQueue.widget, 'margin-bottom',
+                                GObject.BindingFlags.SYNC_CREATE);
+
         // Make sure user-list button is at least as wide as icon buttons
         this._joinMenuButton.connect('size-allocate', Lang.bind(this,
             function(w, rect) {
diff --git a/src/roomStack.js b/src/roomStack.js
index 03a5868..fed59de 100644
--- a/src/roomStack.js
+++ b/src/roomStack.js
@@ -1,4 +1,6 @@
 const Gio = imports.gi.Gio;
+const GLib = imports.gi.GLib;
+const GObject = imports.gi.GObject;
 const Gtk = imports.gi.Gtk;
 
 const AccountsMonitor = imports.accountsMonitor;
@@ -9,13 +11,20 @@ const Lang = imports.lang;
 
 const RoomStack = new Lang.Class({
     Name: 'RoomStack',
+    Extends: Gtk.Stack,
+    Properties: {
+        'entry-area-height': GObject.ParamSpec.uint('entry-area-height',
+                                                    'entry-area-height',
+                                                    'entry-area-height',
+                                                    GObject.ParamFlags.READABLE,
+                                                    0, GLib.MAXUINT32, 0)
+    },
 
-    _init: function(inputSizeGroup) {
-        this._inputSizeGroup = inputSizeGroup;
+    _init: function(params) {
+        this.parent(params);
 
-        this.widget = new Gtk.Stack({ homogeneous: true,
-                                      transition_type: Gtk.StackTransitionType.CROSSFADE });
-        this.widget.show_all();
+        this._sizeGroup = new Gtk.SizeGroup({ mode: Gtk.SizeGroupMode.VERTICAL });
+        this._rooms = {};
 
         this._roomManager = ChatroomManager.getDefault();
 
@@ -28,30 +37,36 @@ const RoomStack = new Lang.Class({
         this._roomManager.connect('active-state-changed',
                                   Lang.bind(this, this._updateSensitivity));
 
-        this._rooms = {};
+        this.add_named(new ChatPlaceholder(this._sizeGroup), 'placeholder');
+
+        this._entryAreaHeight = 0;
+        this._sizeGroup.get_widgets()[0].connect('size-allocate', Lang.bind(this,
+            function(w, rect) {
+                this._entryAreaHeight = rect.height - 1;
+                this.notify('entry-area-height');
+            }));
+    },
 
-        let placeholder = new ChatPlaceholder(inputSizeGroup);
-        this.widget.add_named(placeholder.widget, 'placeholder');
+    get entry_area_height() {
+        return this._entryAreaHeight;
     },
 
     _addView: function(id, view) {
         this._rooms[id] = view;
-
-        this._inputSizeGroup.add_widget(view.inputWidget);
-        this.widget.add_named(view.widget, id);
+        this.add_named(view, id);
     },
 
     _roomAdded: function(roomManager, room) {
-        this._addView(room.id, new RoomView(room));
+        this._addView(room.id, new RoomView(room, this._sizeGroup));
     },
 
     _roomRemoved: function(roomManager, room) {
-        this._rooms[room.id].widget.destroy();
+        this._rooms[room.id].destroy();
         delete this._rooms[room.id];
     },
 
     _activeRoomChanged: function(manager, room) {
-        this.widget.set_visible_child_name(room ? room.id : 'placeholder');
+        this.set_visible_child_name(room ? room.id : 'placeholder');
     },
 
     _updateSensitivity: function() {
@@ -65,6 +80,7 @@ const RoomStack = new Lang.Class({
 
 const ChatPlaceholder = new Lang.Class({
     Name: 'ChatPlaceholder',
+    Extends: Gtk.Overlay,
 
     _init: function(sizeGroup) {
         this._accountsMonitor = AccountsMonitor.getDefault();
@@ -86,7 +102,7 @@ const ChatPlaceholder = new Lang.Class({
         let inputPlaceholder = new Gtk.Box({ valign: Gtk.Align.END });
         sizeGroup.add_widget(inputPlaceholder);
 
-        this.widget = new Gtk.Overlay();
+        this.parent();
         let grid = new Gtk.Grid({ column_homogeneous: true, can_focus: false,
                                   column_spacing: 18, hexpand: true, vexpand: true,
                                   valign: Gtk.Align.CENTER });
@@ -94,33 +110,36 @@ const ChatPlaceholder = new Lang.Class({
         grid.attach(image, 0, 0, 1, 1);
         grid.attach(title, 1, 0, 1, 1);
         grid.attach(description, 0, 1, 2, 1);
-        this.widget.add(grid);
-        this.widget.add_overlay(inputPlaceholder);
-        this.widget.show_all();
+        this.add(grid);
+        this.add_overlay(inputPlaceholder);
+        this.show_all();
     }
 });
 
 const RoomView = new Lang.Class({
     Name: 'RoomView',
+    Extends: Gtk.Box,
+
+    _init: function(room, sizeGroup) {
+        this.parent({ orientation: Gtk.Orientation.VERTICAL });
 
-    _init: function(room) {
         this._view = new ChatView.ChatView(room);
+        this.add(this._view.widget);
+
+        this._entryArea = new EntryArea.EntryArea({ room: room,
+                                                    sensitive: false });
+        this.add(this._entryArea);
+
         this._view.connect('max-nick-chars-changed', Lang.bind(this,
             function() {
-                this.inputWidget.maxNickChars = this._view.maxNickChars;
+                this._entryArea.maxNickChars = this._view.maxNickChars;
             }));
+        sizeGroup.add_widget(this._entryArea);
 
-        this.widget = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL });
-        this.widget.add(this._view.widget);
-
-        this.inputWidget = new EntryArea.EntryArea({ room: room,
-                                                     sensitive: false });
-        this.widget.add(this.inputWidget);
-
-        this.widget.show_all();
+        this.show_all();
     },
 
     set inputSensitive(sensitive) {
-        this.inputWidget.sensitive = sensitive;
+        this._entryArea.sensitive = sensitive;
     }
 });


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