[gnome-calendar] year-view: implement scrolling to change year



commit 62aa66b8da971a1bb067c56ab3b0c339188631f3
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Sat Oct 1 19:16:18 2016 -0300

    year-view: implement scrolling to change year
    
    Following the previous work on month view, this commit
    implements scrolling capabilities to Year view. We do
    this not only with the :scroll-event, but also using the
    :edge-overshot signal of GtkScrolledWindow.

 data/ui/year-view.ui |    4 ++-
 src/gcal-year-view.c |   61 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 1 deletions(-)
---
diff --git a/data/ui/year-view.ui b/data/ui/year-view.ui
index 99b43b1..5b1ff1b 100644
--- a/data/ui/year-view.ui
+++ b/data/ui/year-view.ui
@@ -6,10 +6,12 @@
       <class name="year-view"/>
     </style>
     <child>
-      <object class="GtkScrolledWindow">
+      <object class="GtkScrolledWindow" id="scrolled_window">
         <property name="visible">True</property>
         <property name="shadow_type">none</property>
         <property name="vexpand">True</property>
+        <signal name="edge-overshot" handler="navigator_edge_overshot_cb" object="GcalYearView" 
swapped="yes"/>
+        <signal name="scroll-event" handler="navigator_scroll_event_cb" object="GcalYearView" swapped="yes"/>
         <child>
           <object class="GtkViewport">
             <property name="visible">True</property>
diff --git a/src/gcal-year-view.c b/src/gcal-year-view.c
index 7bf2979..e1c5d4c 100644
--- a/src/gcal-year-view.c
+++ b/src/gcal-year-view.c
@@ -55,6 +55,7 @@ struct _GcalYearView
   GtkWidget    *navigator_stack;
   GtkWidget    *no_events_title;
   GtkWidget    *navigator_sidebar;
+  GtkWidget    *scrolled_window;
 
   GtkWidget    *popover; /* Popover for popover_mode */
 
@@ -87,6 +88,9 @@ struct _GcalYearView
   guint         first_week_of_year;
   guint         last_week_of_year;
 
+  /* Storage for the accumulated scrolling */
+  gdouble         scroll_value;
+
     /**
    * clock format from GNOME desktop settings
    */
@@ -1164,6 +1168,60 @@ navigator_motion_notify_cb (GcalYearView   *year_view,
 }
 
 static void
+navigator_edge_overshot_cb (GcalYearView    *self,
+                            GtkPositionType  position_type)
+{
+  GtkAdjustment *adjustment;
+
+  /* Ignore horizontal scrolling (and the year view never scrolls horizontally anyway) */
+  if (position_type == GTK_POS_LEFT || position_type == GTK_POS_RIGHT)
+    return;
+
+  adjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (self->scrolled_window));
+
+  self->date->year += position_type == GTK_POS_BOTTOM ? 1 : -1;
+  *self->date = icaltime_normalize (*self->date);
+
+  gtk_adjustment_set_value (adjustment, 0.0);
+  gtk_widget_queue_draw (self->navigator);
+
+  g_object_notify (G_OBJECT (self), "active-date");
+}
+
+static gboolean
+navigator_scroll_event_cb (GcalYearView   *self,
+                           GdkEventScroll *scroll_event)
+{
+  GtkWidget *vscrollbar;
+
+  vscrollbar = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (self->scrolled_window));
+
+  /*
+   * If the vertical scrollbar is visible, delegate the year changing check
+   * to GtkScrolledWindow:edge-overshot callback.
+   */
+  if (gtk_widget_get_child_visible (vscrollbar))
+    return GDK_EVENT_PROPAGATE;
+
+  /*
+   * If we accumulated enough scrolling, change the month. Otherwise, we'd scroll
+   * waaay too fast.
+   */
+  if (should_change_date_for_scroll (&self->scroll_value, scroll_event))
+    {
+      self->date->year += self->scroll_value > 0 ? 1 : -1;
+      *self->date = icaltime_normalize (*self->date);
+      self->scroll_value = 0;
+
+      gtk_widget_queue_draw (self->navigator);
+
+      g_object_notify (G_OBJECT (self), "active-date");
+    }
+
+  return GDK_EVENT_STOP;
+}
+
+static void
 add_event_clicked_cb (GcalYearView *year_view,
                       GtkButton    *button)
 {
@@ -1819,6 +1877,7 @@ gcal_year_view_class_init (GcalYearViewClass *klass)
   gtk_widget_class_bind_template_child (widget_class, GcalYearView, navigator_sidebar);
   gtk_widget_class_bind_template_child (widget_class, GcalYearView, no_events_title);
   gtk_widget_class_bind_template_child (widget_class, GcalYearView, popover);
+  gtk_widget_class_bind_template_child (widget_class, GcalYearView, scrolled_window);
 
   gtk_widget_class_bind_template_callback (widget_class, draw_navigator);
   gtk_widget_class_bind_template_callback (widget_class, navigator_button_press_cb);
@@ -1826,7 +1885,9 @@ gcal_year_view_class_init (GcalYearViewClass *klass)
   gtk_widget_class_bind_template_callback (widget_class, navigator_drag_drop_cb);
   gtk_widget_class_bind_template_callback (widget_class, navigator_drag_leave_cb);
   gtk_widget_class_bind_template_callback (widget_class, navigator_drag_motion_cb);
+  gtk_widget_class_bind_template_callback (widget_class, navigator_edge_overshot_cb);
   gtk_widget_class_bind_template_callback (widget_class, navigator_motion_notify_cb);
+  gtk_widget_class_bind_template_callback (widget_class, navigator_scroll_event_cb);
   gtk_widget_class_bind_template_callback (widget_class, add_event_clicked_cb);
   gtk_widget_class_bind_template_callback (widget_class, popover_closed_cb);
 


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