mirror of
https://github.com/tauri-apps/tauri.git
synced 2026-04-01 10:01:07 +02:00
refactor(core): reduce usage on arc and mutex (#1361)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
committed by
GitHub
parent
3047a18975
commit
55c2db460e
@@ -30,7 +30,7 @@ thiserror = "1.0.24"
|
||||
once_cell = "1.7.2"
|
||||
tauri-api = { version = "0.7.5", path = "../tauri-api" }
|
||||
tauri-macros = { version = "0.1", path = "../tauri-macros" }
|
||||
wry = { git = "https://github.com/tauri-apps/wry", rev = "8dd58eec77d4c89491b1af427d06c4ee6cfa8e58" }
|
||||
wry = "0.6"
|
||||
rand = "0.8"
|
||||
|
||||
[build-dependencies]
|
||||
|
||||
@@ -163,19 +163,12 @@ type WebviewContext<A> = (
|
||||
Option<FileDropHandler>,
|
||||
);
|
||||
|
||||
#[async_trait::async_trait]
|
||||
trait WebviewInitializer<A: ApplicationExt> {
|
||||
fn init_webview(&self, webview: Webview<A>) -> crate::Result<WebviewContext<A>>;
|
||||
|
||||
async fn on_webview_created(
|
||||
&self,
|
||||
webview_label: String,
|
||||
dispatcher: A::Dispatcher,
|
||||
manager: WebviewManager<A>,
|
||||
);
|
||||
fn on_webview_created(&self, webview_label: String, dispatcher: A::Dispatcher);
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<A: ApplicationExt + 'static> WebviewInitializer<A> for Arc<App<A>> {
|
||||
fn init_webview(&self, webview: Webview<A>) -> crate::Result<WebviewContext<A>> {
|
||||
let webview_manager = WebviewManager::new(
|
||||
@@ -212,18 +205,11 @@ impl<A: ApplicationExt + 'static> WebviewInitializer<A> for Arc<App<A>> {
|
||||
))
|
||||
}
|
||||
|
||||
async fn on_webview_created(
|
||||
&self,
|
||||
webview_label: String,
|
||||
dispatcher: A::Dispatcher,
|
||||
manager: WebviewManager<A>,
|
||||
) {
|
||||
fn on_webview_created(&self, webview_label: String, dispatcher: A::Dispatcher) {
|
||||
self.dispatchers.lock().unwrap().insert(
|
||||
webview_label.to_string(),
|
||||
WebviewDispatcher::new(dispatcher, webview_label),
|
||||
);
|
||||
|
||||
crate::plugin::created(A::plugin_store(), &manager).await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -382,11 +368,8 @@ fn run<A: ApplicationExt + 'static>(mut application: App<A>) -> crate::Result<()
|
||||
custom_protocol,
|
||||
file_drop_handler,
|
||||
)?;
|
||||
crate::async_runtime::block_on(application.on_webview_created(
|
||||
webview_label,
|
||||
dispatcher,
|
||||
webview_manager,
|
||||
));
|
||||
application.on_webview_created(webview_label, dispatcher);
|
||||
crate::async_runtime::block_on(crate::plugin::created(A::plugin_store(), &webview_manager));
|
||||
}
|
||||
|
||||
if let Some(main_webview_manager) = main_webview_manager {
|
||||
|
||||
@@ -196,7 +196,7 @@ pub enum FileDropEvent {
|
||||
pub type FileDropHandler = Box<dyn Fn(FileDropEvent) -> bool + Send>;
|
||||
|
||||
/// Webview dispatcher. A thread-safe handle to the webview API.
|
||||
pub trait ApplicationDispatcherExt: Clone + Send + Sync + Sized {
|
||||
pub trait ApplicationDispatcherExt: Clone + Send + Sized {
|
||||
/// The webview builder type.
|
||||
type WebviewBuilder: WebviewBuilderExt
|
||||
+ WebviewBuilderExtPrivate
|
||||
|
||||
@@ -14,7 +14,6 @@ use tauri_api::path::{resolve_path, BaseDirectory};
|
||||
use std::{
|
||||
convert::{TryFrom, TryInto},
|
||||
path::PathBuf,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
impl TryFrom<Icon> for wry::Icon {
|
||||
@@ -231,10 +230,7 @@ impl From<wry::FileDropEvent> for FileDropEvent {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct WryDispatcher(
|
||||
Arc<Mutex<wry::WindowProxy>>,
|
||||
Arc<Mutex<wry::ApplicationProxy>>,
|
||||
);
|
||||
pub struct WryDispatcher(wry::WindowProxy, wry::ApplicationProxy);
|
||||
|
||||
impl ApplicationDispatcherExt for WryDispatcher {
|
||||
type WebviewBuilder = wry::Attributes;
|
||||
@@ -252,7 +248,7 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
move |dispatcher: wry::WindowProxy, request: wry::RpcRequest| {
|
||||
if let Some(handler) = &rpc_handler {
|
||||
handler(
|
||||
WryDispatcher(Arc::new(Mutex::new(dispatcher)), app_dispatcher.clone()),
|
||||
WryDispatcher(dispatcher, app_dispatcher.clone()),
|
||||
request.into(),
|
||||
);
|
||||
}
|
||||
@@ -270,8 +266,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
|
||||
let window_dispatcher = self
|
||||
.1
|
||||
.lock()
|
||||
.unwrap()
|
||||
.add_window_with_configs(
|
||||
attributes,
|
||||
Some(wry_rpc_handler),
|
||||
@@ -282,17 +276,12 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
Some(file_drop_handler),
|
||||
)
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)?;
|
||||
Ok(Self(
|
||||
Arc::new(Mutex::new(window_dispatcher)),
|
||||
self.1.clone(),
|
||||
))
|
||||
Ok(Self(window_dispatcher, self.1.clone()))
|
||||
}
|
||||
|
||||
fn set_resizable(&self, resizable: bool) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_resizable(resizable)
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -300,8 +289,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn set_title<S: Into<String>>(&self, title: S) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_title(title)
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -309,8 +296,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn maximize(&self) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.maximize()
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -318,8 +303,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn unmaximize(&self) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.unmaximize()
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -327,8 +310,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn minimize(&self) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.minimize()
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -336,35 +317,21 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn unminimize(&self) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.unminimize()
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
|
||||
fn show(&self) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.show()
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
self.0.show().map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
|
||||
fn hide(&self) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.hide()
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
self.0.hide().map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
|
||||
fn close(&self) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.close()
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -372,8 +339,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn set_decorations(&self, decorations: bool) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_decorations(decorations)
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -381,8 +346,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn set_always_on_top(&self, always_on_top: bool) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_always_on_top(always_on_top)
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -390,8 +353,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn set_width(&self, width: f64) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_width(width)
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -399,8 +360,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn set_height(&self, height: f64) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_height(height)
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -408,8 +367,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn resize(&self, width: f64, height: f64) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.resize(width, height)
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -417,8 +374,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn set_min_size(&self, min_width: f64, min_height: f64) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_min_size(min_width, min_height)
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -426,8 +381,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn set_max_size(&self, max_width: f64, max_height: f64) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_max_size(max_width, max_height)
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -435,8 +388,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn set_x(&self, x: f64) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_x(x)
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -444,8 +395,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn set_y(&self, y: f64) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_y(y)
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -453,8 +402,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn set_position(&self, x: f64, y: f64) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_position(x, y)
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -462,8 +409,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn set_fullscreen(&self, fullscreen: bool) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_fullscreen(fullscreen)
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -471,8 +416,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn set_icon(&self, icon: Icon) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_icon(icon.try_into()?)
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -480,8 +423,6 @@ impl ApplicationDispatcherExt for WryDispatcher {
|
||||
fn eval_script<S: Into<String>>(&self, script: S) -> crate::Result<()> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.evaluate_script(script)
|
||||
.map_err(|_| crate::Error::FailedToSendMessage)
|
||||
}
|
||||
@@ -513,14 +454,14 @@ impl ApplicationExt for WryApplication {
|
||||
custom_protocol: Option<CustomProtocol>,
|
||||
file_drop_handler: Option<FileDropHandler>,
|
||||
) -> crate::Result<Self::Dispatcher> {
|
||||
let app_dispatcher = Arc::new(Mutex::new(self.inner.application_proxy()));
|
||||
let app_dispatcher = self.inner.application_proxy();
|
||||
|
||||
let app_dispatcher_ = app_dispatcher.clone();
|
||||
let wry_rpc_handler = Box::new(
|
||||
move |dispatcher: wry::WindowProxy, request: wry::RpcRequest| {
|
||||
if let Some(handler) = &rpc_handler {
|
||||
handler(
|
||||
WryDispatcher(Arc::new(Mutex::new(dispatcher)), app_dispatcher_.clone()),
|
||||
WryDispatcher(dispatcher, app_dispatcher_.clone()),
|
||||
request.into(),
|
||||
);
|
||||
}
|
||||
@@ -548,10 +489,7 @@ impl ApplicationExt for WryApplication {
|
||||
Some(file_drop_handler),
|
||||
)
|
||||
.map_err(|_| crate::Error::CreateWebview)?;
|
||||
Ok(WryDispatcher(
|
||||
Arc::new(Mutex::new(dispatcher)),
|
||||
app_dispatcher,
|
||||
))
|
||||
Ok(WryDispatcher(dispatcher, app_dispatcher))
|
||||
}
|
||||
|
||||
fn run(self) {
|
||||
|
||||
@@ -277,12 +277,8 @@ impl<A: ApplicationExt + 'static> WebviewManager<A> {
|
||||
);
|
||||
self
|
||||
.application
|
||||
.on_webview_created(
|
||||
label.to_string(),
|
||||
window_dispatcher.clone(),
|
||||
webview_manager,
|
||||
)
|
||||
.await;
|
||||
.on_webview_created(label.to_string(), window_dispatcher.clone());
|
||||
crate::plugin::created(A::plugin_store(), &webview_manager).await;
|
||||
Ok(WebviewDispatcher::new(window_dispatcher, label))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user