Skip to content

Commit

Permalink
Use jextract to generate bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
lukellmann committed Aug 30, 2023
1 parent de6f06e commit 6d85ac4
Show file tree
Hide file tree
Showing 14 changed files with 504 additions and 151 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ jobs:
if: matrix.os == 'windows-latest'
with:
toolchain: 'stable'
- name: Setup jextract
if: matrix.os == 'windows-latest'
shell: powershell
run: |
Invoke-WebRequest https://download.java.net/java/early_access/jextract/1/openjdk-20-jextract+1-2_windows-x64_bin.tar.gz -OutFile jextract.tar.gz
tar xzvf jextract.tar.gz
$path = (Resolve-Path jextract-20\bin).Path
echo $path | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
- name: Setup MacOS signing
if: matrix.os == 'macos-latest'
env:
Expand Down
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 2 additions & 9 deletions app/desktop/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins {

dependencies {
implementation(projects.app.shared)
implementation(projects.app.desktop.uwpHelper)
implementation(compose.desktop.currentOs)
implementation(libs.logback)
implementation(libs.ktor.server.netty)
Expand Down Expand Up @@ -79,16 +80,8 @@ tasks {
archiveExtension = "tar.gz"
}

val buildUwpHelper by registering(Exec::class) {
inputs.dir("uwp_helper/src")
outputs.dir("uwp_helper/target")

workingDir = file("uwp_helper")
commandLine("cargo", "build", "--release")
}

val prepareUwpWorkspace by registering(Copy::class) {
dependsOn(buildUwpHelper)
dependsOn("uwp_helper:compileRust")
from(named("packageReleaseAppImage")) {
eachFile {
path = path.substringAfter("Tonbrett")
Expand Down

This file was deleted.

12 changes: 3 additions & 9 deletions app/desktop/src/main/kotlin/ConfigFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ import java.nio.file.StandardOpenOption
import kotlin.io.path.*

val windowsAppDataFolder: String? by lazy {
val result = runCatching {
NativeUtil.getAppdataFolder()
}

if (result.isFailure) {
result.exceptionOrNull()!!.printStackTrace()
}

result.getOrNull()
runCatching { getAppDataRoaming() }
.onFailure(Throwable::printStackTrace)
.getOrNull()
}

@Serializable
Expand Down
37 changes: 37 additions & 0 deletions app/desktop/src/main/kotlin/NativeUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dev.schlaubi.tonbrett.app.desktop

import dev.schlaubi.tonbrett.app.desktop.uwp_helper.AppDataRoamingResult.`is_error$get`
import dev.schlaubi.tonbrett.app.desktop.uwp_helper.AppDataRoamingResult.`length$get`
import dev.schlaubi.tonbrett.app.desktop.uwp_helper.UwpHelper.*
import java.lang.foreign.Arena
import java.net.URI

/**
* Tries to launch the URI using the UWP `Launcher`.
*
* @param uri the [URI] to launch
*/
fun launchUri(uri: URI) = Arena.openConfined().use { arena ->
val url = arena.allocateUtf8String(uri.toString())
launch_uri(url)
}

/**
* Tries to retrieve the current UWP app data folder.
*
* @return the absolute path to the folder
*/
fun getAppDataRoaming() = Arena.openConfined().use { arena ->
val result = get_app_data_roaming(arena)
val isError = `is_error$get`(result)
val length = `length$get`(result).coerceAtLeast(0)
val buffer = arena.allocateArray(uint16_t, length)
copy_string_from_get_app_data_roaming_result_into_buffer(result, buffer)
val shortArray = buffer.toArray(uint16_t)
val charArray = CharArray(shortArray.size)
for ((index, short) in shortArray.withIndex()) {
charArray[index] = short.toInt().toChar()
}
val string = String(charArray)
if (isError) throw Exception(string) else string
}
2 changes: 1 addition & 1 deletion app/desktop/src/main/kotlin/URIUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.net.URI

fun browseUrl(url: URI) {
if (System.getProperty("os.name").contains("windows", ignoreCase = true) && windowsAppDataFolder != null) {
NativeUtil.launchUri(url)
launchUri(url)
} else {
val desktop = Desktop.getDesktop()
if (desktop.isSupported(Desktop.Action.BROWSE)) {
Expand Down
Loading

0 comments on commit 6d85ac4

Please sign in to comment.