Skip to content

Commit e4e926a

Browse files
committed
Handle aspect ratio for ImageVectorIcon rendering
1 parent 8451c79 commit e4e926a

File tree

6 files changed

+39
-6
lines changed

6 files changed

+39
-6
lines changed

components/ir/api/ir.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ public final class io/github/composegears/valkyrie/ir/IrImageVector {
116116
public fun toString ()Ljava/lang/String;
117117
}
118118

119+
public final class io/github/composegears/valkyrie/ir/IrImageVectorKt {
120+
public static final fun getAspectRatio (Lio/github/composegears/valkyrie/ir/IrImageVector;)F
121+
}
122+
119123
public final class io/github/composegears/valkyrie/ir/IrPathFillType : java/lang/Enum {
120124
public static final field EvenOdd Lio/github/composegears/valkyrie/ir/IrPathFillType;
121125
public static final field NonZero Lio/github/composegears/valkyrie/ir/IrPathFillType;

components/ir/api/ir.klib.api

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,5 +658,8 @@ final object io.github.composegears.valkyrie.ir.util/ColorClassification { // io
658658
final fun from(kotlin.collections/List<io.github.composegears.valkyrie.ir/IrColor>): io.github.composegears.valkyrie.ir.util/DominantShade // io.github.composegears.valkyrie.ir.util/ColorClassification.from|from(kotlin.collections.List<io.github.composegears.valkyrie.ir.IrColor>){}[0]
659659
}
660660

661+
final val io.github.composegears.valkyrie.ir/aspectRatio // io.github.composegears.valkyrie.ir/aspectRatio|@io.github.composegears.valkyrie.ir.IrImageVector{}aspectRatio[0]
662+
final fun (io.github.composegears.valkyrie.ir/IrImageVector).<get-aspectRatio>(): kotlin/Float // io.github.composegears.valkyrie.ir/aspectRatio.<get-aspectRatio>|<get-aspectRatio>@io.github.composegears.valkyrie.ir.IrImageVector(){}[0]
663+
661664
final fun (io.github.composegears.valkyrie.ir/IrImageVector).io.github.composegears.valkyrie.ir.util/iconColors(): kotlin.collections/List<io.github.composegears.valkyrie.ir/IrColor> // io.github.composegears.valkyrie.ir.util/iconColors|[email protected](){}[0]
662665
final fun (kotlin/Char).io.github.composegears.valkyrie.ir.util/toPathNodes(kotlin/FloatArray): kotlin.collections/List<io.github.composegears.valkyrie.ir/IrPathNode> // io.github.composegears.valkyrie.ir.util/toPathNodes|[email protected](kotlin.FloatArray){}[0]

components/ir/src/commonMain/kotlin/io/github/composegears/valkyrie/ir/IrImageVector.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ data class IrImageVector(
99
val viewportHeight: Float,
1010
val nodes: List<IrVectorNode>,
1111
)
12+
13+
val IrImageVector.aspectRatio: Float
14+
get() = if (viewportHeight != 0f && viewportWidth != 0f) {
15+
viewportWidth / viewportHeight
16+
} else {
17+
1f
18+
}

tools/idea-plugin/src/main/kotlin/io/github/composegears/valkyrie/completion/ImageVectorCompletionContributor.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.intellij.codeInsight.lookup.LookupElementPresentation
99
import com.intellij.psi.util.CachedValueProvider
1010
import com.intellij.psi.util.CachedValuesManager
1111
import io.github.composegears.valkyrie.extensions.safeAs
12+
import io.github.composegears.valkyrie.ir.aspectRatio
1213
import io.github.composegears.valkyrie.ir.xml.toVectorXmlString
1314
import io.github.composegears.valkyrie.psi.imagevector.ImageVectorPsiParser
1415
import javax.swing.Icon
@@ -58,11 +59,13 @@ class ImageVectorCompletionContributor : CompletionContributor() {
5859
}
5960

6061
private fun createIconFromKtFile(ktFile: KtFile): Icon? {
61-
val vectorXml = ImageVectorPsiParser.parseToIrImageVector(ktFile)
62-
?.toVectorXmlString()
63-
?: return null
62+
val irImageVector = ImageVectorPsiParser.parseToIrImageVector(ktFile)
63+
val vectorXml = irImageVector?.toVectorXmlString() ?: return null
6464

65-
return ImageVectorIcon(vectorXml = vectorXml)
65+
return ImageVectorIcon(
66+
vectorXml = vectorXml,
67+
aspectRatio = irImageVector.aspectRatio,
68+
)
6669
}
6770

6871
private class ComposeColorLookupElementDecorator(

tools/idea-plugin/src/main/kotlin/io/github/composegears/valkyrie/completion/ImageVectorIcon.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import javax.swing.Icon
1111
class ImageVectorIcon(
1212
private val vectorXml: String,
1313
private val size: Int = 16,
14+
private val aspectRatio: Float,
1415
) : Icon {
1516

1617
@Volatile
@@ -23,7 +24,18 @@ class ImageVectorIcon(
2324
cachedImage = renderImage()
2425
}
2526
val img = cachedImage ?: return
26-
g.drawImage(img, x, y, size, size, null)
27+
28+
// Calculate dimensions preserving aspect ratio
29+
val (width, height) = when {
30+
aspectRatio > 1f -> size to (size / aspectRatio).toInt()
31+
else -> (size * aspectRatio).toInt() to size
32+
}
33+
34+
// Center the icon if it's smaller than the allocated space
35+
val offsetX = x + (size - width) / 2
36+
val offsetY = y + (size - height) / 2
37+
38+
g.drawImage(img, offsetX, offsetY, width, height, null)
2739
}
2840

2941
private fun renderImage(): Image? {

tools/idea-plugin/src/main/kotlin/io/github/composegears/valkyrie/gutter/ImageVectorGutterProvider.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.intellij.psi.util.CachedValuesManager
1212
import io.github.composegears.valkyrie.completion.ImageVectorIcon
1313
import io.github.composegears.valkyrie.extensions.safeAs
1414
import io.github.composegears.valkyrie.ir.IrImageVector
15+
import io.github.composegears.valkyrie.ir.aspectRatio
1516
import io.github.composegears.valkyrie.ir.xml.toVectorXmlString
1617
import io.github.composegears.valkyrie.psi.imagevector.ImageVectorPsiParser
1718
import javax.swing.Icon
@@ -83,7 +84,10 @@ class ImageVectorGutterProvider : LineMarkerProvider {
8384
val irImageVector = parseImageVectorProperty(this) ?: return null
8485
val vectorXml = irImageVector.toVectorXmlString()
8586

86-
return ImageVectorIcon(vectorXml = vectorXml)
87+
return ImageVectorIcon(
88+
vectorXml = vectorXml,
89+
aspectRatio = irImageVector.aspectRatio,
90+
)
8791
}
8892

8993
private fun parseImageVectorProperty(property: KtProperty): IrImageVector? {

0 commit comments

Comments
 (0)