-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from psuzn/sp/image-handler
Image handling improvements
- Loading branch information
Showing
13 changed files
with
207 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...m-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/model/ImageTransformer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.mikepenz.markdown.model | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.graphics.painter.Painter | ||
|
||
interface ImageTransformer { | ||
|
||
@Composable | ||
fun transform(link: String): Painter? | ||
|
||
@Composable | ||
fun intrinsicSize(painter: Painter): Size { | ||
return painter.intrinsicSize | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...rkdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/model/ImageTransformerImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.mikepenz.markdown.model | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.graphics.painter.Painter | ||
import com.mikepenz.markdown.utils.imagePainter | ||
import com.mikepenz.markdown.utils.painterIntrinsicSize | ||
|
||
internal class ImageTransformerImpl : ImageTransformer { | ||
|
||
@Composable | ||
override fun transform(link: String): Painter? { | ||
return imagePainter(link) | ||
} | ||
|
||
@Composable | ||
override fun intrinsicSize(painter: Painter): Size { | ||
return painterIntrinsicSize(painter) | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/model/MarkdownImageState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.mikepenz.markdown.model | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.derivedStateOf | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.geometry.isUnspecified | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.unit.Density | ||
import androidx.compose.ui.unit.IntSize | ||
import androidx.compose.ui.unit.sp | ||
import androidx.compose.ui.unit.toSize | ||
|
||
internal interface MarkdownImageState { | ||
val imageSize: Size | ||
fun setContainerSize(intSize: IntSize) | ||
fun setImageSize(size: Size) | ||
} | ||
|
||
internal class MarkdownImageStateImpl(private val density: Density) : MarkdownImageState { | ||
|
||
private var parentSize by mutableStateOf(Size.Unspecified) | ||
|
||
private var intrinsicImageSize by mutableStateOf(Size.Unspecified) | ||
|
||
override val imageSize by derivedStateOf { | ||
with(density) { | ||
if (parentSize.isUnspecified) { | ||
Size(180f,180f) | ||
} else if (intrinsicImageSize.isUnspecified) { | ||
Size(parentSize.width.toSp().value,180f) | ||
} else { | ||
val width = minOf(intrinsicImageSize.width, parentSize.width) | ||
|
||
val height = if (intrinsicImageSize.width < parentSize.width) { | ||
intrinsicImageSize.height | ||
} else { | ||
(intrinsicImageSize.height * parentSize.width) / intrinsicImageSize.width | ||
} | ||
Size(width.toSp().value,height.toSp().value) | ||
} | ||
} | ||
} | ||
|
||
override fun setContainerSize(intSize: IntSize) { | ||
parentSize = intSize.toSize() | ||
} | ||
|
||
override fun setImageSize(size: Size) { | ||
intrinsicImageSize = size | ||
} | ||
} | ||
|
||
@Composable | ||
internal fun rememberMarkdownImageState(): MarkdownImageState { | ||
val density = LocalDensity.current | ||
return remember(density) { | ||
MarkdownImageStateImpl(density) | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...rkdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/utils/ImagePainterProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
package com.mikepenz.markdown.utils | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.graphics.painter.Painter | ||
|
||
@Composable | ||
internal expect fun imagePainter(url: String): Painter? | ||
|
||
@Composable | ||
internal expect fun painterIntrinsicSize(painter: Painter): Size |
6 changes: 6 additions & 0 deletions
6
...in/com/mikepenz/markdown/utils/ImagePainterProvider.multiplatform-markdown-renderer.js.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
package com.mikepenz.markdown.utils | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.graphics.painter.Painter | ||
|
||
@Composable | ||
internal actual fun imagePainter(url: String): Painter? { | ||
return null | ||
} | ||
|
||
@Composable | ||
internal actual fun painterIntrinsicSize(painter: Painter): Size { | ||
return painter.intrinsicSize | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 7 additions & 1 deletion
8
...om/mikepenz/markdown/utils/ImagePainterProvider.multiplatform-markdown-renderer.native.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
package com.mikepenz.markdown.utils | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.graphics.painter.Painter | ||
|
||
@Composable | ||
internal actual fun imagePainter(url: String): Painter? { | ||
return null | ||
return null as Painter? // just `return null` crashes on native | ||
} | ||
|
||
@Composable | ||
internal actual fun painterIntrinsicSize(painter: Painter): Size{ | ||
return painter.intrinsicSize | ||
} |