fix: use webview's resources table (#1191)

* fix: use webview's resources table

* fix clipboard into_img usage

* fix mobile
This commit is contained in:
Amr Bashir
2024-04-18 03:19:24 +02:00
committed by GitHub
parent 8638740223
commit e3d41f4011
13 changed files with 152 additions and 102 deletions
+6 -5
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use tauri::{command, AppHandle, Manager, ResourceId, Runtime, State};
use tauri::{command, AppHandle, Manager, ResourceId, Runtime, State, Webview};
use crate::{ClipKind, Clipboard, ClipboardContents, Result};
@@ -17,11 +17,12 @@ pub(crate) async fn write_text<R: Runtime>(
#[command]
pub(crate) async fn write_image<R: Runtime>(
_app: AppHandle<R>,
webview: Webview<R>,
clipboard: State<'_, Clipboard<R>>,
data: ClipKind,
) -> Result<()> {
clipboard.write_image(data)
let resources_table = webview.resources_table();
clipboard.write_image_inner(data, &resources_table)
}
#[command]
@@ -34,11 +35,11 @@ pub(crate) async fn read_text<R: Runtime>(
#[command]
pub(crate) async fn read_image<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
clipboard: State<'_, Clipboard<R>>,
) -> Result<ResourceId> {
let image = clipboard.read_image()?.to_owned();
let mut resources_table = app.resources_table();
let mut resources_table = webview.resources_table();
let rid = resources_table.add(image);
Ok(rid)
}
+12 -3
View File
@@ -5,7 +5,7 @@
use arboard::ImageData;
use image::ImageEncoder;
use serde::de::DeserializeOwned;
use tauri::{image::Image, plugin::PluginApi, AppHandle, Runtime};
use tauri::{image::Image, plugin::PluginApi, AppHandle, Manager, ResourceTable, Runtime};
use crate::models::*;
@@ -39,11 +39,15 @@ impl<R: Runtime> Clipboard<R> {
}
}
pub fn write_image(&self, kind: ClipKind) -> crate::Result<()> {
pub(crate) fn write_image_inner(
&self,
kind: ClipKind,
resources_table: &ResourceTable,
) -> crate::Result<()> {
match kind {
ClipKind::Image { image, .. } => match &self.clipboard {
Ok(clipboard) => {
let image = image.into_img(&self.app)?;
let image = image.into_img(resources_table)?;
clipboard
.lock()
.unwrap()
@@ -60,6 +64,11 @@ impl<R: Runtime> Clipboard<R> {
}
}
pub fn write_image(&self, kind: ClipKind) -> crate::Result<()> {
let resources_table = self.app.resources_table();
self.write_image_inner(kind, &resources_table)
}
pub fn read_text(&self) -> crate::Result<ClipboardContents> {
match &self.clipboard {
Ok(clipboard) => {
+10
View File
@@ -37,6 +37,16 @@ impl<R: Runtime> Clipboard<R> {
self.0.run_mobile_plugin("write", kind).map_err(Into::into)
}
pub(crate) fn write_image_inner(
&self,
kind: ClipKind,
resources_table: &tauri::ResourceTable,
) -> crate::Result<()> {
Err(crate::Error::Clipboard(
"Unsupported on this platform".to_string(),
))
}
pub fn write_image(&self, kind: ClipKind) -> crate::Result<()> {
Err(crate::Error::Clipboard(
"Unsupported on this platform".to_string(),