window: Delegate changing dates to GcalView

The view knows its time range, and knows what the
next or previous dates should be. So instead of
calculating the next date manually, ask the views
for that.
This commit is contained in:
Georges Basile Stavracas Neto
2019-05-09 17:42:59 -03:00
parent da31180cba
commit 0baeaa160a
7 changed files with 190 additions and 69 deletions
+54 -66
View File
@@ -176,22 +176,8 @@ enum
gtk_application_set_accels_for_action (GTK_APPLICATION (app), action, tmp);\
}
static void on_show_calendars_action_activated (GSimpleAction *action,
GVariant *param,
gpointer user_data);
static void on_view_action_activated (GSimpleAction *action,
GVariant *param,
gpointer user_data);
G_DEFINE_TYPE (GcalWindow, gcal_window, GTK_TYPE_APPLICATION_WINDOW)
static const GActionEntry actions[] = {
{"change-view", on_view_action_activated, "i" },
{"show-calendars", on_show_calendars_action_activated },
};
static GParamSpec* properties[N_PROPS] = { NULL, };
@@ -274,57 +260,6 @@ update_active_date (GcalWindow *window,
GCAL_EXIT;
}
static void
date_updated (GcalWindow *window,
GtkButton *button)
{
g_autoptr (GDateTime) new_date = NULL;
gboolean move_today;
gboolean move_back;
gint factor;
GCAL_ENTRY;
factor = window->rtl ? - 1 : 1;
move_today = window->today_button == (GtkWidget*) button;
move_back = window->back_button == (GtkWidget*) button;
if (move_today)
{
new_date = g_date_time_new_now_local ();
}
else
{
gint diff = factor * (move_back ? -1 : 1);
switch (window->active_view)
{
case GCAL_WINDOW_VIEW_DAY:
new_date = g_date_time_add_days (window->active_date, 1 * diff);
break;
case GCAL_WINDOW_VIEW_WEEK:
new_date = g_date_time_add_weeks (window->active_date, 1 * diff);
break;
case GCAL_WINDOW_VIEW_MONTH:
new_date = g_date_time_add_months (window->active_date, 1 * diff);
break;
case GCAL_WINDOW_VIEW_YEAR:
new_date = g_date_time_add_years (window->active_date, 1 * diff);
break;
case GCAL_WINDOW_VIEW_LIST:
case GCAL_WINDOW_VIEW_SEARCH:
default:
break;
}
}
update_active_date (window, new_date);
GCAL_EXIT;
}
static void
on_show_calendars_action_activated (GSimpleAction *action,
GVariant *param,
@@ -361,6 +296,52 @@ on_view_action_activated (GSimpleAction *action,
g_object_notify_by_pspec (G_OBJECT (user_data), properties[PROP_ACTIVE_VIEW]);
}
static void
on_window_next_date_activated_cb (GSimpleAction *action,
GVariant *param,
gpointer user_data)
{
g_autoptr (GDateTime) next_date = NULL;
GcalWindow *self;
GcalView *current_view;
self = GCAL_WINDOW (user_data);
current_view = GCAL_VIEW (self->views[self->active_view]);
next_date = gcal_view_get_next_date (current_view);
update_active_date (self, next_date);
}
static void
on_window_previous_date_activated_cb (GSimpleAction *action,
GVariant *param,
gpointer user_data)
{
g_autoptr (GDateTime) previous_date = NULL;
GcalWindow *self;
GcalView *current_view;
self = GCAL_WINDOW (user_data);
current_view = GCAL_VIEW (self->views[self->active_view]);
previous_date = gcal_view_get_previous_date (current_view);
update_active_date (self, previous_date);
}
static void
on_window_today_activated_cb (GSimpleAction *action,
GVariant *param,
gpointer user_data)
{
g_autoptr (GDateTime) today = NULL;
GcalWindow *self;
self = GCAL_WINDOW (user_data);
today = g_date_time_new_now_local ();
update_active_date (self, today);
}
static void
load_geometry (GcalWindow *self)
{
@@ -1051,7 +1032,6 @@ gcal_window_class_init (GcalWindowClass *klass)
gtk_widget_class_bind_template_child (widget_class, GcalWindow, notification_close_button);
gtk_widget_class_bind_template_callback (widget_class, view_changed);
gtk_widget_class_bind_template_callback (widget_class, date_updated);
/* Event removal related */
gtk_widget_class_bind_template_callback (widget_class, hide_notification);
@@ -1075,6 +1055,14 @@ gcal_window_class_init (GcalWindowClass *klass)
static void
gcal_window_init (GcalWindow *self)
{
static const GActionEntry actions[] = {
{"change-view", on_view_action_activated, "i" },
{"next-date", on_window_next_date_activated_cb },
{"previous-date", on_window_previous_date_activated_cb },
{"show-calendars", on_show_calendars_action_activated },
{"today", on_window_today_activated_cb }
};
GApplication *app;
/* Setup actions */
+21
View File
@@ -1220,6 +1220,25 @@ gcal_month_view_update_subscription (GcalView *view)
range_end);
}
static GDateTime*
gcal_month_view_get_next_date (GcalView *view)
{
GcalMonthView *self = GCAL_MONTH_VIEW (view);
g_assert (self->date != NULL);
return g_date_time_add_months (self->date, 1);
}
static GDateTime*
gcal_month_view_get_previous_date (GcalView *view)
{
GcalMonthView *self = GCAL_MONTH_VIEW (view);
g_assert (self->date != NULL);
return g_date_time_add_months (self->date, -1);
}
static void
gcal_view_interface_init (GcalViewInterface *iface)
{
@@ -1228,6 +1247,8 @@ gcal_view_interface_init (GcalViewInterface *iface)
iface->clear_marks = gcal_month_view_clear_marks;
iface->get_children_by_uuid = gcal_month_view_get_children_by_uuid;
iface->update_subscription = gcal_month_view_update_subscription;
iface->get_next_date = gcal_month_view_get_next_date;
iface->get_previous_date = gcal_month_view_get_previous_date;
}
+63
View File
@@ -210,3 +210,66 @@ gcal_view_update_subscription (GcalView *self)
GCAL_VIEW_GET_IFACE (self)->update_subscription (self);
}
/**
* gcal_view_get_next_date:
* @self: a #GcalView
*
* Retrieves the next date from @self. Different views have
* different time ranges (e.g. day view ranges 1 day, week
* view ranges 1 week, etc) and pressing the next button
* of the main window may advance the time in different steps.
*
* Returns: (transfer full): a #GDateTime
*/
GDateTime*
gcal_view_get_next_date (GcalView *self)
{
g_autoptr (GDateTime) next_date = NULL;
g_return_val_if_fail (GCAL_IS_VIEW (self), NULL);
g_return_val_if_fail (GCAL_VIEW_GET_IFACE (self)->get_next_date, NULL);
next_date = GCAL_VIEW_GET_IFACE (self)->get_next_date (self);
#ifdef GCAL_ENABLE_TRACE
{
g_autofree gchar *str = NULL;
str = g_date_time_format (next_date, "%x %X %z");
g_debug ("%s's next date: %s", G_OBJECT_TYPE_NAME (self), str);
}
#endif
return g_steal_pointer (&next_date);
}
/**
* gcal_view_get_previous_date:
* @self: a #GcalView
*
* Retrieves the previous date from @self.
*
* Returns: (transfer full): a #GDateTime
*/
GDateTime*
gcal_view_get_previous_date (GcalView *self)
{
g_autoptr (GDateTime) previous_date = NULL;
g_return_val_if_fail (GCAL_IS_VIEW (self), NULL);
g_return_val_if_fail (GCAL_VIEW_GET_IFACE (self)->get_previous_date, NULL);
previous_date = GCAL_VIEW_GET_IFACE (self)->get_previous_date (self);
#ifdef GCAL_ENABLE_TRACE
{
g_autofree gchar *str = NULL;
str = g_date_time_format (previous_date, "%x %X %z");
g_debug ("%s's previous date: %s", G_OBJECT_TYPE_NAME (self), str);
}
#endif
return g_steal_pointer (&previous_date);
}
+7
View File
@@ -50,6 +50,10 @@ struct _GcalViewInterface
void (*update_subscription) (GcalView *self);
GDateTime* (*get_next_date) (GcalView *self);
GDateTime* (*get_previous_date) (GcalView *self);
/* Marks related API */
void (*clear_marks) (GcalView *view);
@@ -73,6 +77,9 @@ GList* gcal_view_get_children_by_uuid (GcalView
void gcal_view_update_subscription (GcalView *self);
GDateTime* gcal_view_get_next_date (GcalView *self);
GDateTime* gcal_view_get_previous_date (GcalView *self);
G_END_DECLS
+21
View File
@@ -248,6 +248,25 @@ gcal_week_view_update_subscription (GcalView *view)
range_end);
}
static GDateTime*
gcal_week_view_get_next_date (GcalView *view)
{
GcalWeekView *self = GCAL_WEEK_VIEW (view);
g_assert (self->date != NULL);
return g_date_time_add_weeks (self->date, 1);
}
static GDateTime*
gcal_week_view_get_previous_date (GcalView *view)
{
GcalWeekView *self = GCAL_WEEK_VIEW (view);
g_assert (self->date != NULL);
return g_date_time_add_weeks (self->date, -1);
}
static void
gcal_view_interface_init (GcalViewInterface *iface)
{
@@ -256,6 +275,8 @@ gcal_view_interface_init (GcalViewInterface *iface)
iface->get_children_by_uuid = gcal_week_view_get_children_by_uuid;
iface->clear_marks = gcal_week_view_clear_marks;
iface->update_subscription = gcal_week_view_update_subscription;
iface->get_next_date = gcal_week_view_get_next_date;
iface->get_previous_date = gcal_week_view_get_previous_date;
}
static void
+21
View File
@@ -1609,6 +1609,25 @@ gcal_year_view_update_subscription (GcalView *view)
range_end);
}
static GDateTime*
gcal_year_view_get_next_date (GcalView *view)
{
GcalYearView *self = GCAL_YEAR_VIEW (view);
g_assert (self->date != NULL);
return g_date_time_add_years (self->date, 1);
}
static GDateTime*
gcal_year_view_get_previous_date (GcalView *view)
{
GcalYearView *self = GCAL_YEAR_VIEW (view);
g_assert (self->date != NULL);
return g_date_time_add_years (self->date, -1);
}
static void
gcal_view_interface_init (GcalViewInterface *iface)
{
@@ -1617,6 +1636,8 @@ gcal_view_interface_init (GcalViewInterface *iface)
iface->set_date = gcal_year_view_set_date;
iface->get_children_by_uuid = gcal_year_view_get_children_by_uuid;
iface->update_subscription = gcal_year_view_update_subscription;
iface->get_next_date = gcal_year_view_get_next_date;
iface->get_previous_date = gcal_year_view_get_previous_date;
}
static void