Skip to content

Commit

Permalink
Fix remembers painter twice and break the cache (#303)
Browse files Browse the repository at this point in the history
* Fix remembers painter twice to break the cache

* Make hints happy
  • Loading branch information
devkanro authored Feb 17, 2024
1 parent d8ddff1 commit d587c1f
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 17 deletions.
4 changes: 4 additions & 0 deletions foundation/api/foundation.api
Original file line number Diff line number Diff line change
Expand Up @@ -843,3 +843,7 @@ public final class org/jetbrains/jewel/foundation/theme/ThemeIconData$Companion
public final fun getEmpty ()Lorg/jetbrains/jewel/foundation/theme/ThemeIconData;
}

public final class org/jetbrains/jewel/foundation/util/DebugKt {
public static final fun getInDebugMode ()Z
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jetbrains.jewel.ui.util
package org.jetbrains.jewel.foundation.util

import org.jetbrains.jewel.foundation.InternalJewelApi

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jetbrains.jewel.foundation.utils
package org.jetbrains.jewel.foundation.util

internal enum class LogLevel(val color: String) {
Trace("\u001b[38;5;33m"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import com.intellij.ui.NewUI
import org.jetbrains.jewel.bridge.theme.isNewUiTheme
import org.jetbrains.jewel.foundation.InternalJewelApi
import org.jetbrains.jewel.foundation.theme.JewelTheme
import org.jetbrains.jewel.foundation.util.inDebugMode
import org.jetbrains.jewel.ui.painter.PainterHint
import org.jetbrains.jewel.ui.painter.PalettePainterHintsProvider
import org.jetbrains.jewel.ui.painter.hints.ColorBasedPaletteReplacement
import org.jetbrains.jewel.ui.painter.hints.Dark
import org.jetbrains.jewel.ui.painter.hints.HiDpi
import org.jetbrains.jewel.ui.painter.hints.KeyBasedPaletteReplacement
import org.jetbrains.jewel.ui.util.inDebugMode
import org.jetbrains.jewel.ui.util.toRgbaHexString

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import org.jetbrains.jewel.foundation.theme.JewelTheme
import org.jetbrains.jewel.foundation.theme.ThemeDefinition
import org.jetbrains.jewel.foundation.util.inDebugMode
import org.jetbrains.jewel.ui.painter.PainterHint
import org.jetbrains.jewel.ui.painter.PalettePainterHintsProvider
import org.jetbrains.jewel.ui.painter.hints.ColorBasedPaletteReplacement
import org.jetbrains.jewel.ui.painter.hints.Dark
import org.jetbrains.jewel.ui.painter.hints.HiDpi
import org.jetbrains.jewel.ui.painter.hints.KeyBasedPaletteReplacement
import org.jetbrains.jewel.ui.painter.hints.PathOverride
import org.jetbrains.jewel.ui.util.inDebugMode

/** Provides the default [PainterHint]s to use to load images. */
public class StandalonePainterHintsProvider(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import org.jetbrains.jewel.ui.component.ToggleableChip
import org.jetbrains.jewel.ui.theme.colorPalette

@Composable
@View(title = "ChipsAndTree", position = 11)
@View(title = "ChipsAndTree", position = 11, icon = "icons/showAsTree.svg")
fun ChipsAndTree() {
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(16.dp)) {
Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(8.dp)) {
Expand Down
6 changes: 6 additions & 0 deletions samples/standalone/src/main/resources/icons/showAsTree.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 0 additions & 4 deletions ui/api/ui.api
Original file line number Diff line number Diff line change
Expand Up @@ -2378,10 +2378,6 @@ public final class org/jetbrains/jewel/ui/util/ColorExtensionsKt {
public static final fun toRgbaHexString-8_81llA (J)Ljava/lang/String;
}

public final class org/jetbrains/jewel/ui/util/DebugKt {
public static final fun getInDebugMode ()Z
}

public final class org/jetbrains/jewel/ui/util/ModifierExtensionsKt {
public static final fun thenIf (Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function1;)Landroidx/compose/ui/Modifier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import androidx.compose.ui.res.loadImageBitmap
import androidx.compose.ui.res.loadSvgPainter
import androidx.compose.ui.res.loadXmlImageVector
import androidx.compose.ui.unit.Density
import org.jetbrains.jewel.ui.util.inDebugMode
import org.jetbrains.jewel.foundation.util.inDebugMode
import org.w3c.dom.Document
import org.xml.sax.InputSource
import java.io.IOException
Expand Down Expand Up @@ -126,7 +126,7 @@ public class ResourcePainterProvider(
when (extension) {
"svg" -> createSvgPainter(chosenScope, url)
"xml" -> createVectorDrawablePainter(chosenScope, url)
else -> createBitmapPainter(chosenScope, url)
else -> createBitmapPainter(url)
}

for (hint in scope.acceptedHints) {
Expand Down Expand Up @@ -166,7 +166,7 @@ public class ResourcePainterProvider(
loadSvgPainter(inputStream, scope)
}
},
rememberAction = { remember(url, scope.density) { it } },
paintAction = { it },
)

private fun patchSvg(
Expand Down Expand Up @@ -205,27 +205,26 @@ public class ResourcePainterProvider(
loadingAction = { resourceUrl ->
resourceUrl.openStream().use { loadXmlImageVector(InputSource(it), scope) }
},
rememberAction = { rememberVectorPainter(it) },
paintAction = { rememberVectorPainter(it) },
)

@Composable
private fun createBitmapPainter(
scope: Scope,
url: URL,
) = tryLoadingResource(
url = url,
loadingAction = { resourceUrl ->
val bitmap = resourceUrl.openStream().use { loadImageBitmap(it) }
BitmapPainter(bitmap)
},
rememberAction = { remember(url, scope.density) { it } },
paintAction = { it },
)

@Composable
private fun <T> tryLoadingResource(
url: URL,
loadingAction: (URL) -> T,
rememberAction: @Composable (T) -> Painter,
paintAction: @Composable (T) -> Painter,
): Painter {
@Suppress("TooGenericExceptionCaught") // This is a last-resort fallback when icons fail to load
val painter =
Expand All @@ -241,7 +240,7 @@ public class ResourcePainterProvider(
return errorPainter
}

return rememberAction(painter)
return paintAction(painter)
}

private class Scope(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ private class StrokeImpl(private val color: Color) : PainterSuffixHint(), Painte
Color(0xFFF28C35),
Color(0xFF955AE0),
)

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is StrokeImpl) return false

if (color != other.color) return false

return true
}

override fun toString(): String = "Stroke(color=$color)"

override fun hashCode(): Int = color.hashCode()
}

/**
Expand Down

0 comments on commit d587c1f

Please sign in to comment.