From 6b3fd25e472ccc3c5c2ffee176389e2fca731618 Mon Sep 17 00:00:00 2001 From: Caoimhe Date: Mon, 15 Jul 2024 12:30:00 +0100 Subject: [PATCH] ScrollBarGripMinHeightConstraint: Convert to `SizeConstraint` We should also apply a minimum width to scrollbar grips. --- api/Elementa.api | 7 ++- .../elementa/components/ScrollComponent.kt | 45 ++++++++++++------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/api/Elementa.api b/api/Elementa.api index 4f53e9ca..6be2802e 100644 --- a/api/Elementa.api +++ b/api/Elementa.api @@ -427,13 +427,16 @@ public final class gg/essential/elementa/components/ScrollComponent$Direction : public static fun values ()[Lgg/essential/elementa/components/ScrollComponent$Direction; } -public final class gg/essential/elementa/components/ScrollComponent$ScrollBarGripMinHeightConstraint : gg/essential/elementa/constraints/HeightConstraint { - public fun (Lgg/essential/elementa/components/ScrollComponent;Lgg/essential/elementa/constraints/HeightConstraint;)V +public final class gg/essential/elementa/components/ScrollComponent$ScrollBarGripMinSizeConstraint : gg/essential/elementa/constraints/SizeConstraint { + public fun (Lgg/essential/elementa/components/ScrollComponent;Lgg/essential/elementa/constraints/SizeConstraint;)V + public fun animationFrame ()V public fun getCachedValue ()Ljava/lang/Float; public synthetic fun getCachedValue ()Ljava/lang/Object; public fun getConstrainTo ()Lgg/essential/elementa/UIComponent; public fun getHeightImpl (Lgg/essential/elementa/UIComponent;)F + public fun getRadiusImpl (Lgg/essential/elementa/UIComponent;)F public fun getRecalculate ()Z + public fun getWidthImpl (Lgg/essential/elementa/UIComponent;)F public fun setCachedValue (F)V public synthetic fun setCachedValue (Ljava/lang/Object;)V public fun setConstrainTo (Lgg/essential/elementa/UIComponent;)V diff --git a/src/main/kotlin/gg/essential/elementa/components/ScrollComponent.kt b/src/main/kotlin/gg/essential/elementa/components/ScrollComponent.kt index 8b8855f6..c5d514df 100644 --- a/src/main/kotlin/gg/essential/elementa/components/ScrollComponent.kt +++ b/src/main/kotlin/gg/essential/elementa/components/ScrollComponent.kt @@ -466,16 +466,17 @@ class ScrollComponent constructor( } } - if (isHorizontal) { - component.setWidth(RelativeConstraint(clampedPercentage)) + val relativeConstraint = RelativeConstraint(clampedPercentage) + val desiredSizeConstraint = if (Window.of(this).version >= ElementaVersion.v6) { + ScrollBarGripMinSizeConstraint(relativeConstraint) } else { - val heightConstraint = RelativeConstraint(clampedPercentage) + relativeConstraint + } - if (Window.of(this).version >= ElementaVersion.v6) { - component.setHeight(ScrollBarGripMinHeightConstraint(heightConstraint)) - } else { - component.setHeight(heightConstraint) - } + if (isHorizontal) { + component.setWidth(desiredSizeConstraint) + } else { + component.setHeight(desiredSizeConstraint) } component.animate { @@ -807,21 +808,29 @@ class ScrollComponent constructor( } /** - * Constraints the scrollbar grip's height to be a certain minimum height, or the [desiredHeight]. - * This is the default constraint for vertical scrollbar grips if [ElementaVersion.V6] is used. + * Constraints the scrollbar grip's size to be a certain minimum size, or the [desiredSize]. + * This is the default constraint for horizontal scrollbar grips if [ElementaVersion.V6] is used. * - * @param desiredHeight The intended height for the scrollbar grip. + * @param desiredSize The intended size for the scrollbar grip. */ - inner class ScrollBarGripMinHeightConstraint( - private val desiredHeight: HeightConstraint - ) : HeightConstraint { + inner class ScrollBarGripMinSizeConstraint( + private val desiredSize: SizeConstraint + ) : SizeConstraint { override var cachedValue: Float = 0f override var recalculate: Boolean = true override var constrainTo: UIComponent? = null override fun animationFrame() { super.animationFrame() - desiredHeight.animationFrame() + desiredSize.animationFrame() + } + + override fun getWidthImpl(component: UIComponent): Float { + val parent = component.parent + val minimumWidthPercentage = if (parent.getWidth() < 200) { 0.15f } else { 0.10f } + val minimumWidth = parent.getWidth() * minimumWidthPercentage + + return desiredSize.getWidth(component).coerceAtLeast(minimumWidth) } override fun getHeightImpl(component: UIComponent): Float { @@ -829,11 +838,15 @@ class ScrollComponent constructor( val minimumHeightPercentage = if (parent.getHeight() < 200) { 0.15f } else { 0.10f } val minimumHeight = parent.getHeight() * minimumHeightPercentage - return desiredHeight.getHeight(component).coerceAtLeast(minimumHeight) + return desiredSize.getHeight(component).coerceAtLeast(minimumHeight) } override fun visitImpl(visitor: ConstraintVisitor, type: ConstraintType) { } + + override fun getRadiusImpl(component: UIComponent): Float { + throw IllegalStateException("`ScrollBarGripMinSizeConstraint` does not support `getRadiusImpl`.") + } } enum class Direction {