Skip to content

Commit

Permalink
Add default values to retrieve* functions (#408)
Browse files Browse the repository at this point in the history
Simple syntactic sugar to avoid elvis operators and other checks.
  • Loading branch information
rock3r committed Jun 14, 2024
1 parent fdbcc05 commit 0ac4e02
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
7 changes: 5 additions & 2 deletions ide-laf-bridge/api/ide-laf-bridge.api
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ public final class org/jetbrains/jewel/bridge/BridgeUtilsKt {
public static final fun retrieveArcAsCornerSize (Ljava/lang/String;)Landroidx/compose/foundation/shape/CornerSize;
public static final fun retrieveArcAsCornerSizeOrDefault (Ljava/lang/String;Landroidx/compose/foundation/shape/CornerSize;)Landroidx/compose/foundation/shape/CornerSize;
public static final fun retrieveArcAsCornerSizeWithFallbacks ([Ljava/lang/String;)Landroidx/compose/foundation/shape/CornerSize;
public static final fun retrieveColor-4WTKRHQ (Ljava/lang/String;J)J
public static final fun retrieveColorOrNull (Ljava/lang/String;)Landroidx/compose/ui/graphics/Color;
public static final fun retrieveColorOrUnspecified (Ljava/lang/String;)J
public static final fun retrieveColorsOrUnspecified ([Ljava/lang/String;)Ljava/util/List;
public static final fun retrieveEditorColorScheme ()Lcom/intellij/openapi/editor/colors/EditorColorsScheme;
public static final fun retrieveInsetsAsPaddingValues (Ljava/lang/String;)Landroidx/compose/foundation/layout/PaddingValues;
public static final fun retrieveIntAsDp (Ljava/lang/String;)F
public static final fun retrieveInsetsAsPaddingValues (Ljava/lang/String;Landroidx/compose/foundation/layout/PaddingValues;)Landroidx/compose/foundation/layout/PaddingValues;
public static synthetic fun retrieveInsetsAsPaddingValues$default (Ljava/lang/String;Landroidx/compose/foundation/layout/PaddingValues;ILjava/lang/Object;)Landroidx/compose/foundation/layout/PaddingValues;
public static final fun retrieveIntAsDp-3F_vd3o (Ljava/lang/String;Landroidx/compose/ui/unit/Dp;)F
public static synthetic fun retrieveIntAsDp-3F_vd3o$default (Ljava/lang/String;Landroidx/compose/ui/unit/Dp;ILjava/lang/Object;)F
public static final fun retrieveIntAsDpOrUnspecified (Ljava/lang/String;)F
public static final fun retrieveTextStyle (Ljava/lang/String;Ljava/lang/String;)Landroidx/compose/ui/text/TextStyle;
public static synthetic fun retrieveTextStyle$default (Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Landroidx/compose/ui/text/TextStyle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public fun java.awt.Color.toComposeColor(): Color = Color(red = red, green = gre

public fun java.awt.Color?.toComposeColorOrUnspecified(): Color = this?.toComposeColor() ?: Color.Unspecified

public fun retrieveColor(
key: String,
default: Color,
): Color = retrieveColorOrNull(key) ?: default

public fun retrieveColorOrNull(key: String): Color? =
try {
JBColor.namedColor(key, marker("JEWEL_JBCOLOR_MARKER")).toComposeColor()
Expand Down Expand Up @@ -78,9 +83,13 @@ public fun List<Color>.createVerticalBrush(
return Brush.verticalGradient(this, startY, endY, tileMode)
}

public fun retrieveIntAsDp(key: String): Dp {
public fun retrieveIntAsDp(
key: String,
default: Dp? = null,
): Dp {
val rawValue = UIManager.get(key)
if (rawValue is Int) return rawValue.dp
if (default != null) return default

keyNotFound(key, "Int")
}
Expand All @@ -92,7 +101,10 @@ public fun retrieveIntAsDpOrUnspecified(key: String): Dp =
Dp.Unspecified
}

public fun retrieveInsetsAsPaddingValues(key: String): PaddingValues = UIManager.getInsets(key)?.toPaddingValues() ?: keyNotFound(key, "Insets")
public fun retrieveInsetsAsPaddingValues(
key: String,
default: PaddingValues? = null,
): PaddingValues = UIManager.getInsets(key)?.toPaddingValues() ?: default ?: keyNotFound(key, "Insets")

/**
* Converts a [Insets] to [PaddingValues]. If the receiver is a [JBInsets]
Expand All @@ -112,7 +124,9 @@ public fun Insets.toPaddingValues(): PaddingValues =
* [JBInsets.getUnscaled] values, treated as [Dp]. This avoids double
* scaling.
*/
public fun JBInsets.toPaddingValues(): PaddingValues = PaddingValues(unscaled.left.dp, unscaled.top.dp, unscaled.right.dp, unscaled.bottom.dp)
@Suppress("ktlint:standard:function-signature") // False positive
public fun JBInsets.toPaddingValues(): PaddingValues =
PaddingValues(unscaled.left.dp, unscaled.top.dp, unscaled.right.dp, unscaled.bottom.dp)

/**
* Converts a [Dimension] to [DpSize]. If the receiver is a [JBDimension]
Expand Down

0 comments on commit 0ac4e02

Please sign in to comment.