scrolledwindow: keep the wheel while scrolling

A child that scrolls too no longer takes the wheel away in the middle
of scrolling the outer window.
This commit is contained in:
Daemonratte 2026-10-17 17:22:23 +02:00
commit 3ac60bfbcf
3 changed files with 121 additions and 14 deletions

View file

@ -1460,17 +1460,6 @@ rewrite_event_for_window (GdkEvent *event,
return event;
}
/* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
* then what X11 does is deliver the event normally if it was going to this
* client, otherwise, delivers it in terms of the grab window. This function
* rewrites events to the effect that events going to the same window group
* are delivered normally, otherwise, the event is delivered in terms of the
* grab window.
*/
/* Smooth scroll events go where GTK+ itself scrolls: to scrollbars
* and scrolled windows that nobody else hears a scroll event before.
* Everything else, most of all what programs do with scroll events,
* keeps the whole steps of the wheel, which X sends as well. */
/* Whether a program, or a widget's own "event", hears events of
* @widget before its scroll handler */
static gboolean
@ -1495,6 +1484,10 @@ hears_scroll (GtkWidget *widget)
(class->event && class->event != text_view_event);
}
/* Smooth scroll events go where GTK+ itself scrolls: to scrollbars
* and scrolled windows that nobody else hears a scroll event before.
* Everything else, most of all what programs do with scroll events,
* keeps the whole steps of the wheel, which X sends as well. */
static gboolean
smooth_scroll_wanted (GtkWidget *widget,
GdkEventScroll *event)
@ -1537,6 +1530,103 @@ smooth_scroll_wanted (GtkWidget *widget,
return FALSE;
}
/* A scrolled window that the wheel has just moved keeps the wheel for
* a while, even when the pointer now stands over something inside that
* scrolls too, a list or a spin button that moved under it: browsers
* do the same. It lets go after a second without moving, or when the
* pointer moves or a button or key is pressed; outside it, nothing
* changes anyway. */
#define SCROLL_LATCH_TIME 1000 /* milliseconds */
#define SCROLL_LATCH_MOVE 100 /* motion closer to the wheel is no motion */
static GtkWidget *scroll_latch;
static guint32 scroll_latch_time;
static void
scroll_latch_release (void)
{
if (scroll_latch)
g_object_remove_weak_pointer (G_OBJECT (scroll_latch), (gpointer *) &scroll_latch);
scroll_latch = NULL;
}
void
_gtk_scroll_latch (GtkWidget *widget,
guint32 time)
{
if (scroll_latch != widget)
{
scroll_latch_release ();
scroll_latch = widget;
g_object_add_weak_pointer (G_OBJECT (widget), (gpointer *) &scroll_latch);
}
scroll_latch_time = time;
}
static void
scroll_latch_see (GdkEvent *event)
{
switch (event->type)
{
case GDK_MOTION_NOTIFY:
if ((gint32) (event->motion.time - scroll_latch_time) > SCROLL_LATCH_MOVE)
scroll_latch_release ();
break;
case GDK_BUTTON_PRESS:
case GDK_KEY_PRESS:
scroll_latch_release ();
break;
default:
break;
}
}
/* Whether the scroll handler of @class is one of GTK+'s own, not one
* that a program's subclass brings */
static gboolean
scroll_handler_of_gtk (GtkWidgetClass *class)
{
GType type = G_TYPE_FROM_CLASS (class), parent;
/* the class that put the handler there */
while ((parent = g_type_parent (type)) != G_TYPE_INVALID &&
g_type_is_a (parent, GTK_TYPE_WIDGET) &&
GTK_WIDGET_CLASS (g_type_class_peek (parent))->scroll_event == class->scroll_event)
type = parent;
return g_str_has_prefix (g_type_name (type), "Gtk");
}
/* Only GTK+'s own widgets inside give way to the latch: where the
* program hears scroll events, it goes on hearing them. And only
* the way the latched window scrolls is kept. */
static GtkWidget *
scroll_latch_target (GtkWidget *widget,
GdkEvent *event)
{
GtkWidget *w;
gboolean inside = FALSE;
if ((gint32) (event->scroll.time - scroll_latch_time) >= SCROLL_LATCH_TIME ||
!gtk_widget_get_mapped (scroll_latch) || !gtk_widget_is_sensitive (scroll_latch) ||
!_gtk_scrolled_window_takes_smooth (scroll_latch, &event->scroll))
return widget;
for (w = widget; w && w != scroll_latch; w = w->parent)
{
GtkWidgetClass *class = GTK_WIDGET_GET_CLASS (w);
if (!gtk_widget_is_sensitive (w))
continue;
if (hears_scroll (w) || (class->scroll_event && !scroll_handler_of_gtk (class)))
return widget;
inside |= class->scroll_event != NULL;
}
return w && inside ? scroll_latch : widget;
}
/* Whether to pass on a scroll event: either the smooth ones or the
* whole steps that X made of them, by direction */
static gboolean
@ -1568,6 +1658,13 @@ scroll_event_wanted (GtkWidget *widget,
return TRUE;
}
/* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
* then what X11 does is deliver the event normally if it was going to this
* client, otherwise, delivers it in terms of the grab window. This function
* rewrites events to the effect that events going to the same window group
* are delivered normally, otherwise, the event is delivered in terms of the
* grab window.
*/
static GdkEvent *
rewrite_event_for_grabs (GdkEvent *event)
{
@ -1672,6 +1769,9 @@ gtk_main_do_event (GdkEvent *event)
window_group = gtk_main_get_window_group (event_widget);
if (scroll_latch)
scroll_latch_see (event);
/* Push the event onto a stack of current events for
* gtk_current_event_get().
*/
@ -1770,6 +1870,8 @@ gtk_main_do_event (GdkEvent *event)
break;
case GDK_SCROLL:
if (scroll_latch)
grab_widget = scroll_latch_target (grab_widget, event);
if (scroll_event_wanted (grab_widget, event))
gtk_propagate_event (grab_widget, event);
break;

View file

@ -193,6 +193,8 @@ gboolean _gtk_scroll_event_get_steps (GdkEventScroll *event,
gboolean *pixels);
gboolean _gtk_scrolled_window_takes_smooth (GtkWidget *widget,
GdkEventScroll *event);
void _gtk_scroll_latch (GtkWidget *widget,
guint32 time);
gboolean _gtk_translate_keyboard_accel_state (GdkKeymap *keymap,
guint hardware_keycode,

View file

@ -1751,7 +1751,7 @@ gtk_scrolled_window_scroll_event (GtkWidget *widget,
GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
GtkWidget *vbar = scrolled_window->vscrollbar, *hbar = scrolled_window->hscrollbar;
gdouble steps_x, steps_y;
gboolean pixels, glide = FALSE, handled = FALSE;
gboolean pixels, glide = FALSE, handled = FALSE, moved = FALSE;
g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
@ -1769,14 +1769,17 @@ gtk_scrolled_window_scroll_event (GtkWidget *widget,
if (steps_y != 0 && vbar && gtk_widget_get_visible (vbar))
{
handled = TRUE;
scroll_bar (vbar, steps_y, pixels, glide);
moved |= scroll_bar (vbar, steps_y, pixels, glide);
}
if (steps_x != 0 && hbar && gtk_widget_get_visible (hbar))
{
handled = TRUE;
scroll_bar (hbar, steps_x, pixels, glide);
moved |= scroll_bar (hbar, steps_x, pixels, glide);
}
if (moved)
_gtk_scroll_latch (widget, event->time);
return handled;
}