Skip to content

Commit 321b09f

Browse files
authored
Merge pull request #100 from overpas/#99/preview
Compose ImageVector preview
2 parents ec544ba + 1b91890 commit 321b09f

24 files changed

+1210
-6
lines changed

.github/workflows/build.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,22 @@ on:
1010

1111
jobs:
1212
build:
13-
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
os:
16+
- macos-latest
17+
- ubuntu-latest
18+
jdk:
19+
- zulu
20+
- corretto
21+
- adopt
22+
runs-on: ${{ matrix.os }}
1423
steps:
1524
- uses: actions/checkout@v3
25+
- name: Set up JDK
26+
uses: actions/setup-java@v3
27+
with:
28+
distribution: ${{ matrix.jdk }}
29+
java-version: 17
1630
- name: Build
1731
run: ./gradlew build

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[![Build](https://github.com/overpas/svg-to-compose-intellij/actions/workflows/build.yml/badge.svg)](https://github.com/overpas/svg-to-compose-intellij/actions/workflows/build.yml)
33
[![Check IDE compatibility](https://github.com/overpas/svg-to-compose-intellij/actions/workflows/verify.yml/badge.svg)](https://github.com/overpas/svg-to-compose-intellij/actions/workflows/verify.yml)
44

5-
A simple Android Studio plugin to generate Jetpack Compose ImageVector icons. In fact, it's a wrapper around the [svg-to-compose](https://github.com/DevSrSouza/svg-to-compose) tool.
5+
A simple Android Studio plugin to generate Jetpack Compose ImageVector icons. In fact, it's a wrapper around the [svg-to-compose](https://github.com/DevSrSouza/svg-to-compose) tool. Also, allows to preview the generated icons.
66

77
## Installation
88
Install from [Jetbrains Marketplace](https://plugins.jetbrains.com/plugin/18619-svg-to-compose)

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ dependencies {
4747
implementation(libs.compose.multiplatform.file.picker) {
4848
exclude(group = "org.jetbrains.kotlinx")
4949
}
50+
testImplementation(kotlin("test"))
5051
testImplementation(libs.junit)
5152
testImplementation(libs.mockito.kotlin)
5253
testImplementation(libs.kotlin.coroutines.test)

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
version=0.14
1+
version=0.15
22
jvm-version=17
33
since-build=232.8660.185
44
until-build=241.*
5-
change-notes=Support Intellij 2024.1; Fix crashes on previous versions
5+
change-notes=ImageVector icons preview
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package by.overpass.svgtocomposeintellij.preview
2+
3+
import by.overpass.svgtocomposeintellij.Bundle
4+
import by.overpass.svgtocomposeintellij.preview.data.KotlinFileIconDataParser
5+
import by.overpass.svgtocomposeintellij.preview.data.imageVectorDeclarationPattern
6+
import by.overpass.svgtocomposeintellij.preview.presentation.ComposeImageVectorPreviewViewModelImpl
7+
import by.overpass.svgtocomposeintellij.preview.ui.ComposeImageVectorPreviewEditor
8+
import com.intellij.openapi.fileEditor.FileEditor
9+
import com.intellij.openapi.fileEditor.FileEditorPolicy
10+
import com.intellij.openapi.fileEditor.FileEditorProvider
11+
import com.intellij.openapi.fileEditor.TextEditor
12+
import com.intellij.openapi.fileEditor.TextEditorWithPreview
13+
import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider
14+
import com.intellij.openapi.project.DumbAware
15+
import com.intellij.openapi.project.Project
16+
import com.intellij.openapi.vfs.VirtualFile
17+
import kotlinx.coroutines.CoroutineScope
18+
import kotlinx.coroutines.SupervisorJob
19+
import org.jetbrains.skiko.MainUIDispatcher
20+
21+
class ComposeImageVectorPreviewEditorProvider : FileEditorProvider, DumbAware {
22+
23+
override fun accept(project: Project, file: VirtualFile): Boolean {
24+
return file.containsImageVector()
25+
}
26+
27+
override fun createEditor(project: Project, file: VirtualFile): FileEditor {
28+
val textEditor = TextEditorProvider.getInstance().createEditor(project, file) as TextEditor
29+
val coroutineScope = CoroutineScope(MainUIDispatcher + SupervisorJob())
30+
return TextEditorWithPreview(
31+
textEditor,
32+
ComposeImageVectorPreviewEditor(
33+
viewModel = ComposeImageVectorPreviewViewModelImpl(
34+
coroutineScope = coroutineScope,
35+
iconDataParser = KotlinFileIconDataParser(
36+
file.toNioPath()
37+
.toFile(),
38+
),
39+
),
40+
coroutineScope = coroutineScope,
41+
),
42+
Bundle.message("preview_editor_title"),
43+
)
44+
}
45+
46+
override fun getEditorTypeId(): String = "compose-image-vector-preview-editor"
47+
48+
override fun getPolicy(): FileEditorPolicy = FileEditorPolicy.HIDE_DEFAULT_EDITOR
49+
50+
private fun VirtualFile.containsImageVector(): Boolean {
51+
return inputStream
52+
.bufferedReader()
53+
.useLines { lines ->
54+
lines.find { line ->
55+
imageVectorDeclarationPattern.containsMatchIn(line)
56+
} != null
57+
}
58+
}
59+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package by.overpass.svgtocomposeintellij.preview.data
2+
3+
@Suppress("MagicNumber")
4+
fun hexToLongColor(hex: String): Long {
5+
check(hex.startsWith("0x"))
6+
// Remove any leading '0x' characters
7+
val hexValue = hex.removePrefix("0x")
8+
9+
// Parse hex string to separate ARGB components
10+
val alpha = hexValue.substring(0, 2).toInt(16)
11+
val red = hexValue.substring(2, 4).toInt(16)
12+
val green = hexValue.substring(4, 6).toInt(16)
13+
val blue = hexValue.substring(6, 8).toInt(16)
14+
15+
// Combine components into a long color value
16+
val color: Long = (alpha.toLong() shl 24) or (red.toLong() shl 16) or (green.toLong() shl 8) or blue.toLong()
17+
18+
return color
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package by.overpass.svgtocomposeintellij.preview.data
2+
3+
import androidx.compose.runtime.Stable
4+
import androidx.compose.ui.graphics.vector.ImageVector
5+
6+
@Stable
7+
data class IconData(
8+
val name: String,
9+
val imageVector: ImageVector,
10+
)

0 commit comments

Comments
 (0)