Skip to content

Commit

Permalink
Move canApplyTo to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
devkanro committed Oct 19, 2023
1 parent ba69fd0 commit fb974a7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
28 changes: 19 additions & 9 deletions core/src/main/kotlin/org/jetbrains/jewel/painter/PainterHint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ import org.w3c.dom.Element
@Immutable
sealed interface PainterHint {

fun canApplyTo(path: String): Boolean = true

/**
* An empty [PainterHint], it will be ignored.
*/
companion object None : PainterHint {

override fun canApplyTo(path: String): Boolean = false

override fun toString(): String = "None"
}
}
Expand All @@ -32,19 +36,31 @@ sealed interface PainterHint {
* Mark this [PainterHint] just available with SVG images.
*/
@Immutable
interface SvgPainterHint : PainterHint
interface SvgPainterHint : PainterHint {

override fun canApplyTo(path: String): Boolean = path.substringAfterLast('.').lowercase() == "svg"
}

/**
* Mark this [PainterHint] just available with Bitmap images.
*/
@Immutable
interface BitmapPainterHint : PainterHint
interface BitmapPainterHint : PainterHint {

override fun canApplyTo(path: String): Boolean = when (path.substringAfterLast('.').lowercase()) {
"svg", "xml" -> false
else -> true
}
}

/**
* Mark this [PainterHint] just available with XML images.
*/
@Immutable
interface XmlPainterHint : PainterHint
interface XmlPainterHint : PainterHint {

override fun canApplyTo(path: String): Boolean = path.substringAfterLast('.').lowercase() == "xml"
}

/**
* A [PainterHint] that modifies the path of the resource being loaded.
Expand Down Expand Up @@ -129,9 +145,3 @@ abstract class PainterSuffixHint : PainterPathHint {

abstract fun suffix(): String
}

fun PainterHint.canApplyTo(format: String): Boolean = when (format.lowercase()) {
"svg" -> this is SvgPainterHint || (this !is BitmapPainterHint && this !is XmlPainterHint)
"xml" -> this is XmlPainterHint || (this !is BitmapPainterHint && this !is SvgPainterHint)
else -> this is BitmapPainterHint || (this !is SvgPainterHint && this !is XmlPainterHint)
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ResourcePainterProvider(
val format = basePath.substringAfterLast(".").lowercase()

val resolvedHints = (hints.toList() + LocalPainterHintsProvider.current.hints(basePath))
.filter { it != PainterHint.None && it.canApplyTo(format) }
.filter { it.canApplyTo(format) }

val cacheKey = resolvedHints.hashCode()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.jetbrains.jewel.painter.PainterHint
import org.jetbrains.jewel.painter.PainterPathHint
import org.jetbrains.jewel.painter.PainterResourcePathHint
import org.jetbrains.jewel.painter.PainterSvgPatchHint
import org.jetbrains.jewel.painter.canApplyTo
import org.jetbrains.jewel.painter.hints.Dark
import org.jetbrains.jewel.painter.hints.HiDpi
import org.jetbrains.jewel.painter.hints.Override
Expand Down

0 comments on commit fb974a7

Please sign in to comment.