Simplify view dragging code a bit

This commit is contained in:
Přemysl Eric Janouch 2022-07-16 14:53:51 +02:00
parent c55500f51a
commit ca57c2632a
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 16 additions and 26 deletions

42
fiv.c
View File

@ -1324,8 +1324,8 @@ on_button_press_browser_paned(
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void static void
on_view_scroller_drag_begin( on_view_scroller_drag_begin(GtkGestureDrag *self, G_GNUC_UNUSED gdouble start_x,
GtkGestureDrag *self, gdouble start_x, gdouble start_y, gpointer user_data) G_GNUC_UNUSED gdouble start_y, gpointer user_data)
{ {
GtkGesture *gesture = GTK_GESTURE(self); GtkGesture *gesture = GTK_GESTURE(self);
GdkEventSequence *sequence = gtk_gesture_get_last_updated_sequence(gesture); GdkEventSequence *sequence = gtk_gesture_get_last_updated_sequence(gesture);
@ -1347,34 +1347,24 @@ on_view_scroller_drag_begin(
gdk_window_set_cursor(window, cursor); gdk_window_set_cursor(window, cursor);
g_object_unref(cursor); g_object_unref(cursor);
double *last = user_data; double *init = user_data;
last[0] = start_x; GtkScrolledWindow *sw = GTK_SCROLLED_WINDOW(
last[1] = start_y; gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(self)));
init[0] = gtk_adjustment_get_value(gtk_scrolled_window_get_hadjustment(sw));
init[1] = gtk_adjustment_get_value(gtk_scrolled_window_get_vadjustment(sw));
} }
static void static void
on_view_scroller_drag(GtkGestureDrag *self, gdouble offset_x, gdouble offset_y, on_view_scroller_drag(GtkGestureDrag *self, gdouble offset_x, gdouble offset_y,
gpointer user_data) gpointer user_data)
{ {
double start_x = 0, start_y = 0; const double *init = user_data;
gtk_gesture_drag_get_start_point(self, &start_x, &start_y);
double *last = user_data,
diff_x = (start_x + offset_x) - last[0],
diff_y = (start_y + offset_y) - last[1];
last[0] = start_x + offset_x;
last[1] = start_y + offset_y;
GtkScrolledWindow *sw = GTK_SCROLLED_WINDOW( GtkScrolledWindow *sw = GTK_SCROLLED_WINDOW(
gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(self))); gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(self)));
GtkAdjustment *h = gtk_scrolled_window_get_hadjustment(sw); gtk_adjustment_set_value(
GtkAdjustment *v = gtk_scrolled_window_get_vadjustment(sw); gtk_scrolled_window_get_hadjustment(sw), init[0] - offset_x);
gtk_adjustment_set_value(
if (diff_x) gtk_scrolled_window_get_vadjustment(sw), init[1] - offset_y);
gtk_adjustment_set_value(h, gtk_adjustment_get_value(h) - diff_x);
if (diff_y)
gtk_adjustment_set_value(v, gtk_adjustment_get_value(v) - diff_y);
} }
static void static void
@ -1968,13 +1958,13 @@ main(int argc, char *argv[])
// Though note that the GdkWindow doesn't register for touch events now. // Though note that the GdkWindow doesn't register for touch events now.
gtk_gesture_single_set_exclusive(GTK_GESTURE_SINGLE(drag), TRUE); gtk_gesture_single_set_exclusive(GTK_GESTURE_SINGLE(drag), TRUE);
double last_drag_point[2] = {}; double init_drag_values[2] = {};
g_signal_connect(drag, "drag-begin", g_signal_connect(drag, "drag-begin",
G_CALLBACK(on_view_scroller_drag_begin), last_drag_point); G_CALLBACK(on_view_scroller_drag_begin), init_drag_values);
g_signal_connect(drag, "drag-update", g_signal_connect(drag, "drag-update",
G_CALLBACK(on_view_scroller_drag), last_drag_point); G_CALLBACK(on_view_scroller_drag), init_drag_values);
g_signal_connect(drag, "drag-end", g_signal_connect(drag, "drag-end",
G_CALLBACK(on_view_scroller_drag_end), last_drag_point); G_CALLBACK(on_view_scroller_drag_end), init_drag_values);
// We need to hide it together with the separator. // We need to hide it together with the separator.
g.view_toolbar = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); g.view_toolbar = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);