Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tauri: implement runtime.readClipboardImage and TempFileFunctions #4533

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.lock

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

4 changes: 3 additions & 1 deletion packages/frontend/src/components/QrReader/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
reject(error)
}
})

image.addEventListener('error', (err)=>{

Check warning on line 53 in packages/frontend/src/components/QrReader/helper.ts

View workflow job for this annotation

GitHub Actions / build-tauri (macos-latest, aarch64, --target aarch64-apple-darwin)

Replace `(err)=>` with `err·=>·`

Check warning on line 53 in packages/frontend/src/components/QrReader/helper.ts

View workflow job for this annotation

GitHub Actions / Code Validation

Replace `(err)=>` with `err·=>·`

Check warning on line 53 in packages/frontend/src/components/QrReader/helper.ts

View workflow job for this annotation

GitHub Actions / build-tauri (macos-latest, x86_64, --target x86_64-apple-darwin)

Replace `(err)=>` with `err·=>·`

Check warning on line 53 in packages/frontend/src/components/QrReader/helper.ts

View workflow job for this annotation

GitHub Actions / build-tauri (ubuntu-22.04, x86_64)

Replace `(err)=>` with `err·=>·`

Check warning on line 53 in packages/frontend/src/components/QrReader/helper.ts

View workflow job for this annotation

GitHub Actions / build-tauri (windows-latest, x86_64)

Replace `(err)=>` with `err·=>·`
reject(err)
})
image.src = base64
})
}
Expand Down
13 changes: 2 additions & 11 deletions packages/target-tauri/runtime-tauri/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import {
writeText,
readText,
readImage,

Check failure on line 14 in packages/target-tauri/runtime-tauri/runtime.ts

View workflow job for this annotation

GitHub Actions / Code Validation

'readImage' is defined but never used. Allowed unused vars must match /^_/u
} from '@tauri-apps/plugin-clipboard-manager'

import {
Expand Down Expand Up @@ -333,17 +333,8 @@
readClipboardText(): Promise<string> {
return readText()
}
async readClipboardImage(): Promise<string | null> {
try {
const clipboardImage = await readImage()
const _blob = new Blob([await clipboardImage.rgba()], { type: 'image' })
//TODO blob to base64

throw new Error('Method not implemented.18')
} catch (error) {
this.log.warn('readClipboardImage', error)
return null
}
readClipboardImage(): Promise<string | null> {
return invoke("get_clipboard_image_as_data_uri")

Check warning on line 337 in packages/target-tauri/runtime-tauri/runtime.ts

View workflow job for this annotation

GitHub Actions / Code Validation

Replace `"get_clipboard_image_as_data_uri"` with `'get_clipboard_image_as_data_uri'`
}
writeClipboardText(text: string): Promise<void> {
return writeText(text)
Expand Down
3 changes: 3 additions & 0 deletions packages/target-tauri/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ tauri-plugin-store = "2"
http = "1.1.0"
mime = "0.3.17"
percent-encoding = "2"
thiserror = "2.0.11"
png = "0.*"
base64 = "0.*"

[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
tauri-plugin-single-instance = "2"
Expand Down
46 changes: 46 additions & 0 deletions packages/target-tauri/src-tauri/src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::io::Cursor;

use base64::encode;
use tauri::AppHandle;
use tauri_plugin_clipboard_manager::ClipboardExt;

// create the error type that represents all errors possible in our program
#[derive(Debug, thiserror::Error)]
pub(crate) enum Error {
#[error(transparent)]
Clipboard(#[from] tauri_plugin_clipboard_manager::Error),
#[error(transparent)]
EncodingError(#[from] png::EncodingError),
}
impl serde::Serialize for Error {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
serializer.serialize_str(format!("{self:?}").as_ref())
}
}

// currently only used for parsing qr code from clipboard image
#[tauri::command]
pub(crate) fn get_clipboard_image_as_data_uri(app: AppHandle) -> Result<String, Error> {
let img = app.clipboard().read_image()?;
let mut bytes: Cursor<Vec<u8>> = Cursor::new(Vec::new());

let mut encoder = png::Encoder::new(&mut bytes, img.width(), img.height());
encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header()?;
writer.write_image_data(img.rgba())?;
writer.finish()?;

let data = bytes.into_inner();

let base64_data = encode(&data);
let mime_type = "image/png";
let data_uri = format!("data:{};base64,{}", mime_type, base64_data);

Ok(data_uri)
}

// Later: figgure out a way to get clipboard image in orginal format and bit depth? is that needed?
2 changes: 2 additions & 0 deletions packages/target-tauri/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

mod app_path;
mod blobs;
mod clipboard;
mod file_dialogs;
mod help_window;
mod locales;
Expand Down Expand Up @@ -93,6 +94,7 @@
ui_frontend_ready,
get_current_logfile,
app_path::get_app_path,
clipboard::get_clipboard_image_as_data_uri,
file_dialogs::download_file,
file_dialogs::show_open_file_dialog,
locales::get_locale_data,
Expand Down Expand Up @@ -171,7 +173,7 @@
#[cfg(debug_assertions)]
app.get_webview_window("main").unwrap().open_devtools();

let webview = app.get_webview_window("main").unwrap();

Check warning on line 176 in packages/target-tauri/src-tauri/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `webview`

warning: unused variable: `webview` --> packages/target-tauri/src-tauri/src/lib.rs:176:17 | 176 | let webview = app.get_webview_window("main").unwrap(); | ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_webview` | = note: `#[warn(unused_variables)]` on by default

Check warning on line 176 in packages/target-tauri/src-tauri/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `webview`

warning: unused variable: `webview` --> packages/target-tauri/src-tauri/src/lib.rs:176:17 | 176 | let webview = app.get_webview_window("main").unwrap(); | ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_webview` | = note: `#[warn(unused_variables)]` on by default

#[cfg(target_os = "macos")]
{
Expand Down
1 change: 0 additions & 1 deletion packages/target-tauri/src-tauri/tauri.conf.json5
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
// or when running `pnpm tauri permission list`
"core:event:allow-emit",
"core:event:allow-listen",
"clipboard-manager:allow-read-image",
"clipboard-manager:allow-read-text",
"clipboard-manager:allow-write-image",
"clipboard-manager:allow-write-text",
Expand Down
Loading