From 779d32747d310e2a347d46ecc45d159750f738e5 Mon Sep 17 00:00:00 2001 From: Matthijs Velsink Date: Sun, 25 Aug 2024 13:07:21 +0200 Subject: [PATCH 1/3] Revert "Merge branch 'scalefix' into 'main'" This reverts commit 2799632c02379918c11fa1d6eea47a9b18428ac9, reversing changes made to 154035e76f19e54ef4649e355b2f5b8fbf77f148. That MR tried to fix #5171, but by doing a forced symmetry it introduced another issue for users on Libadwaita, see #6639. The logic can be much simplified, as the next commits show. --- gtk/gtkscale.c | 71 +++----------------------------------------------- 1 file changed, 4 insertions(+), 67 deletions(-) diff --git a/gtk/gtkscale.c b/gtk/gtkscale.c index 20482c3913..6ebabc5fac 100644 --- a/gtk/gtkscale.c +++ b/gtk/gtkscale.c @@ -159,7 +159,6 @@ struct _GtkScalePrivate guint draw_value : 1; guint value_pos : 2; - guint has_markup : 1; GtkScaleFormatValueFunc format_value_func; gpointer format_value_func_user_data; @@ -385,27 +384,6 @@ gtk_scale_allocate_value (GtkScale *scale) g_return_if_reached (); break; } - if (priv->has_markup && (priv->value_pos == GTK_POS_LEFT || priv->value_pos == GTK_POS_RIGHT)) - { - if (priv->top_marks_widget) - { - int marks_height; - gtk_widget_measure (priv->top_marks_widget, - GTK_ORIENTATION_VERTICAL, -1, - &marks_height, NULL, - NULL, NULL); - value_alloc.y += marks_height / 2; - } - if (priv->bottom_marks_widget) - { - int marks_height; - gtk_widget_measure (priv->bottom_marks_widget, - GTK_ORIENTATION_VERTICAL, -1, - &marks_height, NULL, - NULL, NULL); - value_alloc.y -= marks_height / 2; - } - } } else /* VERTICAL */ { @@ -434,27 +412,6 @@ gtk_scale_allocate_value (GtkScale *scale) default: g_return_if_reached (); } - if (priv->has_markup && (priv->value_pos == GTK_POS_TOP || priv->value_pos == GTK_POS_BOTTOM)) - { - if (priv->top_marks_widget) - { - int marks_width; - gtk_widget_measure (priv->top_marks_widget, - GTK_ORIENTATION_HORIZONTAL, -1, - &marks_width, NULL, - NULL, NULL); - value_alloc.x += marks_width / 2; - } - if (priv->bottom_marks_widget) - { - int marks_width; - gtk_widget_measure (priv->bottom_marks_widget, - GTK_ORIENTATION_HORIZONTAL, -1, - &marks_width, NULL, - NULL, NULL); - value_alloc.x -= marks_width / 2; - } - } } gtk_widget_size_allocate (priv->value_widget, &value_alloc, -1); @@ -1337,7 +1294,6 @@ gtk_scale_get_range_border (GtkRange *range, if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL) { int height; - bool need_symmetry = !priv->has_markup && (priv->value_pos == GTK_POS_LEFT || priv->value_pos == GTK_POS_RIGHT || !priv->draw_value); if (priv->top_marks_widget) { @@ -1346,11 +1302,7 @@ gtk_scale_get_range_border (GtkRange *range, &height, NULL, NULL, NULL); if (height > 0) - { - border->top += height; - if (need_symmetry) - border->bottom += height; - } + border->top += height; } if (priv->bottom_marks_widget) @@ -1360,17 +1312,12 @@ gtk_scale_get_range_border (GtkRange *range, &height, NULL, NULL, NULL); if (height > 0) - { - border->bottom += height; - if (need_symmetry) - border->top += height; - } + border->bottom += height; } } else { int width; - bool need_symmetry = !priv->has_markup && (priv->value_pos == GTK_POS_TOP || priv->value_pos == GTK_POS_BOTTOM || !priv->draw_value); if (priv->top_marks_widget) { @@ -1379,11 +1326,7 @@ gtk_scale_get_range_border (GtkRange *range, &width, NULL, NULL, NULL); if (width > 0) - { - border->left += width; - if (need_symmetry) - border->right += width; - } + border->left += width; } if (priv->bottom_marks_widget) @@ -1393,11 +1336,7 @@ gtk_scale_get_range_border (GtkRange *range, &width, NULL, NULL, NULL); if (width > 0) - { - border->right += width; - if (need_symmetry) - border->left += width; - } + border->right += width; } } } @@ -1712,7 +1651,6 @@ gtk_scale_clear_marks (GtkScale *scale) g_slist_free_full (priv->marks, gtk_scale_mark_free); priv->marks = NULL; - priv->has_markup = false; g_clear_pointer (&priv->top_marks_widget, gtk_widget_unparent); g_clear_pointer (&priv->bottom_marks_widget, gtk_widget_unparent); @@ -1831,7 +1769,6 @@ gtk_scale_add_mark (GtkScale *scale, gtk_widget_insert_after (mark->label_widget, mark->widget, NULL); else gtk_widget_insert_before (mark->label_widget, mark->widget, NULL); - priv->has_markup = true; } m = g_slist_find (priv->marks, mark); From 6d86b16dbee7e474b05fb6eec02b99bf8cdc858e Mon Sep 17 00:00:00 2001 From: Matthijs Velsink Date: Sun, 25 Aug 2024 19:37:38 +0200 Subject: [PATCH 2/3] scale: Refactor value allocation logic slightly This only makes the value allocation logic easier to understand, but doesn't change anything, apart from not mixing integer and floating operations anymore which can give a warning under some compiler settings. --- gtk/gtkscale.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/gtk/gtkscale.c b/gtk/gtkscale.c index 6ebabc5fac..041188c335 100644 --- a/gtk/gtkscale.c +++ b/gtk/gtkscale.c @@ -339,6 +339,7 @@ gtk_scale_allocate_value (GtkScale *scale) GtkAllocation value_alloc; int range_width, range_height; graphene_rect_t slider_bounds; + int slider_center_x, slider_center_y; range_width = gtk_widget_get_width (widget); range_height = gtk_widget_get_height (widget); @@ -347,6 +348,9 @@ gtk_scale_allocate_value (GtkScale *scale) if (!gtk_widget_compute_bounds (slider_widget, widget, &slider_bounds)) graphene_rect_init (&slider_bounds, 0, 0, gtk_widget_get_width (widget), gtk_widget_get_height (widget)); + slider_center_x = slider_bounds.origin.x + slider_bounds.size.width / 2; + slider_center_y = slider_bounds.origin.y + slider_bounds.size.height / 2; + gtk_widget_measure (priv->value_widget, GTK_ORIENTATION_HORIZONTAL, -1, &value_alloc.width, NULL, @@ -371,12 +375,12 @@ gtk_scale_allocate_value (GtkScale *scale) break; case GTK_POS_TOP: - value_alloc.x = slider_bounds.origin.x + (slider_bounds.size.width - value_alloc.width) / 2; + value_alloc.x = slider_center_x - value_alloc.width / 2; value_alloc.y = slider_bounds.origin.y - value_alloc.height; break; case GTK_POS_BOTTOM: - value_alloc.x = slider_bounds.origin.x + (slider_bounds.size.width - value_alloc.width) / 2; + value_alloc.x = slider_center_x - value_alloc.width / 2; value_alloc.y = range_height - value_alloc.height; break; @@ -391,12 +395,12 @@ gtk_scale_allocate_value (GtkScale *scale) { case GTK_POS_LEFT: value_alloc.x = 0; - value_alloc.y = (slider_bounds.origin.y + (slider_bounds.size.height / 2)) - value_alloc.height / 2; + value_alloc.y = slider_center_y - value_alloc.height / 2; break; case GTK_POS_RIGHT: value_alloc.x = range_width - value_alloc.width; - value_alloc.y = (slider_bounds.origin.y + (slider_bounds.size.height / 2)) - value_alloc.height / 2; + value_alloc.y = slider_center_y - value_alloc.height / 2; break; case GTK_POS_TOP: From a9997a3af6f06e1f576bc890d91a6b568fd4db6c Mon Sep 17 00:00:00 2001 From: Matthijs Velsink Date: Sun, 25 Aug 2024 19:41:25 +0200 Subject: [PATCH 3/3] scale: Always align value with the trough We can get the position of the trough from the parent GtkRange class, and can therefore align directly to that. There is no need to add symmetry, which can always be added in CSS if needed. Fixes #5171, #6639 --- gtk/gtkscale.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/gtk/gtkscale.c b/gtk/gtkscale.c index 041188c335..907996ec3c 100644 --- a/gtk/gtkscale.c +++ b/gtk/gtkscale.c @@ -339,7 +339,8 @@ gtk_scale_allocate_value (GtkScale *scale) GtkAllocation value_alloc; int range_width, range_height; graphene_rect_t slider_bounds; - int slider_center_x, slider_center_y; + GdkRectangle trough_rect; + int slider_center_x, slider_center_y, trough_center_x, trough_center_y; range_width = gtk_widget_get_width (widget); range_height = gtk_widget_get_height (widget); @@ -351,6 +352,11 @@ gtk_scale_allocate_value (GtkScale *scale) slider_center_x = slider_bounds.origin.x + slider_bounds.size.width / 2; slider_center_y = slider_bounds.origin.y + slider_bounds.size.height / 2; + gtk_range_get_range_rect (range, &trough_rect); + + trough_center_x = trough_rect.x + trough_rect.width / 2; + trough_center_y = trough_rect.y + trough_rect.height / 2; + gtk_widget_measure (priv->value_widget, GTK_ORIENTATION_HORIZONTAL, -1, &value_alloc.width, NULL, @@ -366,12 +372,12 @@ gtk_scale_allocate_value (GtkScale *scale) { case GTK_POS_LEFT: value_alloc.x = 0; - value_alloc.y = (range_height - value_alloc.height) / 2; + value_alloc.y = trough_center_y - value_alloc.height / 2; break; case GTK_POS_RIGHT: value_alloc.x = range_width - value_alloc.width; - value_alloc.y = (range_height - value_alloc.height) / 2; + value_alloc.y = trough_center_y - value_alloc.height / 2; break; case GTK_POS_TOP: @@ -404,12 +410,12 @@ gtk_scale_allocate_value (GtkScale *scale) break; case GTK_POS_TOP: - value_alloc.x = (range_width - value_alloc.width) / 2; + value_alloc.x = trough_center_x - value_alloc.width / 2; value_alloc.y = 0; break; case GTK_POS_BOTTOM: - value_alloc.x = (range_width - value_alloc.width) / 2; + value_alloc.x = trough_center_x - value_alloc.width / 2; value_alloc.y = range_height - value_alloc.height; break;