commit c3f3e8fd06b5e85d33e62cf1f913ef931fb5d072 Author: Cfp Date: Thu Aug 7 10:01:06 2025 +0200 chore: init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..50d8e32 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +/.vs +.DS_Store +.Thumbs.db +*.sublime* +.idea/ +debug.log +package-lock.json +.vscode/settings.json +yarn.lock + +/.tauri +/target +Cargo.lock +node_modules/ + +dist-js +dist diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..bdf5a31 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "tauri-plugin-duck" +version = "0.1.0" +authors = [ "You" ] +description = "" +edition = "2021" +rust-version = "1.77.2" +exclude = ["/examples", "/dist-js", "/guest-js", "/node_modules"] +links = "tauri-plugin-duck" + +[dependencies] +tauri = { version = "2.7.0" } +serde = "1.0" +thiserror = "2" + +[build-dependencies] +tauri-plugin = { version = "2.3.1", features = ["build"] } +tauri-build = "2.3.1" diff --git a/README.md b/README.md new file mode 100644 index 0000000..a4dd456 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Tauri Plugin duck diff --git a/android/.gitignore b/android/.gitignore new file mode 100644 index 0000000..c0f21ec --- /dev/null +++ b/android/.gitignore @@ -0,0 +1,2 @@ +/build +/.tauri diff --git a/android/build.gradle.kts b/android/build.gradle.kts new file mode 100644 index 0000000..f38214b --- /dev/null +++ b/android/build.gradle.kts @@ -0,0 +1,44 @@ +plugins { + id("com.android.library") + id("org.jetbrains.kotlin.android") +} + +android { + namespace = "de.cfp.duck" + compileSdk = 36 + + defaultConfig { + minSdk = 21 + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + consumerProguardFiles("consumer-rules.pro") + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = "1.8" + } +} + +dependencies { + + implementation("androidx.core:core-ktx:1.9.0") + implementation("androidx.appcompat:appcompat:1.6.0") + implementation("com.google.android.material:material:1.7.0") + testImplementation("junit:junit:4.13.2") + androidTestImplementation("androidx.test.ext:junit:1.1.5") + androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") + implementation(project(":tauri-android")) +} diff --git a/android/proguard-rules.pro b/android/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/android/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/android/settings.gradle b/android/settings.gradle new file mode 100644 index 0000000..d7782a4 --- /dev/null +++ b/android/settings.gradle @@ -0,0 +1,31 @@ +pluginManagement { + repositories { + mavenCentral() + gradlePluginPortal() + google() + } + resolutionStrategy { + eachPlugin { + switch (requested.id.id) { + case "com.android.library": + useVersion("8.0.2") + break + case "org.jetbrains.kotlin.android": + useVersion("1.8.20") + break + } + } + } +} + +dependencyResolutionManagement { + repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) + repositories { + mavenCentral() + google() + + } +} + +include ':tauri-android' +project(':tauri-android').projectDir = new File('./.tauri/tauri-api') diff --git a/android/src/androidTest/java/ExampleInstrumentedTest.kt b/android/src/androidTest/java/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..2982ba8 --- /dev/null +++ b/android/src/androidTest/java/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package de.cfp.duck + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("de.cfp.duck", appContext.packageName) + } +} diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml new file mode 100644 index 0000000..9a40236 --- /dev/null +++ b/android/src/main/AndroidManifest.xml @@ -0,0 +1,3 @@ + + + diff --git a/android/src/main/java/Duck.kt b/android/src/main/java/Duck.kt new file mode 100644 index 0000000..d70c89c --- /dev/null +++ b/android/src/main/java/Duck.kt @@ -0,0 +1,10 @@ +package de.cfp.duck + +import android.util.Log + +class Duck { + fun pong(value: String): String { + Log.i("Pong", value) + return value + } +} diff --git a/android/src/main/java/DuckPlugin.kt b/android/src/main/java/DuckPlugin.kt new file mode 100644 index 0000000..fdeebeb --- /dev/null +++ b/android/src/main/java/DuckPlugin.kt @@ -0,0 +1,51 @@ +package de.cfp.duck + +import android.app.Activity +import app.tauri.annotation.Command +import app.tauri.annotation.InvokeArg +import app.tauri.annotation.TauriPlugin +import app.tauri.plugin.JSObject +import app.tauri.plugin.Plugin +import app.tauri.plugin.Invoke + +import android.media.AudioFocusRequest +import android.media.AudioManager +import android.context.Context +import android.webkit.WebView + +@TauriPlugin +class DuckPlugin(private val activity: Activity): Plugin(activity) { + private val implementation = Duck() + private lateinit var audioManager: AudioManager + private lateinit var focusRequest: AudioFocusRequest + + override fun load(webView: WebView) { + audioManager = (AudioManager) getBridge().getActivity().getSystemService(Context.AUDIO_SERVICE) + } + + @Command + fun duck(invoke: Invoke) { + focusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK) + .setAcceptsDelayedFocusGain(false) + .setWillPauseWhenDucked(false) + .setForceDucking(true) + .build() + + audioManager.requestAudioFocus(focusRequest) + + val ret = JSObject() + ret.put("success", true) + invoke.resolve(ret) + } + + @Command + fun unduck(invoke: Invoke) { + if (focusRequest != null) { + audioManager.abandonAudioFocusRequest(focusRequest) + } + + val ret = JSObject() + ret.put("success", true) + invoke.resolve(ret) + } +} diff --git a/android/src/test/java/ExampleUnitTest.kt b/android/src/test/java/ExampleUnitTest.kt new file mode 100644 index 0000000..32cee19 --- /dev/null +++ b/android/src/test/java/ExampleUnitTest.kt @@ -0,0 +1,17 @@ +package de.cfp.duck + +import org.junit.Test + +import org.junit.Assert.* + +/** + * Example local unit test, which will execute on the development machine (host). + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +class ExampleUnitTest { + @Test + fun addition_isCorrect() { + assertEquals(4, 2 + 2) + } +} diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..e0ff2cc --- /dev/null +++ b/build.rs @@ -0,0 +1,8 @@ +const COMMANDS: &[&str] = &["ping"]; + +fn main() { + tauri_plugin::Builder::new(COMMANDS) + .android_path("android") + .ios_path("ios") + .build(); +} diff --git a/examples/tauri-app/.gitignore b/examples/tauri-app/.gitignore new file mode 100644 index 0000000..a547bf3 --- /dev/null +++ b/examples/tauri-app/.gitignore @@ -0,0 +1,24 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +node_modules +dist +dist-ssr +*.local + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +.DS_Store +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/examples/tauri-app/.vscode/extensions.json b/examples/tauri-app/.vscode/extensions.json new file mode 100644 index 0000000..61343e9 --- /dev/null +++ b/examples/tauri-app/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + "recommendations": [ + "svelte.svelte-vscode", + "tauri-apps.tauri-vscode", + "rust-lang.rust-analyzer" + ] +} diff --git a/examples/tauri-app/README.md b/examples/tauri-app/README.md new file mode 100644 index 0000000..72726a1 --- /dev/null +++ b/examples/tauri-app/README.md @@ -0,0 +1,8 @@ +# Svelte + Vite + +This template should help get you started developing with Tauri and Svelte in Vite. + +## Recommended IDE Setup + +[VS Code](https://code.visualstudio.com/) + [Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode) + [Tauri](https://marketplace.visualstudio.com/items?itemName=tauri-apps.tauri-vscode) + [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer). + diff --git a/examples/tauri-app/index.html b/examples/tauri-app/index.html new file mode 100644 index 0000000..fad1c5d --- /dev/null +++ b/examples/tauri-app/index.html @@ -0,0 +1,14 @@ + + + + + + + Tauri + Svelte + + + +
+ + + diff --git a/examples/tauri-app/jsconfig.json b/examples/tauri-app/jsconfig.json new file mode 100644 index 0000000..ee5e92f --- /dev/null +++ b/examples/tauri-app/jsconfig.json @@ -0,0 +1,34 @@ +{ + "compilerOptions": { + "moduleResolution": "Node", + "target": "ESNext", + "module": "ESNext", + /** + * svelte-preprocess cannot figure out whether you have + * a value or a type, so tell TypeScript to enforce using + * `import type` instead of `import` for Types. + */ + "importsNotUsedAsValues": "error", + "isolatedModules": true, + "resolveJsonModule": true, + /** + * To have warnings / errors of the Svelte compiler at the + * correct position, enable source maps by default. + */ + "sourceMap": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + "baseUrl": ".", + /** + * Typecheck JS in `.svelte` and `.js` files by default. + * Disable this if you'd like to use dynamic types. + */ + "checkJs": true + }, + /** + * Use global.d.ts instead of compilerOptions.types + * to avoid limiting type declarations. + */ + "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"] +} diff --git a/examples/tauri-app/package.json b/examples/tauri-app/package.json new file mode 100644 index 0000000..3e50c8f --- /dev/null +++ b/examples/tauri-app/package.json @@ -0,0 +1,22 @@ +{ + "name": "tauri-app", + "private": true, + "version": "0.1.0", + "type": "module", + "scripts": { + "dev": "vite", + "build": "vite build", + "preview": "vite preview", + "tauri": "tauri" + }, + "dependencies": { + "@tauri-apps/api": "^2.0.0", + "tauri-plugin-duck-api": "file:../../" + }, + "devDependencies": { + "@sveltejs/vite-plugin-svelte": "^6.0.0", + "svelte": "^5.0.0", + "vite": "^7.0.0", + "@tauri-apps/cli": "^2.0.0" + } +} diff --git a/examples/tauri-app/public/svelte.svg b/examples/tauri-app/public/svelte.svg new file mode 100644 index 0000000..c5e0848 --- /dev/null +++ b/examples/tauri-app/public/svelte.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/examples/tauri-app/public/tauri.svg b/examples/tauri-app/public/tauri.svg new file mode 100644 index 0000000..31b62c9 --- /dev/null +++ b/examples/tauri-app/public/tauri.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/examples/tauri-app/public/vite.svg b/examples/tauri-app/public/vite.svg new file mode 100644 index 0000000..e7b8dfb --- /dev/null +++ b/examples/tauri-app/public/vite.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/examples/tauri-app/src-tauri/.gitignore b/examples/tauri-app/src-tauri/.gitignore new file mode 100644 index 0000000..f4dfb82 --- /dev/null +++ b/examples/tauri-app/src-tauri/.gitignore @@ -0,0 +1,4 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + diff --git a/examples/tauri-app/src-tauri/Cargo.toml b/examples/tauri-app/src-tauri/Cargo.toml new file mode 100644 index 0000000..59c05c2 --- /dev/null +++ b/examples/tauri-app/src-tauri/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "tauri-app" +version = "0.1.0" +description = "A Tauri App" +authors = ["you"] +license = "" +repository = "" +edition = "2021" +rust-version = "1.77.2" + +[lib] +name = "tauri_app_lib" +crate-type = ["staticlib", "cdylib", "rlib"] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[build-dependencies] +tauri-build = { version = "2.3.1", default-features = false } + +[dependencies] +tauri = { version = "2.7.0" } +tauri-plugin-duck = { path = "../../../" } + diff --git a/examples/tauri-app/src-tauri/build.rs b/examples/tauri-app/src-tauri/build.rs new file mode 100644 index 0000000..795b9b7 --- /dev/null +++ b/examples/tauri-app/src-tauri/build.rs @@ -0,0 +1,3 @@ +fn main() { + tauri_build::build() +} diff --git a/examples/tauri-app/src-tauri/capabilities/default.json b/examples/tauri-app/src-tauri/capabilities/default.json new file mode 100644 index 0000000..fc9aaeb --- /dev/null +++ b/examples/tauri-app/src-tauri/capabilities/default.json @@ -0,0 +1,12 @@ +{ + "$schema": "../gen/schemas/desktop-schema.json", + "identifier": "default", + "description": "enables the default permissions", + "windows": [ + "main" + ], + "permissions": [ + "core:default", + "duck:default" + ] +} diff --git a/examples/tauri-app/src-tauri/icons/128x128.png b/examples/tauri-app/src-tauri/icons/128x128.png new file mode 100644 index 0000000..77e7d23 Binary files /dev/null and b/examples/tauri-app/src-tauri/icons/128x128.png differ diff --git a/examples/tauri-app/src-tauri/icons/128x128@2x.png b/examples/tauri-app/src-tauri/icons/128x128@2x.png new file mode 100644 index 0000000..0f7976f Binary files /dev/null and b/examples/tauri-app/src-tauri/icons/128x128@2x.png differ diff --git a/examples/tauri-app/src-tauri/icons/32x32.png b/examples/tauri-app/src-tauri/icons/32x32.png new file mode 100644 index 0000000..98fda06 Binary files /dev/null and b/examples/tauri-app/src-tauri/icons/32x32.png differ diff --git a/examples/tauri-app/src-tauri/icons/icon.icns b/examples/tauri-app/src-tauri/icons/icon.icns new file mode 100644 index 0000000..29d6685 Binary files /dev/null and b/examples/tauri-app/src-tauri/icons/icon.icns differ diff --git a/examples/tauri-app/src-tauri/icons/icon.ico b/examples/tauri-app/src-tauri/icons/icon.ico new file mode 100644 index 0000000..06c23c8 Binary files /dev/null and b/examples/tauri-app/src-tauri/icons/icon.ico differ diff --git a/examples/tauri-app/src-tauri/icons/icon.png b/examples/tauri-app/src-tauri/icons/icon.png new file mode 100644 index 0000000..d1756ce Binary files /dev/null and b/examples/tauri-app/src-tauri/icons/icon.png differ diff --git a/examples/tauri-app/src-tauri/src/lib.rs b/examples/tauri-app/src-tauri/src/lib.rs new file mode 100644 index 0000000..e5d68a7 --- /dev/null +++ b/examples/tauri-app/src-tauri/src/lib.rs @@ -0,0 +1,14 @@ +// Learn more about Tauri commands at https://v2.tauri.app/develop/calling-rust/#commands +#[tauri::command] +fn greet(name: &str) -> String { + format!("Hello, {}! You've been greeted from Rust!", name) +} + +#[cfg_attr(mobile, tauri::mobile_entry_point)] +pub fn run() { + tauri::Builder::default() + .invoke_handler(tauri::generate_handler![greet]) + .plugin(tauri_plugin_duck::init()) + .run(tauri::generate_context!()) + .expect("error while running tauri application"); +} diff --git a/examples/tauri-app/src-tauri/src/main.rs b/examples/tauri-app/src-tauri/src/main.rs new file mode 100644 index 0000000..455963e --- /dev/null +++ b/examples/tauri-app/src-tauri/src/main.rs @@ -0,0 +1,6 @@ +// Prevents additional console window on Windows in release, DO NOT REMOVE!! +#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] + +fn main() { + tauri_app_lib::run(); +} diff --git a/examples/tauri-app/src-tauri/tauri.conf.json b/examples/tauri-app/src-tauri/tauri.conf.json new file mode 100644 index 0000000..72ebf40 --- /dev/null +++ b/examples/tauri-app/src-tauri/tauri.conf.json @@ -0,0 +1,37 @@ +{ + "productName": "tauri-app", + "version": "0.1.0", + "identifier": "com.tauri.dev", + "build": { + "beforeDevCommand": "pnpm dev", + "beforeBuildCommand": "pnpm build", + "devUrl": "http://localhost:1420", + "frontendDist": "../dist" + }, + "app": { + "withGlobalTauri": false, + "security": { + "csp": null + }, + "windows": [ + { + "fullscreen": false, + "height": 600, + "resizable": true, + "title": "tauri-app", + "width": 800 + } + ] + }, + "bundle": { + "active": true, + "targets": "all", + "icon": [ + "icons/32x32.png", + "icons/128x128.png", + "icons/128x128@2x.png", + "icons/icon.icns", + "icons/icon.ico" + ] + } +} diff --git a/examples/tauri-app/src/App.svelte b/examples/tauri-app/src/App.svelte new file mode 100644 index 0000000..214c895 --- /dev/null +++ b/examples/tauri-app/src/App.svelte @@ -0,0 +1,59 @@ + + +
+

Welcome to Tauri!

+ + + +

+ Click on the Tauri, Vite, and Svelte logos to learn more. +

+ +
+ +
+ +
+ + +
{@html response}
+
+ +
+ + diff --git a/examples/tauri-app/src/lib/Greet.svelte b/examples/tauri-app/src/lib/Greet.svelte new file mode 100644 index 0000000..41e901b --- /dev/null +++ b/examples/tauri-app/src/lib/Greet.svelte @@ -0,0 +1,22 @@ + + +
+
+ + +
+

{greetMsg}

+
+ diff --git a/examples/tauri-app/src/main.js b/examples/tauri-app/src/main.js new file mode 100644 index 0000000..6b4e1a9 --- /dev/null +++ b/examples/tauri-app/src/main.js @@ -0,0 +1,8 @@ +import "./style.css"; +import App from "./App.svelte"; + +const app = new App({ + target: document.getElementById("app"), +}); + +export default app; diff --git a/examples/tauri-app/src/style.css b/examples/tauri-app/src/style.css new file mode 100644 index 0000000..c0f9e3b --- /dev/null +++ b/examples/tauri-app/src/style.css @@ -0,0 +1,102 @@ +:root { + font-family: Inter, Avenir, Helvetica, Arial, sans-serif; + font-size: 16px; + line-height: 24px; + font-weight: 400; + + color: #0f0f0f; + background-color: #f6f6f6; + + font-synthesis: none; + text-rendering: optimizeLegibility; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + -webkit-text-size-adjust: 100%; +} + +.container { + margin: 0; + padding-top: 10vh; + display: flex; + flex-direction: column; + justify-content: center; + text-align: center; +} + +.logo { + height: 6em; + padding: 1.5em; + will-change: filter; + transition: 0.75s; +} + +.logo.tauri:hover { + filter: drop-shadow(0 0 2em #24c8db); +} + +.row { + display: flex; + justify-content: center; +} + +a { + font-weight: 500; + color: #646cff; + text-decoration: inherit; +} + +a:hover { + color: #535bf2; +} + +h1 { + text-align: center; +} + +input, +button { + border-radius: 8px; + border: 1px solid transparent; + padding: 0.6em 1.2em; + font-size: 1em; + font-weight: 500; + font-family: inherit; + color: #0f0f0f; + background-color: #ffffff; + transition: border-color 0.25s; + box-shadow: 0 2px 2px rgba(0, 0, 0, 0.2); +} + +button { + cursor: pointer; +} + +button:hover { + border-color: #396cd8; +} + +input, +button { + outline: none; +} + +#greet-input { + margin-right: 5px; +} + +@media (prefers-color-scheme: dark) { + :root { + color: #f6f6f6; + background-color: #2f2f2f; + } + + a:hover { + color: #24c8db; + } + + input, + button { + color: #ffffff; + background-color: #0f0f0f98; + } +} diff --git a/examples/tauri-app/src/vite-env.d.ts b/examples/tauri-app/src/vite-env.d.ts new file mode 100644 index 0000000..4078e74 --- /dev/null +++ b/examples/tauri-app/src/vite-env.d.ts @@ -0,0 +1,2 @@ +/// +/// diff --git a/examples/tauri-app/vite.config.js b/examples/tauri-app/vite.config.js new file mode 100644 index 0000000..82e1f23 --- /dev/null +++ b/examples/tauri-app/vite.config.js @@ -0,0 +1,24 @@ +import { defineConfig } from "vite"; +import { svelte } from "@sveltejs/vite-plugin-svelte"; + +const host = process.env.TAURI_DEV_HOST; + +// https://vite.dev/config/ +export default defineConfig({ + plugins: [svelte()], + + // Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build` + // prevent Vite from obscuring rust errors + clearScreen: false, + // tauri expects a fixed port, fail if that port is not available + server: { + host: host || false, + port: 1420, + strictPort: true, + hmr: host ? { + protocol: 'ws', + host, + port: 1421 + } : undefined, + }, +}) diff --git a/guest-js/index.ts b/guest-js/index.ts new file mode 100644 index 0000000..8dd6855 --- /dev/null +++ b/guest-js/index.ts @@ -0,0 +1,13 @@ +import { invoke } from '@tauri-apps/api/core' + +type DuckResponse = { + success: boolean +} + +export async function duck(): Promise { + return await invoke<{value?: string}>('plugin:duck|duck').then((r: DuckResponse) => r.success); +} + +export async function unduck(value: string): Promise { + return await invoke<{value?: string}>('plugin:duck|unduck').then((r: DuckResponse) => r.success); +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..7fcd081 --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "tauri-plugin-duck-api", + "version": "0.1.0", + "author": "You", + "description": "", + "type": "module", + "types": "./dist-js/index.d.ts", + "main": "./dist-js/index.cjs", + "module": "./dist-js/index.js", + "exports": { + "types": "./dist-js/index.d.ts", + "import": "./dist-js/index.js", + "require": "./dist-js/index.cjs" + }, + "files": [ + "dist-js", + "README.md" + ], + "scripts": { + "build": "rollup -c", + "prepublishOnly": "pnpm build", + "pretest": "pnpm build" + }, + "dependencies": { + "@tauri-apps/api": ">=2.0.0-beta.6" + }, + "devDependencies": { + "@rollup/plugin-typescript": "^12.0.0", + "rollup": "^4.9.6", + "typescript": "^5.3.3", + "tslib": "^2.6.2" + } +} diff --git a/permissions/autogenerated/commands/ping.toml b/permissions/autogenerated/commands/ping.toml new file mode 100644 index 0000000..1d13588 --- /dev/null +++ b/permissions/autogenerated/commands/ping.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-ping" +description = "Enables the ping command without any pre-configured scope." +commands.allow = ["ping"] + +[[permission]] +identifier = "deny-ping" +description = "Denies the ping command without any pre-configured scope." +commands.deny = ["ping"] diff --git a/permissions/autogenerated/reference.md b/permissions/autogenerated/reference.md new file mode 100644 index 0000000..ce31f32 --- /dev/null +++ b/permissions/autogenerated/reference.md @@ -0,0 +1,43 @@ +## Default Permission + +Default permissions for the plugin + +#### This default permission set includes the following: + +- `allow-ping` + +## Permission Table + + + + + + + + + + + + + + + + + +
IdentifierDescription
+ +`duck:allow-ping` + + + +Enables the ping command without any pre-configured scope. + +
+ +`duck:deny-ping` + + + +Denies the ping command without any pre-configured scope. + +
diff --git a/permissions/default.toml b/permissions/default.toml new file mode 100644 index 0000000..cc5a76f --- /dev/null +++ b/permissions/default.toml @@ -0,0 +1,3 @@ +[default] +description = "Default permissions for the plugin" +permissions = ["allow-ping"] diff --git a/permissions/schemas/schema.json b/permissions/schemas/schema.json new file mode 100644 index 0000000..ac68e12 --- /dev/null +++ b/permissions/schemas/schema.json @@ -0,0 +1,318 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "PermissionFile", + "description": "Permission file that can define a default permission, a set of permissions or a list of inlined permissions.", + "type": "object", + "properties": { + "default": { + "description": "The default permission set for the plugin", + "anyOf": [ + { + "$ref": "#/definitions/DefaultPermission" + }, + { + "type": "null" + } + ] + }, + "set": { + "description": "A list of permissions sets defined", + "type": "array", + "items": { + "$ref": "#/definitions/PermissionSet" + } + }, + "permission": { + "description": "A list of inlined permissions", + "default": [], + "type": "array", + "items": { + "$ref": "#/definitions/Permission" + } + } + }, + "definitions": { + "DefaultPermission": { + "description": "The default permission set of the plugin.\n\nWorks similarly to a permission with the \"default\" identifier.", + "type": "object", + "required": [ + "permissions" + ], + "properties": { + "version": { + "description": "The version of the permission.", + "type": [ + "integer", + "null" + ], + "format": "uint64", + "minimum": 1.0 + }, + "description": { + "description": "Human-readable description of what the permission does. Tauri convention is to use `

` headings in markdown content for Tauri documentation generation purposes.", + "type": [ + "string", + "null" + ] + }, + "permissions": { + "description": "All permissions this set contains.", + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "PermissionSet": { + "description": "A set of direct permissions grouped together under a new name.", + "type": "object", + "required": [ + "description", + "identifier", + "permissions" + ], + "properties": { + "identifier": { + "description": "A unique identifier for the permission.", + "type": "string" + }, + "description": { + "description": "Human-readable description of what the permission does.", + "type": "string" + }, + "permissions": { + "description": "All permissions this set contains.", + "type": "array", + "items": { + "$ref": "#/definitions/PermissionKind" + } + } + } + }, + "Permission": { + "description": "Descriptions of explicit privileges of commands.\n\nIt can enable commands to be accessible in the frontend of the application.\n\nIf the scope is defined it can be used to fine grain control the access of individual or multiple commands.", + "type": "object", + "required": [ + "identifier" + ], + "properties": { + "version": { + "description": "The version of the permission.", + "type": [ + "integer", + "null" + ], + "format": "uint64", + "minimum": 1.0 + }, + "identifier": { + "description": "A unique identifier for the permission.", + "type": "string" + }, + "description": { + "description": "Human-readable description of what the permission does. Tauri internal convention is to use `

` headings in markdown content for Tauri documentation generation purposes.", + "type": [ + "string", + "null" + ] + }, + "commands": { + "description": "Allowed or denied commands when using this permission.", + "default": { + "allow": [], + "deny": [] + }, + "allOf": [ + { + "$ref": "#/definitions/Commands" + } + ] + }, + "scope": { + "description": "Allowed or denied scoped when using this permission.", + "allOf": [ + { + "$ref": "#/definitions/Scopes" + } + ] + }, + "platforms": { + "description": "Target platforms this permission applies. By default all platforms are affected by this permission.", + "type": [ + "array", + "null" + ], + "items": { + "$ref": "#/definitions/Target" + } + } + } + }, + "Commands": { + "description": "Allowed and denied commands inside a permission.\n\nIf two commands clash inside of `allow` and `deny`, it should be denied by default.", + "type": "object", + "properties": { + "allow": { + "description": "Allowed command.", + "default": [], + "type": "array", + "items": { + "type": "string" + } + }, + "deny": { + "description": "Denied command, which takes priority.", + "default": [], + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "Scopes": { + "description": "An argument for fine grained behavior control of Tauri commands.\n\nIt can be of any serde serializable type and is used to allow or prevent certain actions inside a Tauri command. The configured scope is passed to the command and will be enforced by the command implementation.\n\n## Example\n\n```json { \"allow\": [{ \"path\": \"$HOME/**\" }], \"deny\": [{ \"path\": \"$HOME/secret.txt\" }] } ```", + "type": "object", + "properties": { + "allow": { + "description": "Data that defines what is allowed by the scope.", + "type": [ + "array", + "null" + ], + "items": { + "$ref": "#/definitions/Value" + } + }, + "deny": { + "description": "Data that defines what is denied by the scope. This should be prioritized by validation logic.", + "type": [ + "array", + "null" + ], + "items": { + "$ref": "#/definitions/Value" + } + } + } + }, + "Value": { + "description": "All supported ACL values.", + "anyOf": [ + { + "description": "Represents a null JSON value.", + "type": "null" + }, + { + "description": "Represents a [`bool`].", + "type": "boolean" + }, + { + "description": "Represents a valid ACL [`Number`].", + "allOf": [ + { + "$ref": "#/definitions/Number" + } + ] + }, + { + "description": "Represents a [`String`].", + "type": "string" + }, + { + "description": "Represents a list of other [`Value`]s.", + "type": "array", + "items": { + "$ref": "#/definitions/Value" + } + }, + { + "description": "Represents a map of [`String`] keys to [`Value`]s.", + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/Value" + } + } + ] + }, + "Number": { + "description": "A valid ACL number.", + "anyOf": [ + { + "description": "Represents an [`i64`].", + "type": "integer", + "format": "int64" + }, + { + "description": "Represents a [`f64`].", + "type": "number", + "format": "double" + } + ] + }, + "Target": { + "description": "Platform target.", + "oneOf": [ + { + "description": "MacOS.", + "type": "string", + "enum": [ + "macOS" + ] + }, + { + "description": "Windows.", + "type": "string", + "enum": [ + "windows" + ] + }, + { + "description": "Linux.", + "type": "string", + "enum": [ + "linux" + ] + }, + { + "description": "Android.", + "type": "string", + "enum": [ + "android" + ] + }, + { + "description": "iOS.", + "type": "string", + "enum": [ + "iOS" + ] + } + ] + }, + "PermissionKind": { + "type": "string", + "oneOf": [ + { + "description": "Enables the ping command without any pre-configured scope.", + "type": "string", + "const": "allow-ping", + "markdownDescription": "Enables the ping command without any pre-configured scope." + }, + { + "description": "Denies the ping command without any pre-configured scope.", + "type": "string", + "const": "deny-ping", + "markdownDescription": "Denies the ping command without any pre-configured scope." + }, + { + "description": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-ping`", + "type": "string", + "const": "default", + "markdownDescription": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-ping`" + } + ] + } + } +} \ No newline at end of file diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..8b4768f --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,31 @@ +import { readFileSync } from 'node:fs' +import { dirname, join } from 'node:path' +import { cwd } from 'node:process' +import typescript from '@rollup/plugin-typescript' + +const pkg = JSON.parse(readFileSync(join(cwd(), 'package.json'), 'utf8')) + +export default { + input: 'guest-js/index.ts', + output: [ + { + file: pkg.exports.import, + format: 'esm' + }, + { + file: pkg.exports.require, + format: 'cjs' + } + ], + plugins: [ + typescript({ + declaration: true, + declarationDir: dirname(pkg.exports.import) + }) + ], + external: [ + /^@tauri-apps\/api/, + ...Object.keys(pkg.dependencies || {}), + ...Object.keys(pkg.peerDependencies || {}) + ] +} diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..7dea418 --- /dev/null +++ b/src/commands.rs @@ -0,0 +1,19 @@ +use tauri::{AppHandle, command, Runtime}; + +use crate::models::*; +use crate::Result; +use crate::DuckExt; + +#[command] +pub(crate) async fn duck( + app: AppHandle +) -> Result { + app.duck().duck() +} + +#[command] +pub(crate) async fn unduck( + app: AppHandle +) -> Result { + app.duck().unduck() +} diff --git a/src/desktop.rs b/src/desktop.rs new file mode 100644 index 0000000..79f8d03 --- /dev/null +++ b/src/desktop.rs @@ -0,0 +1,28 @@ +use serde::de::DeserializeOwned; +use tauri::{plugin::PluginApi, AppHandle, Runtime}; + +use crate::models::*; + +pub fn init( + app: &AppHandle, + _api: PluginApi, +) -> crate::Result> { + Ok(Duck(app.clone())) +} + +/// Access to the duck APIs. +pub struct Duck(AppHandle); + +impl Duck { + pub fn duck(&self) -> crate::Result { + Ok(DuckResponse { + success: true + }) + } + + pub fn unduck(&self) -> crate::Result { + Ok(DuckResponse { + success: true + }) + } +} diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..895220d --- /dev/null +++ b/src/error.rs @@ -0,0 +1,21 @@ +use serde::{ser::Serializer, Serialize}; + +pub type Result = std::result::Result; + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error(transparent)] + Io(#[from] std::io::Error), + #[cfg(mobile)] + #[error(transparent)] + PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError), +} + +impl Serialize for Error { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: Serializer, + { + serializer.serialize_str(self.to_string().as_ref()) + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..ba9bfa8 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,48 @@ +use tauri::{ + plugin::{Builder, TauriPlugin}, + Manager, Runtime, +}; + +pub use models::*; + +#[cfg(desktop)] +mod desktop; +#[cfg(mobile)] +mod mobile; + +mod commands; +mod error; +mod models; + +pub use error::{Error, Result}; + +#[cfg(desktop)] +use desktop::Duck; +#[cfg(mobile)] +use mobile::Duck; + +/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the duck APIs. +pub trait DuckExt { + fn duck(&self) -> &Duck; +} + +impl> crate::DuckExt for T { + fn duck(&self) -> &Duck { + self.state::>().inner() + } +} + +/// Initializes the plugin. +pub fn init() -> TauriPlugin { + Builder::new("duck") + .invoke_handler(tauri::generate_handler![commands::duck, commands::unduck]) + .setup(|app, api| { + #[cfg(mobile)] + let duck = mobile::init(app, api)?; + #[cfg(desktop)] + let duck = desktop::init(app, api)?; + app.manage(duck); + Ok(()) + }) + .build() +} diff --git a/src/mobile.rs b/src/mobile.rs new file mode 100644 index 0000000..af71443 --- /dev/null +++ b/src/mobile.rs @@ -0,0 +1,41 @@ +use serde::de::DeserializeOwned; +use tauri::{ + plugin::{PluginApi, PluginHandle}, + AppHandle, Runtime, +}; + +use crate::models::*; + +#[cfg(target_os = "ios")] +tauri::ios_plugin_binding!(init_plugin_duck); + +// initializes the Kotlin or Swift plugin classes +pub fn init( + _app: &AppHandle, + api: PluginApi, +) -> crate::Result> { + #[cfg(target_os = "android")] + let handle = api.register_android_plugin("de.cfp.duck", "DuckPlugin")?; + #[cfg(target_os = "ios")] + let handle = api.register_ios_plugin(init_plugin_duck)?; + Ok(Duck(handle)) +} + +/// Access to the duck APIs. +pub struct Duck(PluginHandle); + +impl Duck { + pub fn duck(&self) -> crate::Result { + self + .0 + .run_mobile_plugin("duck") + .map_err(Into::into) + } + + pub fn unduck(&self) -> crate::Result { + self + .0 + .run_mobile_plugin("unduck") + .map_err(Into::into) + } +} diff --git a/src/models.rs b/src/models.rs new file mode 100644 index 0000000..943f05d --- /dev/null +++ b/src/models.rs @@ -0,0 +1,7 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Default, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct DuckResponse { + pub success: bool, +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..0591122 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "es2021", + "module": "esnext", + "moduleResolution": "bundler", + "skipLibCheck": true, + "strict": true, + "noUnusedLocals": true, + "noImplicitAny": true, + "noEmit": true + }, + "include": ["guest-js/*.ts"], + "exclude": ["dist-js", "node_modules"] +}