From c3635967bcbdbc1946c0b4f9fb36cd3cf49ad09a Mon Sep 17 00:00:00 2001 From: Zoey Ahmed Date: Fri, 2 Jan 2026 23:33:59 +0000 Subject: [PATCH] window: Go back/forth views using hardware buttons Refactor `on_window_previous_date_activated_cb` and `on_window_next_date_activated_cb` into separate methods for going to the prev/next view, to be used in the callbacks for both the forward/back buttons in the UI, and for the `Gtk.GestureClick` callbacks that listens for the hardware buttons to be pressed. Closes: https://gitlab.gnome.org/GNOME/gnome-calendar/-/issues/1494 Part-of: --- src/gui/gcal-window.blp | 10 ++++++ src/gui/gcal-window.c | 69 ++++++++++++++++++++++++++++++----------- 2 files changed, 61 insertions(+), 18 deletions(-) diff --git a/src/gui/gcal-window.blp b/src/gui/gcal-window.blp index 1acacbffe..5f4d94f80 100644 --- a/src/gui/gcal-window.blp +++ b/src/gui/gcal-window.blp @@ -105,6 +105,16 @@ template $GcalWindow: Adw.ApplicationWindow { } } + GestureClick { + button: 8; // Hardware back button. + pressed => $on_back_button_pressed_cb(template) not-swapped; + } + + GestureClick { + button: 9; // Hardware forward button. + pressed => $on_forward_button_pressed_cb(template) not-swapped; + } + Adw.ToastOverlay overlay { $GcalDropOverlay drop_overlay { overlayed: Adw.StatusPage drop_status_page { diff --git a/src/gui/gcal-window.c b/src/gui/gcal-window.c index 39c60698b..016b28994 100644 --- a/src/gui/gcal-window.c +++ b/src/gui/gcal-window.c @@ -488,6 +488,30 @@ filter_ics_files (GcalWindow *self, g_task_run_in_thread (task, filter_ics_files_thread); } +static void +switch_next_view (GcalWindow *self) +{ + g_autoptr (GDateTime) next_date = NULL; + GcalView *current_view; + + 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 +switch_prev_view (GcalWindow *self) +{ + g_autoptr (GDateTime) previous_date = NULL; + GcalView *current_view; + + current_view = GCAL_VIEW (self->views[self->active_view]); + previous_date = gcal_view_get_previous_date (current_view); + + update_active_date (self, previous_date); +} + /* * Callbacks */ @@ -534,20 +558,33 @@ on_view_action_activated (GSimpleAction *action, GCAL_EXIT; } +static void +on_back_button_pressed_cb (GtkGestureClick *gesture_click, + gint n_press, + gdouble x, + gdouble y, + gpointer *user_data) +{ + switch_prev_view (GCAL_WINDOW (user_data)); +} + +static void +on_forward_button_pressed_cb (GtkGestureClick *gesture_click, + gint n_press, + gdouble x, + gdouble y, + gpointer *user_data) +{ + + switch_next_view (GCAL_WINDOW (user_data)); +} + 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); + switch_next_view (GCAL_WINDOW (user_data)); } static void @@ -604,15 +641,7 @@ 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); + switch_prev_view (GCAL_WINDOW (user_data)); } static void @@ -1405,6 +1434,10 @@ gcal_window_class_init (GcalWindowClass *klass) /* Drop Target related */ gtk_widget_class_bind_template_callback (widget_class, on_drop_target_accept_cb); gtk_widget_class_bind_template_callback (widget_class, on_drop_target_drop_cb); + + /* Change date related*/ + gtk_widget_class_bind_template_callback (widget_class, on_back_button_pressed_cb); + gtk_widget_class_bind_template_callback (widget_class, on_forward_button_pressed_cb); } static void -- GitLab