From 9b94535cec40ab4823d9324a6fb05576c5408622 Mon Sep 17 00:00:00 2001 From: fscarponi Date: Fri, 29 Sep 2023 11:58:56 +0200 Subject: [PATCH] Improve handling of non-existent colors Added a `FallbackMarker` in BridgeUtils.kt, improving the named color retrieval process. The `retrieveColorOrNull` method now checks for colors that are not found and returns a specific marker, rather than an inappropriate color. This avoids any confusion and enhances the overall color management within the application." --- .../org/jetbrains/jewel/bridge/BridgeUtils.kt | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeUtils.kt b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeUtils.kt index 3fd20bdf18..c3692af662 100644 --- a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeUtils.kt +++ b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeUtils.kt @@ -43,10 +43,20 @@ fun java.awt.Color.toComposeColor() = Color( fun java.awt.Color?.toComposeColorOrUnspecified() = this?.toComposeColor() ?: Color.Unspecified +@Suppress("JavaIoSerializableObjectMustHaveReadResolve") +private object FallbackMarker : JBColor(0x0000, 0x0000) { + + override fun toString() = "%%%%%%COLOR_NOT_FOUND_MARKER%%%%%%" + + override fun equals(other: Any?) = other === this + + override fun hashCode() = toString().hashCode() +} + @Suppress("UnstableApiUsage") -fun retrieveColorOrNull(key: String) = - JBColor.namedColor(key) - .takeUnless { it.name == "NAMED_COLOR_FALLBACK_MARKER" } +fun retrieveColorOrNull(key: String): Color? = + JBColor.namedColor(key, FallbackMarker) + .takeUnless { it.name == "NAMED_COLOR_FALLBACK_MARKER" || it == FallbackMarker } ?.toComposeColor() fun retrieveColorOrUnspecified(key: String): Color {