ProCraft for Android: An Android native painting app written in Rust
Rust
feat(gallery): artworks share as .pcraft, and as the layered package under its other extension
Josh4858072fa0dc
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.ktindex b7c085a..bde672b 100644--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt@@ -1521,34 +1521,19 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {}}- /** The open artwork as a layered package in the cache, to share. */- suspend fun exportPackageFile(): File? {+ /** The open artwork as a layered package in the cache, to share, named with [extension] (ours or the other app's). */+ suspend fun exportPackageFile(extension: String = OWN_EXTENSION): File? {val s = session ?: return nullreturn withContext(Dispatchers.IO) {runCatching {val base = s.name().filter { it.isLetterOrDigit() || it in " _-" }.trim().replace(' ', '_').ifEmpty { "Untitled_Artwork" }- val out = File(File(getApplication<Application>().cacheDir, "exports").apply { mkdirs() }, "$base.$PACKAGE_EXTENSION")+ val out = File(File(getApplication<Application>().cacheDir, "exports").apply { mkdirs() }, "$base.$extension")s.exportPackage(out.path, System.nanoTime().toULong())out}.onFailure { android.util.Log.e("ProCraft", "exporting a package", it) }.getOrNull()}}- /** The open artwork saved and zipped into the cache as one file, to share. */- suspend fun exportArtworkFile(): File? {- val s = session ?: return null- val dir = artworkDir ?: return null- return withContext(Dispatchers.IO) {- runCatching {- s.save(dir.path)- val out = File(File(getApplication<Application>().cacheDir, "exports").apply { mkdirs() },- s.name().filter { it.isLetterOrDigit() || it in " _-" }.trim().ifEmpty { "Untitled Artwork" } + "." + io.github.artcraftandroid.procraft.gallery.ArtworkFile.EXTENSION)- io.github.artcraftandroid.procraft.gallery.ArtworkFile.write(dir, out)- out- }.getOrNull()- }- }-/*** The gallery zooming into an artwork as it opens ([opening]), or out of it as it closes: the* artwork's cell and where its canvas is (window pixels), and closing, a picture of the canvas.@@ -1641,7 +1626,8 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {"JPEG" -> "jpg" to "image/jpeg""PNG" -> "png" to "image/png""TIFF" -> "tif" to "image/tiff"- else -> PACKAGE_EXTENSION to "application/octet-stream"+ PACKAGE_LABEL -> PACKAGE_EXTENSION to "application/octet-stream"+ else -> OWN_EXTENSION to "application/octet-stream"}val dir = File(getApplication<Application>().cacheDir, "exports").apply { mkdirs() }val used = mutableSetOf<String>()@@ -5837,7 +5823,12 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {const val SINGLE_TOUCH_COMPANION = "single_touch_companion"const val TAP_ASSISTANCE = "tap_assistance"- /** The layered package's file extension, and its name in the Share lists. */+ /**+ * The layered package's file extensions, and their names in the Share lists: ours, and the+ * other app's (the same package, so either opens in both).+ */+ const val OWN_EXTENSION = "pcraft"+ const val OWN_LABEL = "ProCraft"const val PACKAGE_EXTENSION = "procreate"/** Brush and brush set files, as other painting apps share them. */@@ -5846,7 +5837,7 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {val PACKAGE_LABEL = PACKAGE_EXTENSION.replaceFirstChar(Char::titlecase)/** What the gallery's Share offers, in order: still pictures, then animations. */- val SHARE_FORMATS = listOf(PACKAGE_LABEL, "PSD", "PDF", "JPEG", "PNG", "TIFF") + Animated.entries.map { it.title }+ val SHARE_FORMATS = listOf(OWN_LABEL, PACKAGE_LABEL, "PSD", "PDF", "JPEG", "PNG", "TIFF") + Animated.entries.map { it.title }/** How often, and for how long at most, opening waits for the canvas's first frame, milliseconds. */const val CANVAS_POLL_MS = 32L@@ -5866,7 +5857,7 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {/** What an artwork dragged out of the gallery becomes (an index into [DRAG_FORMATS]), and a picture's format when one is wanted. */const val DRAG_FORMAT = "drag_format"const val DRAG_IMAGE_FORMAT = "drag_image_format"- val DRAG_FORMATS = listOf("ProCraft", "PSD", "JPEG", "PNG", "TIFF")+ val DRAG_FORMATS = listOf(OWN_LABEL, "PSD", "JPEG", "PNG", "TIFF")val DRAG_IMAGE_FORMATS = listOf("JPEG", "PNG", "TIFF")const val QUICK_MENU = "quick_menu"/** A press of □ this short, used by nothing, is a tap. */diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/ArtworkFile.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/ArtworkFile.ktindex a340570..22baec3 100644--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/ArtworkFile.kt+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/ArtworkFile.kt@@ -2,28 +2,13 @@ package io.github.artcraftandroid.procraft.galleryimport java.io.Fileimport java.io.InputStream-import java.util.zip.ZipEntryimport java.util.zip.ZipInputStream-import java.util.zip.ZipOutputStream/**- * An artwork as one file to share or keep elsewhere: its folder (the document, every layer, the- * thumbnail and the time-lapse) in a zip.+ * An artwork as one file, as earlier versions shared it: its folder (the document, every layer, the+ * thumbnail and the time-lapse) in a zip. Still read; artworks are shared as packages now.*/object ArtworkFile {- const val EXTENSION = "procraft"-- /** Zips the artwork folder [dir] into [out]. */- fun write(dir: File, out: File) {- ZipOutputStream(out.outputStream().buffered()).use { zip ->- dir.walkTopDown().filter { it.isFile && !it.name.endsWith(".tmp") }.forEach { f ->- zip.putNextEntry(ZipEntry(f.relativeTo(dir).invariantSeparatorsPath))- f.inputStream().use { it.copyTo(zip) }- zip.closeEntry()- }- }- }-/*** Unzips an artwork file into the empty folder [dir]; false (and nothing kept) if it isn't* one. Only the files an artwork has are taken, and none outside the folder.diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/DragOut.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/DragOut.ktindex 661f66f..23c4e53 100644--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/DragOut.kt+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/DragOut.kt@@ -57,7 +57,7 @@ class DragProvider : ContentProvider() {override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<out String>?) = 0companion object {- private val Extensions = mapOf("ProCraft" to ArtworkFile.EXTENSION, "PSD" to "psd", "JPEG" to "jpg", "PNG" to "png", "TIFF" to "tif")+ private val Extensions = mapOf(CanvasViewModel.OWN_LABEL to CanvasViewModel.OWN_EXTENSION, "PSD" to "psd", "JPEG" to "jpg", "PNG" to "png", "TIFF" to "tif")fun mime(format: String): String = when (format) {"PSD" -> "image/vnd.adobe.photoshop"@@ -89,14 +89,11 @@ class DragProvider : ContentProvider() {* of the gallery's still [CanvasViewModel.SHARE_FORMATS]).*/fun exportArtwork(dir: File, format: String, out: File, pdfQuality: Int = 100): Boolean = runCatching {- if (format == "ProCraft") {- ArtworkFile.write(dir, out)- return@runCatching true- }CanvasSession.open(dir.path).use { s ->if (!s.prepareOffscreen()) return@runCatching falsewhen (format) {- CanvasViewModel.PACKAGE_LABEL -> s.exportPackage(out.path, System.nanoTime().toULong())+ // ours and the other app's are the same package+ CanvasViewModel.OWN_LABEL, CanvasViewModel.PACKAGE_LABEL -> s.exportPackage(out.path, System.nanoTime().toULong())"PDF" -> s.exportPdf(out.path, false, pdfQuality.toUByte())"PSD" -> s.exportPsd(out.path)"TIFF" -> s.exportTiff(out.path)diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/ActionsPanel.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/ActionsPanel.ktindex d2b16fb..079fe64 100644--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/ActionsPanel.kt+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/ActionsPanel.kt@@ -146,6 +146,13 @@ fun ActionsPanel(vm: CanvasViewModel, arrowX: Dp, modifier: Modifier = Modifier)if (files.isNotEmpty()) shareFiles(context, files, kind.mime) else dialog = "Share" to "The artwork could not be exported."}}+ // the layered package, named with ours or the other app's extension+ fun sharePackage(extension: String) {+ scope.launch {+ val file = vm.exportPackageFile(extension)+ if (file != null) shareFile(context, file, "application/octet-stream") else dialog = "Share" to "The artwork could not be exported."+ }+ }fun share(jpeg: Boolean) {scope.launch {val file = vm.exportImage(jpeg)@@ -224,12 +231,9 @@ fun ActionsPanel(vm: CanvasViewModel, arrowX: Dp, modifier: Modifier = Modifier)2 -> listOf(listOf(Heading("Share Image"),- Act(CanvasViewModel.PACKAGE_LABEL) {- scope.launch {- val file = vm.exportPackageFile()- if (file != null) shareFile(context, file, "application/octet-stream") else dialog = "Share" to "The artwork could not be exported."- }- },+ // ours first, then the same package named for the other app+ Act(CanvasViewModel.OWN_LABEL) { sharePackage(CanvasViewModel.OWN_EXTENSION) },+ Act(CanvasViewModel.PACKAGE_LABEL) { sharePackage(CanvasViewModel.PACKAGE_EXTENSION) },Act("PSD") { share(CanvasViewModel.Export.PSD) },Act("PDF") { pdfAsking = CanvasViewModel.Export.PDF },Act("JPEG") { share(jpeg = true) },@@ -426,6 +430,7 @@ private fun modelGroups(listOf(Heading("Share Model"),// the native package doesn't hold a model yet+ Act(CanvasViewModel.OWN_LABEL),Act(CanvasViewModel.PACKAGE_LABEL),Act("USDZ") { share(CanvasViewModel.Export.MODEL_USDZ, 100) },Act("OBJ") { share(CanvasViewModel.Export.MODEL_OBJ, 100) },diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/GalleryScreen.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/GalleryScreen.ktindex 6d9238b..b94db48 100644--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/GalleryScreen.kt+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/GalleryScreen.kt@@ -779,7 +779,7 @@ private class GalleryShare(val artworks: List<Artwork>, val from: androidx.compoprivate val FormatsWidth = 260.dpprivate val FormatsTitleHeight = 55.dpprivate val FormatsRow = 40.dp-private val FormatsHeight = FormatsTitleHeight + FormatsRow * 10 + 9.dp + 10.dp+private val FormatsHeight = FormatsTitleHeight + FormatsRow * CanvasViewModel.SHARE_FORMATS.size + (CanvasViewModel.SHARE_FORMATS.size - 1).dp + 10.dp/** Where Share's formats go: their top left, the arrow's side and place, and where they grow from. */private class FormatsPlace(val left: Dp, val top: Dp, val side: ArrowSide, val arrowAt: Dp, val origin: androidx.compose.ui.graphics.TransformOrigin)