From 876fda4f55cf583813041df7471ceb3ae2e0e23e Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Thu, 21 Jul 2022 11:09:16 +0200 Subject: Handle the long press gesture on browser items Unfortunately, this doesn't work on X11, though Wayland seems fine. --- fiv-browser.c | 40 ++++++++++++++++++++++++++++++++++++++++ fiv-view.c | 4 ++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/fiv-browser.c b/fiv-browser.c index cd6c3e3..bff5b13 100644 --- a/fiv-browser.c +++ b/fiv-browser.c @@ -1473,6 +1473,37 @@ fiv_browser_popup_menu(GtkWidget *widget) return TRUE; } +static void +on_long_press(GtkGestureLongPress *lp, gdouble x, gdouble y, gpointer user_data) +{ + FivBrowser *self = FIV_BROWSER(user_data); + const Entry *entry = entry_at(self, x, y); + abort_button_tracking(self); + if (!entry) + return; + + GtkGesture *gesture = GTK_GESTURE(lp); + GdkEventSequence *sequence = gtk_gesture_get_last_updated_sequence(gesture); + const GdkEvent *event = gtk_gesture_get_last_event(gesture, sequence); + + GtkWidget *widget = GTK_WIDGET(self); + GdkWindow *window = gtk_widget_get_window(widget); +#ifdef GDK_WINDOWING_X11 + // FIXME: Once the finger is lifted, this menu is immediately closed. + if (GDK_IS_X11_WINDOW(window)) + return; +#endif // GDK_WINDOWING_X11 + + GFile *file = g_file_new_for_uri(entry->uri); + gtk_menu_popup_at_rect(fiv_context_menu_new(widget, file), window, + &(GdkRectangle) {.x = x, .y = y}, GDK_GRAVITY_NORTH_WEST, + GDK_GRAVITY_NORTH_WEST, event); + g_object_unref(file); + + self->selected = entry; + gtk_widget_queue_draw(widget); +} + static void fiv_browser_style_updated(GtkWidget *widget) { @@ -1621,6 +1652,15 @@ fiv_browser_init(FivBrowser *self) g_signal_connect_swapped(gtk_settings_get_default(), "notify::gtk-icon-theme-name", G_CALLBACK(reload_thumbnails), self); + + GtkGesture *lp = gtk_gesture_long_press_new(GTK_WIDGET(self)); + gtk_gesture_single_set_touch_only(GTK_GESTURE_SINGLE(lp), TRUE); + gtk_event_controller_set_propagation_phase( + GTK_EVENT_CONTROLLER(lp), GTK_PHASE_BUBBLE); + g_object_set_data_full( + G_OBJECT(self), "fiv-browser-long-press-gesture", lp, g_object_unref); + + g_signal_connect(lp, "pressed", G_CALLBACK(on_long_press), self); } // --- Public interface -------------------------------------------------------- diff --git a/fiv-view.c b/fiv-view.c index 484f314..ff201db 100644 --- a/fiv-view.c +++ b/fiv-view.c @@ -1394,10 +1394,10 @@ fiv_view_init(FivView *self) self->scale = 1.0; GtkGesture *drag = gtk_gesture_drag_new(GTK_WIDGET(self)); - g_object_set_data_full( - G_OBJECT(self), "fiv-view-drag-gesture", drag, g_object_unref); gtk_event_controller_set_propagation_phase( GTK_EVENT_CONTROLLER(drag), GTK_PHASE_BUBBLE); + g_object_set_data_full( + G_OBJECT(self), "fiv-view-drag-gesture", drag, g_object_unref); // GtkScrolledWindow's internal GtkGestureDrag is set to only look for // touch events (and its "event_controllers" are perfectly private, -- cgit v1.2.3-70-g09d2