[bijiben] loadMoreButton: do not show unless scrollbar at bottom



commit 8807f390d2112aaacd171e61b6041a52c92dc4e9
Author: Pierre-Yves Luyten <py luyten fr>
Date:   Sun Feb 16 02:36:43 2014 +0100

    loadMoreButton: do not show unless scrollbar at bottom
    
    Only propose to load more when the user already saw
    what is loaded.
    See 723846

 src/bjb-controller.c       |    8 +++++
 src/bjb-controller.h       |    5 +++
 src/bjb-load-more-button.c |   56 ++++++++++++++++++++++++++++----
 src/bjb-load-more-button.h |    6 +++
 src/bjb-main-view.c        |   75 +++++++++++++++++++++++++++++++++++++++++---
 5 files changed, 138 insertions(+), 12 deletions(-)
---
diff --git a/src/bjb-controller.c b/src/bjb-controller.c
index bad9743..625efb9 100644
--- a/src/bjb-controller.c
+++ b/src/bjb-controller.c
@@ -876,3 +876,11 @@ bjb_controller_show_more (BjbController *self)
   else
     on_needle_changed (self);
 }
+
+
+
+gboolean
+bjb_controller_get_remaining_items (BjbController *self)
+{
+  return self->priv->remaining_items;
+}
diff --git a/src/bjb-controller.h b/src/bjb-controller.h
index 142c5cb..c54ac81 100644
--- a/src/bjb-controller.h
+++ b/src/bjb-controller.h
@@ -85,6 +85,11 @@ void bjb_controller_set_group (BjbController   *self,
 
 void bjb_controller_show_more (BjbController *controller);
 
+
+gboolean bjb_controller_get_remaining_items (BjbController *self);
+
+
 G_END_DECLS
 
+
 #endif /* _BJB_CONTROLLER_H_ */
diff --git a/src/bjb-load-more-button.c b/src/bjb-load-more-button.c
index b1cf41e..e233d43 100644
--- a/src/bjb-load-more-button.c
+++ b/src/bjb-load-more-button.c
@@ -34,6 +34,7 @@ struct _BjbLoadMoreButtonPrivate
   GtkWidget *revealer;
   GtkWidget *label;
   GtkWidget *spinner;
+  gboolean   block;
 
   BjbController *controller;
 };
@@ -55,18 +56,33 @@ G_DEFINE_TYPE (BjbLoadMoreButton, bjb_load_more_button, GTK_TYPE_BUTTON);
 
 static void
 on_displayed_items_changed (BjbController     *controller,
-                            gboolean          some_is_shown,
-                            gboolean          remaining,
+                            gboolean           some_is_shown,
+                            gboolean           remaining,
                             BjbLoadMoreButton *self)
 {
   BjbLoadMoreButtonPrivate *priv;
 
   priv = self->priv;
+
   gtk_spinner_stop (GTK_SPINNER (priv->spinner));
-  gtk_widget_hide (priv->spinner);
-  gtk_label_set_label (GTK_LABEL (priv->label), _("Load More"));
 
-  gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), remaining);
+  if (some_is_shown)
+  {
+    gtk_widget_hide (priv->spinner);
+    gtk_label_set_label (GTK_LABEL (priv->label), _("Load More"));
+
+    if (remaining && (priv->block == FALSE))
+      gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), TRUE);
+
+    else
+      gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), FALSE);
+  }
+
+  else
+  {
+    gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), FALSE);
+  }
+
 }
 
 
@@ -100,6 +116,7 @@ bjb_load_more_button_constructed (GObject *object)
                     G_CALLBACK (on_displayed_items_changed), self);
 
   G_OBJECT_CLASS (bjb_load_more_button_parent_class)->constructed (object);
+  gtk_widget_show_all (GTK_WIDGET (self));
 }
 
 
@@ -134,6 +151,7 @@ bjb_load_more_button_init (BjbLoadMoreButton *self)
 
   priv = self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, BJB_TYPE_LOAD_MORE_BUTTON, 
BjbLoadMoreButtonPrivate);
 
+  priv->block = FALSE;
   context = gtk_widget_get_style_context (GTK_WIDGET (self));
   gtk_style_context_add_class (context, "documents-load-more");
 
@@ -229,7 +247,31 @@ GtkWidget *
 bjb_load_more_button_new (BjbController *controller)
 {
   BjbLoadMoreButton *button;
-  
+
   button = g_object_new (BJB_TYPE_LOAD_MORE_BUTTON, "controller", controller, NULL);
-  return button->priv->revealer;
+  return GTK_WIDGET (button);
+}
+
+
+void
+bjb_load_more_button_set_block (BjbLoadMoreButton *self, gboolean block)
+{
+  BjbController *controller;
+
+  if (self->priv->block == block)
+    return;
+
+  self->priv->block = block;
+  controller = self->priv->controller;
+  on_displayed_items_changed (controller,
+                              bjb_controller_shows_item (controller),
+                              bjb_controller_get_remaining_items (controller),
+                              self);
+}
+
+
+GtkWidget *
+bjb_load_more_button_get_revealer (BjbLoadMoreButton *self)
+{
+  return self->priv->revealer;
 }
diff --git a/src/bjb-load-more-button.h b/src/bjb-load-more-button.h
index fd2dc54..8d4ffe0 100644
--- a/src/bjb-load-more-button.h
+++ b/src/bjb-load-more-button.h
@@ -73,6 +73,12 @@ GType                  bjb_load_more_button_get_type               (void) G_GNUC
 
 GtkWidget             *bjb_load_more_button_new                    (BjbController *controller);
 
+
+GtkWidget             *bjb_load_more_button_get_revealer           (BjbLoadMoreButton* button);
+
+
+void                   bjb_load_more_button_set_block              (BjbLoadMoreButton *button, gboolean 
block);
+
 G_END_DECLS
 
 #endif /* BJB_LOAD_MORE_BUTTON_H */
diff --git a/src/bjb-main-view.c b/src/bjb-main-view.c
index b6d5d6d..6b5a19d 100644
--- a/src/bjb-main-view.c
+++ b/src/bjb-main-view.c
@@ -54,7 +54,7 @@ static guint bjb_main_view_signals [BJB_MAIN_VIEW_SIGNALS] = { 0 };
 
 /************************** Gobject ***************************/
 
-struct _BjbMainViewPriv {  
+struct _BjbMainViewPriv {
   GtkWidget        *window;
   GtkWidget        *label;
 
@@ -65,7 +65,7 @@ struct _BjbMainViewPriv {
   BjbSelectionToolbar  *select_bar;
 
   /* View Notes , model */
-  GdMainView       *view ;
+  GdMainView       *view;
   BjbController    *controller;
   GtkWidget        *load_more;
 
@@ -585,11 +585,53 @@ add_list_renderers (BjbMainView *self)
 }
 
 
+
+static void
+bjb_main_view_view_changed (BjbMainView *self)
+{
+  BjbMainViewPriv *priv;
+  GtkAdjustment *vadjustment;
+  GtkWidget *vscrollbar;
+  gboolean end;
+  gdouble page_size;
+  gdouble upper;
+  gdouble value;
+  gint reveal_area_height;
+
+  priv = self->priv;
+  reveal_area_height = 32;
+  end = FALSE;
+  vscrollbar = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (priv->view));
+
+  if (vscrollbar == NULL || !gtk_widget_get_visible (GTK_WIDGET (vscrollbar)))
+  {
+    bjb_load_more_button_set_block (BJB_LOAD_MORE_BUTTON (priv->load_more), TRUE);
+    return;
+  }
+
+  vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->view));
+  page_size = gtk_adjustment_get_page_size (vadjustment);
+  upper = gtk_adjustment_get_upper (vadjustment);
+  value = gtk_adjustment_get_value (vadjustment);
+
+  /* Special case these values which happen at construction */
+  if ((gint) value == 0 && (gint) upper == 1 && (gint) page_size == 1)
+    end = FALSE;
+  else
+    end = !(value < (upper - page_size - reveal_area_height));
+
+  bjb_load_more_button_set_block (BJB_LOAD_MORE_BUTTON (priv->load_more), !end);
+}
+
+
 static void
 bjb_main_view_constructed(GObject *o)
 {
   BjbMainView          *self;
   BjbMainViewPriv      *priv;
+  GtkAdjustment        *vadjustment;
+  GtkWidget            *vscrollbar;
+  GtkWidget            *button;
 
   G_OBJECT_CLASS (bjb_main_view_parent_class)->constructed(G_OBJECT(o));
 
@@ -606,10 +648,33 @@ bjb_main_view_constructed(GObject *o)
   gd_main_view_set_model (priv->view, bjb_controller_get_model(priv->controller));
   gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (priv->view));
 
+  vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->view));
+
+  g_signal_connect_object (vadjustment,
+                           "changed",
+                           G_CALLBACK (bjb_main_view_view_changed),
+                           self,
+                           G_CONNECT_SWAPPED);
+  g_signal_connect_object (vadjustment,
+                           "value-changed",
+                           G_CALLBACK (bjb_main_view_view_changed),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  vscrollbar = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (priv->view));
+  g_signal_connect_object (vscrollbar,
+                           "notify::visible",
+                           G_CALLBACK (bjb_main_view_view_changed),
+                           self,
+                           G_CONNECT_SWAPPED);
+
 
   /* Load more */
   priv->load_more = bjb_load_more_button_new (priv->controller);
-  gtk_container_add (GTK_CONTAINER (self), priv->load_more);
+  button = bjb_load_more_button_get_revealer (BJB_LOAD_MORE_BUTTON (priv->load_more));
+  gtk_container_add (GTK_CONTAINER (self), button);
+  bjb_main_view_view_changed (self);
+
 
   /* Selection Panel */
   priv->select_bar = bjb_selection_toolbar_new (priv->view, self);
@@ -633,9 +698,9 @@ bjb_main_view_class_init (BjbMainViewClass *klass)
   object_class->set_property = bjb_main_view_set_property;
   object_class->constructor = biji_main_view_constructor;
   object_class->constructed = bjb_main_view_constructed;
-    
+
   g_type_class_add_private (klass, sizeof (BjbMainViewPriv));
-  
+
   properties[PROP_WINDOW] = g_param_spec_object ("window",
                                                  "Window",
                                                  "Parent Window",


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