Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
scroll_latch.c
Go to the documentation of this file.
1/*
2 * This file is part of Ansel,
3 * Copyright (C) 2026 Guillaume Stutin.
4 *
5 * Ansel is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * Ansel is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Ansel. If not, see <http://www.gnu.org/licenses/>.
17 */
18
20#include "system/macros.h" // IS_NULL_PTR
21#include "system/mem_alloc.h" // dt_free
22
23// How long the wheel may rest before a step goes to the widget under the pointer again.
24#define DT_SCROLL_LATCH_REST_MS 500
25// How far the pointer may drift from where the last step found it: a hand on the wheel nudges the mouse.
26#define DT_SCROLL_LATCH_DRIFT_PX 4.0
27
28#define DT_SCROLL_LATCH_KEY "dt-scroll-latch"
29
30typedef struct dt_scroll_latch_t
31{
33 GtkEventController *steps; // on the child, capture phase while latched, none otherwise
34 GtkEventController *motion; // on the child, capture phase while latched, none otherwise
35 gdouble x_root; // pointer at the last step the window took
36 gdouble y_root;
37 guint rest; // timeout ending the latch, 0 while not latched
39
41{
42 gtk_event_controller_set_propagation_phase(latch->steps, GTK_PHASE_NONE);
43 gtk_event_controller_set_propagation_phase(latch->motion, GTK_PHASE_NONE);
44 if(latch->rest) g_source_remove(latch->rest);
45 latch->rest = 0;
46}
47
48static gboolean _latch_rested(gpointer data)
49{
51 latch->rest = 0;
52 _latch_release(latch);
53 return G_SOURCE_REMOVE;
54}
55
56// Runs before the window scrolls, for a step no child took and for a step the latch hands over.
57static gboolean _window_step(GtkWidget *widget, const GdkEventScroll *event, dt_scroll_latch_t *latch)
58{
59 // The window takes a step only along an axis that shows a scrollbar.
60 GtkScrolledWindow *window = GTK_SCROLLED_WINDOW(widget);
61 if(!gtk_widget_get_child_visible(gtk_scrolled_window_get_vscrollbar(window))
62 && !gtk_widget_get_child_visible(gtk_scrolled_window_get_hscrollbar(window)))
63 return FALSE;
64
65 latch->x_root = event->x_root;
66 latch->y_root = event->y_root;
67 gtk_event_controller_set_propagation_phase(latch->steps, GTK_PHASE_CAPTURE);
68 gtk_event_controller_set_propagation_phase(latch->motion, GTK_PHASE_CAPTURE);
69 if(latch->rest) g_source_remove(latch->rest);
70 latch->rest = g_timeout_add(DT_SCROLL_LATCH_REST_MS, _latch_rested, latch);
71 return FALSE;
72}
73
74// A window still gliding after a touchpad flick is still scrolling.
75static void _window_moved(GtkAdjustment *adjustment __attribute__((unused)), gpointer scrolled_window)
76{
77 dt_scroll_latch_t *latch = g_object_get_data(G_OBJECT(scrolled_window), DT_SCROLL_LATCH_KEY);
78 if(latch->rest == 0) return;
79 g_source_remove(latch->rest);
80 latch->rest = g_timeout_add(DT_SCROLL_LATCH_REST_MS, _latch_rested, latch);
81}
82
83// The steps controller has taken the step from the widget under the pointer: the window gets it instead.
85{
86 GdkEvent *event = gtk_get_current_event();
87 if(IS_NULL_PTR(event)) return;
88 gtk_widget_event(latch->scrolled_window, event);
89 gdk_event_free(event);
90}
91
92static void _step(GtkEventControllerScroll *controller __attribute__((unused)), gdouble dx __attribute__((unused)),
93 gdouble dy __attribute__((unused)), dt_scroll_latch_t *latch)
94{
95 _step_to_window(latch);
96}
97
98// The last event of a touchpad gesture carries no delta: it starts the window's deceleration.
99static void _step_end(GtkEventControllerScroll *controller __attribute__((unused)), dt_scroll_latch_t *latch)
100{
101 _step_to_window(latch);
102}
103
104// A pointer that moves is aiming at something: the next step goes to what it points at.
105static void _pointer_moved(GtkEventControllerMotion *controller __attribute__((unused)), gdouble x __attribute__((unused)),
106 gdouble y __attribute__((unused)), dt_scroll_latch_t *latch)
107{
108 GdkEvent *event = gtk_get_current_event();
109 if(IS_NULL_PTR(event)) return;
110 gdouble x_root = 0.0;
111 gdouble y_root = 0.0;
112 gdk_event_get_root_coords(event, &x_root, &y_root);
113 gdk_event_free(event);
114
115 const gdouble dx = x_root - latch->x_root;
116 const gdouble dy = y_root - latch->y_root;
118}
119
120static void _latch_free(gpointer data)
121{
123 if(latch->rest) g_source_remove(latch->rest);
124 g_object_unref(latch->steps);
125 g_object_unref(latch->motion);
126 dt_free(latch);
127}
128
129void dt_ui_scroll_latch(GtkWidget *scrolled_window)
130{
131 GtkWidget *child = gtk_bin_get_child(GTK_BIN(scrolled_window));
132 dt_scroll_latch_t *latch = calloc(1, sizeof(dt_scroll_latch_t));
133 latch->scrolled_window = scrolled_window;
134 g_object_set_data_full(G_OBJECT(scrolled_window), DT_SCROLL_LATCH_KEY, latch, _latch_free);
135
136 // Held on the child, not on the window: in the capture phase, the window's own handler runs
137 // first and stops any deceleration. Held on the window, a touchpad step handed over would
138 // start the deceleration that the window's handler then stops.
139 latch->steps = gtk_event_controller_scroll_new(child, GTK_EVENT_CONTROLLER_SCROLL_BOTH_AXES);
140 gtk_event_controller_set_propagation_phase(latch->steps, GTK_PHASE_NONE);
141 g_signal_connect(G_OBJECT(latch->steps), "scroll", G_CALLBACK(_step), latch);
142 g_signal_connect(G_OBJECT(latch->steps), "scroll-end", G_CALLBACK(_step_end), latch);
143
144 latch->motion = gtk_event_controller_motion_new(child);
145 gtk_event_controller_set_propagation_phase(latch->motion, GTK_PHASE_NONE);
146 gtk_widget_add_events(child, GDK_POINTER_MOTION_MASK);
147 g_signal_connect(G_OBJECT(latch->motion), "motion", G_CALLBACK(_pointer_moved), latch);
148
149 g_signal_connect(G_OBJECT(scrolled_window), "scroll-event", G_CALLBACK(_window_step), latch);
150 g_signal_connect_object(gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrolled_window)), "value-changed",
151 G_CALLBACK(_window_moved), scrolled_window, 0);
152 g_signal_connect_object(gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(scrolled_window)), "value-changed",
153 G_CALLBACK(_window_moved), scrolled_window, 0);
154}
#define FALSE
Definition ashift_lsd.c:158
static const float x
struct _GtkWidget GtkWidget
GtkWidget, opaque, spelled exactly as GTK spells it.
Definition colorspaces.h:98
GtkWidget * window
void * data
Definition geometry.h:111
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
Definition macros.h:96
#define dt_free(ptr)
g_free() ptr and set it to NULL, skipping both if it is already NULL.
Definition mem_alloc.h:171
static void _step_to_window(dt_scroll_latch_t *latch)
static void _window_moved(GtkAdjustment *adjustment __attribute__((unused)), gpointer scrolled_window)
#define DT_SCROLL_LATCH_DRIFT_PX
static void _latch_release(dt_scroll_latch_t *latch)
#define DT_SCROLL_LATCH_KEY
void dt_ui_scroll_latch(GtkWidget *scrolled_window)
Keep the mouse wheel on scrolled_window once it has started scrolling.
static void _step_end(GtkEventControllerScroll *controller __attribute__((unused)), dt_scroll_latch_t *latch)
static void _step(GtkEventControllerScroll *controller __attribute__((unused)), gdouble dx __attribute__((unused)), gdouble dy __attribute__((unused)), dt_scroll_latch_t *latch)
static void _latch_free(gpointer data)
#define DT_SCROLL_LATCH_REST_MS
static void _pointer_moved(GtkEventControllerMotion *controller __attribute__((unused)), gdouble x __attribute__((unused)), gdouble y __attribute__((unused)), dt_scroll_latch_t *latch)
static gboolean _latch_rested(gpointer data)
static gboolean _window_step(GtkWidget *widget, const GdkEventScroll *event, dt_scroll_latch_t *latch)
float dt_aligned_pixel_simd_t __attribute__((vector_size(16), aligned(16)))
Apply one channel's tone curve to each of the three colour channels, or pass the channel through unto...
Definition simd.h:55
GtkEventController * steps
GtkEventController * motion
GtkWidget * scrolled_window