Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix remembers painter twice and break the cache #303

Merged
merged 2 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -14,7 +14,7 @@ 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.foundation.util.inDebugMode
import org.jetbrains.jewel.ui.util.toRgbaHexString

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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
import org.jetbrains.jewel.foundation.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 @@ -166,7 +166,7 @@ public class ResourcePainterProvider(
loadSvgPainter(inputStream, scope)
}
},
rememberAction = { remember(url, scope.density) { it } },
paintAction = { it },
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not remember painter here, because we have already the cache here.

Before:
截屏2024-02-17 19 30 59

After:
截屏2024-02-17 19 31 57

It only happened with first default selected icon button

)

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can still remember the vector because vector self is a key.

)

@Composable
Expand All @@ -218,14 +218,14 @@ public class ResourcePainterProvider(
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,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I rename the rememberAction to paintAction, it more seems like a action to convert T to a painter

): Painter {
@Suppress("TooGenericExceptionCaught") // This is a last-resort fallback when icons fail to load
val painter =
Expand All @@ -241,7 +241,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
Loading