st/scroll-bar: Add orientation property

Like we did for `BoxLayout`, add an `orientation` property that
better expresses the orientation (duh) and is consistent with
Clutter and GTK.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3614>
This commit is contained in:
Florian Müllner 2025-01-25 02:08:44 +01:00
parent c5546dbe12
commit f4f02fe4dd
2 changed files with 74 additions and 19 deletions

View File

@ -66,7 +66,7 @@ struct _StScrollBarPrivate
guint paging_source_id; guint paging_source_id;
guint paging_event_no; guint paging_event_no;
guint vertical : 1; ClutterOrientation orientation;
}; };
G_DEFINE_TYPE_WITH_PRIVATE (StScrollBar, st_scroll_bar, ST_TYPE_WIDGET) G_DEFINE_TYPE_WITH_PRIVATE (StScrollBar, st_scroll_bar, ST_TYPE_WIDGET)
@ -78,6 +78,7 @@ enum
PROP_0, PROP_0,
PROP_ADJUSTMENT, PROP_ADJUSTMENT,
PROP_ORIENTATION,
PROP_VERTICAL, PROP_VERTICAL,
N_PROPS N_PROPS
@ -102,24 +103,49 @@ handle_button_press_event_cb (ClutterActor *actor,
static void stop_scrolling (StScrollBar *bar); static void stop_scrolling (StScrollBar *bar);
static void ClutterOrientation
st_scroll_bar_set_vertical (StScrollBar *bar, st_scroll_bar_get_orientation (StScrollBar *bar)
gboolean vertical)
{ {
StScrollBarPrivate *priv = ST_SCROLL_BAR_PRIVATE (bar); StScrollBarPrivate *priv;
if (priv->vertical == vertical) g_return_val_if_fail (ST_IS_SCROLL_BAR (bar), CLUTTER_ORIENTATION_HORIZONTAL);
priv = ST_SCROLL_BAR_PRIVATE (bar);
return priv->orientation;
}
void
st_scroll_bar_set_orientation (StScrollBar *bar,
ClutterOrientation orientation)
{
StScrollBarPrivate *priv;
g_return_if_fail (ST_IS_SCROLL_BAR (bar));
priv = ST_SCROLL_BAR_PRIVATE (bar);
if (priv->orientation == orientation)
return; return;
priv->vertical = vertical; priv->orientation = orientation;
if (priv->vertical) if (priv->orientation == CLUTTER_ORIENTATION_VERTICAL)
clutter_actor_set_name (CLUTTER_ACTOR (priv->handle), clutter_actor_set_name (CLUTTER_ACTOR (priv->handle),
"vhandle"); "vhandle");
else else
clutter_actor_set_name (CLUTTER_ACTOR (priv->handle), clutter_actor_set_name (CLUTTER_ACTOR (priv->handle),
"hhandle"); "hhandle");
clutter_actor_queue_relayout (CLUTTER_ACTOR (bar)); clutter_actor_queue_relayout (CLUTTER_ACTOR (bar));
g_object_notify_by_pspec (G_OBJECT (bar), props[PROP_ORIENTATION]);
}
static void
st_scroll_bar_set_vertical (StScrollBar *bar,
gboolean vertical)
{
st_scroll_bar_set_orientation (bar,
vertical ? CLUTTER_ORIENTATION_VERTICAL
: CLUTTER_ORIENTATION_HORIZONTAL);
g_object_notify_by_pspec (G_OBJECT (bar), props[PROP_VERTICAL]); g_object_notify_by_pspec (G_OBJECT (bar), props[PROP_VERTICAL]);
} }
@ -137,8 +163,12 @@ st_scroll_bar_get_property (GObject *gobject,
g_value_set_object (value, priv->adjustment); g_value_set_object (value, priv->adjustment);
break; break;
case PROP_ORIENTATION:
g_value_set_enum (value, priv->orientation);
break;
case PROP_VERTICAL: case PROP_VERTICAL:
g_value_set_boolean (value, priv->vertical); g_value_set_boolean (value, priv->orientation == CLUTTER_ORIENTATION_VERTICAL);
break; break;
default: default:
@ -161,6 +191,10 @@ st_scroll_bar_set_property (GObject *gobject,
st_scroll_bar_set_adjustment (bar, g_value_get_object (value)); st_scroll_bar_set_adjustment (bar, g_value_get_object (value));
break; break;
case PROP_ORIENTATION:
st_scroll_bar_set_orientation (bar, g_value_get_enum (value));
break;
case PROP_VERTICAL: case PROP_VERTICAL:
st_scroll_bar_set_vertical (bar, g_value_get_boolean (value)); st_scroll_bar_set_vertical (bar, g_value_get_boolean (value));
break; break;
@ -249,7 +283,7 @@ scroll_bar_allocate_children (StScrollBar *bar,
else else
position = (value - lower) / (upper - lower - page_size); position = (value - lower) / (upper - lower - page_size);
if (priv->vertical) if (priv->orientation == CLUTTER_ORIENTATION_VERTICAL)
{ {
avail_size = content_box.y2 - content_box.y1; avail_size = content_box.y2 - content_box.y1;
handle_size = increment * avail_size; handle_size = increment * avail_size;
@ -309,7 +343,7 @@ st_scroll_bar_get_preferred_width (ClutterActor *self,
_st_actor_get_preferred_width (priv->handle, for_height, TRUE, _st_actor_get_preferred_width (priv->handle, for_height, TRUE,
&handle_min_width, &handle_natural_width); &handle_min_width, &handle_natural_width);
if (priv->vertical) if (priv->orientation == CLUTTER_ORIENTATION_VERTICAL)
{ {
if (min_width_p) if (min_width_p)
*min_width_p = MAX (trough_min_width, handle_min_width); *min_width_p = MAX (trough_min_width, handle_min_width);
@ -349,7 +383,7 @@ st_scroll_bar_get_preferred_height (ClutterActor *self,
_st_actor_get_preferred_height (priv->handle, for_width, TRUE, _st_actor_get_preferred_height (priv->handle, for_width, TRUE,
&handle_min_height, &handle_natural_height); &handle_min_height, &handle_natural_height);
if (priv->vertical) if (priv->orientation == CLUTTER_ORIENTATION_VERTICAL)
{ {
if (min_height_p) if (min_height_p)
*min_height_p = trough_min_height + handle_min_height; *min_height_p = trough_min_height + handle_min_height;
@ -485,7 +519,7 @@ st_scroll_bar_scroll_event (ClutterActor *actor,
if (direction == CLUTTER_TEXT_DIRECTION_RTL) if (direction == CLUTTER_TEXT_DIRECTION_RTL)
delta_x *= -1; delta_x *= -1;
if (priv->vertical) if (priv->orientation == CLUTTER_ORIENTATION_VERTICAL)
st_adjustment_adjust_for_scroll_event (priv->adjustment, delta_y); st_adjustment_adjust_for_scroll_event (priv->adjustment, delta_y);
else else
st_adjustment_adjust_for_scroll_event (priv->adjustment, delta_x); st_adjustment_adjust_for_scroll_event (priv->adjustment, delta_x);
@ -536,6 +570,17 @@ st_scroll_bar_class_init (StScrollBarClass *klass)
ST_TYPE_ADJUSTMENT, ST_TYPE_ADJUSTMENT,
ST_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY); ST_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
/**
* StScrollBar:orientation:
*
* The orientation of the #StScrollBar, horizontal or vertical.
*/
props[PROP_ORIENTATION] =
g_param_spec_enum ("orientation", NULL, NULL,
CLUTTER_TYPE_ORIENTATION,
CLUTTER_ORIENTATION_HORIZONTAL,
ST_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
/** /**
* StScrollBar:vertical: * StScrollBar:vertical:
* *
@ -585,6 +630,7 @@ move_slider (StScrollBar *bar,
{ {
StScrollBarPrivate *priv = st_scroll_bar_get_instance_private (bar); StScrollBarPrivate *priv = st_scroll_bar_get_instance_private (bar);
ClutterTextDirection direction; ClutterTextDirection direction;
gboolean vertical;
gdouble position, lower, upper, page_size; gdouble position, lower, upper, page_size;
gfloat ux, uy, pos, size; gfloat ux, uy, pos, size;
@ -594,7 +640,9 @@ move_slider (StScrollBar *bar,
if (!clutter_actor_transform_stage_point (priv->trough, x, y, &ux, &uy)) if (!clutter_actor_transform_stage_point (priv->trough, x, y, &ux, &uy))
return; return;
if (priv->vertical) vertical = priv->orientation == CLUTTER_ORIENTATION_VERTICAL;
if (vertical)
size = clutter_actor_get_height (priv->trough) size = clutter_actor_get_height (priv->trough)
- clutter_actor_get_height (priv->handle); - clutter_actor_get_height (priv->handle);
else else
@ -604,7 +652,7 @@ move_slider (StScrollBar *bar,
if (size == 0) if (size == 0)
return; return;
if (priv->vertical) if (vertical)
pos = uy - priv->y_origin; pos = uy - priv->y_origin;
else else
pos = ux - priv->x_origin; pos = ux - priv->x_origin;
@ -619,7 +667,7 @@ move_slider (StScrollBar *bar,
&page_size); &page_size);
direction = clutter_actor_get_text_direction (CLUTTER_ACTOR (bar)); direction = clutter_actor_get_text_direction (CLUTTER_ACTOR (bar));
if (!priv->vertical && direction == CLUTTER_TEXT_DIRECTION_RTL) if (!vertical && direction == CLUTTER_TEXT_DIRECTION_RTL)
pos = size - pos; pos = size - pos;
position = ((pos / size) position = ((pos / size)
@ -719,6 +767,7 @@ trough_paging_cb (StScrollBar *self)
{ {
StScrollBarPrivate *priv = st_scroll_bar_get_instance_private (self); StScrollBarPrivate *priv = st_scroll_bar_get_instance_private (self);
ClutterTextDirection direction; ClutterTextDirection direction;
gboolean vertical;
g_autoptr (ClutterTransition) transition = NULL; g_autoptr (ClutterTransition) transition = NULL;
StSettings *settings; StSettings *settings;
gfloat handle_pos, event_pos, tx, ty; gfloat handle_pos, event_pos, tx, ty;
@ -729,6 +778,8 @@ trough_paging_cb (StScrollBar *self)
gulong mode; gulong mode;
vertical = priv->orientation == CLUTTER_ORIENTATION_VERTICAL;
if (priv->paging_event_no == 0) if (priv->paging_event_no == 0)
{ {
/* Scroll on after initial timeout. */ /* Scroll on after initial timeout. */
@ -766,7 +817,7 @@ trough_paging_cb (StScrollBar *self)
&value, NULL, NULL, &value, NULL, NULL,
NULL, &page_increment, NULL); NULL, &page_increment, NULL);
if (priv->vertical) if (vertical)
handle_pos = clutter_actor_get_y (priv->handle); handle_pos = clutter_actor_get_y (priv->handle);
else else
handle_pos = clutter_actor_get_x (priv->handle); handle_pos = clutter_actor_get_x (priv->handle);
@ -777,10 +828,10 @@ trough_paging_cb (StScrollBar *self)
&tx, &ty); &tx, &ty);
direction = clutter_actor_get_text_direction (CLUTTER_ACTOR (self)); direction = clutter_actor_get_text_direction (CLUTTER_ACTOR (self));
if (!priv->vertical && direction == CLUTTER_TEXT_DIRECTION_RTL) if (!vertical && direction == CLUTTER_TEXT_DIRECTION_RTL)
page_increment *= -1; page_increment *= -1;
if (priv->vertical) if (vertical)
event_pos = ty; event_pos = ty;
else else
event_pos = tx; event_pos = tx;

View File

@ -47,4 +47,8 @@ void st_scroll_bar_set_adjustment (StScrollBar *bar,
StAdjustment *adjustment); StAdjustment *adjustment);
StAdjustment *st_scroll_bar_get_adjustment (StScrollBar *bar); StAdjustment *st_scroll_bar_get_adjustment (StScrollBar *bar);
void st_scroll_bar_set_orientation (StScrollBar *bar,
ClutterOrientation orientation);
ClutterOrientation st_scroll_bar_get_orientation (StScrollBar *bar);
G_END_DECLS G_END_DECLS