[gnome-documents] view: move the View/LoadMore related code from Embed to ViewContainer



commit 6591f1aa1198a0b52a2769adaa597be86c975e92
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Tue Jun 26 13:35:03 2012 -0400

    view: move the View/LoadMore related code from Embed to ViewContainer
    
    Consolidate code that deals with packing/showing of the LoadMore button
    in a ViewContainer class.

 src/Makefile-js.am |    1 -
 src/embed.js       |   70 +----------------------
 src/loadMore.js    |   81 --------------------------
 src/view.js        |  158 +++++++++++++++++++++++++++++++++++++++++++++++-----
 4 files changed, 148 insertions(+), 162 deletions(-)
---
diff --git a/src/Makefile-js.am b/src/Makefile-js.am
index 39d5ee3..8e981d8 100644
--- a/src/Makefile-js.am
+++ b/src/Makefile-js.am
@@ -8,7 +8,6 @@ dist_js_DATA = \
     errorBox.js \
     gDataMiner.js \
     global.js \
-    loadMore.js \
     main.js \
     mainToolbar.js \
     mainWindow.js \
diff --git a/src/embed.js b/src/embed.js
index bec499a..a063055 100644
--- a/src/embed.js
+++ b/src/embed.js
@@ -24,7 +24,6 @@ const Mainloop = imports.mainloop;
 
 const ErrorBox = imports.errorBox;
 const Global = imports.global;
-const LoadMore = imports.loadMore;
 const MainToolbar = imports.mainToolbar;
 const Preview = imports.preview;
 const Searchbar = imports.searchbar;
@@ -47,11 +46,8 @@ const Embed = new Lang.Class({
     Name: 'Embed',
 
     _init: function() {
-        this._adjustmentValueId = 0;
-        this._adjustmentChangedId = 0;
         this._loaderCancellable = null;
         this._queryErrorId = 0;
-        this._scrollbarVisibleId = 0;
 
         // the embed is a vertical ClutterBox
         this._overlayLayout = new Clutter.BinLayout();
@@ -115,6 +111,10 @@ const Embed = new Lang.Class({
         this._viewLayout.add(Global.notificationManager.actor,
             Clutter.BinAlignment.CENTER, Clutter.BinAlignment.START);
 
+        // now create the actual content widgets
+        this._view = new View.ViewContainer();
+        this._viewPage = this._notebook.append_page(this._view.widget, null);
+
         Global.errorHandler.connect('load-error',
                                     Lang.bind(this, this._onLoadError));
 
@@ -228,62 +228,13 @@ const Embed = new Lang.Class({
         this._spinnerBox.moveOut();
         this._errorBox.moveOut();
 
-        if (!this._view) {
-            let grid = new Gtk.Grid({ orientation: Gtk.Orientation.VERTICAL });
-            this._view = new View.View();
-            grid.add(this._view.widget);
-
-            this._loadMore = new LoadMore.LoadMoreButton();
-            grid.add(this._loadMore.widget);
-
-            grid.show_all();
-            this._viewPage = this._notebook.append_page(grid, null);
-        }
-
         this._queryErrorId =
             Global.errorHandler.connect('query-error',
                                         Lang.bind(this, this._onQueryError));
-        this._adjustmentValueId =
-            this._view.widget.vadjustment.connect('value-changed',
-                                                  Lang.bind(this, this._onScrolledWinChange));
-        this._adjustmentChangedId =
-            this._view.widget.vadjustment.connect('changed',
-                                                  Lang.bind(this, this._onScrolledWinChange));
-        this._scrollbarVisibleId =
-            this._view.widget.get_vscrollbar().connect('notify::visible',
-                                                       Lang.bind(this, this._onScrolledWinChange));
-        this._onScrolledWinChange();
 
         this._notebook.set_current_page(this._viewPage);
     },
 
-    _onScrolledWinChange: function() {
-        let vScrollbar = this._view.widget.get_vscrollbar();
-        let adjustment = this._view.widget.vadjustment;
-        let revealAreaHeight = 32;
-
-        // if there's no vscrollbar, or if it's not visible, hide the button
-        if (!vScrollbar ||
-            !vScrollbar.get_visible()) {
-            this._loadMore.setBlock(true);
-            return;
-        }
-
-        let value = adjustment.value;
-        let upper = adjustment.upper;
-        let page_size = adjustment.page_size;
-
-        let end = false;
-
-        // special case this values which happen at construction
-        if ((value == 0) && (upper == 1) && (page_size == 1))
-            end = false;
-        else
-            end = !(value < (upper - page_size - revealAreaHeight));
-
-        this._loadMore.setBlock(!end);
-    },
-
     _onQueryError: function(manager, message, exception) {
         this._setError(message, exception.message);
     },
@@ -294,19 +245,6 @@ const Embed = new Lang.Class({
             this._queryErrorId = 0;
         }
 
-        if (this._adjustmentValueId != 0) {
-            this._view.widget.vadjustment.disconnect(this._adjustmentValueId);
-            this._adjustmentValueId = 0;
-        }
-        if (this._adjustmentChangedId != 0) {
-            this._view.widget.vadjustment.disconnect(this._adjustmentChangedId);
-            this._adjustmentChangedId = 0;
-        }
-        if (this._scrollbarVisibleId != 0) {
-            this._view.widget.get_vscrollbar().disconnect(this._scrollbarVisibleId);
-            this._scrollbarVisibleId = 0;
-        }
-
         if (!this._preview) {
             this._preview = new Preview.PreviewView();
             this._previewPage = this._notebook.append_page(this._preview.widget, null);
diff --git a/src/view.js b/src/view.js
index 7d4652a..7523bda 100644
--- a/src/view.js
+++ b/src/view.js
@@ -35,6 +35,59 @@ const TrackerUtils = imports.trackerUtils;
 const WindowMode = imports.windowMode;
 const Utils = imports.utils;
 
+const LoadMoreButton = new Lang.Class({
+    Name: 'LoadMoreButton',
+
+    _init: function() {
+        this._block = false;
+
+        this._controller = Global.offsetController;
+        this._controllerId =
+            this._controller.connect('item-count-changed',
+                                     Lang.bind(this, this._onItemCountChanged));
+
+        this.widget = new Gtk.Button({ no_show_all: true });
+        this.widget.get_style_context().add_class('documents-load-more');
+        this.widget.connect('clicked', Lang.bind(this,
+            function() {
+                this._controller.increaseOffset();
+            }));
+
+        this.widget.connect('destroy', Lang.bind(this,
+            function() {
+                this._controller.disconnect(this._controllerId);
+            }));
+
+        this._onItemCountChanged();
+    },
+
+    _onItemCountChanged: function() {
+        let remainingDocs = this._controller.getRemainingDocs();
+        let offsetStep = this._controller.getOffsetStep();
+
+        if (remainingDocs <= 0 || this._block) {
+            this.widget.hide();
+            return;
+        }
+
+        if (remainingDocs > offsetStep)
+            remainingDocs = offsetStep;
+
+        this.widget.label = Gettext.ngettext("Load %d more document",
+                                             "Load %d more documents",
+                                             remainingDocs).format(remainingDocs);
+        this.widget.show();
+    },
+
+    setBlock: function(block) {
+        if (this._block == block)
+            return;
+
+        this._block = block;
+        this._onItemCountChanged();
+    }
+});
+
 const ContextMenu = new Lang.Class({
     Name: 'ContextMenu',
 
@@ -103,17 +156,28 @@ const ContextMenu = new Lang.Class({
     }
 });
 
-const View = new Lang.Class({
-    Name: 'View',
+const ViewContainer = new Lang.Class({
+    Name: 'ViewContainer',
 
     _init: function() {
-        this.widget = new Gd.MainView();
+        this._adjustmentValueId = 0;
+        this._adjustmentChangedId = 0;
+        this._scrollbarVisibleId = 0;
+
+        this.widget = new Gtk.Grid({ orientation: Gtk.Orientation.VERTICAL });
+        this.view = new Gd.MainView();
+        this.widget.add(this.view);
+
+        this._loadMore = new LoadMoreButton();
+        this.widget.add(this._loadMore.widget);
 
-        this.widget.connect('item-activated',
+        this.widget.show_all();
+
+        this.view.connect('item-activated',
                             Lang.bind(this, this._onItemActivated));
-        this.widget.connect('selection-mode-request',
+        this.view.connect('selection-mode-request',
                             Lang.bind(this, this._onSelectionModeRequest));
-        this.widget.connect('view-selection-changed',
+        this.view.connect('view-selection-changed',
                             Lang.bind(this, this._onViewSelectionChanged));
 
         // connect to settings change for list/grid view
@@ -128,6 +192,10 @@ const View = new Lang.Class({
                                                Lang.bind(this, this._onSelectionModeChanged));
         this._onSelectionModeChanged();
 
+        Global.modeController.connect('window-mode-changed',
+                                      Lang.bind(this, this._onWindowModeChanged));
+        this._onWindowModeChanged();
+
         this._queryId =
             Global.trackerController.connect('query-status-changed',
                                              Lang.bind(this, this._onQueryStatusChanged));
@@ -136,19 +204,18 @@ const View = new Lang.Class({
 
         // this will create the model if we're done querying
         this._onQueryStatusChanged();
-        this.widget.show();
     },
 
     _updateTypeForSettings: function() {
         let viewType = Global.settings.get_enum('view-as');
-        this.widget.set_view_type(viewType);
+        this.view.set_view_type(viewType);
 
         if (viewType == Gd.MainViewType.LIST)
             this._addListRenderers();
     },
 
     _addListRenderers: function() {
-        let listWidget = this.widget.get_generic_view();
+        let listWidget = this.view.get_generic_view();
 
         let typeRenderer =
             new Gd.StyledTextRenderer({ xpad: 16 });
@@ -232,7 +299,7 @@ const View = new Lang.Class({
         if (!status) {
             // setup a model if we're not querying
             this._treeModel = Global.documentManager.getModel().model;
-            this.widget.set_model(this._treeModel);
+            this.view.set_model(this._treeModel);
 
             // unfreeze selection
             Global.selectionController.freezeSelection(false);
@@ -244,7 +311,7 @@ const View = new Lang.Class({
             // if we're querying, clear the model from the view,
             // so that we don't uselessly refresh the rows
             this._treeModel = null;
-            this.widget.set_model(null);
+            this.view.set_model(null);
         }
     },
 
@@ -255,7 +322,7 @@ const View = new Lang.Class({
         if (!selected.length)
             return;
 
-        let generic = this.widget.get_generic_view();
+        let generic = this.view.get_generic_view();
         let first = true;
         this._treeModel.foreach(Lang.bind(this,
             function(model, path, iter) {
@@ -283,13 +350,76 @@ const View = new Lang.Class({
 
     _onSelectionModeChanged: function() {
         let selectionMode = Global.selectionController.getSelectionMode();
-        this.widget.set_selection_mode(selectionMode);
+        this.view.set_selection_mode(selectionMode);
     },
 
     _onViewSelectionChanged: function() {
         // update the selection on the controller when the view signals a change
-        let selectedURNs = Utils.getURNsFromPaths(this.widget.get_selection(),
+        let selectedURNs = Utils.getURNsFromPaths(this.view.get_selection(),
                                                   this._treeModel);
         Global.selectionController.setSelection(selectedURNs);
+    },
+
+    _onWindowModeChanged: function() {
+        let mode = Global.modeController.getWindowMode();
+        if (mode == WindowMode.WindowMode.OVERVIEW)
+            this._connectView();
+        else
+            this._disconnectView();
+    },
+
+    _connectView: function() {
+        this._adjustmentValueId =
+            this.view.vadjustment.connect('value-changed',
+                                          Lang.bind(this, this._onScrolledWinChange));
+        this._adjustmentChangedId =
+            this.view.vadjustment.connect('changed',
+                                          Lang.bind(this, this._onScrolledWinChange));
+        this._scrollbarVisibleId =
+            this.view.get_vscrollbar().connect('notify::visible',
+                                               Lang.bind(this, this._onScrolledWinChange));
+        this._onScrolledWinChange();
+    },
+
+    _onScrolledWinChange: function() {
+        let vScrollbar = this.view.get_vscrollbar();
+        let adjustment = this.view.vadjustment;
+        let revealAreaHeight = 32;
+
+        // if there's no vscrollbar, or if it's not visible, hide the button
+        if (!vScrollbar ||
+            !vScrollbar.get_visible()) {
+            this._loadMore.setBlock(true);
+            return;
+        }
+
+        let value = adjustment.value;
+        let upper = adjustment.upper;
+        let page_size = adjustment.page_size;
+
+        let end = false;
+
+        // special case this values which happen at construction
+        if ((value == 0) && (upper == 1) && (page_size == 1))
+            end = false;
+        else
+            end = !(value < (upper - page_size - revealAreaHeight));
+
+        this._loadMore.setBlock(!end);
+    },
+
+    _disconnectView: function() {
+        if (this._adjustmentValueId != 0) {
+            this.view.vadjustment.disconnect(this._adjustmentValueId);
+            this._adjustmentValueId = 0;
+        }
+        if (this._adjustmentChangedId != 0) {
+            this.view.vadjustment.disconnect(this._adjustmentChangedId);
+            this._adjustmentChangedId = 0;
+        }
+        if (this._scrollbarVisibleId != 0) {
+            this.view.get_vscrollbar().disconnect(this._scrollbarVisibleId);
+            this._scrollbarVisibleId = 0;
+        }
     }
 });



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