range: Make steppers use smooth autoscrolling

This makes them work much nicer than the old timer-based
implementation.
This commit is contained in:
Matthias Clasen 2015-02-22 20:02:02 -05:00
parent 3222de2f91
commit c73ab94d3f

View File

@ -244,6 +244,8 @@ static void update_slider_position (GtkRange *range,
gint mouse_x,
gint mouse_y);
static void stop_scrolling (GtkRange *range);
static void add_autoscroll (GtkRange *range);
static void remove_autoscroll (GtkRange *range);
/* Range methods */
@ -1842,7 +1844,7 @@ draw_stepper (GtkRange *range,
gtk_style_context_save (context);
/* don't set juction sides on scrollbar steppers */
/* don't set junction sides on scrollbar steppers */
if (gtk_style_context_has_class (context, GTK_STYLE_CLASS_SCROLLBAR))
gtk_style_context_set_junction_sides (context, GTK_JUNCTION_NONE);
else
@ -2429,6 +2431,13 @@ gtk_range_key_press (GtkWidget *widget,
return TRUE;
}
else if (priv->in_drag &&
(event->keyval == GDK_KEY_Shift_L ||
event->keyval == GDK_KEY_Shift_R))
{
update_zoom_state (range, !priv->zoom);
return TRUE;
}
return GTK_WIDGET_CLASS (gtk_range_parent_class)->key_press_event (widget, event);
}
@ -2441,7 +2450,9 @@ gtk_range_long_press_gesture_pressed (GtkGestureLongPress *gesture,
{
GtkRangePrivate *priv = range->priv;
if (!priv->zoom)
gtk_range_update_mouse_location (range);
if (priv->mouse_location == MOUSE_SLIDER && !priv->zoom)
{
if (priv->orientation == GTK_ORIENTATION_VERTICAL)
{
@ -2549,8 +2560,14 @@ gtk_range_multipress_gesture_pressed (GtkGestureMultiPress *gesture,
gtk_range_queue_draw_location (range, priv->mouse_location);
scroll = range_get_scroll_for_grab (range);
if (scroll != GTK_SCROLL_NONE)
gtk_range_add_step_timer (range, scroll);
if (scroll == GTK_SCROLL_START || scroll == GTK_SCROLL_END)
gtk_range_scroll (range, scroll);
else if (scroll != GTK_SCROLL_NONE)
{
remove_autoscroll (range);
range->priv->autoscroll_mode = scroll;
add_autoscroll (range);
}
}
else if ((priv->mouse_location == MOUSE_TROUGH &&
(source == GDK_SOURCE_TOUCHSCREEN ||
@ -2733,21 +2750,36 @@ autoscroll_cb (GtkWidget *widget,
GdkFrameClock *frame_clock,
gpointer data)
{
GtkRange *range;
GtkRange *range = GTK_RANGE (data);
GtkRangePrivate *priv = range->priv;
GtkAdjustment *adj = priv->adjustment;
gdouble increment;
gdouble value;
gboolean handled;
range = GTK_RANGE (data);
switch (priv->autoscroll_mode)
{
case GTK_SCROLL_STEP_FORWARD:
increment = gtk_adjustment_get_step_increment (adj) / AUTOSCROLL_FACTOR;
break;
case GTK_SCROLL_PAGE_FORWARD:
increment = gtk_adjustment_get_page_increment (adj) / AUTOSCROLL_FACTOR;
break;
case GTK_SCROLL_STEP_BACKWARD:
increment = - gtk_adjustment_get_step_increment (adj) / AUTOSCROLL_FACTOR;
break;
case GTK_SCROLL_PAGE_BACKWARD:
increment = - gtk_adjustment_get_page_increment (adj) / AUTOSCROLL_FACTOR;
break;
default:
g_assert_not_reached ();
break;
}
increment = gtk_adjustment_get_step_increment (range->priv->adjustment) / AUTOSCROLL_FACTOR;
if (range->priv->autoscroll_mode == GTK_SCROLL_STEP_BACKWARD)
increment *= -1;
value = gtk_adjustment_get_value (range->priv->adjustment);
value = gtk_adjustment_get_value (adj);
value += increment;
g_signal_emit (range, signals[CHANGE_VALUE], 0, GTK_SCROLL_JUMP, value,
&handled);
g_signal_emit (range, signals[CHANGE_VALUE], 0, GTK_SCROLL_JUMP, value, &handled);
return G_SOURCE_CONTINUE;
}
@ -2757,14 +2789,12 @@ add_autoscroll (GtkRange *range)
{
GtkRangePrivate *priv = range->priv;
if (priv->autoscroll_id != 0
|| priv->autoscroll_mode == GTK_SCROLL_NONE)
if (priv->autoscroll_id != 0 ||
priv->autoscroll_mode == GTK_SCROLL_NONE)
return;
priv->autoscroll_id = gtk_widget_add_tick_callback (GTK_WIDGET (range),
autoscroll_cb,
range,
NULL);
autoscroll_cb, range, NULL);
}
static void
@ -4032,7 +4062,7 @@ gtk_range_real_change_value (GtkRange *range,
value = floor ((value * power) + 0.5) / power;
}
if (priv->in_drag)
if (priv->in_drag || priv->autoscroll_id)
gtk_adjustment_set_value (priv->adjustment, value);
else
gtk_adjustment_animate_to_value (priv->adjustment, value);