Skip to content

Commit

Permalink
Add Preview support (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealfonso93 authored Dec 27, 2023
1 parent f7fe015 commit f12160b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class IconWriter(
private val icons: Collection<Icon>,
private val groupClass: ClassName,
private val groupPackage: String,
private val generatePreview: Boolean
) {
/**
* Generates icons and writes them to [outputSrcDirectory], using [iconNamePredicate] to
Expand Down Expand Up @@ -60,7 +61,8 @@ class IconWriter(
val (fileSpec, accessProperty) = VectorAssetGenerator(
iconName,
groupPackage,
vector
vector,
generatePreview
).createFileSpec(groupClass)

fileSpec.writeTo(outputSrcDirectory)
Expand Down
13 changes: 10 additions & 3 deletions src/main/kotlin/androidx/compose/material/icons/generator/Names.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ import com.squareup.kotlinpoet.MemberName
*/
enum class PackageNames(val packageName: String) {
MaterialIconsPackage("androidx.compose.material.icons"),
GraphicsPackage("androidx.compose.ui.graphics"),
UiPackage("androidx.compose.ui"),
GraphicsPackage(UiPackage.packageName + ".graphics"),
VectorPackage(GraphicsPackage.packageName + ".vector"),
GeometryPackage("androidx.compose.ui.geometry"),
Unit("androidx.compose.ui.unit"),
GeometryPackage(UiPackage.packageName + ".geometry"),
Unit(UiPackage.packageName + ".unit"),
FoundationPackage("androidx.compose.foundation"),
LayoutPackage(FoundationPackage.packageName + ".layout"),
PreviewPackage(UiPackage.packageName + ".tooling.preview"),
RuntimePackage("androidx.compose.runtime"),
}

/**
Expand All @@ -40,6 +45,8 @@ object ClassNames {
val StrokeCap = PackageNames.GraphicsPackage.className("StrokeCap", CompanionImportName)
val StrokeJoin = PackageNames.GraphicsPackage.className("StrokeJoin", CompanionImportName)
val Brush = PackageNames.GraphicsPackage.className("Brush", CompanionImportName)
val Preview = PackageNames.PreviewPackage.className("Preview")
val Composable = PackageNames.RuntimePackage.className("Composable")
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ data class VectorAssetGenerationResult(
* @param iconTheme the theme that this vector belongs to. Used to scope the property to the
* correct receiver object, and also for the package name of the generated file.
* @param vector the parsed vector to generate VectorAssetBuilder commands for
* @param generatePreview if true a preview for the icon will be created.
*/
class VectorAssetGenerator(
private val iconName: String,
private val iconGroupPackage: String,
private val vector: Vector
private val vector: Vector,
private val generatePreview: Boolean
) {
/**
* @return a [FileSpec] representing a Kotlin source file containing the property for this
Expand Down Expand Up @@ -70,7 +72,9 @@ class VectorAssetGenerator(
.build()
).addProperty(
backingProperty
).setIndent().build()
)
.apply { if (generatePreview) addFunction(iconPreview(MemberName(groupClassName, iconName))) }
.setIndent().build()

return VectorAssetGenerationResult(generation, iconName)
}
Expand Down Expand Up @@ -116,7 +120,40 @@ class VectorAssetGenerator(
.build()
}

/**
* @param iconName Name that will be used to call the Icon inside the preview.
*
* Example:
* ```kotlin
* @Preview
* @Composable
* private fun Preview(): Unit {
* Box(modifier = Modifier.padding(12.dp)) {
* Image(imageVector = Icon.Foo, contentDescription = "")
* }
* }
* ```
*/
private fun iconPreview(iconName: MemberName): FunSpec {
val previewAnnotation = AnnotationSpec.builder(ClassNames.Preview).build()
val composableAnnotation = AnnotationSpec.builder(ClassNames.Composable).build()
val box = MemberName(PackageNames.LayoutPackage.packageName, "Box")
val modifier = MemberName(PackageNames.UiPackage.packageName, "Modifier")
val padding = MemberName(PackageNames.LayoutPackage.packageName, "padding")
val paddingValue = MemberNames.Dp
val composeImage = MemberName(PackageNames.FoundationPackage.packageName, "Image")

return FunSpec.builder("Preview")
.addModifiers(KModifier.PRIVATE)
.addAnnotation(previewAnnotation)
.addAnnotation(composableAnnotation)
.addCode(buildCodeBlock {
beginControlFlow("%M(modifier = %M.%M(12.%M))", box, modifier, padding, paddingValue)
addStatement("%M(imageVector = %M, contentDescription = \"\")", composeImage, iconName)
endControlFlow()
})
.build()
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/br/com/devsrsouza/svg2compose/Svg2Compose.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ object Svg2Compose {
vectorsDirectory: File,
type: VectorType = VectorType.SVG,
iconNameTransformer: IconNameTransformer = { it, _ -> it },
allAssetsPropertyName: String = "AllAssets"
allAssetsPropertyName: String = "AllAssets",
generatePreview: Boolean = true,
): ParsingResult {
fun nameRelative(vectorFile: File) = vectorFile.relativeTo(vectorsDirectory).path

Expand Down
3 changes: 2 additions & 1 deletion src/test/kotlin/EmojiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fun main(){
iconNameTransformer = { name, group ->
name.split("-").joinToString(separator = "").removePrefix(group)
},
allAssetsPropertyName = "AllIcons"
allAssetsPropertyName = "AllIcons",
generatePreview = true,
)
}

0 comments on commit f12160b

Please sign in to comment.