From 003f4006cb98e8dd3d0b17c2f927d94bd3d6f3bf Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Tue, 2 May 2023 18:39:54 +0200 Subject: [PATCH] Allow the use of an ImageCache with MarkdownComponent --- api/Elementa.api | 4 +++- .../gg/essential/elementa/markdown/MarkdownComponent.kt | 5 ++++- .../essential/elementa/markdown/drawables/ImageDrawable.kt | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/api/Elementa.api b/api/Elementa.api index aa008e91..99024eb6 100644 --- a/api/Elementa.api +++ b/api/Elementa.api @@ -3152,14 +3152,16 @@ public final class gg/essential/elementa/markdown/MarkdownComponent : gg/essenti public fun (Ljava/lang/String;Lgg/essential/elementa/markdown/MarkdownConfig;)V public fun (Ljava/lang/String;Lgg/essential/elementa/markdown/MarkdownConfig;F)V public fun (Ljava/lang/String;Lgg/essential/elementa/markdown/MarkdownConfig;FLgg/essential/elementa/font/FontProvider;)V - public synthetic fun (Ljava/lang/String;Lgg/essential/elementa/markdown/MarkdownConfig;FLgg/essential/elementa/font/FontProvider;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun (Ljava/lang/String;Lgg/essential/elementa/markdown/MarkdownConfig;FLgg/essential/elementa/font/FontProvider;Z)V public synthetic fun (Ljava/lang/String;Lgg/essential/elementa/markdown/MarkdownConfig;FLgg/essential/elementa/font/FontProvider;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Ljava/lang/String;Lgg/essential/elementa/markdown/MarkdownConfig;FLgg/essential/elementa/font/FontProvider;ZLgg/essential/elementa/components/image/ImageCache;)V + public synthetic fun (Ljava/lang/String;Lgg/essential/elementa/markdown/MarkdownConfig;FLgg/essential/elementa/font/FontProvider;ZLgg/essential/elementa/components/image/ImageCache;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun animationFrame ()V public final fun bindText (Lgg/essential/elementa/state/State;)Lgg/essential/elementa/markdown/MarkdownComponent; public fun draw (Lgg/essential/universal/UMatrixStack;)V public final fun getConfig ()Lgg/essential/elementa/markdown/MarkdownConfig; public final fun getDrawables ()Lgg/essential/elementa/markdown/drawables/DrawableList; + public final fun getImageCache ()Lgg/essential/elementa/components/image/ImageCache; public final fun getMaxTextLineWidth ()F public final fun getSectionOffsets ()Ljava/util/Map; public final fun layout ()V diff --git a/src/main/kotlin/gg/essential/elementa/markdown/MarkdownComponent.kt b/src/main/kotlin/gg/essential/elementa/markdown/MarkdownComponent.kt index 872ff0c9..50333b6c 100644 --- a/src/main/kotlin/gg/essential/elementa/markdown/MarkdownComponent.kt +++ b/src/main/kotlin/gg/essential/elementa/markdown/MarkdownComponent.kt @@ -5,6 +5,7 @@ import gg.essential.elementa.components.MarkdownNode import gg.essential.elementa.components.TreeListComponent import gg.essential.elementa.components.TreeNode import gg.essential.elementa.components.Window +import gg.essential.elementa.components.image.ImageCache import gg.essential.elementa.constraints.HeightConstraint import gg.essential.elementa.dsl.pixels import gg.essential.elementa.events.UIEvent @@ -34,6 +35,7 @@ class MarkdownComponent( private val codeFontPointSize: Float = 10f, private val codeFontRenderer: FontProvider = ElementaFonts.JETBRAINS_MONO, private val disableSelection: Boolean = false, + val imageCache: ImageCache? = null ) : UIComponent() { @JvmOverloads @@ -42,7 +44,8 @@ class MarkdownComponent( config: MarkdownConfig = MarkdownConfig(), codeFontPointSize: Float = 10f, codeFontRenderer: FontProvider = ElementaFonts.JETBRAINS_MONO, - ) : this(text, config, codeFontPointSize, codeFontRenderer, false) + disableSelection: Boolean = false + ) : this(text, config, codeFontPointSize, codeFontRenderer, disableSelection, null) private val configState = BasicState(config) val config: MarkdownConfig diff --git a/src/main/kotlin/gg/essential/elementa/markdown/drawables/ImageDrawable.kt b/src/main/kotlin/gg/essential/elementa/markdown/drawables/ImageDrawable.kt index dcc4167c..be2b187f 100644 --- a/src/main/kotlin/gg/essential/elementa/markdown/drawables/ImageDrawable.kt +++ b/src/main/kotlin/gg/essential/elementa/markdown/drawables/ImageDrawable.kt @@ -30,7 +30,8 @@ class ImageDrawable(md: MarkdownComponent, val url: URL, private val fallback: D private lateinit var imageX: ShiftableMDPixelConstraint private lateinit var imageY: ShiftableMDPixelConstraint - private val image = UIImage.ofURL(url) childOf md + private val image = + (if (md.imageCache == null) UIImage.ofURL(url) else UIImage.ofURL(url, md.imageCache)) childOf md private var hasLoaded = false override fun layoutImpl(x: Float, y: Float, width: Float): Layout {