Skip to content

Commit

Permalink
First step to migrate tonal color scheme bundles to use per-state con…
Browse files Browse the repository at this point in the history
…tainer tokens

For #400
  • Loading branch information
kirill-grouchnikov committed Jan 5, 2025
1 parent fc307c3 commit 694c280
Showing 1 changed file with 21 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
import org.pushingpixels.radiance.theming.api.palette.RadianceColorScheme2;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Color scheme bundle. Defines the visual appearance of a single decoration area of a skin.
Expand All @@ -50,13 +48,6 @@ public class RadianceColorSchemeBundle2 {
// The main color scheme of this bundle
private RadianceColorScheme2 mainColorScheme;

/**
* Maps from component state to the alpha channel applied on color scheme.
* This map doesn't have to contain entries for all {@link ComponentState}
* instances.
*/
private Map<ComponentState, Float> stateAlphaMap;

/**
* Maps from color scheme association kinds to the map of color schemes.
* Different visual parts of controls in the specific decoration are can be
Expand Down Expand Up @@ -91,8 +82,6 @@ public RadianceColorSchemeBundle2(RadianceColorScheme2 mainColorScheme) {
}

this.mainColorScheme = mainColorScheme;
this.stateAlphaMap = new HashMap<>();
//this.stateHighlightAlphaMap = new HashMap<>();

this.colorSchemeMap = new HashMap<>();
for (RadianceThemingSlices.ContainerColorTokensAssociationKind associationKind :
Expand All @@ -101,24 +90,6 @@ public RadianceColorSchemeBundle2(RadianceColorScheme2 mainColorScheme) {
}
}

/**
* Registers an alpha channel value for the specific component states.
*
* @param alpha Alpha channel value.
* @param states Component states.
*/
public void registerAlpha(float alpha, ComponentState... states) {
if ((states == null) || (states.length == 0)) {
for (ComponentState state : ComponentState.getAllStates()) {
this.stateAlphaMap.put(state, alpha);
}
} else {
for (ComponentState state : states) {
this.stateAlphaMap.put(state, alpha);
}
}
}

/**
* Registers a color scheme for the specific component state.
*
Expand Down Expand Up @@ -148,11 +119,13 @@ public ContainerColorTokens getContainerTokens(ComponentState componentState,
RadianceColorScheme2 registered = this.colorSchemeMap.get(
RadianceThemingSlices.ContainerColorTokensAssociationKind.DEFAULT).get(componentState);
if (registered != null) {
return componentState.isActive() ? registered.getActiveContainerTokens()
: registered.getContainerTokens(inactiveContainerType);
// If we're here, the component state is guaranteed to be active due to restrictions
// in registerColorScheme
return registered.getActiveContainerTokens();
}

return componentState.isActive() ? this.mainColorScheme.getContainerTokensForState(componentState)
return componentState.isActive()
? this.mainColorScheme.getContainerTokensForState(componentState)
: this.mainColorScheme.getContainerTokens(inactiveContainerType);
}

Expand All @@ -165,11 +138,13 @@ public ExtendedContainerColorTokens getExtendedContainerTokens(ComponentState co
RadianceColorScheme2 registered = this.colorSchemeMap.get(
RadianceThemingSlices.ContainerColorTokensAssociationKind.DEFAULT).get(componentState);
if (registered != null) {
return componentState.isActive() ? registered.getExtendedContainerTokens(componentState)
: registered.getExtendedContainerTokens(inactiveContainerType);
// If we're here, the component state is guaranteed to be active due to restrictions
// in registerColorScheme
return registered.getExtendedContainerTokens(componentState);
}

return componentState.isActive() ? this.mainColorScheme.getExtendedContainerTokens(componentState)
return componentState.isActive()
? this.mainColorScheme.getExtendedContainerTokens(componentState)
: this.mainColorScheme.getExtendedContainerTokens(inactiveContainerType);
}

Expand All @@ -187,27 +162,6 @@ public ContainerColorTokens getSystemContainerTokens(
}
}

public boolean hasAlphaFor(ComponentState componentState) {
return this.stateAlphaMap.containsKey(componentState);
}

/**
* Returns the alpha channel of color schemes for the specified component state.
* Before calling this API, call {@link #hasAlphaFor(ComponentState)}. This API returns
* 1.0f for states that do not have an explicitly registered alpha channel value.
*
* @param componentState Component state.
* @return Color scheme alpha channel.
*/
public float getAlpha(ComponentState componentState) {
Float registered = this.stateAlphaMap.get(componentState);
if (registered != null) {
return registered.floatValue();
}

return 1.0f;
}

/**
* Returns the main color scheme of this bundle.
*
Expand Down Expand Up @@ -246,21 +200,14 @@ public void registerColorScheme(RadianceColorScheme2 scheme,
}

if ((states == null) || (states.length == 0)) {
for (ComponentState state : ComponentState.getAllStates()) {
if (this.colorSchemeMap.get(associationKind).containsKey(state)) {
continue;
}
if (state.getHardFallback() != null) {
// Skip states with hard fallback - that link will be traversed in
// getColorScheme() logic
continue;
}
this.colorSchemeMap.get(associationKind).put(state, scheme);
}
} else {
for (ComponentState state : states) {
this.colorSchemeMap.get(associationKind).put(state, scheme);
throw new IllegalArgumentException("Must pass at least one state");
}

for (ComponentState state : states) {
if (state.isDisabled() || !state.isActive()) {
throw new IllegalArgumentException("Only active states can have custom color schemes");
}
this.colorSchemeMap.get(associationKind).put(state, scheme);
}
}

Expand All @@ -280,6 +227,7 @@ public ContainerColorTokens getContainerTokens(
RadianceThemingSlices.ContainerColorTokensAssociationKind associationKind,
ComponentState componentState, boolean allowFallback,
RadianceThemingSlices.ContainerType inactiveContainerType) {

if (associationKind == RadianceThemingSlices.ContainerColorTokensAssociationKind.DEFAULT) {
return this.getContainerTokens(componentState, inactiveContainerType);
}
Expand All @@ -294,8 +242,9 @@ public ContainerColorTokens getContainerTokens(
RadianceColorScheme2 registered =
this.colorSchemeMap.get(associationKind).get(componentState);
if (registered != null) {
return componentState.isActive() ? registered.getActiveContainerTokens()
: registered.getContainerTokens(inactiveContainerType);
// If we're here, the component state is guaranteed to be active due to restrictions
// in registerColorScheme
return registered.getActiveContainerTokens();
}

RadianceColorScheme2 enabledForAssociationKind =
Expand All @@ -314,21 +263,4 @@ public ContainerColorTokens getContainerTokens(
RadianceThemingSlices.ContainerColorTokensAssociationKind.DEFAULT, componentState,
true, inactiveContainerType);
}

/**
* Returns the set of all component states that have non-trivial alpha
* associated with them. Non-trivial alpha is a value that is strictly less
* than 1.0.
*
* @return All component states that have associated non-trivial alpha values.
*/
Set<ComponentState> getStatesWithAlpha() {
Set<ComponentState> result = new HashSet<>();
for (Map.Entry<ComponentState, Float> alphaEntry : this.stateAlphaMap.entrySet()) {
if (alphaEntry.getValue() < 1.0f) {
result.add(alphaEntry.getKey());
}
}
return result;
}
}

0 comments on commit 694c280

Please sign in to comment.