Skip to content

Commit

Permalink
Allow scrollbars to hide instead of disable
Browse files Browse the repository at this point in the history
Affects: #14
Affects: #13
  • Loading branch information
io7m committed Nov 26, 2023
1 parent 9017bd0 commit 5f3a18d
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ default PAreaSizeI<SySpaceParentRelativeType> layout(
Objects.requireNonNull(constraints, "constraints");

/*
* First, consult the theme to see if there are size constraints specified
* Consult the theme to see if there are size constraints specified
* for this component. Derive a new set of constraints that try to
* satisfy the theme whilst also satisfying the passed in constraints.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public record SyConstraints(
int sizeMaximumX,
int sizeMaximumY)
{
private static final SyConstraints ZERO =
new SyConstraints(0, 0, 0, 0);

/**
* A set of size constraints.
*
Expand All @@ -62,6 +65,15 @@ public SyConstraints(
Math.clamp(sizeMaximumY, this.sizeMinimumY, MAX_VALUE);
}

/**
* @return Constraints that force a zero size
*/

public static SyConstraints zero()
{
return ZERO;
}

/**
* @param <T> The coordinate space type
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
package com.io7m.jsycamore.api.components;

/**
* The presence policy for scrollbars.
* A specification of whether to collapse scrollbars to zero size when
* disabled.
*/

public enum SyScrollBarPresencePolicy
public enum SyScrollBarHideIfDisabled
{
/**
* The scrollbar should always be present and enabled.
* When disabled, the scrollbar collapses to zero size and is effectively
* hidden.
*/

ALWAYS_ENABLED,
HIDE_IF_DISABLED,

/**
* The scrollbar is disabled if the entire range is shown.
*
* @see SyScrollBarType#setScrollAmountShown(double)
* When disabled, the scrollbar remains visible.
*/

DISABLED_IF_ENTIRE_RANGE_SHOWN
SHOW_EVEN_IF_DISABLED
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public interface SyScrollBarReadableType

SyComponentReadableType track();

/**
* @return The scroll amount shown in the range {@code [0, 1]}
*/

double scrollAmountShown();

/**
* @return The scroll position in the range {@code [0, 1]}
*/
Expand All @@ -59,10 +65,4 @@ public interface SyScrollBarReadableType
*/

double scrollIncrementSize();

/**
* @return The scrollbar presence policy
*/

AttributeReadableType<SyScrollBarPresencePolicy> presencePolicy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.io7m.jsycamore.api.components;

import com.io7m.jattribute.core.AttributeType;
import com.io7m.jsycamore.api.layout.SyLayoutContextType;

import java.util.function.Consumer;

Expand All @@ -27,8 +27,17 @@
public interface SyScrollBarType
extends SyScrollBarReadableType, SyComponentType
{
@Override
AttributeType<SyScrollBarPresencePolicy> presencePolicy();
/**
* Allow for hiding the scrollbar if it is disabled. If hiding-if-disabled
* is enabled, then the scrollbar's size will collapse to zero when
* {@link #layout(SyLayoutContextType, SyConstraints)} is called and the
* scrollbar is disabled.
*
* @param hideIfDisabled The hide-if-disabled setting
*/

void setHideIfDisabled(
SyScrollBarHideIfDisabled hideIfDisabled);

/**
* Set the scroll position in the range {@code [0, 1]}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,29 @@

package com.io7m.jsycamore.components.standard.internal.scrollbars;

import com.io7m.jattribute.core.AttributeType;
import com.io7m.jregions.core.parameterized.areas.PAreasI;
import com.io7m.jregions.core.parameterized.sizes.PAreaSizeI;
import com.io7m.jsycamore.api.components.SyButtonReadableType;
import com.io7m.jsycamore.api.components.SyComponentReadableType;
import com.io7m.jsycamore.api.components.SyConstraints;
import com.io7m.jsycamore.api.components.SyScrollBarDrag;
import com.io7m.jsycamore.api.components.SyScrollBarHideIfDisabled;
import com.io7m.jsycamore.api.components.SyScrollBarHorizontalType;
import com.io7m.jsycamore.api.components.SyScrollBarPresencePolicy;
import com.io7m.jsycamore.api.events.SyEventConsumed;
import com.io7m.jsycamore.api.events.SyEventType;
import com.io7m.jsycamore.api.layout.SyLayoutContextType;
import com.io7m.jsycamore.api.spaces.SySpaceParentRelativeType;
import com.io7m.jsycamore.api.themes.SyThemeClassNameType;
import com.io7m.jsycamore.components.standard.SyComponentAbstract;
import com.io7m.jsycamore.components.standard.SyComponentAttributes;
import com.io7m.jtensors.core.parameterized.vectors.PVector2I;

import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

import static com.io7m.jsycamore.api.active.SyActive.ACTIVE;
import static com.io7m.jsycamore.api.active.SyActive.INACTIVE;
import static com.io7m.jsycamore.api.components.SyScrollBarPresencePolicy.DISABLED_IF_ENTIRE_RANGE_SHOWN;
import static com.io7m.jsycamore.api.components.SyScrollBarHideIfDisabled.HIDE_IF_DISABLED;
import static com.io7m.jsycamore.api.events.SyEventConsumed.EVENT_NOT_CONSUMED;

/**
Expand All @@ -57,7 +56,8 @@ public final class SyScrollBarH
private final SyScrollBarHButtonLeft buttonLeft;
private final SyScrollBarHButtonRight buttonRight;
private final SyScrollBarHTrack track;
private final AttributeType<SyScrollBarPresencePolicy> presencePolicy;
private SyScrollBarHideIfDisabled hideIfDisabled =
SyScrollBarHideIfDisabled.SHOW_EVEN_IF_DISABLED;

/**
* A horizontal scrollbar.
Expand All @@ -70,9 +70,6 @@ public SyScrollBarH(
{
super(inThemeClassesExtra);

this.presencePolicy =
SyComponentAttributes.get().create(DISABLED_IF_ENTIRE_RANGE_SHOWN);

this.buttonLeft =
new SyScrollBarHButtonLeft();
this.buttonRight =
Expand All @@ -90,6 +87,19 @@ public PAreaSizeI<SySpaceParentRelativeType> layout(
final SyLayoutContextType layoutContext,
final SyConstraints constraints)
{
/*
* Scrollbars are able to hide themselves by collapsing to zero size.
*/

if (this.hideIfDisabled == HIDE_IF_DISABLED) {
if (!this.isActive()) {
final var zero =
PAreaSizeI.<SySpaceParentRelativeType>of(0, 0);
this.setSize(zero);
return zero;
}
}

var limitedConstraints =
layoutContext.deriveThemeConstraints(constraints, this);

Expand Down Expand Up @@ -262,6 +272,14 @@ public double scrollIncrementSize()
return this.track.scrollIncrementSize();
}

@Override
public void setHideIfDisabled(
final SyScrollBarHideIfDisabled newHideIfDisabled)
{
this.hideIfDisabled =
Objects.requireNonNull(newHideIfDisabled, "newHideIfDisabled");
}

@Override
public void setScrollPosition(
final double position)
Expand All @@ -281,23 +299,7 @@ public void setScrollAmountShown(
final double amount)
{
this.track.setScrollAmountShown(amount);

final var all =
this.track.scrollAmountShown() >= 1.0;

final var active =
switch (this.presencePolicy.get()) {
case ALWAYS_ENABLED -> {
yield ACTIVE;
}
case DISABLED_IF_ENTIRE_RANGE_SHOWN -> {
yield all ? INACTIVE : ACTIVE;
}
};

this.buttonLeft.setActive(active);
this.buttonRight.setActive(active);
this.track.setActive(active);
this.setActive(this.track.scrollAmountShown() >= 1.0 ? INACTIVE : ACTIVE);
}

@Override
Expand Down Expand Up @@ -325,20 +327,20 @@ public SyComponentReadableType track()
}

@Override
public double scrollPosition()
public double scrollAmountShown()
{
return this.track.scrollPosition();
return this.track.scrollAmountShown();
}

@Override
public double scrollPositionSnapping()
public double scrollPosition()
{
return this.track.scrollPositionSnapping();
return this.track.scrollPosition();
}

@Override
public AttributeType<SyScrollBarPresencePolicy> presencePolicy()
public double scrollPositionSnapping()
{
return this.presencePolicy;
return this.track.scrollPositionSnapping();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,29 @@

package com.io7m.jsycamore.components.standard.internal.scrollbars;

import com.io7m.jattribute.core.AttributeType;
import com.io7m.jregions.core.parameterized.areas.PAreasI;
import com.io7m.jregions.core.parameterized.sizes.PAreaSizeI;
import com.io7m.jsycamore.api.components.SyButtonReadableType;
import com.io7m.jsycamore.api.components.SyComponentReadableType;
import com.io7m.jsycamore.api.components.SyConstraints;
import com.io7m.jsycamore.api.components.SyScrollBarDrag;
import com.io7m.jsycamore.api.components.SyScrollBarPresencePolicy;
import com.io7m.jsycamore.api.components.SyScrollBarHideIfDisabled;
import com.io7m.jsycamore.api.components.SyScrollBarVerticalType;
import com.io7m.jsycamore.api.events.SyEventConsumed;
import com.io7m.jsycamore.api.events.SyEventType;
import com.io7m.jsycamore.api.layout.SyLayoutContextType;
import com.io7m.jsycamore.api.spaces.SySpaceParentRelativeType;
import com.io7m.jsycamore.api.themes.SyThemeClassNameType;
import com.io7m.jsycamore.components.standard.SyComponentAbstract;
import com.io7m.jsycamore.components.standard.SyComponentAttributes;
import com.io7m.jtensors.core.parameterized.vectors.PVector2I;

import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

import static com.io7m.jsycamore.api.active.SyActive.ACTIVE;
import static com.io7m.jsycamore.api.active.SyActive.INACTIVE;
import static com.io7m.jsycamore.api.components.SyScrollBarPresencePolicy.DISABLED_IF_ENTIRE_RANGE_SHOWN;
import static com.io7m.jsycamore.api.components.SyScrollBarHideIfDisabled.HIDE_IF_DISABLED;
import static com.io7m.jsycamore.api.events.SyEventConsumed.EVENT_NOT_CONSUMED;

/**
Expand All @@ -57,7 +56,8 @@ public final class SyScrollBarV
private final SyScrollBarVButtonUp buttonUp;
private final SyScrollBarVButtonDown buttonDown;
private final SyScrollBarVTrack track;
private final AttributeType<SyScrollBarPresencePolicy> presencePolicy;
private SyScrollBarHideIfDisabled hideIfDisabled =
SyScrollBarHideIfDisabled.SHOW_EVEN_IF_DISABLED;

/**
* A vertical scrollbar.
Expand All @@ -70,9 +70,6 @@ public SyScrollBarV(
{
super(inThemeClassesExtra);

this.presencePolicy =
SyComponentAttributes.get().create(DISABLED_IF_ENTIRE_RANGE_SHOWN);

this.buttonUp =
new SyScrollBarVButtonUp();
this.buttonDown =
Expand All @@ -90,6 +87,19 @@ public PAreaSizeI<SySpaceParentRelativeType> layout(
final SyLayoutContextType layoutContext,
final SyConstraints constraints)
{
/*
* Scrollbars are able to hide themselves by collapsing to zero size.
*/

if (this.hideIfDisabled == HIDE_IF_DISABLED) {
if (!this.isActive()) {
final var zero =
PAreaSizeI.<SySpaceParentRelativeType>of(0, 0);
this.setSize(zero);
return zero;
}
}

var limitedConstraints =
layoutContext.deriveThemeConstraints(constraints, this);

Expand Down Expand Up @@ -249,23 +259,7 @@ public void setScrollAmountShown(
final double amount)
{
this.track.setScrollAmountShown(amount);

final var all =
this.track.scrollAmountShown() >= 1.0;

final var active =
switch (this.presencePolicy.get()) {
case ALWAYS_ENABLED -> {
yield ACTIVE;
}
case DISABLED_IF_ENTIRE_RANGE_SHOWN -> {
yield all ? INACTIVE : ACTIVE;
}
};

this.buttonUp.setActive(active);
this.buttonDown.setActive(active);
this.track.setActive(active);
this.setActive(this.track.scrollAmountShown() >= 1.0 ? INACTIVE : ACTIVE);
}

@Override
Expand All @@ -292,6 +286,12 @@ public SyComponentReadableType track()
return this.track;
}

@Override
public double scrollAmountShown()
{
return this.track.scrollAmountShown();
}

@Override
public double scrollPosition()
{
Expand All @@ -310,12 +310,6 @@ public double scrollIncrementSize()
return this.track.scrollIncrementSize();
}

@Override
public AttributeType<SyScrollBarPresencePolicy> presencePolicy()
{
return this.presencePolicy;
}

@Override
public void setOnClickUpListener(
final Runnable runnable)
Expand All @@ -341,4 +335,12 @@ public void removeOnClickDownListener()
{
this.buttonDown.removeOnClickListener();
}

@Override
public void setHideIfDisabled(
final SyScrollBarHideIfDisabled newHideIfDisabled)
{
this.hideIfDisabled =
Objects.requireNonNull(newHideIfDisabled, "newHideIfDisabled");
}
}
Loading

0 comments on commit 5f3a18d

Please sign in to comment.