Skip to content

Commit

Permalink
ScrollBarGripMinHeightConstraint: Convert to SizeConstraint
Browse files Browse the repository at this point in the history
We should also apply a minimum width to scrollbar grips.
  • Loading branch information
caoimhebyrne committed Jul 15, 2024
1 parent 79a4357 commit 6b3fd25
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
7 changes: 5 additions & 2 deletions api/Elementa.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 <init> (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 <init> (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
Expand Down
45 changes: 29 additions & 16 deletions src/main/kotlin/gg/essential/elementa/components/ScrollComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -807,33 +808,45 @@ 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 {
val parent = component.parent
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 {
Expand Down

0 comments on commit 6b3fd25

Please sign in to comment.