-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from MohamedRejeb/0.3.1
Add WasmJs Support
- Loading branch information
Showing
45 changed files
with
866 additions
and
402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
plugins { | ||
id("root.publication") | ||
//trick: for the same plugin versions in all sub-modules | ||
// id("root.publication") | ||
// trick: for the same plugin versions in all sub-modules | ||
alias(libs.plugins.androidApplication).apply(false) | ||
alias(libs.plugins.androidLibrary).apply(false) | ||
alias(libs.plugins.kotlinMultiplatform).apply(false) | ||
alias(libs.plugins.kotlinAndroid).apply(false) | ||
alias(libs.plugins.kotlinJvm).apply(false) | ||
alias(libs.plugins.kotlinSerialization).apply(false) | ||
alias(libs.plugins.composeMultiplatform).apply(false) | ||
} | ||
alias(libs.plugins.conventionPlugin).apply(false) | ||
} | ||
|
||
rootPublicationSetup() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,15 @@ | ||
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi | ||
|
||
plugins { | ||
alias(libs.plugins.kotlinMultiplatform) | ||
alias(libs.plugins.composeMultiplatform) | ||
alias(libs.plugins.androidLibrary) | ||
id("module.publication") | ||
} | ||
|
||
kotlin { | ||
@OptIn(ExperimentalKotlinGradlePluginApi::class) | ||
applyDefaultHierarchyTemplate { | ||
common { | ||
group("nonAndroid") { | ||
withJvm() | ||
withIos() | ||
withJs() | ||
} | ||
} | ||
} | ||
androidTarget { | ||
publishLibraryVariants("release") | ||
compilations.all { | ||
kotlinOptions { | ||
jvmTarget = "11" | ||
} | ||
} | ||
} | ||
jvm("desktop") { | ||
jvmToolchain(11) | ||
} | ||
js(IR) { | ||
browser() | ||
} | ||
iosX64() | ||
iosArm64() | ||
iosSimulatorArm64() | ||
composeMultiplatformSetup() | ||
modulePublicationSetup() | ||
|
||
kotlin { | ||
sourceSets.commonMain.dependencies { | ||
implementation(compose.runtime) | ||
implementation(compose.foundation) | ||
} | ||
} | ||
|
||
android { | ||
namespace = "com.mohamedrejeb.calf.core" | ||
compileSdk = libs.versions.android.compileSdk.get().toInt() | ||
defaultConfig { | ||
minSdk = libs.versions.android.minSdk.get().toInt() | ||
} | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_11 | ||
targetCompatibility = JavaVersion.VERSION_11 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...-file-picker/src/wasmJsMain/kotlin/com.mohamedrejeb.calf/picker/ComposeResource.wasmJs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.mohamedrejeb.calf.picker | ||
|
||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.toComposeImageBitmap | ||
import org.jetbrains.skia.Image | ||
|
||
actual fun ByteArray.toImageBitmap(): ImageBitmap = Image.makeFromEncoded(this).toComposeImageBitmap() |
72 changes: 72 additions & 0 deletions
72
...le-picker/src/wasmJsMain/kotlin/com.mohamedrejeb.calf/picker/FilePickerLauncher.wasmJs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.mohamedrejeb.calf.picker | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.runtime.setValue | ||
import com.mohamedrejeb.calf.io.KmpFile | ||
import kotlinx.browser.document | ||
import org.w3c.dom.Element | ||
import org.w3c.files.File | ||
|
||
@Composable | ||
actual fun rememberFilePickerLauncher( | ||
type: FilePickerFileType, | ||
selectionMode: FilePickerSelectionMode, | ||
onResult: (List<KmpFile>) -> Unit, | ||
): FilePickerLauncher { | ||
var fileDialogVisible by rememberSaveable { mutableStateOf(false) } | ||
|
||
return remember { | ||
FilePickerLauncher( | ||
type = type, | ||
selectionMode = selectionMode, | ||
onLaunch = { | ||
val fileInputElement = document.createElement("input") | ||
fileInputElement.setAttribute("style", "display='none'") | ||
fileInputElement.setAttribute("type", "file") | ||
fileInputElement.setAttribute("name", "file") | ||
|
||
fileInputElement.setAttribute("accept", type.value.joinToString(", ")) | ||
|
||
if (selectionMode == FilePickerSelectionMode.Multiple) | ||
fileInputElement.setAttribute("multiple", "true") | ||
else | ||
fileInputElement.removeAttribute("multiple") | ||
|
||
fileInputElement.addEventListener("change") { | ||
val filesCount = getInputElementFilesCount(fileInputElement) | ||
val files = | ||
List(filesCount) { index -> | ||
getInputElementFile(fileInputElement, index) | ||
} | ||
onResult(files.map { KmpFile(it) }) | ||
fileDialogVisible = false | ||
} | ||
|
||
openFileDialog(fileInputElement) | ||
}, | ||
) | ||
} | ||
} | ||
|
||
actual class FilePickerLauncher actual constructor( | ||
type: FilePickerFileType, | ||
selectionMode: FilePickerSelectionMode, | ||
private val onLaunch: () -> Unit, | ||
) { | ||
actual fun launch() { | ||
onLaunch() | ||
} | ||
} | ||
|
||
private fun getInputElementFilesCount(element: Element): Int = js("element.files.length") | ||
|
||
private fun getInputElementFile( | ||
element: Element, | ||
index: Int, | ||
): File = js("element.files[index]") | ||
|
||
private fun openFileDialog(element: Element): Unit = js("element.click()") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,17 @@ | ||
plugins { | ||
alias(libs.plugins.kotlinMultiplatform) | ||
alias(libs.plugins.androidLibrary) | ||
id("module.publication") | ||
} | ||
|
||
kotlin { | ||
kotlin.applyDefaultHierarchyTemplate() | ||
androidTarget { | ||
publishLibraryVariants("release") | ||
compilations.all { | ||
kotlinOptions { | ||
jvmTarget = "11" | ||
} | ||
} | ||
} | ||
jvm("desktop") { | ||
jvmToolchain(11) | ||
} | ||
js(IR) { | ||
browser() | ||
} | ||
iosX64() | ||
iosArm64() | ||
iosSimulatorArm64() | ||
kotlinMultiplatformSetup() | ||
modulePublicationSetup() | ||
|
||
kotlin { | ||
sourceSets.commonMain.dependencies { | ||
api(projects.calfCore) | ||
|
||
implementation(libs.documentfile) | ||
} | ||
} | ||
|
||
android { | ||
namespace = "com.mohamedrejeb.calf.io" | ||
compileSdk = libs.versions.android.compileSdk.get().toInt() | ||
defaultConfig { | ||
minSdk = libs.versions.android.minSdk.get().toInt() | ||
} | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_11 | ||
targetCompatibility = JavaVersion.VERSION_11 | ||
sourceSets.androidMain.dependencies { | ||
implementation(libs.documentfile) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
calf-io/src/wasmJsMain/kotlin/com.mohamedrejeb.calf/io/KmpFile.wasmJs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.mohamedrejeb.calf.io | ||
|
||
import com.mohamedrejeb.calf.core.PlatformContext | ||
import org.khronos.webgl.ArrayBuffer | ||
import org.khronos.webgl.Uint8Array | ||
import org.khronos.webgl.get | ||
import org.w3c.dom.events.Event | ||
import org.w3c.files.File | ||
import org.w3c.files.FileReader | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.suspendCoroutine | ||
|
||
/** | ||
* Represents a file in a platform-agnostic way. | ||
*/ | ||
actual class KmpFile( | ||
val file: File, | ||
) | ||
|
||
actual fun KmpFile.exists(context: PlatformContext) = true | ||
|
||
actual suspend fun KmpFile.readByteArray(context: PlatformContext): ByteArray = | ||
suspendCoroutine { continuation -> | ||
val fileReader = FileReader() | ||
fileReader.readAsArrayBuffer(file) | ||
fileReader.onloadend = { event -> | ||
val readyState = getReadyState(event) | ||
if (readyState == FileReader.DONE) { | ||
val arrayBuffer: ArrayBuffer = getResult(event) | ||
val array = Uint8Array(arrayBuffer) | ||
val byteArray = | ||
ByteArray(array.length) { index -> | ||
array[index] | ||
} | ||
continuation.resume(byteArray) | ||
} else { | ||
continuation.resume(ByteArray(0)) | ||
} | ||
} | ||
} | ||
|
||
actual fun KmpFile.getName(context: PlatformContext): String? = file.name | ||
|
||
actual fun KmpFile.getPath(context: PlatformContext): String? = file.name | ||
|
||
actual fun KmpFile.isDirectory(context: PlatformContext): Boolean = !file.name.contains(".") | ||
|
||
private fun getReadyState(event: Event): Short = js("event.target.readyState") | ||
|
||
private fun getResult(event: Event): ArrayBuffer = js("event.target.result") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.