From 3e8df1ba6f8062f7a324c06ab384116015eec18a Mon Sep 17 00:00:00 2001 From: John Costa Date: Mon, 21 Apr 2025 11:44:34 +0100 Subject: [PATCH] feat(images): share target working and receiving images! --- frontend/package.json | 2 + frontend/src-tauri/Cargo.lock | 16 +++++++ frontend/src-tauri/Cargo.toml | 2 + frontend/src-tauri/capabilities/default.json | 37 +++++++++------- .../android/app/src/main/AndroidManifest.xml | 4 ++ frontend/src-tauri/src/lib.rs | 2 + frontend/src/App.tsx | 42 ++++++++++++++++++- 7 files changed, 89 insertions(+), 16 deletions(-) diff --git a/frontend/package.json b/frontend/package.json index d83836f..9002825 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -20,6 +20,7 @@ "@tabler/icons-solidjs": "^3.30.0", "@tauri-apps/api": "^2", "@tauri-apps/plugin-dialog": "~2", + "@tauri-apps/plugin-fs": "~2", "@tauri-apps/plugin-http": "~2", "@tauri-apps/plugin-opener": "^2", "clsx": "^2.1.1", @@ -30,6 +31,7 @@ "solid-motionone": "^1.0.3", "solidjs-markdown": "^0.2.0", "tailwind-scrollbar-hide": "^2.0.0", + "tauri-plugin-sharetarget-api": "^0.1.6", "valibot": "^1.0.0-rc.2" }, "devDependencies": { diff --git a/frontend/src-tauri/Cargo.lock b/frontend/src-tauri/Cargo.lock index 3c4c835..6246d82 100644 --- a/frontend/src-tauri/Cargo.lock +++ b/frontend/src-tauri/Cargo.lock @@ -14,9 +14,11 @@ dependencies = [ "serde_json", "tauri", "tauri-build", + "tauri-plugin-fs", "tauri-plugin-global-shortcut", "tauri-plugin-http", "tauri-plugin-log", + "tauri-plugin-sharetarget", "tauri-plugin-store", "tokio", ] @@ -4060,6 +4062,20 @@ dependencies = [ "time", ] +[[package]] +name = "tauri-plugin-sharetarget" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15a6e4638b6a5492a46847fc9e994df8cfd2dbc1bacc11f15c207d6a2163c341" +dependencies = [ + "serde", + "serde_json", + "tauri", + "tauri-build", + "tauri-plugin", + "thiserror 1.0.69", +] + [[package]] name = "tauri-plugin-store" version = "2.2.0" diff --git a/frontend/src-tauri/Cargo.toml b/frontend/src-tauri/Cargo.toml index bc143a2..97e96a0 100644 --- a/frontend/src-tauri/Cargo.toml +++ b/frontend/src-tauri/Cargo.toml @@ -28,6 +28,8 @@ tauri-plugin-store = "2.0.0-beta.12" tauri-plugin-http = "2.0.0-beta.12" chrono = "0.4" tauri-plugin-log = "2" +tauri-plugin-sharetarget = "0.1.6" +tauri-plugin-fs = "2" [target."cfg(target_os = \"macos\")".dependencies] cocoa = "0.26" diff --git a/frontend/src-tauri/capabilities/default.json b/frontend/src-tauri/capabilities/default.json index a7c442f..b28ce34 100644 --- a/frontend/src-tauri/capabilities/default.json +++ b/frontend/src-tauri/capabilities/default.json @@ -10,19 +10,26 @@ "core:window:allow-show", "core:window:allow-set-focus", { - "identifier": "http:default", - "allow": [ - { - "url": "https://haystack.johncosta.tech" - }, - { - "url": "http://localhost:3040" - }, - { - "url": "http://192.168.1.199:3040" - } - ] + "path": "$APPDATA/databases/*" + } + ] + }, + { + "identifier": "http:default", + "allow": [ + { + "url": "https://haystack.johncosta.tech" }, - "log:default" - ] -} + { + "url": "http://localhost:3040" + }, + { + "url": "http://192.168.1.199:3040" + } + ] + }, + "log:default", + "fs:default", + "fs:default" + ] +} \ No newline at end of file diff --git a/frontend/src-tauri/gen/android/app/src/main/AndroidManifest.xml b/frontend/src-tauri/gen/android/app/src/main/AndroidManifest.xml index a8d9fb6..faad303 100644 --- a/frontend/src-tauri/gen/android/app/src/main/AndroidManifest.xml +++ b/frontend/src-tauri/gen/android/app/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ + @@ -18,7 +19,10 @@ android:exported="true"> + + + diff --git a/frontend/src-tauri/src/lib.rs b/frontend/src-tauri/src/lib.rs index b58c03f..6c81928 100644 --- a/frontend/src-tauri/src/lib.rs +++ b/frontend/src-tauri/src/lib.rs @@ -13,9 +13,11 @@ pub fn run() { let watcher_state = new_shared_watcher_state(); tauri::Builder::default() + .plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_log::Builder::new().build()) .plugin(tauri_plugin_store::Builder::new().build()) .plugin(tauri_plugin_http::init()) + .plugin(tauri_plugin_sharetarget::init()) // .plugin(tauri_plugin_dialog::init()) // .plugin(tauri_plugin_opener::init()) .manage(watcher_state) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index c4fefac..0f796de 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,13 +1,22 @@ import { Route, Router } from "@solidjs/router"; import { listen } from "@tauri-apps/api/event"; -import { createEffect, onCleanup } from "solid-js"; +import { createEffect, createSignal, For, onCleanup, Show } from "solid-js"; import { Login } from "./Login"; import { ProtectedRoute } from "./ProtectedRoute"; import { Search } from "./Search"; import { Settings } from "./Settings"; import { ImageViewer } from "./components/ImageViewer"; +import type { PluginListener } from "@tauri-apps/api/core"; +import { + listenForShareEvents, + type ShareEvent, +} from "tauri-plugin-sharetarget-api"; +import { readFile } from "@tauri-apps/plugin-fs"; export const App = () => { + const [logs, setLogs] = createSignal([]); + const [file, setFile] = createSignal(); + createEffect(() => { // TODO: Don't use window.location.href const unlisten = listen("focus-search", () => { @@ -19,9 +28,40 @@ export const App = () => { }); }); + createEffect(() => { + let listener: PluginListener; + const setupListener = async () => { + listener = await listenForShareEvents( + async (intent: ShareEvent) => { + const contents = await readFile(intent.stream).catch( + (error: Error) => { + console.warn("fetching shared content failed:"); + throw error; + }, + ); + setFile( + new File([contents], intent.name ?? "no-name", { + type: intent.content_type, + }), + ); + setLogs((l) => [...l, intent.uri]); + }, + ); + }; + setupListener(); + return () => { + listener?.unregister(); + }; + }); + return ( <> +

Hello

+ + {(f) => my-image} + + {(log) =>

{log}

}