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(),
@@ -8,7 +8,6 @@ pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
+55 -55
View File
@@ -9,7 +9,7 @@ use tauri::{
ipc::{CommandScope, GlobalScope},
path::{BaseDirectory, SafePathBuf},
utils::config::FsScope,
AppHandle, Manager, Resource, ResourceId, Runtime,
Manager, Resource, ResourceId, Runtime, Webview,
};
use std::{
@@ -72,14 +72,14 @@ pub struct BaseOptions {
#[tauri::command]
pub fn create<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
path: SafePathBuf,
options: Option<BaseOptions>,
) -> CommandResult<ResourceId> {
let resolved_path = resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
path,
@@ -91,7 +91,7 @@ pub fn create<R: Runtime>(
resolved_path.display()
)
})?;
let rid = app.resources_table().add(StdFileResource::new(file));
let rid = webview.resources_table().add(StdFileResource::new(file));
Ok(rid)
}
@@ -122,14 +122,14 @@ fn default_true() -> bool {
#[tauri::command]
pub fn open<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
path: SafePathBuf,
options: Option<OpenOptions>,
) -> CommandResult<ResourceId> {
let resolved_path = resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
path,
@@ -164,14 +164,14 @@ pub fn open<R: Runtime>(
)
})?;
let rid = app.resources_table().add(StdFileResource::new(file));
let rid = webview.resources_table().add(StdFileResource::new(file));
Ok(rid)
}
#[tauri::command]
pub fn close<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> CommandResult<()> {
app.resources_table().close(rid).map_err(Into::into)
pub fn close<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> CommandResult<()> {
webview.resources_table().close(rid).map_err(Into::into)
}
#[derive(Debug, Clone, Deserialize)]
@@ -183,7 +183,7 @@ pub struct CopyFileOptions {
#[tauri::command]
pub fn copy_file<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
from_path: SafePathBuf,
@@ -191,14 +191,14 @@ pub fn copy_file<R: Runtime>(
options: Option<CopyFileOptions>,
) -> CommandResult<()> {
let resolved_from_path = resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
from_path,
options.as_ref().and_then(|o| o.from_path_base_dir),
)?;
let resolved_to_path = resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
to_path,
@@ -225,14 +225,14 @@ pub struct MkdirOptions {
#[tauri::command]
pub fn mkdir<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
path: SafePathBuf,
options: Option<MkdirOptions>,
) -> CommandResult<()> {
let resolved_path = resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
path,
@@ -292,14 +292,14 @@ fn read_dir_inner<P: AsRef<Path>>(path: P) -> crate::Result<Vec<DirEntry>> {
#[tauri::command]
pub fn read_dir<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
path: SafePathBuf,
options: Option<BaseOptions>,
) -> CommandResult<Vec<DirEntry>> {
let resolved_path = resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
path,
@@ -318,12 +318,12 @@ pub fn read_dir<R: Runtime>(
#[tauri::command]
pub fn read<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
rid: ResourceId,
len: u32,
) -> CommandResult<(Vec<u8>, usize)> {
let mut data = vec![0; len as usize];
let file = app.resources_table().get::<StdFileResource>(rid)?;
let file = webview.resources_table().get::<StdFileResource>(rid)?;
let nread = StdFileResource::with_lock(&file, |mut file| file.read(&mut data))
.map_err(|e| format!("faied to read bytes from file with error: {e}"))?;
Ok((data, nread))
@@ -331,14 +331,14 @@ pub fn read<R: Runtime>(
#[tauri::command]
pub fn read_file<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
path: SafePathBuf,
options: Option<BaseOptions>,
) -> CommandResult<Vec<u8>> {
let resolved_path = resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
path,
@@ -356,14 +356,14 @@ pub fn read_file<R: Runtime>(
#[tauri::command]
pub fn read_text_file<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
path: SafePathBuf,
options: Option<BaseOptions>,
) -> CommandResult<String> {
let resolved_path = resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
path,
@@ -381,7 +381,7 @@ pub fn read_text_file<R: Runtime>(
#[tauri::command]
pub fn read_text_file_lines<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
path: SafePathBuf,
@@ -390,7 +390,7 @@ pub fn read_text_file_lines<R: Runtime>(
use std::io::BufRead;
let resolved_path = resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
path,
@@ -405,17 +405,17 @@ pub fn read_text_file_lines<R: Runtime>(
})?;
let lines = BufReader::new(file).lines();
let rid = app.resources_table().add(StdLinesResource::new(lines));
let rid = webview.resources_table().add(StdLinesResource::new(lines));
Ok(rid)
}
#[tauri::command]
pub fn read_text_file_lines_next<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
rid: ResourceId,
) -> CommandResult<(Option<String>, bool)> {
let mut resource_table = app.resources_table();
let mut resource_table = webview.resources_table();
let lines = resource_table.get::<StdLinesResource>(rid)?;
let ret = StdLinesResource::with_lock(&lines, |lines| {
@@ -437,14 +437,14 @@ pub struct RemoveOptions {
#[tauri::command]
pub fn remove<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
path: SafePathBuf,
options: Option<RemoveOptions>,
) -> CommandResult<()> {
let resolved_path = resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
path,
@@ -505,7 +505,7 @@ pub struct RenameOptions {
#[tauri::command]
pub fn rename<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
old_path: SafePathBuf,
@@ -513,14 +513,14 @@ pub fn rename<R: Runtime>(
options: Option<RenameOptions>,
) -> CommandResult<()> {
let resolved_old_path = resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
old_path,
options.as_ref().and_then(|o| o.old_path_base_dir),
)?;
let resolved_new_path = resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
new_path,
@@ -547,13 +547,13 @@ pub enum SeekMode {
#[tauri::command]
pub fn seek<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
rid: ResourceId,
offset: i64,
whence: SeekMode,
) -> CommandResult<u64> {
use std::io::{Seek, SeekFrom};
let file = app.resources_table().get::<StdFileResource>(rid)?;
let file = webview.resources_table().get::<StdFileResource>(rid)?;
StdFileResource::with_lock(&file, |mut file| {
file.seek(match whence {
SeekMode::Start => SeekFrom::Start(offset as u64),
@@ -567,14 +567,14 @@ pub fn seek<R: Runtime>(
#[tauri::command]
pub fn stat<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
path: SafePathBuf,
options: Option<BaseOptions>,
) -> CommandResult<FileInfo> {
let resolved_path = resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
path,
@@ -591,14 +591,14 @@ pub fn stat<R: Runtime>(
#[tauri::command]
pub fn lstat<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
path: SafePathBuf,
options: Option<BaseOptions>,
) -> CommandResult<FileInfo> {
let resolved_path = resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
path,
@@ -614,8 +614,8 @@ pub fn lstat<R: Runtime>(
}
#[tauri::command]
pub fn fstat<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> CommandResult<FileInfo> {
let file = app.resources_table().get::<StdFileResource>(rid)?;
pub fn fstat<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> CommandResult<FileInfo> {
let file = webview.resources_table().get::<StdFileResource>(rid)?;
let metadata = StdFileResource::with_lock(&file, |file| file.metadata())
.map_err(|e| format!("failed to get metadata of file with error: {e}"))?;
Ok(get_stat(metadata))
@@ -623,7 +623,7 @@ pub fn fstat<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> CommandResult<Fi
#[tauri::command]
pub fn truncate<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
path: SafePathBuf,
@@ -631,7 +631,7 @@ pub fn truncate<R: Runtime>(
options: Option<BaseOptions>,
) -> CommandResult<()> {
let resolved_path = resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
path,
@@ -658,11 +658,11 @@ pub fn truncate<R: Runtime>(
#[tauri::command]
pub fn ftruncate<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
rid: ResourceId,
len: Option<u64>,
) -> CommandResult<()> {
let file = app.resources_table().get::<StdFileResource>(rid)?;
let file = webview.resources_table().get::<StdFileResource>(rid)?;
StdFileResource::with_lock(&file, |file| file.set_len(len.unwrap_or(0)))
.map_err(|e| format!("failed to truncate file with error: {e}"))
.map_err(Into::into)
@@ -670,11 +670,11 @@ pub fn ftruncate<R: Runtime>(
#[tauri::command]
pub fn write<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
rid: ResourceId,
data: Vec<u8>,
) -> CommandResult<usize> {
let file = app.resources_table().get::<StdFileResource>(rid)?;
let file = webview.resources_table().get::<StdFileResource>(rid)?;
StdFileResource::with_lock(&file, |mut file| file.write(&data))
.map_err(|e| format!("failed to write bytes to file with error: {e}"))
.map_err(Into::into)
@@ -700,7 +700,7 @@ fn default_create_value() -> bool {
}
fn write_file_inner<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: &GlobalScope<Entry>,
command_scope: &CommandScope<Entry>,
path: SafePathBuf,
@@ -708,7 +708,7 @@ fn write_file_inner<R: Runtime>(
options: Option<WriteFileOptions>,
) -> CommandResult<()> {
let resolved_path = resolve_path(
&app,
&webview,
global_scope,
command_scope,
path,
@@ -753,19 +753,19 @@ fn write_file_inner<R: Runtime>(
#[tauri::command]
pub fn write_file<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
path: SafePathBuf,
data: Vec<u8>,
options: Option<WriteFileOptions>,
) -> CommandResult<()> {
write_file_inner(app, &global_scope, &command_scope, path, &data, options)
write_file_inner(webview, &global_scope, &command_scope, path, &data, options)
}
#[tauri::command]
pub fn write_text_file<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
path: SafePathBuf,
@@ -773,7 +773,7 @@ pub fn write_text_file<R: Runtime>(
options: Option<WriteFileOptions>,
) -> CommandResult<()> {
write_file_inner(
app,
webview,
&global_scope,
&command_scope,
path,
@@ -784,14 +784,14 @@ pub fn write_text_file<R: Runtime>(
#[tauri::command]
pub fn exists<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
path: SafePathBuf,
options: Option<BaseOptions>,
) -> CommandResult<bool> {
let resolved_path = resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
path,
@@ -801,7 +801,7 @@ pub fn exists<R: Runtime>(
}
pub fn resolve_path<R: Runtime>(
app: &AppHandle<R>,
app: &Webview<R>,
global_scope: &GlobalScope<Entry>,
command_scope: &CommandScope<Entry>,
path: SafePathBuf,
+6 -6
View File
@@ -8,7 +8,7 @@ use serde::Deserialize;
use tauri::{
ipc::{Channel, CommandScope, GlobalScope},
path::{BaseDirectory, SafePathBuf},
AppHandle, Manager, Resource, ResourceId, Runtime,
Manager, Resource, ResourceId, Runtime, Webview,
};
use std::{
@@ -82,7 +82,7 @@ pub struct WatchOptions {
#[tauri::command]
pub async fn watch<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
paths: Vec<SafePathBuf>,
options: WatchOptions,
on_event: Channel,
@@ -92,7 +92,7 @@ pub async fn watch<R: Runtime>(
let mut resolved_paths = Vec::with_capacity(paths.capacity());
for path in paths {
resolved_paths.push(resolve_path(
&app,
&webview,
&global_scope,
&command_scope,
path,
@@ -124,7 +124,7 @@ pub async fn watch<R: Runtime>(
WatcherKind::Watcher(watcher)
};
let rid = app
let rid = webview
.resources_table()
.add(WatcherResource::new(kind, resolved_paths));
@@ -132,8 +132,8 @@ pub async fn watch<R: Runtime>(
}
#[tauri::command]
pub async fn unwatch<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> CommandResult<()> {
let watcher = app.resources_table().take::<WatcherResource>(rid)?;
pub async fn unwatch<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> CommandResult<()> {
let watcher = webview.resources_table().take::<WatcherResource>(rid)?;
WatcherResource::with_lock(&watcher, |watcher| {
match &mut watcher.kind {
WatcherKind::Debouncer(ref mut debouncer) => {
+11 -11
View File
@@ -11,7 +11,7 @@ use tauri::{
async_runtime::Mutex,
command,
ipc::{CommandScope, GlobalScope},
AppHandle, Manager, ResourceId, Runtime,
Manager, ResourceId, Runtime, Webview,
};
use crate::{
@@ -137,7 +137,7 @@ fn attach_proxy(
#[command]
pub async fn fetch<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
client_config: ClientConfig,
command_scope: CommandScope<Entry>,
global_scope: GlobalScope<Entry>,
@@ -249,7 +249,7 @@ pub async fn fetch<R: Runtime>(
}
let fut = async move { Ok(request.send().await.map_err(Into::into)) };
let mut resources_table = app.resources_table();
let mut resources_table = webview.resources_table();
let rid = resources_table.add(FetchRequest::new(Box::pin(fut)));
Ok(rid)
@@ -270,7 +270,7 @@ pub async fn fetch<R: Runtime>(
.body(reqwest::Body::from(body))?;
let fut = async move { Ok(Ok(reqwest::Response::from(response))) };
let mut resources_table = app.resources_table();
let mut resources_table = webview.resources_table();
let rid = resources_table.add(FetchRequest::new(Box::pin(fut)));
Ok(rid)
}
@@ -279,9 +279,9 @@ pub async fn fetch<R: Runtime>(
}
#[command]
pub async fn fetch_cancel<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> crate::Result<()> {
pub async fn fetch_cancel<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> crate::Result<()> {
let req = {
let resources_table = app.resources_table();
let resources_table = webview.resources_table();
resources_table.get::<FetchRequest>(rid)?
};
let mut req = req.0.lock().await;
@@ -292,11 +292,11 @@ pub async fn fetch_cancel<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> cra
#[tauri::command]
pub async fn fetch_send<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
rid: ResourceId,
) -> crate::Result<FetchResponse> {
let req = {
let mut resources_table = app.resources_table();
let mut resources_table = webview.resources_table();
resources_table.take::<FetchRequest>(rid)?
};
@@ -315,7 +315,7 @@ pub async fn fetch_send<R: Runtime>(
));
}
let mut resources_table = app.resources_table();
let mut resources_table = webview.resources_table();
let rid = resources_table.add(ReqwestResponse(res));
Ok(FetchResponse {
@@ -329,11 +329,11 @@ pub async fn fetch_send<R: Runtime>(
#[tauri::command]
pub(crate) async fn fetch_read_body<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
rid: ResourceId,
) -> crate::Result<tauri::ipc::Response> {
let res = {
let mut resources_table = app.resources_table();
let mut resources_table = webview.resources_table();
resources_table.take::<ReqwestResponse>(rid)?
};
let res = Arc::into_inner(res).unwrap().0;
+6 -6
View File
@@ -5,7 +5,7 @@
use crate::{Result, Update, UpdaterExt};
use serde::Serialize;
use tauri::{ipc::Channel, AppHandle, Manager, ResourceId, Runtime};
use tauri::{ipc::Channel, Manager, ResourceId, Runtime, Webview};
use std::time::Duration;
use url::Url;
@@ -37,13 +37,13 @@ pub(crate) struct Metadata {
#[tauri::command]
pub(crate) async fn check<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
headers: Option<Vec<(String, String)>>,
timeout: Option<u64>,
proxy: Option<String>,
target: Option<String>,
) -> Result<Metadata> {
let mut builder = app.updater_builder();
let mut builder = webview.updater_builder();
if let Some(headers) = headers {
for (k, v) in headers {
builder = builder.header(k, v)?;
@@ -69,7 +69,7 @@ pub(crate) async fn check<R: Runtime>(
metadata.version = update.version.clone();
metadata.date = update.date.map(|d| d.to_string());
metadata.body = update.body.clone();
metadata.rid = Some(app.resources_table().add(update));
metadata.rid = Some(webview.resources_table().add(update));
}
Ok(metadata)
@@ -77,11 +77,11 @@ pub(crate) async fn check<R: Runtime>(
#[tauri::command]
pub(crate) async fn download_and_install<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
rid: ResourceId,
on_event: Channel,
) -> Result<()> {
let update = app.resources_table().get::<Update>(rid)?;
let update = webview.resources_table().get::<Update>(rid)?;
let mut first_chunk = true;
@@ -2,7 +2,7 @@
"identifier": "com.tauri.dev",
"build": {
"devUrl": "http://localhost:5173/",
"frontendDist": "../build",
"frontendDist": "../dist",
"beforeDevCommand": "pnpm dev",
"beforeBuildCommand": "pnpm build"
},