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
+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) => {