Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Preview support #19

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed generatePreview in

val writer = IconWriter(
icons.values,
groupClassName,
iconsPackage,
)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing in #22.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed it was also addressed to #20.

).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,
)
}