2021-04-11 01:09:09 +03:00
|
|
|
|
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
2021-04-12 02:44:01 +03:00
|
|
|
|
//! The [`wry`] Tauri [`Runtime`].
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
use tauri_runtime::{
|
2021-08-24 17:40:10 +03:00
|
|
|
|
http::{
|
|
|
|
|
Request as HttpRequest, RequestParts as HttpRequestParts, Response as HttpResponse,
|
|
|
|
|
ResponseParts as HttpResponseParts,
|
|
|
|
|
},
|
2022-03-28 16:20:38 +03:00
|
|
|
|
menu::{AboutMetadata, CustomMenuItem, Menu, MenuEntry, MenuHash, MenuId, MenuItem, MenuUpdate},
|
2021-05-10 00:43:50 +03:00
|
|
|
|
monitor::Monitor,
|
2022-03-13 17:28:16 +03:00
|
|
|
|
webview::{WebviewIpcHandler, WindowBuilder, WindowBuilderBase},
|
2021-05-09 21:19:37 +03:00
|
|
|
|
window::{
|
|
|
|
|
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
|
2022-03-13 17:28:16 +03:00
|
|
|
|
DetachedWindow, FileDropEvent, JsEventListenerKey, PendingWindow, WindowEvent,
|
2021-04-04 03:41:04 +03:00
|
|
|
|
},
|
2022-03-15 17:20:23 +03:00
|
|
|
|
ClipboardManager, Dispatch, Error, EventLoopProxy, ExitRequestedEventAction,
|
|
|
|
|
GlobalShortcutManager, Result, RunEvent, RunIteration, Runtime, RuntimeHandle, UserAttentionType,
|
|
|
|
|
UserEvent, WindowIcon,
|
2021-02-17 17:15:04 +03:00
|
|
|
|
};
|
2021-04-29 01:56:05 +03:00
|
|
|
|
|
2021-05-10 19:27:42 +03:00
|
|
|
|
use tauri_runtime::window::MenuEvent;
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
2021-06-04 19:51:15 +03:00
|
|
|
|
use tauri_runtime::{SystemTray, SystemTrayEvent};
|
2021-05-21 22:16:05 +03:00
|
|
|
|
#[cfg(windows)]
|
2021-12-29 13:07:08 +03:00
|
|
|
|
use webview2_com::FocusChangedEventHandler;
|
2021-11-12 00:38:41 +03:00
|
|
|
|
#[cfg(windows)]
|
2021-12-30 17:28:41 +03:00
|
|
|
|
use windows::Win32::{Foundation::HWND, System::WinRT::EventRegistrationToken};
|
2021-07-29 22:29:59 +03:00
|
|
|
|
#[cfg(all(feature = "system-tray", target_os = "macos"))]
|
|
|
|
|
use wry::application::platform::macos::{SystemTrayBuilderExtMacOS, SystemTrayExtMacOS};
|
2021-07-15 18:47:19 +03:00
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
use wry::application::platform::unix::{WindowBuilderExtUnix, WindowExtUnix};
|
2021-05-21 22:16:05 +03:00
|
|
|
|
#[cfg(windows)]
|
2021-07-15 18:47:19 +03:00
|
|
|
|
use wry::application::platform::windows::{WindowBuilderExtWindows, WindowExtWindows};
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
use wry::application::system_tray::{SystemTray as WrySystemTray, SystemTrayBuilder};
|
2021-05-10 19:27:42 +03:00
|
|
|
|
|
2021-05-09 21:19:37 +03:00
|
|
|
|
use tauri_utils::config::WindowConfig;
|
2021-05-06 06:43:02 +03:00
|
|
|
|
use uuid::Uuid;
|
2021-04-29 01:56:05 +03:00
|
|
|
|
use wry::{
|
|
|
|
|
application::{
|
2021-06-21 18:29:26 +03:00
|
|
|
|
accelerator::{Accelerator, AcceleratorId},
|
2021-06-21 19:32:22 +03:00
|
|
|
|
clipboard::Clipboard,
|
2021-05-06 02:15:08 +03:00
|
|
|
|
dpi::{
|
|
|
|
|
LogicalPosition as WryLogicalPosition, LogicalSize as WryLogicalSize,
|
|
|
|
|
PhysicalPosition as WryPhysicalPosition, PhysicalSize as WryPhysicalSize,
|
|
|
|
|
Position as WryPosition, Size as WrySize,
|
|
|
|
|
},
|
2021-08-15 23:10:22 +03:00
|
|
|
|
event::{Event, StartCause, WindowEvent as WryWindowEvent},
|
2022-03-15 17:20:23 +03:00
|
|
|
|
event_loop::{
|
|
|
|
|
ControlFlow, EventLoop, EventLoopProxy as WryEventLoopProxy, EventLoopWindowTarget,
|
|
|
|
|
},
|
2021-07-29 20:35:26 +03:00
|
|
|
|
global_shortcut::{GlobalShortcut, ShortcutManager as WryShortcutManager},
|
2021-08-13 16:23:32 +03:00
|
|
|
|
menu::{
|
2022-03-28 16:20:38 +03:00
|
|
|
|
AboutMetadata as WryAboutMetadata, CustomMenuItem as WryCustomMenuItem, MenuBar,
|
|
|
|
|
MenuId as WryMenuId, MenuItem as WryMenuItem, MenuItemAttributes as WryMenuItemAttributes,
|
|
|
|
|
MenuType,
|
2021-08-13 16:23:32 +03:00
|
|
|
|
},
|
2021-05-06 02:15:08 +03:00
|
|
|
|
monitor::MonitorHandle,
|
2022-03-05 20:19:24 +03:00
|
|
|
|
window::{Fullscreen, Icon as WryWindowIcon, UserAttentionType as WryUserAttentionType},
|
2021-04-29 01:56:05 +03:00
|
|
|
|
},
|
2021-08-24 17:40:10 +03:00
|
|
|
|
http::{
|
|
|
|
|
Request as WryHttpRequest, RequestParts as WryRequestParts, Response as WryHttpResponse,
|
|
|
|
|
ResponseParts as WryResponseParts,
|
|
|
|
|
},
|
2022-01-09 17:29:29 +03:00
|
|
|
|
webview::{FileDropEvent as WryFileDropEvent, WebContext, WebView, WebViewBuilder},
|
2021-04-29 01:56:05 +03:00
|
|
|
|
};
|
|
|
|
|
|
2021-08-02 22:45:24 +03:00
|
|
|
|
pub use wry::application::window::{Window, WindowBuilder as WryWindowBuilder, WindowId};
|
2021-07-29 20:35:26 +03:00
|
|
|
|
|
2022-03-15 18:59:37 +03:00
|
|
|
|
#[cfg(windows)]
|
2021-07-23 16:31:17 +03:00
|
|
|
|
use wry::webview::WebviewExtWindows;
|
|
|
|
|
|
2021-08-13 16:23:32 +03:00
|
|
|
|
#[cfg(target_os = "macos")]
|
2021-08-13 19:25:31 +03:00
|
|
|
|
use tauri_runtime::{menu::NativeImage, ActivationPolicy};
|
2021-08-13 16:23:32 +03:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
pub use wry::application::platform::macos::{
|
2021-08-13 19:25:31 +03:00
|
|
|
|
ActivationPolicy as WryActivationPolicy, CustomMenuItemExtMacOS, EventLoopExtMacOS,
|
|
|
|
|
NativeImage as WryNativeImage, WindowExtMacOS,
|
2021-08-13 16:23:32 +03:00
|
|
|
|
};
|
|
|
|
|
|
2021-04-21 21:43:11 +03:00
|
|
|
|
use std::{
|
2021-08-02 05:54:10 +03:00
|
|
|
|
collections::{
|
|
|
|
|
hash_map::Entry::{Occupied, Vacant},
|
2021-12-09 18:22:12 +03:00
|
|
|
|
HashMap, HashSet,
|
2021-08-02 05:54:10 +03:00
|
|
|
|
},
|
2021-08-11 08:07:39 +03:00
|
|
|
|
fmt,
|
2021-09-28 02:18:06 +03:00
|
|
|
|
ops::Deref,
|
2021-08-02 05:54:10 +03:00
|
|
|
|
path::PathBuf,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
sync::{
|
2021-06-05 04:23:03 +03:00
|
|
|
|
mpsc::{channel, Sender},
|
2021-08-31 21:50:40 +03:00
|
|
|
|
Arc, Mutex, MutexGuard, Weak,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
},
|
2021-06-16 17:07:41 +03:00
|
|
|
|
thread::{current as current_thread, ThreadId},
|
2021-04-21 21:43:11 +03:00
|
|
|
|
};
|
2021-02-08 17:19:22 +03:00
|
|
|
|
|
2022-03-15 18:59:37 +03:00
|
|
|
|
type WebviewId = u64;
|
|
|
|
|
|
2021-08-13 16:23:32 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
mod system_tray;
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
use system_tray::*;
|
2021-05-10 19:27:42 +03:00
|
|
|
|
|
2021-09-26 07:50:27 +03:00
|
|
|
|
type WebContextStore = Arc<Mutex<HashMap<Option<PathBuf>, WebContext>>>;
|
2021-08-13 16:23:32 +03:00
|
|
|
|
// window
|
2021-05-06 06:43:02 +03:00
|
|
|
|
type WindowEventHandler = Box<dyn Fn(&WindowEvent) + Send>;
|
2021-06-23 22:20:09 +03:00
|
|
|
|
type WindowEventListenersMap = Arc<Mutex<HashMap<Uuid, WindowEventHandler>>>;
|
2022-03-15 18:59:37 +03:00
|
|
|
|
type WindowEventListeners = Arc<Mutex<HashMap<WebviewId, WindowEventListenersMap>>>;
|
2021-08-13 16:23:32 +03:00
|
|
|
|
// global shortcut
|
2021-06-21 18:29:26 +03:00
|
|
|
|
type GlobalShortcutListeners = Arc<Mutex<HashMap<AcceleratorId, Box<dyn Fn() + Send>>>>;
|
2021-08-13 16:23:32 +03:00
|
|
|
|
// menu
|
|
|
|
|
pub type MenuEventHandler = Box<dyn Fn(&MenuEvent) + Send>;
|
2022-03-15 18:59:37 +03:00
|
|
|
|
pub type MenuEventListeners = Arc<Mutex<HashMap<WebviewId, WindowMenuEventListeners>>>;
|
2021-08-13 16:23:32 +03:00
|
|
|
|
pub type WindowMenuEventListeners = Arc<Mutex<HashMap<Uuid, MenuEventHandler>>>;
|
2021-06-21 18:29:26 +03:00
|
|
|
|
|
2022-03-15 18:59:37 +03:00
|
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
|
struct WebviewIdStore(Arc<Mutex<HashMap<WindowId, WebviewId>>>);
|
|
|
|
|
|
|
|
|
|
impl WebviewIdStore {
|
|
|
|
|
fn insert(&self, w: WindowId, id: WebviewId) {
|
|
|
|
|
self.0.lock().unwrap().insert(w, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get(&self, w: &WindowId) -> WebviewId {
|
|
|
|
|
*self.0.lock().unwrap().get(w).unwrap()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 02:18:06 +03:00
|
|
|
|
macro_rules! getter {
|
|
|
|
|
($self: ident, $rx: expr, $message: expr) => {{
|
|
|
|
|
send_user_message(&$self.context, $message)?;
|
2022-01-10 21:33:35 +03:00
|
|
|
|
$rx.recv().map_err(|_| Error::FailedToReceiveMessage)
|
2021-09-26 07:50:27 +03:00
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 02:18:06 +03:00
|
|
|
|
macro_rules! window_getter {
|
2021-06-21 18:29:26 +03:00
|
|
|
|
($self: ident, $message: expr) => {{
|
2021-09-28 02:18:06 +03:00
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
getter!($self, rx, Message::Window($self.window_id, $message(tx)))
|
2021-06-21 18:29:26 +03:00
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
fn send_user_message<T: UserEvent>(context: &Context<T>, message: Message<T>) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
if current_thread().id() == context.main_thread_id {
|
|
|
|
|
handle_user_message(
|
|
|
|
|
&context.main_thread.window_target,
|
|
|
|
|
message,
|
|
|
|
|
UserMessageContext {
|
|
|
|
|
window_event_listeners: &context.window_event_listeners,
|
|
|
|
|
global_shortcut_manager: context.main_thread.global_shortcut_manager.clone(),
|
|
|
|
|
clipboard_manager: context.main_thread.clipboard_manager.clone(),
|
|
|
|
|
menu_event_listeners: &context.menu_event_listeners,
|
|
|
|
|
windows: context.main_thread.windows.clone(),
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
tray_context: &context.main_thread.tray_context,
|
|
|
|
|
},
|
|
|
|
|
&context.main_thread.web_context,
|
|
|
|
|
);
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
context
|
|
|
|
|
.proxy
|
|
|
|
|
.send_event(message)
|
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
|
|
|
|
}
|
2021-06-21 18:29:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-26 07:50:27 +03:00
|
|
|
|
#[derive(Clone)]
|
2022-03-15 17:20:23 +03:00
|
|
|
|
struct Context<T: UserEvent> {
|
2022-03-15 18:59:37 +03:00
|
|
|
|
webview_id_map: WebviewIdStore,
|
2021-06-21 18:29:26 +03:00
|
|
|
|
main_thread_id: ThreadId,
|
2022-03-15 17:20:23 +03:00
|
|
|
|
proxy: WryEventLoopProxy<Message<T>>,
|
2021-09-26 07:50:27 +03:00
|
|
|
|
window_event_listeners: WindowEventListeners,
|
|
|
|
|
menu_event_listeners: MenuEventListeners,
|
2022-03-15 17:20:23 +03:00
|
|
|
|
main_thread: DispatcherMainThreadContext<T>,
|
2021-09-26 07:50:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 18:59:37 +03:00
|
|
|
|
impl<T: UserEvent> Context<T> {
|
|
|
|
|
fn prepare_window(&self, window_id: WebviewId) {
|
|
|
|
|
self
|
|
|
|
|
.window_event_listeners
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.insert(window_id, WindowEventListenersMap::default());
|
|
|
|
|
|
|
|
|
|
self
|
|
|
|
|
.menu_event_listeners
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.insert(window_id, WindowMenuEventListeners::default());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn create_webview(&self, pending: PendingWindow<T, Wry<T>>) -> Result<DetachedWindow<T, Wry<T>>> {
|
|
|
|
|
let label = pending.label.clone();
|
|
|
|
|
let menu_ids = pending.menu_ids.clone();
|
|
|
|
|
let js_event_listeners = pending.js_event_listeners.clone();
|
|
|
|
|
let context = self.clone();
|
|
|
|
|
let window_id = rand::random();
|
|
|
|
|
|
|
|
|
|
self.prepare_window(window_id);
|
|
|
|
|
|
|
|
|
|
self
|
|
|
|
|
.proxy
|
|
|
|
|
.send_event(Message::CreateWebview(
|
|
|
|
|
window_id,
|
|
|
|
|
Box::new(move |event_loop, web_context| {
|
|
|
|
|
create_webview(window_id, event_loop, web_context, context, pending)
|
|
|
|
|
}),
|
|
|
|
|
))
|
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)?;
|
|
|
|
|
|
|
|
|
|
let dispatcher = WryDispatcher {
|
|
|
|
|
window_id,
|
|
|
|
|
context: self.clone(),
|
|
|
|
|
};
|
|
|
|
|
Ok(DetachedWindow {
|
|
|
|
|
label,
|
|
|
|
|
dispatcher,
|
|
|
|
|
menu_ids,
|
|
|
|
|
js_event_listeners,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 02:18:06 +03:00
|
|
|
|
#[derive(Debug, Clone)]
|
2022-03-15 17:20:23 +03:00
|
|
|
|
struct DispatcherMainThreadContext<T: UserEvent> {
|
|
|
|
|
window_target: EventLoopWindowTarget<Message<T>>,
|
2021-09-28 02:18:06 +03:00
|
|
|
|
web_context: WebContextStore,
|
|
|
|
|
global_shortcut_manager: Arc<Mutex<WryShortcutManager>>,
|
|
|
|
|
clipboard_manager: Arc<Mutex<Clipboard>>,
|
2022-03-15 18:59:37 +03:00
|
|
|
|
windows: Arc<Mutex<HashMap<WebviewId, WindowWrapper>>>,
|
2021-09-28 02:18:06 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
tray_context: TrayContext,
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-30 19:46:05 +03:00
|
|
|
|
// SAFETY: we ensure this type is only used on the main thread.
|
2021-11-10 17:12:05 +03:00
|
|
|
|
#[allow(clippy::non_send_fields_in_send_ty)]
|
2022-03-15 17:20:23 +03:00
|
|
|
|
unsafe impl<T: UserEvent> Send for DispatcherMainThreadContext<T> {}
|
2021-09-28 02:18:06 +03:00
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
impl<T: UserEvent> fmt::Debug for Context<T> {
|
2021-09-26 07:50:27 +03:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
f.debug_struct("Context")
|
2021-09-26 07:50:27 +03:00
|
|
|
|
.field("main_thread_id", &self.main_thread_id)
|
|
|
|
|
.field("proxy", &self.proxy)
|
|
|
|
|
.field("main_thread", &self.main_thread)
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
2021-06-21 18:29:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 17:40:10 +03:00
|
|
|
|
struct HttpRequestPartsWrapper(HttpRequestParts);
|
|
|
|
|
|
|
|
|
|
impl From<HttpRequestPartsWrapper> for HttpRequestParts {
|
|
|
|
|
fn from(parts: HttpRequestPartsWrapper) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
method: parts.0.method,
|
|
|
|
|
uri: parts.0.uri,
|
|
|
|
|
headers: parts.0.headers,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<HttpRequestParts> for HttpRequestPartsWrapper {
|
|
|
|
|
fn from(request: HttpRequestParts) -> Self {
|
|
|
|
|
Self(HttpRequestParts {
|
|
|
|
|
method: request.method,
|
|
|
|
|
uri: request.uri,
|
|
|
|
|
headers: request.headers,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<WryRequestParts> for HttpRequestPartsWrapper {
|
|
|
|
|
fn from(request: WryRequestParts) -> Self {
|
|
|
|
|
Self(HttpRequestParts {
|
|
|
|
|
method: request.method,
|
|
|
|
|
uri: request.uri,
|
|
|
|
|
headers: request.headers,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct HttpRequestWrapper(HttpRequest);
|
|
|
|
|
|
|
|
|
|
impl From<&WryHttpRequest> for HttpRequestWrapper {
|
|
|
|
|
fn from(req: &WryHttpRequest) -> Self {
|
2022-03-06 16:15:43 +03:00
|
|
|
|
Self(HttpRequest::new_internal(
|
|
|
|
|
HttpRequestPartsWrapper::from(req.head.clone()).0,
|
|
|
|
|
req.body.clone(),
|
|
|
|
|
))
|
2021-08-24 17:40:10 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// response
|
|
|
|
|
struct HttpResponsePartsWrapper(WryResponseParts);
|
|
|
|
|
impl From<HttpResponseParts> for HttpResponsePartsWrapper {
|
|
|
|
|
fn from(response: HttpResponseParts) -> Self {
|
|
|
|
|
Self(WryResponseParts {
|
|
|
|
|
mimetype: response.mimetype,
|
|
|
|
|
status: response.status,
|
|
|
|
|
version: response.version,
|
|
|
|
|
headers: response.headers,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct HttpResponseWrapper(WryHttpResponse);
|
|
|
|
|
impl From<HttpResponse> for HttpResponseWrapper {
|
|
|
|
|
fn from(response: HttpResponse) -> Self {
|
2022-03-06 16:15:43 +03:00
|
|
|
|
let (parts, body) = response.into_parts();
|
2021-08-24 17:40:10 +03:00
|
|
|
|
Self(WryHttpResponse {
|
2022-03-06 16:15:43 +03:00
|
|
|
|
body,
|
|
|
|
|
head: HttpResponsePartsWrapper::from(parts).0,
|
2021-08-24 17:40:10 +03:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 16:23:32 +03:00
|
|
|
|
pub struct MenuItemAttributesWrapper<'a>(pub WryMenuItemAttributes<'a>);
|
|
|
|
|
|
|
|
|
|
impl<'a> From<&'a CustomMenuItem> for MenuItemAttributesWrapper<'a> {
|
|
|
|
|
fn from(item: &'a CustomMenuItem) -> Self {
|
|
|
|
|
let mut attributes = WryMenuItemAttributes::new(&item.title)
|
|
|
|
|
.with_enabled(item.enabled)
|
|
|
|
|
.with_selected(item.selected)
|
|
|
|
|
.with_id(WryMenuId(item.id));
|
|
|
|
|
if let Some(accelerator) = item.keyboard_accelerator.as_ref() {
|
|
|
|
|
attributes = attributes.with_accelerators(&accelerator.parse().expect("invalid accelerator"));
|
|
|
|
|
}
|
|
|
|
|
Self(attributes)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-28 16:20:38 +03:00
|
|
|
|
pub struct AboutMetadataWrapper(pub WryAboutMetadata);
|
|
|
|
|
|
|
|
|
|
impl From<AboutMetadata> for AboutMetadataWrapper {
|
|
|
|
|
fn from(metadata: AboutMetadata) -> Self {
|
|
|
|
|
Self(WryAboutMetadata {
|
|
|
|
|
version: metadata.version,
|
|
|
|
|
authors: metadata.authors,
|
|
|
|
|
comments: metadata.comments,
|
|
|
|
|
copyright: metadata.copyright,
|
|
|
|
|
license: metadata.license,
|
|
|
|
|
website: metadata.website,
|
|
|
|
|
website_label: metadata.website_label,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 16:23:32 +03:00
|
|
|
|
pub struct MenuItemWrapper(pub WryMenuItem);
|
|
|
|
|
|
|
|
|
|
impl From<MenuItem> for MenuItemWrapper {
|
|
|
|
|
fn from(item: MenuItem) -> Self {
|
|
|
|
|
match item {
|
2022-03-28 16:20:38 +03:00
|
|
|
|
MenuItem::About(name, metadata) => Self(WryMenuItem::About(
|
|
|
|
|
name,
|
|
|
|
|
AboutMetadataWrapper::from(metadata).0,
|
|
|
|
|
)),
|
2021-08-13 16:23:32 +03:00
|
|
|
|
MenuItem::Hide => Self(WryMenuItem::Hide),
|
|
|
|
|
MenuItem::Services => Self(WryMenuItem::Services),
|
|
|
|
|
MenuItem::HideOthers => Self(WryMenuItem::HideOthers),
|
|
|
|
|
MenuItem::ShowAll => Self(WryMenuItem::ShowAll),
|
|
|
|
|
MenuItem::CloseWindow => Self(WryMenuItem::CloseWindow),
|
|
|
|
|
MenuItem::Quit => Self(WryMenuItem::Quit),
|
|
|
|
|
MenuItem::Copy => Self(WryMenuItem::Copy),
|
|
|
|
|
MenuItem::Cut => Self(WryMenuItem::Cut),
|
|
|
|
|
MenuItem::Undo => Self(WryMenuItem::Undo),
|
|
|
|
|
MenuItem::Redo => Self(WryMenuItem::Redo),
|
|
|
|
|
MenuItem::SelectAll => Self(WryMenuItem::SelectAll),
|
|
|
|
|
MenuItem::Paste => Self(WryMenuItem::Paste),
|
|
|
|
|
MenuItem::EnterFullScreen => Self(WryMenuItem::EnterFullScreen),
|
|
|
|
|
MenuItem::Minimize => Self(WryMenuItem::Minimize),
|
|
|
|
|
MenuItem::Zoom => Self(WryMenuItem::Zoom),
|
|
|
|
|
MenuItem::Separator => Self(WryMenuItem::Separator),
|
|
|
|
|
_ => unimplemented!(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
pub struct NativeImageWrapper(pub WryNativeImage);
|
|
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
impl std::fmt::Debug for NativeImageWrapper {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
f.debug_struct("NativeImageWrapper").finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
impl From<NativeImage> for NativeImageWrapper {
|
|
|
|
|
fn from(image: NativeImage) -> NativeImageWrapper {
|
|
|
|
|
let wry_image = match image {
|
|
|
|
|
NativeImage::Add => WryNativeImage::Add,
|
|
|
|
|
NativeImage::Advanced => WryNativeImage::Advanced,
|
|
|
|
|
NativeImage::Bluetooth => WryNativeImage::Bluetooth,
|
|
|
|
|
NativeImage::Bookmarks => WryNativeImage::Bookmarks,
|
|
|
|
|
NativeImage::Caution => WryNativeImage::Caution,
|
|
|
|
|
NativeImage::ColorPanel => WryNativeImage::ColorPanel,
|
|
|
|
|
NativeImage::ColumnView => WryNativeImage::ColumnView,
|
|
|
|
|
NativeImage::Computer => WryNativeImage::Computer,
|
|
|
|
|
NativeImage::EnterFullScreen => WryNativeImage::EnterFullScreen,
|
|
|
|
|
NativeImage::Everyone => WryNativeImage::Everyone,
|
|
|
|
|
NativeImage::ExitFullScreen => WryNativeImage::ExitFullScreen,
|
|
|
|
|
NativeImage::FlowView => WryNativeImage::FlowView,
|
|
|
|
|
NativeImage::Folder => WryNativeImage::Folder,
|
|
|
|
|
NativeImage::FolderBurnable => WryNativeImage::FolderBurnable,
|
|
|
|
|
NativeImage::FolderSmart => WryNativeImage::FolderSmart,
|
|
|
|
|
NativeImage::FollowLinkFreestanding => WryNativeImage::FollowLinkFreestanding,
|
|
|
|
|
NativeImage::FontPanel => WryNativeImage::FontPanel,
|
|
|
|
|
NativeImage::GoLeft => WryNativeImage::GoLeft,
|
|
|
|
|
NativeImage::GoRight => WryNativeImage::GoRight,
|
|
|
|
|
NativeImage::Home => WryNativeImage::Home,
|
|
|
|
|
NativeImage::IChatTheater => WryNativeImage::IChatTheater,
|
|
|
|
|
NativeImage::IconView => WryNativeImage::IconView,
|
|
|
|
|
NativeImage::Info => WryNativeImage::Info,
|
|
|
|
|
NativeImage::InvalidDataFreestanding => WryNativeImage::InvalidDataFreestanding,
|
|
|
|
|
NativeImage::LeftFacingTriangle => WryNativeImage::LeftFacingTriangle,
|
|
|
|
|
NativeImage::ListView => WryNativeImage::ListView,
|
|
|
|
|
NativeImage::LockLocked => WryNativeImage::LockLocked,
|
|
|
|
|
NativeImage::LockUnlocked => WryNativeImage::LockUnlocked,
|
|
|
|
|
NativeImage::MenuMixedState => WryNativeImage::MenuMixedState,
|
|
|
|
|
NativeImage::MenuOnState => WryNativeImage::MenuOnState,
|
|
|
|
|
NativeImage::MobileMe => WryNativeImage::MobileMe,
|
|
|
|
|
NativeImage::MultipleDocuments => WryNativeImage::MultipleDocuments,
|
|
|
|
|
NativeImage::Network => WryNativeImage::Network,
|
|
|
|
|
NativeImage::Path => WryNativeImage::Path,
|
|
|
|
|
NativeImage::PreferencesGeneral => WryNativeImage::PreferencesGeneral,
|
|
|
|
|
NativeImage::QuickLook => WryNativeImage::QuickLook,
|
|
|
|
|
NativeImage::RefreshFreestanding => WryNativeImage::RefreshFreestanding,
|
|
|
|
|
NativeImage::Refresh => WryNativeImage::Refresh,
|
|
|
|
|
NativeImage::Remove => WryNativeImage::Remove,
|
|
|
|
|
NativeImage::RevealFreestanding => WryNativeImage::RevealFreestanding,
|
|
|
|
|
NativeImage::RightFacingTriangle => WryNativeImage::RightFacingTriangle,
|
|
|
|
|
NativeImage::Share => WryNativeImage::Share,
|
|
|
|
|
NativeImage::Slideshow => WryNativeImage::Slideshow,
|
|
|
|
|
NativeImage::SmartBadge => WryNativeImage::SmartBadge,
|
|
|
|
|
NativeImage::StatusAvailable => WryNativeImage::StatusAvailable,
|
|
|
|
|
NativeImage::StatusNone => WryNativeImage::StatusNone,
|
|
|
|
|
NativeImage::StatusPartiallyAvailable => WryNativeImage::StatusPartiallyAvailable,
|
|
|
|
|
NativeImage::StatusUnavailable => WryNativeImage::StatusUnavailable,
|
|
|
|
|
NativeImage::StopProgressFreestanding => WryNativeImage::StopProgressFreestanding,
|
|
|
|
|
NativeImage::StopProgress => WryNativeImage::StopProgress,
|
|
|
|
|
|
|
|
|
|
NativeImage::TrashEmpty => WryNativeImage::TrashEmpty,
|
|
|
|
|
NativeImage::TrashFull => WryNativeImage::TrashFull,
|
|
|
|
|
NativeImage::User => WryNativeImage::User,
|
|
|
|
|
NativeImage::UserAccounts => WryNativeImage::UserAccounts,
|
|
|
|
|
NativeImage::UserGroup => WryNativeImage::UserGroup,
|
|
|
|
|
NativeImage::UserGuest => WryNativeImage::UserGuest,
|
|
|
|
|
};
|
|
|
|
|
Self(wry_image)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 19:32:22 +03:00
|
|
|
|
#[derive(Debug, Clone)]
|
2021-08-02 22:45:24 +03:00
|
|
|
|
pub struct GlobalShortcutWrapper(GlobalShortcut);
|
2021-06-21 19:32:22 +03:00
|
|
|
|
|
2021-12-30 19:46:05 +03:00
|
|
|
|
// SAFETY: usage outside of main thread is guarded, we use the event loop on such cases.
|
2021-11-24 17:12:26 +03:00
|
|
|
|
#[allow(clippy::non_send_fields_in_send_ty)]
|
2021-06-21 19:32:22 +03:00
|
|
|
|
unsafe impl Send for GlobalShortcutWrapper {}
|
|
|
|
|
|
2021-06-21 18:29:26 +03:00
|
|
|
|
/// Wrapper around [`WryShortcutManager`].
|
|
|
|
|
#[derive(Clone)]
|
2022-03-15 17:20:23 +03:00
|
|
|
|
pub struct GlobalShortcutManagerHandle<T: UserEvent> {
|
|
|
|
|
context: Context<T>,
|
2021-06-30 16:38:22 +03:00
|
|
|
|
shortcuts: Arc<Mutex<HashMap<String, (AcceleratorId, GlobalShortcutWrapper)>>>,
|
2021-06-21 18:29:26 +03:00
|
|
|
|
listeners: GlobalShortcutListeners,
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-30 19:46:05 +03:00
|
|
|
|
// SAFETY: this is safe since the `Context` usage is guarded on `send_user_message`.
|
|
|
|
|
#[allow(clippy::non_send_fields_in_send_ty)]
|
2022-03-15 17:20:23 +03:00
|
|
|
|
unsafe impl<T: UserEvent> Sync for GlobalShortcutManagerHandle<T> {}
|
2021-12-30 19:46:05 +03:00
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
impl<T: UserEvent> fmt::Debug for GlobalShortcutManagerHandle<T> {
|
2021-08-11 08:07:39 +03:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.debug_struct("GlobalShortcutManagerHandle")
|
|
|
|
|
.field("context", &self.context)
|
|
|
|
|
.field("shortcuts", &self.shortcuts)
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
impl<T: UserEvent> GlobalShortcutManager for GlobalShortcutManagerHandle<T> {
|
2021-06-21 18:29:26 +03:00
|
|
|
|
fn is_registered(&self, accelerator: &str) -> Result<bool> {
|
|
|
|
|
let (tx, rx) = channel();
|
2022-01-10 21:33:35 +03:00
|
|
|
|
getter!(
|
2021-06-21 18:29:26 +03:00
|
|
|
|
self,
|
|
|
|
|
rx,
|
|
|
|
|
Message::GlobalShortcut(GlobalShortcutMessage::IsRegistered(
|
|
|
|
|
accelerator.parse().expect("invalid accelerator"),
|
|
|
|
|
tx
|
|
|
|
|
))
|
2022-01-10 21:33:35 +03:00
|
|
|
|
)
|
2021-06-21 18:29:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn register<F: Fn() + Send + 'static>(&mut self, accelerator: &str, handler: F) -> Result<()> {
|
|
|
|
|
let wry_accelerator: Accelerator = accelerator.parse().expect("invalid accelerator");
|
|
|
|
|
let id = wry_accelerator.clone().id();
|
|
|
|
|
let (tx, rx) = channel();
|
2021-06-21 19:32:22 +03:00
|
|
|
|
let shortcut = getter!(
|
2021-06-21 18:29:26 +03:00
|
|
|
|
self,
|
|
|
|
|
rx,
|
|
|
|
|
Message::GlobalShortcut(GlobalShortcutMessage::Register(wry_accelerator, tx))
|
2022-01-10 21:33:35 +03:00
|
|
|
|
)??;
|
2021-06-21 18:29:26 +03:00
|
|
|
|
|
|
|
|
|
self.listeners.lock().unwrap().insert(id, Box::new(handler));
|
2021-06-30 16:38:22 +03:00
|
|
|
|
self
|
|
|
|
|
.shortcuts
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.insert(accelerator.into(), (id, shortcut));
|
2021-06-21 18:29:26 +03:00
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn unregister_all(&mut self) -> Result<()> {
|
|
|
|
|
let (tx, rx) = channel();
|
2021-06-21 19:32:22 +03:00
|
|
|
|
getter!(
|
2021-06-21 18:29:26 +03:00
|
|
|
|
self,
|
|
|
|
|
rx,
|
|
|
|
|
Message::GlobalShortcut(GlobalShortcutMessage::UnregisterAll(tx))
|
2022-01-10 21:33:35 +03:00
|
|
|
|
)??;
|
2021-06-21 18:29:26 +03:00
|
|
|
|
self.listeners.lock().unwrap().clear();
|
2021-06-30 16:38:22 +03:00
|
|
|
|
self.shortcuts.lock().unwrap().clear();
|
2021-06-21 18:29:26 +03:00
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn unregister(&mut self, accelerator: &str) -> Result<()> {
|
2021-06-30 16:38:22 +03:00
|
|
|
|
if let Some((accelerator_id, shortcut)) = self.shortcuts.lock().unwrap().remove(accelerator) {
|
2021-06-21 18:29:26 +03:00
|
|
|
|
let (tx, rx) = channel();
|
2021-06-21 19:32:22 +03:00
|
|
|
|
getter!(
|
2021-06-21 18:29:26 +03:00
|
|
|
|
self,
|
|
|
|
|
rx,
|
2021-06-21 19:32:22 +03:00
|
|
|
|
Message::GlobalShortcut(GlobalShortcutMessage::Unregister(shortcut, tx))
|
2022-01-10 21:33:35 +03:00
|
|
|
|
)??;
|
2021-06-21 18:29:26 +03:00
|
|
|
|
self.listeners.lock().unwrap().remove(&accelerator_id);
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 01:56:05 +03:00
|
|
|
|
|
2021-08-11 08:07:39 +03:00
|
|
|
|
#[derive(Debug, Clone)]
|
2022-03-15 17:20:23 +03:00
|
|
|
|
pub struct ClipboardManagerWrapper<T: UserEvent> {
|
|
|
|
|
context: Context<T>,
|
2021-06-21 19:32:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-30 19:46:05 +03:00
|
|
|
|
// SAFETY: this is safe since the `Context` usage is guarded on `send_user_message`.
|
|
|
|
|
#[allow(clippy::non_send_fields_in_send_ty)]
|
2022-03-15 17:20:23 +03:00
|
|
|
|
unsafe impl<T: UserEvent> Sync for ClipboardManagerWrapper<T> {}
|
2021-12-30 19:46:05 +03:00
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
impl<T: UserEvent> ClipboardManager for ClipboardManagerWrapper<T> {
|
2021-06-21 19:32:22 +03:00
|
|
|
|
fn read_text(&self) -> Result<Option<String>> {
|
|
|
|
|
let (tx, rx) = channel();
|
2022-01-10 21:33:35 +03:00
|
|
|
|
getter!(self, rx, Message::Clipboard(ClipboardMessage::ReadText(tx)))
|
2021-06-21 19:32:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
fn write_text<V: Into<String>>(&mut self, text: V) -> Result<()> {
|
2021-06-21 19:32:22 +03:00
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
getter!(
|
|
|
|
|
self,
|
|
|
|
|
rx,
|
|
|
|
|
Message::Clipboard(ClipboardMessage::WriteText(text.into(), tx))
|
2022-01-10 21:33:35 +03:00
|
|
|
|
)?;
|
2021-06-21 19:32:22 +03:00
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-06 16:42:24 +03:00
|
|
|
|
/// Wrapper around a [`wry::application::window::Icon`] that can be created from an [`WindowIcon`].
|
2022-03-05 20:19:24 +03:00
|
|
|
|
pub struct WryIcon(WryWindowIcon);
|
2021-02-14 03:35:55 +03:00
|
|
|
|
|
2022-03-22 21:04:47 +03:00
|
|
|
|
fn icon_err<E: std::error::Error + Send + Sync + 'static>(e: E) -> Error {
|
2021-05-19 03:46:21 +03:00
|
|
|
|
Error::InvalidIcon(Box::new(e))
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 20:19:24 +03:00
|
|
|
|
impl TryFrom<WindowIcon> for WryIcon {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
type Error = Error;
|
2022-03-05 20:19:24 +03:00
|
|
|
|
fn try_from(icon: WindowIcon) -> std::result::Result<Self, Self::Error> {
|
|
|
|
|
WryWindowIcon::from_rgba(icon.rgba, icon.width, icon.height)
|
|
|
|
|
.map(Self)
|
|
|
|
|
.map_err(icon_err)
|
2021-02-14 03:35:55 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-08 17:19:22 +03:00
|
|
|
|
|
2021-05-06 06:43:02 +03:00
|
|
|
|
struct WindowEventWrapper(Option<WindowEvent>);
|
|
|
|
|
|
2021-09-30 02:52:05 +03:00
|
|
|
|
impl WindowEventWrapper {
|
2021-10-22 16:04:42 +03:00
|
|
|
|
fn parse(webview: &WindowHandle, event: &WryWindowEvent<'_>) -> Self {
|
2021-09-30 02:52:05 +03:00
|
|
|
|
match event {
|
|
|
|
|
// resized event from tao doesn't include a reliable size on macOS
|
|
|
|
|
// because wry replaces the NSView
|
|
|
|
|
WryWindowEvent::Resized(_) => Self(Some(WindowEvent::Resized(
|
|
|
|
|
PhysicalSizeWrapper(webview.inner_size()).into(),
|
|
|
|
|
))),
|
|
|
|
|
e => e.into(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 06:43:02 +03:00
|
|
|
|
impl<'a> From<&WryWindowEvent<'a>> for WindowEventWrapper {
|
|
|
|
|
fn from(event: &WryWindowEvent<'a>) -> Self {
|
|
|
|
|
let event = match event {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
WryWindowEvent::Resized(size) => WindowEvent::Resized(PhysicalSizeWrapper(*size).into()),
|
|
|
|
|
WryWindowEvent::Moved(position) => {
|
|
|
|
|
WindowEvent::Moved(PhysicalPositionWrapper(*position).into())
|
|
|
|
|
}
|
2021-05-06 06:43:02 +03:00
|
|
|
|
WryWindowEvent::Destroyed => WindowEvent::Destroyed,
|
|
|
|
|
WryWindowEvent::ScaleFactorChanged {
|
|
|
|
|
scale_factor,
|
|
|
|
|
new_inner_size,
|
|
|
|
|
} => WindowEvent::ScaleFactorChanged {
|
|
|
|
|
scale_factor: *scale_factor,
|
2021-05-10 00:43:50 +03:00
|
|
|
|
new_inner_size: PhysicalSizeWrapper(**new_inner_size).into(),
|
2021-05-06 06:43:02 +03:00
|
|
|
|
},
|
2021-08-24 17:47:54 +03:00
|
|
|
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
|
|
|
|
WryWindowEvent::Focused(focused) => WindowEvent::Focused(*focused),
|
2021-05-06 06:43:02 +03:00
|
|
|
|
_ => return Self(None),
|
|
|
|
|
};
|
|
|
|
|
Self(Some(event))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 16:31:17 +03:00
|
|
|
|
impl From<&WebviewEvent> for WindowEventWrapper {
|
|
|
|
|
fn from(event: &WebviewEvent) -> Self {
|
|
|
|
|
let event = match event {
|
|
|
|
|
WebviewEvent::Focused(focused) => WindowEvent::Focused(*focused),
|
|
|
|
|
};
|
|
|
|
|
Self(Some(event))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
pub struct MonitorHandleWrapper(MonitorHandle);
|
|
|
|
|
|
|
|
|
|
impl From<MonitorHandleWrapper> for Monitor {
|
|
|
|
|
fn from(monitor: MonitorHandleWrapper) -> Monitor {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
name: monitor.0.name(),
|
|
|
|
|
position: PhysicalPositionWrapper(monitor.0.position()).into(),
|
|
|
|
|
size: PhysicalSizeWrapper(monitor.0.size()).into(),
|
|
|
|
|
scale_factor: monitor.0.scale_factor(),
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
struct PhysicalPositionWrapper<T>(WryPhysicalPosition<T>);
|
|
|
|
|
|
|
|
|
|
impl<T> From<PhysicalPositionWrapper<T>> for PhysicalPosition<T> {
|
|
|
|
|
fn from(position: PhysicalPositionWrapper<T>) -> Self {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
x: position.0.x,
|
|
|
|
|
y: position.0.y,
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
impl<T> From<PhysicalPosition<T>> for PhysicalPositionWrapper<T> {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
fn from(position: PhysicalPosition<T>) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(WryPhysicalPosition {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
x: position.x,
|
|
|
|
|
y: position.y,
|
2021-05-10 00:43:50 +03:00
|
|
|
|
})
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
struct LogicalPositionWrapper<T>(WryLogicalPosition<T>);
|
|
|
|
|
|
|
|
|
|
impl<T> From<LogicalPosition<T>> for LogicalPositionWrapper<T> {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
fn from(position: LogicalPosition<T>) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(WryLogicalPosition {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
x: position.x,
|
|
|
|
|
y: position.y,
|
2021-05-10 00:43:50 +03:00
|
|
|
|
})
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
struct PhysicalSizeWrapper<T>(WryPhysicalSize<T>);
|
|
|
|
|
|
|
|
|
|
impl<T> From<PhysicalSizeWrapper<T>> for PhysicalSize<T> {
|
|
|
|
|
fn from(size: PhysicalSizeWrapper<T>) -> Self {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
width: size.0.width,
|
|
|
|
|
height: size.0.height,
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
impl<T> From<PhysicalSize<T>> for PhysicalSizeWrapper<T> {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
fn from(size: PhysicalSize<T>) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(WryPhysicalSize {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
width: size.width,
|
|
|
|
|
height: size.height,
|
2021-05-10 00:43:50 +03:00
|
|
|
|
})
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
struct LogicalSizeWrapper<T>(WryLogicalSize<T>);
|
|
|
|
|
|
|
|
|
|
impl<T> From<LogicalSize<T>> for LogicalSizeWrapper<T> {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
fn from(size: LogicalSize<T>) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(WryLogicalSize {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
width: size.width,
|
|
|
|
|
height: size.height,
|
2021-05-10 00:43:50 +03:00
|
|
|
|
})
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
struct SizeWrapper(WrySize);
|
|
|
|
|
|
|
|
|
|
impl From<Size> for SizeWrapper {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
fn from(size: Size) -> Self {
|
|
|
|
|
match size {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Size::Logical(s) => Self(WrySize::Logical(LogicalSizeWrapper::from(s).0)),
|
|
|
|
|
Size::Physical(s) => Self(WrySize::Physical(PhysicalSizeWrapper::from(s).0)),
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
struct PositionWrapper(WryPosition);
|
|
|
|
|
|
|
|
|
|
impl From<Position> for PositionWrapper {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
fn from(position: Position) -> Self {
|
|
|
|
|
match position {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Position::Logical(s) => Self(WryPosition::Logical(LogicalPositionWrapper::from(s).0)),
|
|
|
|
|
Position::Physical(s) => Self(WryPosition::Physical(PhysicalPositionWrapper::from(s).0)),
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 05:42:38 +03:00
|
|
|
|
#[derive(Debug, Clone)]
|
2021-08-02 22:45:24 +03:00
|
|
|
|
pub struct UserAttentionTypeWrapper(WryUserAttentionType);
|
2021-06-21 05:42:38 +03:00
|
|
|
|
|
|
|
|
|
impl From<UserAttentionType> for UserAttentionTypeWrapper {
|
|
|
|
|
fn from(request_type: UserAttentionType) -> UserAttentionTypeWrapper {
|
|
|
|
|
let o = match request_type {
|
|
|
|
|
UserAttentionType::Critical => WryUserAttentionType::Critical,
|
|
|
|
|
UserAttentionType::Informational => WryUserAttentionType::Informational,
|
|
|
|
|
};
|
|
|
|
|
Self(o)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
#[derive(Debug, Clone, Default)]
|
2021-06-04 19:51:15 +03:00
|
|
|
|
pub struct WindowBuilderWrapper {
|
|
|
|
|
inner: WryWindowBuilder,
|
2021-06-06 00:20:16 +03:00
|
|
|
|
center: bool,
|
2021-10-08 17:38:24 +03:00
|
|
|
|
menu: Option<Menu>,
|
2021-06-04 19:51:15 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-30 19:46:05 +03:00
|
|
|
|
// SAFETY: this type is `Send` since `menu_items` are read only here
|
2021-11-24 17:12:26 +03:00
|
|
|
|
#[allow(clippy::non_send_fields_in_send_ty)]
|
2021-06-04 19:51:15 +03:00
|
|
|
|
unsafe impl Send for WindowBuilderWrapper {}
|
2021-05-10 00:43:50 +03:00
|
|
|
|
|
|
|
|
|
impl WindowBuilderBase for WindowBuilderWrapper {}
|
|
|
|
|
impl WindowBuilder for WindowBuilderWrapper {
|
2021-04-12 02:44:01 +03:00
|
|
|
|
fn new() -> Self {
|
|
|
|
|
Default::default()
|
2021-02-17 17:15:04 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-12 02:44:01 +03:00
|
|
|
|
fn with_config(config: WindowConfig) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
let mut window = WindowBuilderWrapper::new()
|
2021-04-12 02:44:01 +03:00
|
|
|
|
.title(config.title.to_string())
|
2021-04-29 01:56:05 +03:00
|
|
|
|
.inner_size(config.width, config.height)
|
2021-04-12 02:44:01 +03:00
|
|
|
|
.visible(config.visible)
|
|
|
|
|
.resizable(config.resizable)
|
2022-01-09 17:29:29 +03:00
|
|
|
|
.fullscreen(config.fullscreen)
|
2021-04-12 02:44:01 +03:00
|
|
|
|
.decorations(config.decorations)
|
|
|
|
|
.maximized(config.maximized)
|
2021-05-31 00:43:28 +03:00
|
|
|
|
.always_on_top(config.always_on_top)
|
|
|
|
|
.skip_taskbar(config.skip_taskbar);
|
2021-04-12 02:44:01 +03:00
|
|
|
|
|
2022-01-09 17:29:29 +03:00
|
|
|
|
#[cfg(any(not(target_os = "macos"), feature = "macos-private-api"))]
|
|
|
|
|
{
|
|
|
|
|
window = window.transparent(config.transparent);
|
|
|
|
|
}
|
2022-02-19 16:22:54 +03:00
|
|
|
|
#[cfg(all(
|
|
|
|
|
target_os = "macos",
|
|
|
|
|
not(feature = "macos-private-api"),
|
|
|
|
|
debug_assertions
|
|
|
|
|
))]
|
|
|
|
|
if config.transparent {
|
|
|
|
|
eprintln!(
|
|
|
|
|
"The window is set to be transparent but the `macos-private-api` is not enabled.
|
|
|
|
|
This can be enabled via the `tauri.macOSPrivateApi` configuration property <https://tauri.studio/docs/api/config#tauri.macOSPrivateApi>
|
|
|
|
|
");
|
|
|
|
|
}
|
2022-01-09 17:29:29 +03:00
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
if let (Some(min_width), Some(min_height)) = (config.min_width, config.min_height) {
|
|
|
|
|
window = window.min_inner_size(min_width, min_height);
|
2021-02-17 17:15:04 +03:00
|
|
|
|
}
|
2021-04-29 01:56:05 +03:00
|
|
|
|
if let (Some(max_width), Some(max_height)) = (config.max_width, config.max_height) {
|
|
|
|
|
window = window.max_inner_size(max_width, max_height);
|
2021-02-17 17:15:04 +03:00
|
|
|
|
}
|
2021-04-29 01:56:05 +03:00
|
|
|
|
if let (Some(x), Some(y)) = (config.x, config.y) {
|
|
|
|
|
window = window.position(x, y);
|
2021-02-17 17:15:04 +03:00
|
|
|
|
}
|
2021-03-18 03:17:37 +03:00
|
|
|
|
|
2021-07-12 17:59:32 +03:00
|
|
|
|
if config.center {
|
|
|
|
|
window = window.center();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
window
|
2021-02-12 03:50:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 13:05:29 +03:00
|
|
|
|
fn menu(mut self, menu: Menu) -> Self {
|
2021-10-08 17:38:24 +03:00
|
|
|
|
self.menu.replace(menu);
|
2021-06-04 19:51:15 +03:00
|
|
|
|
self
|
2021-05-08 18:11:40 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 00:20:16 +03:00
|
|
|
|
fn center(mut self) -> Self {
|
|
|
|
|
self.center = true;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn position(mut self, x: f64, y: f64) -> Self {
|
|
|
|
|
self.inner = self.inner.with_position(WryLogicalPosition::new(x, y));
|
|
|
|
|
self
|
2021-02-12 03:50:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn inner_size(mut self, width: f64, height: f64) -> Self {
|
|
|
|
|
self.inner = self
|
|
|
|
|
.inner
|
|
|
|
|
.with_inner_size(WryLogicalSize::new(width, height));
|
|
|
|
|
self
|
2021-02-12 03:50:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn min_inner_size(mut self, min_width: f64, min_height: f64) -> Self {
|
|
|
|
|
self.inner = self
|
|
|
|
|
.inner
|
|
|
|
|
.with_min_inner_size(WryLogicalSize::new(min_width, min_height));
|
|
|
|
|
self
|
2021-02-12 03:50:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn max_inner_size(mut self, max_width: f64, max_height: f64) -> Self {
|
|
|
|
|
self.inner = self
|
|
|
|
|
.inner
|
|
|
|
|
.with_max_inner_size(WryLogicalSize::new(max_width, max_height));
|
|
|
|
|
self
|
2021-02-12 03:50:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn resizable(mut self, resizable: bool) -> Self {
|
|
|
|
|
self.inner = self.inner.with_resizable(resizable);
|
|
|
|
|
self
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn title<S: Into<String>>(mut self, title: S) -> Self {
|
|
|
|
|
self.inner = self.inner.with_title(title.into());
|
|
|
|
|
self
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn fullscreen(mut self, fullscreen: bool) -> Self {
|
|
|
|
|
self.inner = if fullscreen {
|
|
|
|
|
self
|
|
|
|
|
.inner
|
|
|
|
|
.with_fullscreen(Some(Fullscreen::Borderless(None)))
|
2021-04-29 01:56:05 +03:00
|
|
|
|
} else {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
self.inner.with_fullscreen(None)
|
|
|
|
|
};
|
|
|
|
|
self
|
2021-02-12 03:50:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 18:47:19 +03:00
|
|
|
|
/// Deprecated since 0.1.4 (noop)
|
|
|
|
|
/// Windows is automatically focused when created.
|
|
|
|
|
fn focus(self) -> Self {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
self
|
2021-05-30 23:41:28 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn maximized(mut self, maximized: bool) -> Self {
|
|
|
|
|
self.inner = self.inner.with_maximized(maximized);
|
|
|
|
|
self
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn visible(mut self, visible: bool) -> Self {
|
|
|
|
|
self.inner = self.inner.with_visible(visible);
|
|
|
|
|
self
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-09 17:29:29 +03:00
|
|
|
|
#[cfg(any(not(target_os = "macos"), feature = "macos-private-api"))]
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn transparent(mut self, transparent: bool) -> Self {
|
|
|
|
|
self.inner = self.inner.with_transparent(transparent);
|
|
|
|
|
self
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn decorations(mut self, decorations: bool) -> Self {
|
|
|
|
|
self.inner = self.inner.with_decorations(decorations);
|
|
|
|
|
self
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn always_on_top(mut self, always_on_top: bool) -> Self {
|
|
|
|
|
self.inner = self.inner.with_always_on_top(always_on_top);
|
|
|
|
|
self
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
2021-02-16 20:42:08 +03:00
|
|
|
|
|
2021-05-21 22:16:05 +03:00
|
|
|
|
#[cfg(windows)]
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn parent_window(mut self, parent: HWND) -> Self {
|
|
|
|
|
self.inner = self.inner.with_parent_window(parent);
|
|
|
|
|
self
|
2021-05-21 22:16:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 19:30:44 +03:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
fn parent_window(mut self, parent: *mut std::ffi::c_void) -> Self {
|
|
|
|
|
use wry::application::platform::macos::WindowBuilderExtMacOS;
|
|
|
|
|
|
|
|
|
|
self.inner = self.inner.with_parent_window(parent);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-21 22:16:05 +03:00
|
|
|
|
#[cfg(windows)]
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn owner_window(mut self, owner: HWND) -> Self {
|
|
|
|
|
self.inner = self.inner.with_owner_window(owner);
|
|
|
|
|
self
|
2021-05-21 22:16:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 20:19:24 +03:00
|
|
|
|
fn icon(mut self, icon: WindowIcon) -> Result<Self> {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
self.inner = self
|
|
|
|
|
.inner
|
|
|
|
|
.with_window_icon(Some(WryIcon::try_from(icon)?.0));
|
|
|
|
|
Ok(self)
|
2021-02-23 17:57:58 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 18:59:37 +03:00
|
|
|
|
#[cfg(any(windows, target_os = "linux"))]
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn skip_taskbar(mut self, skip: bool) -> Self {
|
|
|
|
|
self.inner = self.inner.with_skip_taskbar(skip);
|
|
|
|
|
self
|
2021-05-31 00:43:28 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 18:47:19 +03:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
fn skip_taskbar(self, _skip: bool) -> Self {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 17:57:58 +03:00
|
|
|
|
fn has_icon(&self) -> bool {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
self.inner.window.window_icon.is_some()
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
2021-05-08 18:11:40 +03:00
|
|
|
|
|
2021-10-08 17:38:24 +03:00
|
|
|
|
fn get_menu(&self) -> Option<&Menu> {
|
|
|
|
|
self.menu.as_ref()
|
2021-05-08 18:11:40 +03:00
|
|
|
|
}
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
pub struct FileDropEventWrapper(WryFileDropEvent);
|
|
|
|
|
|
|
|
|
|
impl From<FileDropEventWrapper> for FileDropEvent {
|
|
|
|
|
fn from(event: FileDropEventWrapper) -> Self {
|
|
|
|
|
match event.0 {
|
2021-04-29 01:56:05 +03:00
|
|
|
|
WryFileDropEvent::Hovered(paths) => FileDropEvent::Hovered(paths),
|
|
|
|
|
WryFileDropEvent::Dropped(paths) => FileDropEvent::Dropped(paths),
|
2021-07-15 18:47:19 +03:00
|
|
|
|
// default to cancelled
|
|
|
|
|
// FIXME(maybe): Add `FileDropEvent::Unknown` event?
|
|
|
|
|
_ => FileDropEvent::Cancelled,
|
2021-03-13 03:02:36 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 20:35:26 +03:00
|
|
|
|
#[cfg(target_os = "macos")]
|
2021-08-02 22:45:24 +03:00
|
|
|
|
pub struct NSWindow(*mut std::ffi::c_void);
|
2021-07-29 20:35:26 +03:00
|
|
|
|
#[cfg(target_os = "macos")]
|
2021-11-10 17:12:05 +03:00
|
|
|
|
#[allow(clippy::non_send_fields_in_send_ty)]
|
2021-07-29 20:35:26 +03:00
|
|
|
|
unsafe impl Send for NSWindow {}
|
|
|
|
|
|
2021-05-21 22:53:46 +03:00
|
|
|
|
#[cfg(windows)]
|
2021-08-02 22:45:24 +03:00
|
|
|
|
pub struct Hwnd(HWND);
|
2021-05-21 22:53:46 +03:00
|
|
|
|
#[cfg(windows)]
|
2021-11-10 17:12:05 +03:00
|
|
|
|
#[allow(clippy::non_send_fields_in_send_ty)]
|
2021-05-21 22:53:46 +03:00
|
|
|
|
unsafe impl Send for Hwnd {}
|
|
|
|
|
|
2021-07-02 19:08:51 +03:00
|
|
|
|
#[cfg(any(
|
|
|
|
|
target_os = "linux",
|
|
|
|
|
target_os = "dragonfly",
|
|
|
|
|
target_os = "freebsd",
|
|
|
|
|
target_os = "netbsd",
|
|
|
|
|
target_os = "openbsd"
|
|
|
|
|
))]
|
2021-08-02 22:45:24 +03:00
|
|
|
|
pub struct GtkWindow(gtk::ApplicationWindow);
|
2021-07-02 19:08:51 +03:00
|
|
|
|
#[cfg(any(
|
|
|
|
|
target_os = "linux",
|
|
|
|
|
target_os = "dragonfly",
|
|
|
|
|
target_os = "freebsd",
|
|
|
|
|
target_os = "netbsd",
|
|
|
|
|
target_os = "openbsd"
|
|
|
|
|
))]
|
2021-11-10 17:12:05 +03:00
|
|
|
|
#[allow(clippy::non_send_fields_in_send_ty)]
|
2021-07-02 19:08:51 +03:00
|
|
|
|
unsafe impl Send for GtkWindow {}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
#[derive(Debug, Clone)]
|
2021-08-02 22:45:24 +03:00
|
|
|
|
pub enum WindowMessage {
|
2022-02-07 16:04:33 +03:00
|
|
|
|
#[cfg(any(debug_assertions, feature = "devtools"))]
|
|
|
|
|
OpenDevTools,
|
2021-05-06 02:15:08 +03:00
|
|
|
|
// Getters
|
|
|
|
|
ScaleFactor(Sender<f64>),
|
2021-05-10 00:43:50 +03:00
|
|
|
|
InnerPosition(Sender<Result<PhysicalPosition<i32>>>),
|
|
|
|
|
OuterPosition(Sender<Result<PhysicalPosition<i32>>>),
|
2021-05-06 02:15:08 +03:00
|
|
|
|
InnerSize(Sender<PhysicalSize<u32>>),
|
|
|
|
|
OuterSize(Sender<PhysicalSize<u32>>),
|
|
|
|
|
IsFullscreen(Sender<bool>),
|
|
|
|
|
IsMaximized(Sender<bool>),
|
2021-05-30 23:16:07 +03:00
|
|
|
|
IsDecorated(Sender<bool>),
|
2021-05-30 23:23:52 +03:00
|
|
|
|
IsResizable(Sender<bool>),
|
2021-05-31 00:06:24 +03:00
|
|
|
|
IsVisible(Sender<bool>),
|
2021-06-16 04:04:44 +03:00
|
|
|
|
IsMenuVisible(Sender<bool>),
|
2021-05-06 02:15:08 +03:00
|
|
|
|
CurrentMonitor(Sender<Option<MonitorHandle>>),
|
|
|
|
|
PrimaryMonitor(Sender<Option<MonitorHandle>>),
|
|
|
|
|
AvailableMonitors(Sender<Vec<MonitorHandle>>),
|
2021-07-29 20:35:26 +03:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
NSWindow(Sender<NSWindow>),
|
2021-05-21 22:53:46 +03:00
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
Hwnd(Sender<Hwnd>),
|
2021-07-02 19:08:51 +03:00
|
|
|
|
#[cfg(any(
|
|
|
|
|
target_os = "linux",
|
|
|
|
|
target_os = "dragonfly",
|
|
|
|
|
target_os = "freebsd",
|
|
|
|
|
target_os = "netbsd",
|
|
|
|
|
target_os = "openbsd"
|
|
|
|
|
))]
|
|
|
|
|
GtkWindow(Sender<GtkWindow>),
|
2021-05-06 02:15:08 +03:00
|
|
|
|
// Setters
|
2021-06-06 00:20:16 +03:00
|
|
|
|
Center(Sender<Result<()>>),
|
2021-06-21 05:42:38 +03:00
|
|
|
|
RequestUserAttention(Option<UserAttentionTypeWrapper>),
|
2021-04-29 01:56:05 +03:00
|
|
|
|
SetResizable(bool),
|
|
|
|
|
SetTitle(String),
|
|
|
|
|
Maximize,
|
|
|
|
|
Unmaximize,
|
|
|
|
|
Minimize,
|
|
|
|
|
Unminimize,
|
2021-06-16 04:04:44 +03:00
|
|
|
|
ShowMenu,
|
|
|
|
|
HideMenu,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
Show,
|
|
|
|
|
Hide,
|
|
|
|
|
Close,
|
|
|
|
|
SetDecorations(bool),
|
|
|
|
|
SetAlwaysOnTop(bool),
|
2021-05-06 02:15:08 +03:00
|
|
|
|
SetSize(Size),
|
|
|
|
|
SetMinSize(Option<Size>),
|
|
|
|
|
SetMaxSize(Option<Size>),
|
|
|
|
|
SetPosition(Position),
|
2021-04-29 01:56:05 +03:00
|
|
|
|
SetFullscreen(bool),
|
2021-05-30 23:29:04 +03:00
|
|
|
|
SetFocus,
|
2022-03-05 20:19:24 +03:00
|
|
|
|
SetIcon(WryWindowIcon),
|
2021-05-31 02:48:21 +03:00
|
|
|
|
SetSkipTaskbar(bool),
|
2021-04-29 01:56:05 +03:00
|
|
|
|
DragWindow,
|
2021-08-13 16:23:32 +03:00
|
|
|
|
UpdateMenuItem(u16, MenuUpdate),
|
2022-01-05 12:56:55 +03:00
|
|
|
|
RequestRedraw,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2021-08-02 22:45:24 +03:00
|
|
|
|
pub enum WebviewMessage {
|
2021-04-29 01:56:05 +03:00
|
|
|
|
EvaluateScript(String),
|
2021-07-23 16:31:17 +03:00
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
WebviewEvent(WebviewEvent),
|
2021-05-07 16:58:44 +03:00
|
|
|
|
Print,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 16:31:17 +03:00
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
#[derive(Debug, Clone)]
|
2021-08-02 22:45:24 +03:00
|
|
|
|
pub enum WebviewEvent {
|
2021-07-23 16:31:17 +03:00
|
|
|
|
Focused(bool),
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
2021-08-11 08:07:39 +03:00
|
|
|
|
#[derive(Debug, Clone)]
|
2021-08-02 22:45:24 +03:00
|
|
|
|
pub enum TrayMessage {
|
2021-08-13 16:23:32 +03:00
|
|
|
|
UpdateItem(u16, MenuUpdate),
|
2021-10-02 21:57:53 +03:00
|
|
|
|
UpdateMenu(SystemTrayMenu),
|
2022-03-05 20:19:24 +03:00
|
|
|
|
UpdateIcon(TrayIcon),
|
2021-07-29 22:29:59 +03:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
UpdateIconAsTemplate(bool),
|
2022-02-07 19:04:38 +03:00
|
|
|
|
Close,
|
2021-06-04 19:51:15 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 08:07:39 +03:00
|
|
|
|
#[derive(Debug, Clone)]
|
2021-08-02 22:45:24 +03:00
|
|
|
|
pub enum GlobalShortcutMessage {
|
2021-06-21 18:29:26 +03:00
|
|
|
|
IsRegistered(Accelerator, Sender<bool>),
|
|
|
|
|
Register(Accelerator, Sender<Result<GlobalShortcutWrapper>>),
|
|
|
|
|
Unregister(GlobalShortcutWrapper, Sender<Result<()>>),
|
|
|
|
|
UnregisterAll(Sender<Result<()>>),
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 08:07:39 +03:00
|
|
|
|
#[derive(Debug, Clone)]
|
2021-08-02 22:45:24 +03:00
|
|
|
|
pub enum ClipboardMessage {
|
2021-06-21 19:32:22 +03:00
|
|
|
|
WriteText(String, Sender<()>),
|
|
|
|
|
ReadText(Sender<Option<String>>),
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
pub type CreateWebviewClosure<T> = Box<
|
|
|
|
|
dyn FnOnce(&EventLoopWindowTarget<Message<T>>, &WebContextStore) -> Result<WindowWrapper> + Send,
|
|
|
|
|
>;
|
|
|
|
|
|
|
|
|
|
pub enum Message<T: 'static> {
|
2021-07-02 06:06:58 +03:00
|
|
|
|
Task(Box<dyn FnOnce() + Send>),
|
2022-03-15 18:59:37 +03:00
|
|
|
|
Window(WebviewId, WindowMessage),
|
|
|
|
|
Webview(WebviewId, WebviewMessage),
|
2021-06-04 19:51:15 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
Tray(TrayMessage),
|
2022-03-15 18:59:37 +03:00
|
|
|
|
CreateWebview(WebviewId, CreateWebviewClosure<T>),
|
2021-07-29 20:35:26 +03:00
|
|
|
|
CreateWindow(
|
2022-03-15 18:59:37 +03:00
|
|
|
|
WebviewId,
|
2021-07-29 20:35:26 +03:00
|
|
|
|
Box<dyn FnOnce() -> (String, WryWindowBuilder) + Send>,
|
2021-08-31 21:50:40 +03:00
|
|
|
|
Sender<Result<Weak<Window>>>,
|
2021-07-29 20:35:26 +03:00
|
|
|
|
),
|
2021-06-21 18:29:26 +03:00
|
|
|
|
GlobalShortcut(GlobalShortcutMessage),
|
2021-06-21 19:32:22 +03:00
|
|
|
|
Clipboard(ClipboardMessage),
|
2022-03-15 17:20:23 +03:00
|
|
|
|
UserEvent(T),
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
impl<T: UserEvent> Clone for Message<T> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Window(i, m) => Self::Window(*i, m.clone()),
|
|
|
|
|
Self::Webview(i, m) => Self::Webview(*i, m.clone()),
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
Self::Tray(m) => Self::Tray(m.clone()),
|
|
|
|
|
Self::GlobalShortcut(m) => Self::GlobalShortcut(m.clone()),
|
|
|
|
|
Self::Clipboard(m) => Self::Clipboard(m.clone()),
|
2022-03-15 17:20:23 +03:00
|
|
|
|
Self::UserEvent(t) => Self::UserEvent(t.clone()),
|
2021-09-28 02:18:06 +03:00
|
|
|
|
_ => unimplemented!(),
|
|
|
|
|
}
|
2021-08-11 08:07:39 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 18:11:40 +03:00
|
|
|
|
/// The Tauri [`Dispatch`] for [`Wry`].
|
2021-08-11 08:07:39 +03:00
|
|
|
|
#[derive(Debug, Clone)]
|
2022-03-15 17:20:23 +03:00
|
|
|
|
pub struct WryDispatcher<T: UserEvent> {
|
2022-03-15 18:59:37 +03:00
|
|
|
|
window_id: WebviewId,
|
2022-03-15 17:20:23 +03:00
|
|
|
|
context: Context<T>,
|
2021-04-04 03:41:04 +03:00
|
|
|
|
}
|
2021-02-17 17:15:04 +03:00
|
|
|
|
|
2021-12-30 19:46:05 +03:00
|
|
|
|
// SAFETY: this is safe since the `Context` usage is guarded on `send_user_message`.
|
|
|
|
|
#[allow(clippy::non_send_fields_in_send_ty)]
|
2022-03-15 17:20:23 +03:00
|
|
|
|
unsafe impl<T: UserEvent> Sync for WryDispatcher<T> {}
|
2021-12-30 19:46:05 +03:00
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
impl<T: UserEvent> Dispatch<T> for WryDispatcher<T> {
|
|
|
|
|
type Runtime = Wry<T>;
|
2021-05-10 00:43:50 +03:00
|
|
|
|
type WindowBuilder = WindowBuilderWrapper;
|
2021-03-13 03:02:36 +03:00
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(&self.context, Message::Task(Box::new(f)))
|
2021-05-04 04:06:50 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 06:43:02 +03:00
|
|
|
|
fn on_window_event<F: Fn(&WindowEvent) + Send + 'static>(&self, f: F) -> Uuid {
|
|
|
|
|
let id = Uuid::new_v4();
|
|
|
|
|
self
|
2021-05-08 18:11:40 +03:00
|
|
|
|
.context
|
2021-05-06 06:43:02 +03:00
|
|
|
|
.window_event_listeners
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
2021-06-23 22:20:09 +03:00
|
|
|
|
.get(&self.window_id)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
2021-05-06 06:43:02 +03:00
|
|
|
|
.insert(id, Box::new(f));
|
|
|
|
|
id
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 18:11:40 +03:00
|
|
|
|
fn on_menu_event<F: Fn(&MenuEvent) + Send + 'static>(&self, f: F) -> Uuid {
|
|
|
|
|
let id = Uuid::new_v4();
|
|
|
|
|
self
|
|
|
|
|
.context
|
|
|
|
|
.menu_event_listeners
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
2021-06-23 17:29:30 +03:00
|
|
|
|
.get(&self.window_id)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
2021-05-08 18:11:40 +03:00
|
|
|
|
.insert(id, Box::new(f));
|
|
|
|
|
id
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-07 16:04:33 +03:00
|
|
|
|
#[cfg(any(debug_assertions, feature = "devtools"))]
|
|
|
|
|
fn open_devtools(&self) {
|
|
|
|
|
let _ = send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::OpenDevTools),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 16:58:44 +03:00
|
|
|
|
// Getters
|
2021-05-06 02:15:08 +03:00
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn scale_factor(&self) -> Result<f64> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
window_getter!(self, WindowMessage::ScaleFactor)
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn inner_position(&self) -> Result<PhysicalPosition<i32>> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
window_getter!(self, WindowMessage::InnerPosition)?
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn outer_position(&self) -> Result<PhysicalPosition<i32>> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
window_getter!(self, WindowMessage::OuterPosition)?
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn inner_size(&self) -> Result<PhysicalSize<u32>> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
window_getter!(self, WindowMessage::InnerSize)
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn outer_size(&self) -> Result<PhysicalSize<u32>> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
window_getter!(self, WindowMessage::OuterSize)
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn is_fullscreen(&self) -> Result<bool> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
window_getter!(self, WindowMessage::IsFullscreen)
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn is_maximized(&self) -> Result<bool> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
window_getter!(self, WindowMessage::IsMaximized)
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-30 23:16:07 +03:00
|
|
|
|
/// Gets the window’s current decoration state.
|
|
|
|
|
fn is_decorated(&self) -> Result<bool> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
window_getter!(self, WindowMessage::IsDecorated)
|
2021-05-30 23:16:07 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-30 23:23:52 +03:00
|
|
|
|
/// Gets the window’s current resizable state.
|
|
|
|
|
fn is_resizable(&self) -> Result<bool> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
window_getter!(self, WindowMessage::IsResizable)
|
2021-05-30 23:23:52 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 00:06:24 +03:00
|
|
|
|
fn is_visible(&self) -> Result<bool> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
window_getter!(self, WindowMessage::IsVisible)
|
2021-05-31 00:06:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-16 04:04:44 +03:00
|
|
|
|
fn is_menu_visible(&self) -> Result<bool> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
window_getter!(self, WindowMessage::IsMenuVisible)
|
2021-06-16 04:04:44 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn current_monitor(&self) -> Result<Option<Monitor>> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
Ok(window_getter!(self, WindowMessage::CurrentMonitor)?.map(|m| MonitorHandleWrapper(m).into()))
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn primary_monitor(&self) -> Result<Option<Monitor>> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
Ok(window_getter!(self, WindowMessage::PrimaryMonitor)?.map(|m| MonitorHandleWrapper(m).into()))
|
2021-05-06 02:15:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn available_monitors(&self) -> Result<Vec<Monitor>> {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
Ok(
|
2022-01-10 21:33:35 +03:00
|
|
|
|
window_getter!(self, WindowMessage::AvailableMonitors)?
|
2021-05-06 02:15:08 +03:00
|
|
|
|
.into_iter()
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map(|m| MonitorHandleWrapper(m).into())
|
2021-05-06 02:15:08 +03:00
|
|
|
|
.collect(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 20:35:26 +03:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
fn ns_window(&self) -> Result<*mut std::ffi::c_void> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
window_getter!(self, WindowMessage::NSWindow).map(|w| w.0)
|
2021-07-29 20:35:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-21 22:53:46 +03:00
|
|
|
|
#[cfg(windows)]
|
2021-06-27 17:02:17 +03:00
|
|
|
|
fn hwnd(&self) -> Result<HWND> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
window_getter!(self, WindowMessage::Hwnd).map(|w| w.0)
|
2021-05-21 22:53:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 19:08:51 +03:00
|
|
|
|
/// Returns the `ApplicatonWindow` from gtk crate that is used by this window.
|
|
|
|
|
#[cfg(any(
|
|
|
|
|
target_os = "linux",
|
|
|
|
|
target_os = "dragonfly",
|
|
|
|
|
target_os = "freebsd",
|
|
|
|
|
target_os = "netbsd",
|
|
|
|
|
target_os = "openbsd"
|
|
|
|
|
))]
|
|
|
|
|
fn gtk_window(&self) -> Result<gtk::ApplicationWindow> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
window_getter!(self, WindowMessage::GtkWindow).map(|w| w.0)
|
2021-07-02 19:08:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 16:58:44 +03:00
|
|
|
|
// Setters
|
|
|
|
|
|
2021-06-06 00:20:16 +03:00
|
|
|
|
fn center(&self) -> Result<()> {
|
2022-01-10 21:33:35 +03:00
|
|
|
|
window_getter!(self, WindowMessage::Center)?
|
2021-06-06 00:20:16 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn print(&self) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Webview(self.window_id, WebviewMessage::Print),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-05-07 16:58:44 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 05:42:38 +03:00
|
|
|
|
fn request_user_attention(&self, request_type: Option<UserAttentionType>) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
2021-09-26 07:50:27 +03:00
|
|
|
|
Message::Window(
|
2021-06-21 05:42:38 +03:00
|
|
|
|
self.window_id,
|
|
|
|
|
WindowMessage::RequestUserAttention(request_type.map(Into::into)),
|
2021-09-28 02:18:06 +03:00
|
|
|
|
),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-06-21 05:42:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 22:49:01 +03:00
|
|
|
|
// Creates a window by dispatching a message to the event loop.
|
|
|
|
|
// Note that this must be called from a separate thread, otherwise the channel will introduce a deadlock.
|
2021-07-15 13:05:29 +03:00
|
|
|
|
fn create_window(
|
2021-04-04 03:41:04 +03:00
|
|
|
|
&mut self,
|
2022-03-15 17:20:23 +03:00
|
|
|
|
pending: PendingWindow<T, Self::Runtime>,
|
|
|
|
|
) -> Result<DetachedWindow<T, Self::Runtime>> {
|
2022-03-15 18:59:37 +03:00
|
|
|
|
self.context.create_webview(pending)
|
2021-02-17 17:15:04 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn set_resizable(&self, resizable: bool) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::SetResizable(resizable)),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn set_title<S: Into<String>>(&self, title: S) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::SetTitle(title.into())),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn maximize(&self) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::Maximize),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn unmaximize(&self) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::Unmaximize),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn minimize(&self) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::Minimize),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
2021-02-08 17:19:22 +03:00
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn unminimize(&self) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::Unminimize),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
2021-02-08 17:19:22 +03:00
|
|
|
|
|
2021-06-16 04:04:44 +03:00
|
|
|
|
fn show_menu(&self) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::ShowMenu),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-06-16 04:04:44 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn hide_menu(&self) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::HideMenu),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-06-16 04:04:44 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn show(&self) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::Show),
|
|
|
|
|
)
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn hide(&self) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::Hide),
|
|
|
|
|
)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn close(&self) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
// NOTE: close cannot use the `send_user_message` function because it accesses the event loop callback
|
2021-03-06 03:59:10 +03:00
|
|
|
|
self
|
2021-05-08 18:11:40 +03:00
|
|
|
|
.context
|
2021-04-29 01:56:05 +03:00
|
|
|
|
.proxy
|
|
|
|
|
.send_event(Message::Window(self.window_id, WindowMessage::Close))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
2021-03-06 03:59:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn set_decorations(&self, decorations: bool) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::SetDecorations(decorations)),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn set_always_on_top(&self, always_on_top: bool) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::SetAlwaysOnTop(always_on_top)),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn set_size(&self, size: Size) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::SetSize(size)),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn set_min_size(&self, size: Option<Size>) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::SetMinSize(size)),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn set_max_size(&self, size: Option<Size>) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::SetMaxSize(size)),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn set_position(&self, position: Position) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::SetPosition(position)),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn set_fullscreen(&self, fullscreen: bool) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::SetFullscreen(fullscreen)),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-30 23:29:04 +03:00
|
|
|
|
fn set_focus(&self) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::SetFocus),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-05-30 23:29:04 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 20:19:24 +03:00
|
|
|
|
fn set_icon(&self, icon: WindowIcon) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
2021-09-26 07:50:27 +03:00
|
|
|
|
Message::Window(
|
2021-04-29 01:56:05 +03:00
|
|
|
|
self.window_id,
|
|
|
|
|
WindowMessage::SetIcon(WryIcon::try_from(icon)?.0),
|
2021-09-28 02:18:06 +03:00
|
|
|
|
),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 02:48:21 +03:00
|
|
|
|
fn set_skip_taskbar(&self, skip: bool) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::SetSkipTaskbar(skip)),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-05-31 02:48:21 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn start_dragging(&self) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::DragWindow),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn eval_script<S: Into<String>>(&self, script: S) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
2021-09-26 07:50:27 +03:00
|
|
|
|
Message::Webview(
|
2021-04-29 01:56:05 +03:00
|
|
|
|
self.window_id,
|
|
|
|
|
WebviewMessage::EvaluateScript(script.into()),
|
2021-09-28 02:18:06 +03:00
|
|
|
|
),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
2021-06-04 19:51:15 +03:00
|
|
|
|
|
2021-08-13 16:23:32 +03:00
|
|
|
|
fn update_menu_item(&self, id: u16, update: MenuUpdate) -> Result<()> {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::Window(self.window_id, WindowMessage::UpdateMenuItem(id, update)),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
)
|
2021-06-04 19:51:15 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
#[derive(Clone, Default)]
|
|
|
|
|
struct TrayContext {
|
|
|
|
|
tray: Arc<Mutex<Option<Arc<Mutex<WrySystemTray>>>>>,
|
|
|
|
|
listeners: SystemTrayEventListeners,
|
|
|
|
|
items: SystemTrayItems,
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-26 07:50:27 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
impl fmt::Debug for TrayContext {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.debug_struct("TrayContext")
|
|
|
|
|
.field("items", &self.items)
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 20:35:26 +03:00
|
|
|
|
enum WindowHandle {
|
|
|
|
|
Webview(WebView),
|
|
|
|
|
Window(Arc<Window>),
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-26 07:50:27 +03:00
|
|
|
|
impl fmt::Debug for WindowHandle {
|
|
|
|
|
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 20:35:26 +03:00
|
|
|
|
impl WindowHandle {
|
|
|
|
|
fn window(&self) -> &Window {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Webview(w) => w.window(),
|
|
|
|
|
Self::Window(w) => w,
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-30 02:52:05 +03:00
|
|
|
|
|
|
|
|
|
fn inner_size(&self) -> WryPhysicalSize<u32> {
|
|
|
|
|
match self {
|
|
|
|
|
WindowHandle::Window(w) => w.inner_size(),
|
|
|
|
|
WindowHandle::Webview(w) => w.inner_size(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-29 20:35:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-26 07:50:27 +03:00
|
|
|
|
#[derive(Debug)]
|
2021-08-02 22:45:24 +03:00
|
|
|
|
pub struct WindowWrapper {
|
2021-06-20 20:12:28 +03:00
|
|
|
|
label: String,
|
2021-07-29 20:35:26 +03:00
|
|
|
|
inner: WindowHandle,
|
2021-10-08 17:38:24 +03:00
|
|
|
|
menu_items: Option<HashMap<u16, WryCustomMenuItem>>,
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct EventProxy<T: UserEvent>(WryEventLoopProxy<Message<T>>);
|
|
|
|
|
|
|
|
|
|
impl<T: UserEvent> EventLoopProxy<T> for EventProxy<T> {
|
|
|
|
|
fn send_event(&self, event: T) -> Result<()> {
|
|
|
|
|
self
|
|
|
|
|
.0
|
|
|
|
|
.send_event(Message::UserEvent(event))
|
|
|
|
|
.map_err(|_| Error::EventLoopClosed)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
/// A Tauri [`Runtime`] wrapper around wry.
|
2022-03-15 17:20:23 +03:00
|
|
|
|
pub struct Wry<T: UserEvent> {
|
2021-06-16 17:07:41 +03:00
|
|
|
|
main_thread_id: ThreadId,
|
2021-06-21 18:29:26 +03:00
|
|
|
|
global_shortcut_manager: Arc<Mutex<WryShortcutManager>>,
|
2022-03-15 17:20:23 +03:00
|
|
|
|
global_shortcut_manager_handle: GlobalShortcutManagerHandle<T>,
|
2021-06-21 19:32:22 +03:00
|
|
|
|
clipboard_manager: Arc<Mutex<Clipboard>>,
|
2022-03-15 17:20:23 +03:00
|
|
|
|
clipboard_manager_handle: ClipboardManagerWrapper<T>,
|
|
|
|
|
event_loop: EventLoop<Message<T>>,
|
2022-03-15 18:59:37 +03:00
|
|
|
|
windows: Arc<Mutex<HashMap<WebviewId, WindowWrapper>>>,
|
|
|
|
|
webview_id_map: WebviewIdStore,
|
2021-08-02 05:54:10 +03:00
|
|
|
|
web_context: WebContextStore,
|
2021-05-06 06:43:02 +03:00
|
|
|
|
window_event_listeners: WindowEventListeners,
|
2021-05-08 18:11:40 +03:00
|
|
|
|
menu_event_listeners: MenuEventListeners,
|
2021-06-04 19:51:15 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
tray_context: TrayContext,
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
impl<T: UserEvent> fmt::Debug for Wry<T> {
|
2022-03-06 16:15:43 +03:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
let mut d = f.debug_struct("Wry");
|
|
|
|
|
d.field("main_thread_id", &self.main_thread_id)
|
|
|
|
|
.field("global_shortcut_manager", &self.global_shortcut_manager)
|
|
|
|
|
.field(
|
|
|
|
|
"global_shortcut_manager_handle",
|
|
|
|
|
&self.global_shortcut_manager_handle,
|
|
|
|
|
)
|
|
|
|
|
.field("clipboard_manager", &self.clipboard_manager)
|
|
|
|
|
.field("clipboard_manager_handle", &self.clipboard_manager_handle)
|
|
|
|
|
.field("event_loop", &self.event_loop)
|
|
|
|
|
.field("windows", &self.windows)
|
|
|
|
|
.field("web_context", &self.web_context);
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
d.field("tray_context", &self.tray_context);
|
|
|
|
|
d.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 22:49:01 +03:00
|
|
|
|
/// A handle to the Wry runtime.
|
2021-08-11 08:07:39 +03:00
|
|
|
|
#[derive(Debug, Clone)]
|
2022-03-15 17:20:23 +03:00
|
|
|
|
pub struct WryHandle<T: UserEvent> {
|
|
|
|
|
context: Context<T>,
|
2021-05-18 22:49:01 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-30 19:46:05 +03:00
|
|
|
|
// SAFETY: this is safe since the `Context` usage is guarded on `send_user_message`.
|
|
|
|
|
#[allow(clippy::non_send_fields_in_send_ty)]
|
2022-03-15 17:20:23 +03:00
|
|
|
|
unsafe impl<T: UserEvent> Sync for WryHandle<T> {}
|
2021-12-30 19:46:05 +03:00
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
impl<T: UserEvent> WryHandle<T> {
|
2021-07-29 20:35:26 +03:00
|
|
|
|
/// Creates a new tao window using a callback, and returns its window id.
|
|
|
|
|
pub fn create_tao_window<F: FnOnce() -> (String, WryWindowBuilder) + Send + 'static>(
|
|
|
|
|
&self,
|
|
|
|
|
f: F,
|
2021-08-31 21:50:40 +03:00
|
|
|
|
) -> Result<Weak<Window>> {
|
2021-07-29 20:35:26 +03:00
|
|
|
|
let (tx, rx) = channel();
|
2022-03-15 18:59:37 +03:00
|
|
|
|
send_user_message(
|
|
|
|
|
&self.context,
|
|
|
|
|
Message::CreateWindow(rand::random(), Box::new(f), tx),
|
|
|
|
|
)?;
|
2021-07-29 20:35:26 +03:00
|
|
|
|
rx.recv().unwrap()
|
|
|
|
|
}
|
2021-08-02 22:45:24 +03:00
|
|
|
|
|
2022-03-15 18:59:37 +03:00
|
|
|
|
/// Gets the [`WebviewId'] associated with the given [`WindowId`].
|
|
|
|
|
pub fn window_id(&self, window_id: WindowId) -> WebviewId {
|
|
|
|
|
*self
|
|
|
|
|
.context
|
|
|
|
|
.webview_id_map
|
|
|
|
|
.0
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.get(&window_id)
|
|
|
|
|
.unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-02 22:45:24 +03:00
|
|
|
|
/// Send a message to the event loop.
|
2022-03-15 17:20:23 +03:00
|
|
|
|
pub fn send_event(&self, message: Message<T>) -> Result<()> {
|
2021-08-02 22:45:24 +03:00
|
|
|
|
self
|
2021-09-28 02:18:06 +03:00
|
|
|
|
.context
|
2021-08-02 22:45:24 +03:00
|
|
|
|
.proxy
|
|
|
|
|
.send_event(message)
|
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2021-07-29 20:35:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
|
|
|
|
|
type Runtime = Wry<T>;
|
|
|
|
|
|
|
|
|
|
fn create_proxy(&self) -> EventProxy<T> {
|
|
|
|
|
EventProxy(self.context.proxy.clone())
|
|
|
|
|
}
|
2021-05-18 22:49:01 +03:00
|
|
|
|
|
|
|
|
|
// Creates a window by dispatching a message to the event loop.
|
|
|
|
|
// Note that this must be called from a separate thread, otherwise the channel will introduce a deadlock.
|
2021-07-15 13:05:29 +03:00
|
|
|
|
fn create_window(
|
2021-05-18 22:49:01 +03:00
|
|
|
|
&self,
|
2022-03-15 17:20:23 +03:00
|
|
|
|
pending: PendingWindow<T, Self::Runtime>,
|
|
|
|
|
) -> Result<DetachedWindow<T, Self::Runtime>> {
|
2022-03-15 18:59:37 +03:00
|
|
|
|
self.context.create_webview(pending)
|
2021-05-18 22:49:01 +03:00
|
|
|
|
}
|
2021-06-04 19:51:15 +03:00
|
|
|
|
|
2021-10-04 18:54:38 +03:00
|
|
|
|
fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()> {
|
|
|
|
|
send_user_message(&self.context, Message::Task(Box::new(f)))
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
#[cfg(all(windows, feature = "system-tray"))]
|
2021-07-29 20:35:26 +03:00
|
|
|
|
/// Deprecated. (not needed anymore)
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn remove_system_tray(&self) -> Result<()> {
|
2022-02-07 19:04:38 +03:00
|
|
|
|
send_user_message(&self.context, Message::Tray(TrayMessage::Close))
|
2021-06-04 19:51:15 +03:00
|
|
|
|
}
|
2021-05-18 22:49:01 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
impl<T: UserEvent> Wry<T> {
|
|
|
|
|
fn init(event_loop: EventLoop<Message<T>>) -> Result<Self> {
|
2021-06-21 18:29:26 +03:00
|
|
|
|
let proxy = event_loop.create_proxy();
|
|
|
|
|
let main_thread_id = current_thread().id();
|
2021-09-26 07:50:27 +03:00
|
|
|
|
let web_context = WebContextStore::default();
|
|
|
|
|
let global_shortcut_manager = Arc::new(Mutex::new(WryShortcutManager::new(&event_loop)));
|
|
|
|
|
let clipboard_manager = Arc::new(Mutex::new(Clipboard::new()));
|
|
|
|
|
let windows = Arc::new(Mutex::new(HashMap::default()));
|
2022-03-15 18:59:37 +03:00
|
|
|
|
let webview_id_map = WebviewIdStore::default();
|
2021-09-26 07:50:27 +03:00
|
|
|
|
let window_event_listeners = WindowEventListeners::default();
|
|
|
|
|
let menu_event_listeners = MenuEventListeners::default();
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
let tray_context = TrayContext::default();
|
2021-06-21 19:32:22 +03:00
|
|
|
|
|
2021-09-28 02:18:06 +03:00
|
|
|
|
let event_loop_context = Context {
|
2022-03-15 18:59:37 +03:00
|
|
|
|
webview_id_map: webview_id_map.clone(),
|
2021-06-21 19:32:22 +03:00
|
|
|
|
main_thread_id,
|
|
|
|
|
proxy,
|
2021-09-26 07:50:27 +03:00
|
|
|
|
window_event_listeners: window_event_listeners.clone(),
|
|
|
|
|
menu_event_listeners: menu_event_listeners.clone(),
|
|
|
|
|
main_thread: DispatcherMainThreadContext {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
window_target: event_loop.deref().clone(),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
web_context: web_context.clone(),
|
|
|
|
|
global_shortcut_manager: global_shortcut_manager.clone(),
|
|
|
|
|
clipboard_manager: clipboard_manager.clone(),
|
|
|
|
|
windows: windows.clone(),
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
tray_context: tray_context.clone(),
|
|
|
|
|
},
|
2021-06-21 19:32:22 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let global_shortcut_listeners = GlobalShortcutListeners::default();
|
|
|
|
|
let clipboard_manager_handle = ClipboardManagerWrapper {
|
|
|
|
|
context: event_loop_context.clone(),
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
Ok(Self {
|
2021-06-21 18:29:26 +03:00
|
|
|
|
main_thread_id,
|
2021-09-26 07:50:27 +03:00
|
|
|
|
global_shortcut_manager,
|
2021-06-21 18:29:26 +03:00
|
|
|
|
global_shortcut_manager_handle: GlobalShortcutManagerHandle {
|
2021-06-21 19:32:22 +03:00
|
|
|
|
context: event_loop_context,
|
2021-06-21 18:29:26 +03:00
|
|
|
|
shortcuts: Default::default(),
|
|
|
|
|
listeners: global_shortcut_listeners,
|
|
|
|
|
},
|
2021-09-26 07:50:27 +03:00
|
|
|
|
clipboard_manager,
|
2021-06-21 19:32:22 +03:00
|
|
|
|
clipboard_manager_handle,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
event_loop,
|
2021-09-26 07:50:27 +03:00
|
|
|
|
windows,
|
2022-03-15 18:59:37 +03:00
|
|
|
|
webview_id_map,
|
2021-09-26 07:50:27 +03:00
|
|
|
|
web_context,
|
|
|
|
|
window_event_listeners,
|
|
|
|
|
menu_event_listeners,
|
2021-05-10 19:27:42 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
2021-09-26 07:50:27 +03:00
|
|
|
|
tray_context,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
})
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
2022-02-07 17:31:07 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
impl<T: UserEvent> Runtime<T> for Wry<T> {
|
|
|
|
|
type Dispatcher = WryDispatcher<T>;
|
|
|
|
|
type Handle = WryHandle<T>;
|
|
|
|
|
type GlobalShortcutManager = GlobalShortcutManagerHandle<T>;
|
|
|
|
|
type ClipboardManager = ClipboardManagerWrapper<T>;
|
2022-02-07 17:31:07 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
2022-03-15 17:20:23 +03:00
|
|
|
|
type TrayHandler = SystemTrayHandle<T>;
|
|
|
|
|
type EventLoopProxy = EventProxy<T>;
|
2022-02-07 17:31:07 +03:00
|
|
|
|
|
|
|
|
|
fn new() -> Result<Self> {
|
2022-03-15 17:20:23 +03:00
|
|
|
|
let event_loop = EventLoop::<Message<T>>::with_user_event();
|
2022-02-07 17:31:07 +03:00
|
|
|
|
Self::init(event_loop)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(any(windows, target_os = "linux"))]
|
|
|
|
|
fn new_any_thread() -> Result<Self> {
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
use wry::application::platform::unix::EventLoopExtUnix;
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
use wry::application::platform::windows::EventLoopExtWindows;
|
2022-03-15 17:20:23 +03:00
|
|
|
|
let event_loop = EventLoop::<Message<T>>::new_any_thread();
|
2022-02-07 17:31:07 +03:00
|
|
|
|
Self::init(event_loop)
|
|
|
|
|
}
|
2021-02-08 17:19:22 +03:00
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
fn create_proxy(&self) -> EventProxy<T> {
|
|
|
|
|
EventProxy(self.event_loop.create_proxy())
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 22:49:01 +03:00
|
|
|
|
fn handle(&self) -> Self::Handle {
|
|
|
|
|
WryHandle {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
context: Context {
|
2022-03-15 18:59:37 +03:00
|
|
|
|
webview_id_map: self.webview_id_map.clone(),
|
2021-06-16 17:07:41 +03:00
|
|
|
|
main_thread_id: self.main_thread_id,
|
2021-05-18 22:49:01 +03:00
|
|
|
|
proxy: self.event_loop.create_proxy(),
|
|
|
|
|
window_event_listeners: self.window_event_listeners.clone(),
|
|
|
|
|
menu_event_listeners: self.menu_event_listeners.clone(),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
main_thread: DispatcherMainThreadContext {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
window_target: self.event_loop.deref().clone(),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
web_context: self.web_context.clone(),
|
|
|
|
|
global_shortcut_manager: self.global_shortcut_manager.clone(),
|
|
|
|
|
clipboard_manager: self.clipboard_manager.clone(),
|
|
|
|
|
windows: self.windows.clone(),
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
tray_context: self.tray_context.clone(),
|
|
|
|
|
},
|
2021-05-18 22:49:01 +03:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 18:29:26 +03:00
|
|
|
|
fn global_shortcut_manager(&self) -> Self::GlobalShortcutManager {
|
|
|
|
|
self.global_shortcut_manager_handle.clone()
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 19:32:22 +03:00
|
|
|
|
fn clipboard_manager(&self) -> Self::ClipboardManager {
|
|
|
|
|
self.clipboard_manager_handle.clone()
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
fn create_window(&self, pending: PendingWindow<T, Self>) -> Result<DetachedWindow<T, Self>> {
|
2021-04-29 01:56:05 +03:00
|
|
|
|
let label = pending.label.clone();
|
2021-10-08 17:38:24 +03:00
|
|
|
|
let menu_ids = pending.menu_ids.clone();
|
2021-12-09 18:22:12 +03:00
|
|
|
|
let js_event_listeners = pending.js_event_listeners.clone();
|
2021-04-29 01:56:05 +03:00
|
|
|
|
let proxy = self.event_loop.create_proxy();
|
2022-03-15 18:59:37 +03:00
|
|
|
|
let window_id = rand::random();
|
|
|
|
|
|
|
|
|
|
let context = Context {
|
|
|
|
|
webview_id_map: self.webview_id_map.clone(),
|
|
|
|
|
main_thread_id: self.main_thread_id,
|
|
|
|
|
proxy,
|
|
|
|
|
window_event_listeners: self.window_event_listeners.clone(),
|
|
|
|
|
menu_event_listeners: self.menu_event_listeners.clone(),
|
|
|
|
|
main_thread: DispatcherMainThreadContext {
|
|
|
|
|
window_target: self.event_loop.deref().clone(),
|
|
|
|
|
web_context: self.web_context.clone(),
|
|
|
|
|
global_shortcut_manager: self.global_shortcut_manager.clone(),
|
|
|
|
|
clipboard_manager: self.clipboard_manager.clone(),
|
|
|
|
|
windows: self.windows.clone(),
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
tray_context: self.tray_context.clone(),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
context.prepare_window(window_id);
|
|
|
|
|
|
2021-05-04 04:06:50 +03:00
|
|
|
|
let webview = create_webview(
|
2022-03-15 18:59:37 +03:00
|
|
|
|
window_id,
|
2021-05-04 04:06:50 +03:00
|
|
|
|
&self.event_loop,
|
2021-08-02 05:54:10 +03:00
|
|
|
|
&self.web_context,
|
2022-03-15 18:59:37 +03:00
|
|
|
|
context.clone(),
|
2021-05-04 04:06:50 +03:00
|
|
|
|
pending,
|
|
|
|
|
)?;
|
2021-04-04 03:41:04 +03:00
|
|
|
|
|
2022-03-15 18:59:37 +03:00
|
|
|
|
let dispatcher = WryDispatcher { window_id, context };
|
2021-07-23 16:31:17 +03:00
|
|
|
|
|
2022-03-15 18:59:37 +03:00
|
|
|
|
self.windows.lock().unwrap().insert(window_id, webview);
|
2021-04-29 01:56:05 +03:00
|
|
|
|
|
2021-10-08 17:38:24 +03:00
|
|
|
|
Ok(DetachedWindow {
|
|
|
|
|
label,
|
|
|
|
|
dispatcher,
|
|
|
|
|
menu_ids,
|
2021-12-09 18:22:12 +03:00
|
|
|
|
js_event_listeners,
|
2021-10-08 17:38:24 +03:00
|
|
|
|
})
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 14:38:08 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
2021-07-15 13:05:29 +03:00
|
|
|
|
fn system_tray(&self, system_tray: SystemTray) -> Result<Self::TrayHandler> {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
let icon = system_tray
|
|
|
|
|
.icon
|
|
|
|
|
.expect("tray icon not set")
|
2022-03-05 20:19:24 +03:00
|
|
|
|
.into_platform_icon();
|
2021-05-11 14:38:08 +03:00
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
let mut items = HashMap::new();
|
2021-05-09 14:15:37 +03:00
|
|
|
|
|
2021-07-29 22:29:59 +03:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
let tray = SystemTrayBuilder::new(
|
|
|
|
|
icon,
|
|
|
|
|
system_tray
|
|
|
|
|
.menu
|
|
|
|
|
.map(|menu| to_wry_context_menu(&mut items, menu)),
|
|
|
|
|
)
|
|
|
|
|
.with_icon_as_template(system_tray.icon_as_template)
|
|
|
|
|
.build(&self.event_loop)
|
|
|
|
|
.map_err(|e| Error::SystemTray(Box::new(e)))?;
|
|
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
2021-06-04 19:51:15 +03:00
|
|
|
|
let tray = SystemTrayBuilder::new(
|
2021-05-10 00:43:50 +03:00
|
|
|
|
icon,
|
2021-06-04 19:51:15 +03:00
|
|
|
|
system_tray
|
|
|
|
|
.menu
|
|
|
|
|
.map(|menu| to_wry_context_menu(&mut items, menu)),
|
2021-05-10 00:43:50 +03:00
|
|
|
|
)
|
|
|
|
|
.build(&self.event_loop)
|
|
|
|
|
.map_err(|e| Error::SystemTray(Box::new(e)))?;
|
2021-06-04 19:51:15 +03:00
|
|
|
|
|
|
|
|
|
*self.tray_context.items.lock().unwrap() = items;
|
|
|
|
|
*self.tray_context.tray.lock().unwrap() = Some(Arc::new(Mutex::new(tray)));
|
|
|
|
|
|
|
|
|
|
Ok(SystemTrayHandle {
|
|
|
|
|
proxy: self.event_loop.create_proxy(),
|
|
|
|
|
})
|
2021-05-09 14:15:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 19:27:42 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
2021-05-09 14:15:37 +03:00
|
|
|
|
fn on_system_tray_event<F: Fn(&SystemTrayEvent) + Send + 'static>(&mut self, f: F) -> Uuid {
|
|
|
|
|
let id = Uuid::new_v4();
|
2021-05-21 22:16:05 +03:00
|
|
|
|
self
|
2021-06-04 19:51:15 +03:00
|
|
|
|
.tray_context
|
|
|
|
|
.listeners
|
2021-05-21 22:16:05 +03:00
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
2022-03-25 19:17:32 +03:00
|
|
|
|
.insert(id, Arc::new(Box::new(f)));
|
2021-05-09 14:15:37 +03:00
|
|
|
|
id
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 19:25:31 +03:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {
|
|
|
|
|
self
|
|
|
|
|
.event_loop
|
|
|
|
|
.set_activation_policy(match activation_policy {
|
|
|
|
|
ActivationPolicy::Regular => WryActivationPolicy::Regular,
|
|
|
|
|
ActivationPolicy::Accessory => WryActivationPolicy::Accessory,
|
|
|
|
|
ActivationPolicy::Prohibited => WryActivationPolicy::Prohibited,
|
|
|
|
|
_ => unimplemented!(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
fn run_iteration<F: FnMut(RunEvent<T>) + 'static>(&mut self, mut callback: F) -> RunIteration {
|
2021-05-21 22:16:05 +03:00
|
|
|
|
use wry::application::platform::run_return::EventLoopExtRunReturn;
|
2021-07-29 20:35:26 +03:00
|
|
|
|
let windows = self.windows.clone();
|
2022-03-15 18:59:37 +03:00
|
|
|
|
let webview_id_map = self.webview_id_map.clone();
|
2021-08-02 05:54:10 +03:00
|
|
|
|
let web_context = &self.web_context;
|
2021-05-21 22:16:05 +03:00
|
|
|
|
let window_event_listeners = self.window_event_listeners.clone();
|
|
|
|
|
let menu_event_listeners = self.menu_event_listeners.clone();
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
2021-06-04 19:51:15 +03:00
|
|
|
|
let tray_context = self.tray_context.clone();
|
2021-06-21 18:29:26 +03:00
|
|
|
|
let global_shortcut_manager = self.global_shortcut_manager.clone();
|
|
|
|
|
let global_shortcut_manager_handle = self.global_shortcut_manager_handle.clone();
|
2021-06-21 19:32:22 +03:00
|
|
|
|
let clipboard_manager = self.clipboard_manager.clone();
|
2021-05-21 22:16:05 +03:00
|
|
|
|
let mut iteration = RunIteration::default();
|
2022-01-05 17:39:43 +03:00
|
|
|
|
|
2021-05-21 22:16:05 +03:00
|
|
|
|
self
|
|
|
|
|
.event_loop
|
|
|
|
|
.run_return(|event, event_loop, control_flow| {
|
2021-10-18 19:34:06 +03:00
|
|
|
|
*control_flow = ControlFlow::Wait;
|
2021-05-21 22:16:05 +03:00
|
|
|
|
if let Event::MainEventsCleared = &event {
|
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
|
}
|
2022-01-05 17:39:43 +03:00
|
|
|
|
|
2021-05-21 22:16:05 +03:00
|
|
|
|
iteration = handle_event_loop(
|
|
|
|
|
event,
|
|
|
|
|
event_loop,
|
|
|
|
|
control_flow,
|
|
|
|
|
EventLoopIterationContext {
|
2021-09-27 22:27:37 +03:00
|
|
|
|
callback: &mut callback,
|
2021-09-26 07:50:27 +03:00
|
|
|
|
windows: windows.clone(),
|
2022-03-15 18:59:37 +03:00
|
|
|
|
webview_id_map: webview_id_map.clone(),
|
2021-06-30 16:38:22 +03:00
|
|
|
|
window_event_listeners: &window_event_listeners,
|
2021-06-21 18:29:26 +03:00
|
|
|
|
global_shortcut_manager: global_shortcut_manager.clone(),
|
2021-06-30 16:38:22 +03:00
|
|
|
|
global_shortcut_manager_handle: &global_shortcut_manager_handle,
|
2021-06-21 19:32:22 +03:00
|
|
|
|
clipboard_manager: clipboard_manager.clone(),
|
2021-06-30 16:38:22 +03:00
|
|
|
|
menu_event_listeners: &menu_event_listeners,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
2021-06-30 16:38:22 +03:00
|
|
|
|
tray_context: &tray_context,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
},
|
2021-08-02 05:54:10 +03:00
|
|
|
|
web_context,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
iteration
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
fn run<F: FnMut(RunEvent<T>) + 'static>(self, mut callback: F) {
|
2021-07-29 20:35:26 +03:00
|
|
|
|
let windows = self.windows.clone();
|
2022-03-15 18:59:37 +03:00
|
|
|
|
let webview_id_map = self.webview_id_map.clone();
|
2021-08-02 05:54:10 +03:00
|
|
|
|
let web_context = self.web_context;
|
2021-05-06 06:43:02 +03:00
|
|
|
|
let window_event_listeners = self.window_event_listeners.clone();
|
2021-05-08 18:11:40 +03:00
|
|
|
|
let menu_event_listeners = self.menu_event_listeners.clone();
|
2021-05-10 19:27:42 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
2021-06-04 19:51:15 +03:00
|
|
|
|
let tray_context = self.tray_context;
|
2021-06-21 18:29:26 +03:00
|
|
|
|
let global_shortcut_manager = self.global_shortcut_manager.clone();
|
|
|
|
|
let global_shortcut_manager_handle = self.global_shortcut_manager_handle.clone();
|
2021-06-21 19:32:22 +03:00
|
|
|
|
let clipboard_manager = self.clipboard_manager.clone();
|
2021-05-10 19:27:42 +03:00
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
self.event_loop.run(move |event, event_loop, control_flow| {
|
2021-05-21 22:16:05 +03:00
|
|
|
|
handle_event_loop(
|
|
|
|
|
event,
|
|
|
|
|
event_loop,
|
|
|
|
|
control_flow,
|
|
|
|
|
EventLoopIterationContext {
|
2021-09-27 22:27:37 +03:00
|
|
|
|
callback: &mut callback,
|
2022-03-15 18:59:37 +03:00
|
|
|
|
webview_id_map: webview_id_map.clone(),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
windows: windows.clone(),
|
2021-06-30 16:38:22 +03:00
|
|
|
|
window_event_listeners: &window_event_listeners,
|
2021-06-21 18:29:26 +03:00
|
|
|
|
global_shortcut_manager: global_shortcut_manager.clone(),
|
2021-06-30 16:38:22 +03:00
|
|
|
|
global_shortcut_manager_handle: &global_shortcut_manager_handle,
|
2021-06-21 19:32:22 +03:00
|
|
|
|
clipboard_manager: clipboard_manager.clone(),
|
2021-06-30 16:38:22 +03:00
|
|
|
|
menu_event_listeners: &menu_event_listeners,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
2021-06-30 16:38:22 +03:00
|
|
|
|
tray_context: &tray_context,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
},
|
2021-08-02 05:54:10 +03:00
|
|
|
|
&web_context,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 01:56:05 +03:00
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
pub struct EventLoopIterationContext<'a, T: UserEvent> {
|
|
|
|
|
callback: &'a mut (dyn FnMut(RunEvent<T>) + 'static),
|
2022-03-15 18:59:37 +03:00
|
|
|
|
webview_id_map: WebviewIdStore,
|
|
|
|
|
windows: Arc<Mutex<HashMap<WebviewId, WindowWrapper>>>,
|
2021-06-30 16:38:22 +03:00
|
|
|
|
window_event_listeners: &'a WindowEventListeners,
|
2021-06-21 18:29:26 +03:00
|
|
|
|
global_shortcut_manager: Arc<Mutex<WryShortcutManager>>,
|
2022-03-15 17:20:23 +03:00
|
|
|
|
global_shortcut_manager_handle: &'a GlobalShortcutManagerHandle<T>,
|
2021-06-21 19:32:22 +03:00
|
|
|
|
clipboard_manager: Arc<Mutex<Clipboard>>,
|
2021-06-30 16:38:22 +03:00
|
|
|
|
menu_event_listeners: &'a MenuEventListeners,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
2021-06-30 16:38:22 +03:00
|
|
|
|
tray_context: &'a TrayContext,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
}
|
2021-04-29 01:56:05 +03:00
|
|
|
|
|
2021-09-26 07:50:27 +03:00
|
|
|
|
struct UserMessageContext<'a> {
|
|
|
|
|
window_event_listeners: &'a WindowEventListeners,
|
|
|
|
|
global_shortcut_manager: Arc<Mutex<WryShortcutManager>>,
|
|
|
|
|
clipboard_manager: Arc<Mutex<Clipboard>>,
|
|
|
|
|
menu_event_listeners: &'a MenuEventListeners,
|
2022-03-15 18:59:37 +03:00
|
|
|
|
windows: Arc<Mutex<HashMap<WebviewId, WindowWrapper>>>,
|
2021-09-26 07:50:27 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
tray_context: &'a TrayContext,
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
fn handle_user_message<T: UserEvent>(
|
|
|
|
|
event_loop: &EventLoopWindowTarget<Message<T>>,
|
|
|
|
|
message: Message<T>,
|
2021-09-26 07:50:27 +03:00
|
|
|
|
context: UserMessageContext<'_>,
|
|
|
|
|
web_context: &WebContextStore,
|
|
|
|
|
) -> RunIteration {
|
|
|
|
|
let UserMessageContext {
|
|
|
|
|
window_event_listeners,
|
|
|
|
|
menu_event_listeners,
|
|
|
|
|
global_shortcut_manager,
|
|
|
|
|
clipboard_manager,
|
|
|
|
|
windows,
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
tray_context,
|
|
|
|
|
} = context;
|
|
|
|
|
match message {
|
|
|
|
|
Message::Task(task) => task(),
|
|
|
|
|
Message::Window(id, window_message) => {
|
2021-09-26 07:48:13 +03:00
|
|
|
|
if let Some(webview) = windows
|
|
|
|
|
.lock()
|
|
|
|
|
.expect("poisoned webview collection")
|
|
|
|
|
.get_mut(&id)
|
|
|
|
|
{
|
2021-09-26 07:50:27 +03:00
|
|
|
|
let window = webview.inner.window();
|
|
|
|
|
match window_message {
|
2022-02-07 16:04:33 +03:00
|
|
|
|
#[cfg(any(debug_assertions, feature = "devtools"))]
|
|
|
|
|
WindowMessage::OpenDevTools => {
|
|
|
|
|
if let WindowHandle::Webview(w) = &webview.inner {
|
2022-03-28 16:20:38 +03:00
|
|
|
|
w.open_devtools();
|
2022-02-07 16:04:33 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-26 07:50:27 +03:00
|
|
|
|
// Getters
|
|
|
|
|
WindowMessage::ScaleFactor(tx) => tx.send(window.scale_factor()).unwrap(),
|
|
|
|
|
WindowMessage::InnerPosition(tx) => tx
|
|
|
|
|
.send(
|
|
|
|
|
window
|
|
|
|
|
.inner_position()
|
|
|
|
|
.map(|p| PhysicalPositionWrapper(p).into())
|
|
|
|
|
.map_err(|_| Error::FailedToSendMessage),
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
WindowMessage::OuterPosition(tx) => tx
|
|
|
|
|
.send(
|
|
|
|
|
window
|
|
|
|
|
.outer_position()
|
|
|
|
|
.map(|p| PhysicalPositionWrapper(p).into())
|
|
|
|
|
.map_err(|_| Error::FailedToSendMessage),
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
WindowMessage::InnerSize(tx) => tx
|
2021-09-30 02:52:05 +03:00
|
|
|
|
.send(PhysicalSizeWrapper(webview.inner.inner_size()).into())
|
2021-09-26 07:50:27 +03:00
|
|
|
|
.unwrap(),
|
|
|
|
|
WindowMessage::OuterSize(tx) => tx
|
|
|
|
|
.send(PhysicalSizeWrapper(window.outer_size()).into())
|
|
|
|
|
.unwrap(),
|
|
|
|
|
WindowMessage::IsFullscreen(tx) => tx.send(window.fullscreen().is_some()).unwrap(),
|
|
|
|
|
WindowMessage::IsMaximized(tx) => tx.send(window.is_maximized()).unwrap(),
|
|
|
|
|
WindowMessage::IsDecorated(tx) => tx.send(window.is_decorated()).unwrap(),
|
|
|
|
|
WindowMessage::IsResizable(tx) => tx.send(window.is_resizable()).unwrap(),
|
|
|
|
|
WindowMessage::IsVisible(tx) => tx.send(window.is_visible()).unwrap(),
|
|
|
|
|
WindowMessage::IsMenuVisible(tx) => tx.send(window.is_menu_visible()).unwrap(),
|
|
|
|
|
WindowMessage::CurrentMonitor(tx) => tx.send(window.current_monitor()).unwrap(),
|
|
|
|
|
WindowMessage::PrimaryMonitor(tx) => tx.send(window.primary_monitor()).unwrap(),
|
|
|
|
|
WindowMessage::AvailableMonitors(tx) => {
|
|
|
|
|
tx.send(window.available_monitors().collect()).unwrap()
|
|
|
|
|
}
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
WindowMessage::NSWindow(tx) => tx.send(NSWindow(window.ns_window())).unwrap(),
|
|
|
|
|
#[cfg(windows)]
|
2022-01-15 19:20:30 +03:00
|
|
|
|
WindowMessage::Hwnd(tx) => tx.send(Hwnd(HWND(window.hwnd() as _))).unwrap(),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
#[cfg(any(
|
|
|
|
|
target_os = "linux",
|
|
|
|
|
target_os = "dragonfly",
|
|
|
|
|
target_os = "freebsd",
|
|
|
|
|
target_os = "netbsd",
|
|
|
|
|
target_os = "openbsd"
|
|
|
|
|
))]
|
|
|
|
|
WindowMessage::GtkWindow(tx) => tx.send(GtkWindow(window.gtk_window().clone())).unwrap(),
|
|
|
|
|
// Setters
|
|
|
|
|
WindowMessage::Center(tx) => {
|
2021-09-30 02:52:05 +03:00
|
|
|
|
tx.send(center_window(window, webview.inner.inner_size()))
|
|
|
|
|
.unwrap();
|
2021-09-26 07:50:27 +03:00
|
|
|
|
}
|
|
|
|
|
WindowMessage::RequestUserAttention(request_type) => {
|
|
|
|
|
window.request_user_attention(request_type.map(|r| r.0));
|
|
|
|
|
}
|
|
|
|
|
WindowMessage::SetResizable(resizable) => window.set_resizable(resizable),
|
|
|
|
|
WindowMessage::SetTitle(title) => window.set_title(&title),
|
|
|
|
|
WindowMessage::Maximize => window.set_maximized(true),
|
|
|
|
|
WindowMessage::Unmaximize => window.set_maximized(false),
|
|
|
|
|
WindowMessage::Minimize => window.set_minimized(true),
|
|
|
|
|
WindowMessage::Unminimize => window.set_minimized(false),
|
|
|
|
|
WindowMessage::ShowMenu => window.show_menu(),
|
|
|
|
|
WindowMessage::HideMenu => window.hide_menu(),
|
|
|
|
|
WindowMessage::Show => window.set_visible(true),
|
|
|
|
|
WindowMessage::Hide => window.set_visible(false),
|
2021-09-28 02:18:06 +03:00
|
|
|
|
WindowMessage::Close => panic!("cannot handle `WindowMessage::Close` on the main thread"),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
WindowMessage::SetDecorations(decorations) => window.set_decorations(decorations),
|
|
|
|
|
WindowMessage::SetAlwaysOnTop(always_on_top) => window.set_always_on_top(always_on_top),
|
|
|
|
|
WindowMessage::SetSize(size) => {
|
|
|
|
|
window.set_inner_size(SizeWrapper::from(size).0);
|
|
|
|
|
}
|
|
|
|
|
WindowMessage::SetMinSize(size) => {
|
|
|
|
|
window.set_min_inner_size(size.map(|s| SizeWrapper::from(s).0));
|
|
|
|
|
}
|
|
|
|
|
WindowMessage::SetMaxSize(size) => {
|
|
|
|
|
window.set_max_inner_size(size.map(|s| SizeWrapper::from(s).0));
|
|
|
|
|
}
|
|
|
|
|
WindowMessage::SetPosition(position) => {
|
|
|
|
|
window.set_outer_position(PositionWrapper::from(position).0)
|
|
|
|
|
}
|
|
|
|
|
WindowMessage::SetFullscreen(fullscreen) => {
|
|
|
|
|
if fullscreen {
|
|
|
|
|
window.set_fullscreen(Some(Fullscreen::Borderless(None)))
|
|
|
|
|
} else {
|
|
|
|
|
window.set_fullscreen(None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
WindowMessage::SetFocus => {
|
|
|
|
|
window.set_focus();
|
|
|
|
|
}
|
|
|
|
|
WindowMessage::SetIcon(icon) => {
|
|
|
|
|
window.set_window_icon(Some(icon));
|
|
|
|
|
}
|
|
|
|
|
WindowMessage::SetSkipTaskbar(_skip) => {
|
2022-03-15 18:59:37 +03:00
|
|
|
|
#[cfg(any(windows, target_os = "linux"))]
|
2021-09-26 07:50:27 +03:00
|
|
|
|
window.set_skip_taskbar(_skip);
|
|
|
|
|
}
|
|
|
|
|
WindowMessage::DragWindow => {
|
|
|
|
|
let _ = window.drag_window();
|
|
|
|
|
}
|
|
|
|
|
WindowMessage::UpdateMenuItem(id, update) => {
|
2021-10-08 17:38:24 +03:00
|
|
|
|
if let Some(menu_items) = webview.menu_items.as_mut() {
|
|
|
|
|
let item = menu_items.get_mut(&id).expect("menu item not found");
|
|
|
|
|
match update {
|
|
|
|
|
MenuUpdate::SetEnabled(enabled) => item.set_enabled(enabled),
|
|
|
|
|
MenuUpdate::SetTitle(title) => item.set_title(&title),
|
|
|
|
|
MenuUpdate::SetSelected(selected) => item.set_selected(selected),
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
MenuUpdate::SetNativeImage(image) => {
|
|
|
|
|
item.set_native_image(NativeImageWrapper::from(image).0)
|
|
|
|
|
}
|
2021-09-26 07:50:27 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-05 12:56:55 +03:00
|
|
|
|
WindowMessage::RequestRedraw => {
|
|
|
|
|
window.request_redraw();
|
|
|
|
|
}
|
2021-09-26 07:50:27 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-26 16:01:03 +03:00
|
|
|
|
Message::Webview(id, webview_message) => match webview_message {
|
|
|
|
|
WebviewMessage::EvaluateScript(script) => {
|
|
|
|
|
if let Some(WindowHandle::Webview(webview)) = windows
|
|
|
|
|
.lock()
|
|
|
|
|
.expect("poisoned webview collection")
|
|
|
|
|
.get(&id)
|
|
|
|
|
.map(|w| &w.inner)
|
|
|
|
|
{
|
|
|
|
|
if let Err(e) = webview.evaluate_script(&script) {
|
2021-10-23 20:31:01 +03:00
|
|
|
|
#[cfg(debug_assertions)]
|
2021-09-26 16:01:03 +03:00
|
|
|
|
eprintln!("{}", e);
|
2021-09-26 07:50:27 +03:00
|
|
|
|
}
|
2021-09-26 16:01:03 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
WebviewMessage::Print => {
|
|
|
|
|
if let Some(WindowHandle::Webview(webview)) = windows
|
|
|
|
|
.lock()
|
|
|
|
|
.expect("poisoned webview collection")
|
|
|
|
|
.get(&id)
|
|
|
|
|
.map(|w| &w.inner)
|
|
|
|
|
{
|
|
|
|
|
let _ = webview.print();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
WebviewMessage::WebviewEvent(event) => {
|
|
|
|
|
if let Some(event) = WindowEventWrapper::from(&event).0 {
|
|
|
|
|
for handler in window_event_listeners
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.get(&id)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.values()
|
|
|
|
|
{
|
|
|
|
|
handler(&event);
|
2021-09-26 07:50:27 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-26 16:01:03 +03:00
|
|
|
|
},
|
2022-03-15 18:59:37 +03:00
|
|
|
|
Message::CreateWebview(window_id, handler) => match handler(event_loop, web_context) {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
Ok(webview) => {
|
|
|
|
|
windows
|
|
|
|
|
.lock()
|
|
|
|
|
.expect("poisoned webview collection")
|
|
|
|
|
.insert(window_id, webview);
|
2021-09-26 07:50:27 +03:00
|
|
|
|
}
|
2021-09-28 02:18:06 +03:00
|
|
|
|
Err(e) => {
|
2021-10-23 20:31:01 +03:00
|
|
|
|
#[cfg(debug_assertions)]
|
2021-09-28 02:18:06 +03:00
|
|
|
|
eprintln!("{}", e);
|
|
|
|
|
}
|
|
|
|
|
},
|
2022-03-15 18:59:37 +03:00
|
|
|
|
Message::CreateWindow(window_id, handler, sender) => {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
let (label, builder) = handler();
|
|
|
|
|
if let Ok(window) = builder.build(event_loop) {
|
|
|
|
|
window_event_listeners
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
2022-03-15 18:59:37 +03:00
|
|
|
|
.insert(window_id, WindowEventListenersMap::default());
|
2021-09-26 07:50:27 +03:00
|
|
|
|
|
2021-09-28 02:18:06 +03:00
|
|
|
|
menu_event_listeners
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
2022-03-15 18:59:37 +03:00
|
|
|
|
.insert(window_id, WindowMenuEventListeners::default());
|
2021-09-26 07:50:27 +03:00
|
|
|
|
|
2021-09-28 02:18:06 +03:00
|
|
|
|
let w = Arc::new(window);
|
2021-09-26 07:50:27 +03:00
|
|
|
|
|
2021-09-28 02:18:06 +03:00
|
|
|
|
windows.lock().expect("poisoned webview collection").insert(
|
|
|
|
|
window_id,
|
|
|
|
|
WindowWrapper {
|
|
|
|
|
label,
|
|
|
|
|
inner: WindowHandle::Window(w.clone()),
|
|
|
|
|
menu_items: Default::default(),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
sender.send(Ok(Arc::downgrade(&w))).unwrap();
|
|
|
|
|
} else {
|
|
|
|
|
sender.send(Err(Error::CreateWindow)).unwrap();
|
2021-09-26 07:50:27 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-05 12:56:55 +03:00
|
|
|
|
|
2021-09-26 07:50:27 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
Message::Tray(tray_message) => match tray_message {
|
|
|
|
|
TrayMessage::UpdateItem(menu_id, update) => {
|
|
|
|
|
let mut tray = tray_context.items.as_ref().lock().unwrap();
|
|
|
|
|
let item = tray.get_mut(&menu_id).expect("menu item not found");
|
|
|
|
|
match update {
|
|
|
|
|
MenuUpdate::SetEnabled(enabled) => item.set_enabled(enabled),
|
|
|
|
|
MenuUpdate::SetTitle(title) => item.set_title(&title),
|
|
|
|
|
MenuUpdate::SetSelected(selected) => item.set_selected(selected),
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
MenuUpdate::SetNativeImage(image) => {
|
|
|
|
|
item.set_native_image(NativeImageWrapper::from(image).0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-02 21:57:53 +03:00
|
|
|
|
TrayMessage::UpdateMenu(menu) => {
|
|
|
|
|
if let Some(tray) = &*tray_context.tray.lock().unwrap() {
|
|
|
|
|
let mut items = HashMap::new();
|
|
|
|
|
tray
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.set_menu(&to_wry_context_menu(&mut items, menu));
|
|
|
|
|
*tray_context.items.lock().unwrap() = items;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-26 07:50:27 +03:00
|
|
|
|
TrayMessage::UpdateIcon(icon) => {
|
|
|
|
|
if let Some(tray) = &*tray_context.tray.lock().unwrap() {
|
2022-03-05 20:19:24 +03:00
|
|
|
|
tray.lock().unwrap().set_icon(icon.into_platform_icon());
|
2021-09-26 07:50:27 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
TrayMessage::UpdateIconAsTemplate(is_template) => {
|
|
|
|
|
if let Some(tray) = &*tray_context.tray.lock().unwrap() {
|
|
|
|
|
tray.lock().unwrap().set_icon_as_template(is_template);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-07 19:04:38 +03:00
|
|
|
|
TrayMessage::Close => {
|
|
|
|
|
*tray_context.tray.lock().unwrap() = None;
|
|
|
|
|
tray_context.listeners.lock().unwrap().clear();
|
|
|
|
|
tray_context.items.lock().unwrap().clear();
|
|
|
|
|
}
|
2021-09-26 07:50:27 +03:00
|
|
|
|
},
|
|
|
|
|
Message::GlobalShortcut(message) => match message {
|
|
|
|
|
GlobalShortcutMessage::IsRegistered(accelerator, tx) => tx
|
|
|
|
|
.send(
|
|
|
|
|
global_shortcut_manager
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.is_registered(&accelerator),
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
GlobalShortcutMessage::Register(accelerator, tx) => tx
|
|
|
|
|
.send(
|
|
|
|
|
global_shortcut_manager
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.register(accelerator)
|
|
|
|
|
.map(GlobalShortcutWrapper)
|
|
|
|
|
.map_err(|e| Error::GlobalShortcut(Box::new(e))),
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
GlobalShortcutMessage::Unregister(shortcut, tx) => tx
|
|
|
|
|
.send(
|
|
|
|
|
global_shortcut_manager
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.unregister(shortcut.0)
|
|
|
|
|
.map_err(|e| Error::GlobalShortcut(Box::new(e))),
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
GlobalShortcutMessage::UnregisterAll(tx) => tx
|
|
|
|
|
.send(
|
|
|
|
|
global_shortcut_manager
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.unregister_all()
|
|
|
|
|
.map_err(|e| Error::GlobalShortcut(Box::new(e))),
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
},
|
|
|
|
|
Message::Clipboard(message) => match message {
|
|
|
|
|
ClipboardMessage::WriteText(text, tx) => {
|
|
|
|
|
clipboard_manager.lock().unwrap().write_text(text);
|
|
|
|
|
tx.send(()).unwrap();
|
|
|
|
|
}
|
|
|
|
|
ClipboardMessage::ReadText(tx) => tx
|
|
|
|
|
.send(clipboard_manager.lock().unwrap().read_text())
|
|
|
|
|
.unwrap(),
|
|
|
|
|
},
|
2022-03-15 17:20:23 +03:00
|
|
|
|
Message::UserEvent(_) => (),
|
2021-09-26 07:50:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let it = RunIteration {
|
|
|
|
|
window_count: windows.lock().expect("poisoned webview collection").len(),
|
|
|
|
|
};
|
|
|
|
|
it
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
fn handle_event_loop<T: UserEvent>(
|
|
|
|
|
event: Event<'_, Message<T>>,
|
|
|
|
|
event_loop: &EventLoopWindowTarget<Message<T>>,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
control_flow: &mut ControlFlow,
|
2022-03-15 17:20:23 +03:00
|
|
|
|
context: EventLoopIterationContext<'_, T>,
|
2021-08-02 05:54:10 +03:00
|
|
|
|
web_context: &WebContextStore,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
) -> RunIteration {
|
|
|
|
|
let EventLoopIterationContext {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
callback,
|
2022-03-15 18:59:37 +03:00
|
|
|
|
webview_id_map,
|
2021-09-26 07:50:27 +03:00
|
|
|
|
windows,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
window_event_listeners,
|
2021-06-21 18:29:26 +03:00
|
|
|
|
global_shortcut_manager,
|
|
|
|
|
global_shortcut_manager_handle,
|
2021-06-21 19:32:22 +03:00
|
|
|
|
clipboard_manager,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
menu_event_listeners,
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
2021-06-04 19:51:15 +03:00
|
|
|
|
tray_context,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
} = context;
|
2022-03-28 02:22:34 +03:00
|
|
|
|
if *control_flow != ControlFlow::Exit {
|
|
|
|
|
*control_flow = ControlFlow::Wait;
|
2021-07-06 19:36:37 +03:00
|
|
|
|
}
|
2022-01-05 12:56:55 +03:00
|
|
|
|
|
2021-05-21 22:16:05 +03:00
|
|
|
|
match event {
|
2021-08-15 23:10:22 +03:00
|
|
|
|
Event::NewEvents(StartCause::Init) => {
|
|
|
|
|
callback(RunEvent::Ready);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-16 00:14:26 +03:00
|
|
|
|
Event::NewEvents(StartCause::Poll) => {
|
|
|
|
|
callback(RunEvent::Resumed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Event::MainEventsCleared => {
|
|
|
|
|
callback(RunEvent::MainEventsCleared);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-28 02:22:34 +03:00
|
|
|
|
Event::LoopDestroyed => {
|
|
|
|
|
callback(RunEvent::Exit);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 18:29:26 +03:00
|
|
|
|
Event::GlobalShortcutEvent(accelerator_id) => {
|
|
|
|
|
for (id, handler) in &*global_shortcut_manager_handle.listeners.lock().unwrap() {
|
|
|
|
|
if accelerator_id == *id {
|
|
|
|
|
handler();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
Event::MenuEvent {
|
2021-06-23 17:29:30 +03:00
|
|
|
|
window_id,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
menu_id,
|
2021-06-04 19:51:15 +03:00
|
|
|
|
origin: MenuType::MenuBar,
|
2021-07-15 18:47:19 +03:00
|
|
|
|
..
|
2021-05-21 22:16:05 +03:00
|
|
|
|
} => {
|
2021-06-23 17:29:30 +03:00
|
|
|
|
let window_id = window_id.unwrap(); // always Some on MenuBar event
|
2021-05-21 22:16:05 +03:00
|
|
|
|
let event = MenuEvent {
|
|
|
|
|
menu_item_id: menu_id.0,
|
|
|
|
|
};
|
2021-12-29 04:51:33 +03:00
|
|
|
|
let window_menu_event_listeners = {
|
2022-03-15 18:59:37 +03:00
|
|
|
|
let window_id = webview_id_map.get(&window_id);
|
2021-12-29 04:51:33 +03:00
|
|
|
|
let listeners = menu_event_listeners.lock().unwrap();
|
|
|
|
|
listeners.get(&window_id).cloned().unwrap_or_default()
|
|
|
|
|
};
|
2021-06-23 17:29:30 +03:00
|
|
|
|
for handler in window_menu_event_listeners.lock().unwrap().values() {
|
2021-05-21 22:16:05 +03:00
|
|
|
|
handler(&event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
Event::MenuEvent {
|
2021-06-23 17:29:30 +03:00
|
|
|
|
window_id: _,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
menu_id,
|
2021-06-04 19:51:15 +03:00
|
|
|
|
origin: MenuType::ContextMenu,
|
2021-07-15 18:47:19 +03:00
|
|
|
|
..
|
2021-05-21 22:16:05 +03:00
|
|
|
|
} => {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
let event = SystemTrayEvent::MenuItemClick(menu_id.0);
|
2022-03-25 19:17:32 +03:00
|
|
|
|
let listeners = tray_context.listeners.lock().unwrap().clone();
|
|
|
|
|
for handler in listeners.values() {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
handler(&event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
Event::TrayEvent {
|
|
|
|
|
bounds,
|
|
|
|
|
event,
|
|
|
|
|
position: _cursor_position,
|
2021-07-15 18:47:19 +03:00
|
|
|
|
..
|
2021-06-04 19:51:15 +03:00
|
|
|
|
} => {
|
|
|
|
|
let (position, size) = (
|
|
|
|
|
PhysicalPositionWrapper(bounds.position).into(),
|
|
|
|
|
PhysicalSizeWrapper(bounds.size).into(),
|
|
|
|
|
);
|
|
|
|
|
let event = match event {
|
|
|
|
|
TrayEvent::RightClick => SystemTrayEvent::RightClick { position, size },
|
|
|
|
|
TrayEvent::DoubleClick => SystemTrayEvent::DoubleClick { position, size },
|
2021-07-15 18:47:19 +03:00
|
|
|
|
// default to left click
|
|
|
|
|
_ => SystemTrayEvent::LeftClick { position, size },
|
2021-05-21 22:16:05 +03:00
|
|
|
|
};
|
2021-06-04 19:51:15 +03:00
|
|
|
|
for handler in tray_context.listeners.lock().unwrap().values() {
|
2021-05-21 22:16:05 +03:00
|
|
|
|
handler(&event);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-15 18:47:19 +03:00
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
event, window_id, ..
|
|
|
|
|
} => {
|
2022-03-15 18:59:37 +03:00
|
|
|
|
let window_id = webview_id_map.get(&window_id);
|
2021-08-29 15:12:45 +03:00
|
|
|
|
// NOTE(amrbashir): we handle this event here instead of `match` statement below because
|
|
|
|
|
// we want to focus the webview as soon as possible, especially on windows.
|
2021-07-23 16:31:17 +03:00
|
|
|
|
if event == WryWindowEvent::Focused(true) {
|
2021-09-26 07:50:27 +03:00
|
|
|
|
if let Some(WindowHandle::Webview(webview)) = windows
|
|
|
|
|
.lock()
|
|
|
|
|
.expect("poisoned webview collection")
|
|
|
|
|
.get(&window_id)
|
|
|
|
|
.map(|w| &w.inner)
|
|
|
|
|
{
|
2022-03-07 00:44:12 +03:00
|
|
|
|
// only focus the webview if the window is visible
|
|
|
|
|
// somehow tao is sending a Focused(true) event even when the window is invisible,
|
|
|
|
|
// which causes a deadlock: https://github.com/tauri-apps/tauri/issues/3534
|
|
|
|
|
if webview.window().is_visible() {
|
|
|
|
|
webview.focus();
|
|
|
|
|
}
|
2021-07-29 20:35:26 +03:00
|
|
|
|
}
|
2021-07-23 16:31:17 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 02:52:05 +03:00
|
|
|
|
{
|
|
|
|
|
let windows_lock = windows.lock().expect("poisoned webview collection");
|
|
|
|
|
if let Some(window_handle) = windows_lock.get(&window_id).map(|w| &w.inner) {
|
|
|
|
|
if let Some(event) = WindowEventWrapper::parse(window_handle, &event).0 {
|
|
|
|
|
drop(windows_lock);
|
|
|
|
|
for handler in window_event_listeners
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.get(&window_id)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.values()
|
|
|
|
|
{
|
|
|
|
|
handler(&event);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-30 02:52:05 +03:00
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
match event {
|
2021-05-21 22:16:05 +03:00
|
|
|
|
WryWindowEvent::CloseRequested => {
|
2022-01-15 18:23:26 +03:00
|
|
|
|
on_close_requested(
|
|
|
|
|
callback,
|
|
|
|
|
window_id,
|
|
|
|
|
windows.clone(),
|
|
|
|
|
control_flow,
|
|
|
|
|
window_event_listeners,
|
|
|
|
|
menu_event_listeners.clone(),
|
|
|
|
|
);
|
2021-05-08 18:11:40 +03:00
|
|
|
|
}
|
2021-07-12 18:58:49 +03:00
|
|
|
|
WryWindowEvent::Resized(_) => {
|
2021-09-26 07:50:27 +03:00
|
|
|
|
if let Some(WindowHandle::Webview(webview)) = windows
|
|
|
|
|
.lock()
|
|
|
|
|
.expect("poisoned webview collection")
|
|
|
|
|
.get(&window_id)
|
|
|
|
|
.map(|w| &w.inner)
|
|
|
|
|
{
|
2021-07-29 20:35:26 +03:00
|
|
|
|
if let Err(e) = webview.resize() {
|
2021-10-23 20:31:01 +03:00
|
|
|
|
#[cfg(debug_assertions)]
|
2021-07-29 20:35:26 +03:00
|
|
|
|
eprintln!("{}", e);
|
|
|
|
|
}
|
2021-05-09 14:15:37 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-15 17:20:23 +03:00
|
|
|
|
Event::UserEvent(message) => match message {
|
|
|
|
|
Message::Window(id, WindowMessage::Close) => {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
on_window_close(
|
|
|
|
|
callback,
|
|
|
|
|
id,
|
|
|
|
|
windows.lock().expect("poisoned webview collection"),
|
|
|
|
|
control_flow,
|
|
|
|
|
#[cfg(target_os = "linux")]
|
2021-09-26 07:50:27 +03:00
|
|
|
|
window_event_listeners,
|
2021-09-28 02:18:06 +03:00
|
|
|
|
menu_event_listeners.clone(),
|
|
|
|
|
);
|
2022-03-15 17:20:23 +03:00
|
|
|
|
}
|
|
|
|
|
Message::UserEvent(t) => callback(RunEvent::UserEvent(t)),
|
|
|
|
|
message => {
|
2021-09-28 02:18:06 +03:00
|
|
|
|
return handle_user_message(
|
|
|
|
|
event_loop,
|
|
|
|
|
message,
|
|
|
|
|
UserMessageContext {
|
|
|
|
|
window_event_listeners,
|
|
|
|
|
global_shortcut_manager,
|
|
|
|
|
clipboard_manager,
|
|
|
|
|
menu_event_listeners,
|
|
|
|
|
windows,
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
tray_context,
|
|
|
|
|
},
|
|
|
|
|
web_context,
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-03-15 17:20:23 +03:00
|
|
|
|
},
|
2021-05-21 22:16:05 +03:00
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-26 07:50:27 +03:00
|
|
|
|
let it = RunIteration {
|
|
|
|
|
window_count: windows.lock().expect("poisoned webview collection").len(),
|
|
|
|
|
};
|
|
|
|
|
it
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
2021-04-04 03:41:04 +03:00
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
fn on_close_requested<'a, T: UserEvent>(
|
|
|
|
|
callback: &'a mut (dyn FnMut(RunEvent<T>) + 'static),
|
2022-03-15 18:59:37 +03:00
|
|
|
|
window_id: WebviewId,
|
|
|
|
|
windows: Arc<Mutex<HashMap<WebviewId, WindowWrapper>>>,
|
2022-01-05 12:56:55 +03:00
|
|
|
|
control_flow: &mut ControlFlow,
|
2022-01-15 18:23:26 +03:00
|
|
|
|
window_event_listeners: &WindowEventListeners,
|
|
|
|
|
menu_event_listeners: MenuEventListeners,
|
|
|
|
|
) -> Option<WindowWrapper> {
|
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
let windows_guard = windows.lock().expect("poisoned webview collection");
|
|
|
|
|
if let Some(w) = windows_guard.get(&window_id) {
|
|
|
|
|
let label = w.label.clone();
|
|
|
|
|
drop(windows_guard);
|
|
|
|
|
for handler in window_event_listeners
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.get(&window_id)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.values()
|
|
|
|
|
{
|
|
|
|
|
handler(&WindowEvent::CloseRequested {
|
|
|
|
|
label: label.clone(),
|
|
|
|
|
signal_tx: tx.clone(),
|
|
|
|
|
});
|
2022-01-05 12:56:55 +03:00
|
|
|
|
}
|
2022-01-15 18:23:26 +03:00
|
|
|
|
callback(RunEvent::CloseRequested {
|
|
|
|
|
label,
|
|
|
|
|
signal_tx: tx,
|
|
|
|
|
});
|
|
|
|
|
if let Ok(true) = rx.try_recv() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
2022-01-05 12:56:55 +03:00
|
|
|
|
on_window_close(
|
|
|
|
|
callback,
|
2022-01-15 18:23:26 +03:00
|
|
|
|
window_id,
|
|
|
|
|
windows.lock().expect("poisoned webview collection"),
|
2022-01-05 12:56:55 +03:00
|
|
|
|
control_flow,
|
2022-01-15 18:23:26 +03:00
|
|
|
|
#[cfg(target_os = "linux")]
|
2022-01-05 12:56:55 +03:00
|
|
|
|
window_event_listeners,
|
2022-01-15 18:23:26 +03:00
|
|
|
|
menu_event_listeners,
|
|
|
|
|
)
|
2022-01-05 12:56:55 +03:00
|
|
|
|
}
|
2022-01-15 18:23:26 +03:00
|
|
|
|
} else {
|
|
|
|
|
None
|
2022-01-05 12:56:55 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
fn on_window_close<'a, T: UserEvent>(
|
|
|
|
|
callback: &'a mut (dyn FnMut(RunEvent<T>) + 'static),
|
2022-03-15 18:59:37 +03:00
|
|
|
|
window_id: WebviewId,
|
|
|
|
|
mut windows: MutexGuard<'a, HashMap<WebviewId, WindowWrapper>>,
|
2021-06-20 20:12:28 +03:00
|
|
|
|
control_flow: &mut ControlFlow,
|
2021-07-06 19:36:37 +03:00
|
|
|
|
#[cfg(target_os = "linux")] window_event_listeners: &WindowEventListeners,
|
2021-08-13 16:23:32 +03:00
|
|
|
|
menu_event_listeners: MenuEventListeners,
|
2022-01-15 18:23:26 +03:00
|
|
|
|
) -> Option<WindowWrapper> {
|
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
|
let w = if let Some(mut webview) = windows.remove(&window_id) {
|
2021-09-26 07:48:13 +03:00
|
|
|
|
let is_empty = windows.is_empty();
|
|
|
|
|
drop(windows);
|
2021-06-23 17:29:30 +03:00
|
|
|
|
menu_event_listeners.lock().unwrap().remove(&window_id);
|
2021-08-12 20:22:08 +03:00
|
|
|
|
callback(RunEvent::WindowClose(webview.label.clone()));
|
2021-08-09 03:12:32 +03:00
|
|
|
|
|
2021-09-26 07:48:13 +03:00
|
|
|
|
if is_empty {
|
2021-08-12 20:22:08 +03:00
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
callback(RunEvent::ExitRequested {
|
2022-01-15 18:23:26 +03:00
|
|
|
|
window_label: webview.label.clone(),
|
2021-08-12 20:22:08 +03:00
|
|
|
|
tx,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let recv = rx.try_recv();
|
|
|
|
|
let should_prevent = matches!(recv, Ok(ExitRequestedEventAction::Prevent));
|
2021-08-09 03:12:32 +03:00
|
|
|
|
|
2021-08-12 20:22:08 +03:00
|
|
|
|
if !should_prevent {
|
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
|
}
|
2021-08-09 03:12:32 +03:00
|
|
|
|
}
|
2022-01-15 18:23:26 +03:00
|
|
|
|
Some(webview)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
2021-07-06 19:36:37 +03:00
|
|
|
|
// TODO: tao does not fire the destroyed event properly
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
{
|
|
|
|
|
for handler in window_event_listeners
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.get(&window_id)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.values()
|
|
|
|
|
{
|
|
|
|
|
handler(&WindowEvent::Destroyed);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-15 18:23:26 +03:00
|
|
|
|
w
|
2021-06-20 20:12:28 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 02:52:05 +03:00
|
|
|
|
fn center_window(window: &Window, window_size: WryPhysicalSize<u32>) -> Result<()> {
|
2021-06-06 00:20:16 +03:00
|
|
|
|
if let Some(monitor) = window.current_monitor() {
|
|
|
|
|
let screen_size = monitor.size();
|
2021-12-09 06:21:29 +03:00
|
|
|
|
let x = (screen_size.width as i32 - window_size.width as i32) / 2;
|
|
|
|
|
let y = (screen_size.height as i32 - window_size.height as i32) / 2;
|
2021-06-06 00:20:16 +03:00
|
|
|
|
window.set_outer_position(WryPhysicalPosition::new(x, y));
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
Err(Error::FailedToGetMonitor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 16:23:32 +03:00
|
|
|
|
fn to_wry_menu(
|
|
|
|
|
custom_menu_items: &mut HashMap<MenuHash, WryCustomMenuItem>,
|
|
|
|
|
menu: Menu,
|
|
|
|
|
) -> MenuBar {
|
|
|
|
|
let mut wry_menu = MenuBar::new();
|
|
|
|
|
for item in menu.items {
|
|
|
|
|
match item {
|
|
|
|
|
MenuEntry::CustomItem(c) => {
|
|
|
|
|
let mut attributes = MenuItemAttributesWrapper::from(&c).0;
|
|
|
|
|
attributes = attributes.with_id(WryMenuId(c.id));
|
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
|
let mut item = wry_menu.add_item(attributes);
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
if let Some(native_image) = c.native_image {
|
|
|
|
|
item.set_native_image(NativeImageWrapper::from(native_image).0);
|
|
|
|
|
}
|
|
|
|
|
custom_menu_items.insert(c.id, item);
|
|
|
|
|
}
|
|
|
|
|
MenuEntry::NativeItem(i) => {
|
|
|
|
|
wry_menu.add_native_item(MenuItemWrapper::from(i).0);
|
|
|
|
|
}
|
|
|
|
|
MenuEntry::Submenu(submenu) => {
|
|
|
|
|
wry_menu.add_submenu(
|
|
|
|
|
&submenu.title,
|
|
|
|
|
submenu.enabled,
|
|
|
|
|
to_wry_menu(custom_menu_items, submenu.inner),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
wry_menu
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 17:20:23 +03:00
|
|
|
|
fn create_webview<T: UserEvent>(
|
2022-03-15 18:59:37 +03:00
|
|
|
|
window_id: WebviewId,
|
2022-03-15 17:20:23 +03:00
|
|
|
|
event_loop: &EventLoopWindowTarget<Message<T>>,
|
2021-08-02 05:54:10 +03:00
|
|
|
|
web_context: &WebContextStore,
|
2022-03-15 17:20:23 +03:00
|
|
|
|
context: Context<T>,
|
|
|
|
|
pending: PendingWindow<T, Wry<T>>,
|
2021-07-29 20:35:26 +03:00
|
|
|
|
) -> Result<WindowWrapper> {
|
2021-06-22 07:29:03 +03:00
|
|
|
|
#[allow(unused_mut)]
|
2021-04-29 01:56:05 +03:00
|
|
|
|
let PendingWindow {
|
|
|
|
|
webview_attributes,
|
2021-08-24 17:40:10 +03:00
|
|
|
|
uri_scheme_protocols,
|
2021-06-22 07:29:03 +03:00
|
|
|
|
mut window_builder,
|
2022-01-09 17:29:29 +03:00
|
|
|
|
ipc_handler,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
label,
|
|
|
|
|
url,
|
2021-10-08 17:38:24 +03:00
|
|
|
|
menu_ids,
|
2021-12-09 18:22:12 +03:00
|
|
|
|
js_event_listeners,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
..
|
|
|
|
|
} = pending;
|
2022-03-15 18:59:37 +03:00
|
|
|
|
let webview_id_map = context.webview_id_map.clone();
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
let proxy = context.proxy.clone();
|
2021-04-29 01:56:05 +03:00
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
let is_window_transparent = window_builder.inner.window.transparent;
|
2021-10-08 17:38:24 +03:00
|
|
|
|
let menu_items = if let Some(menu) = window_builder.menu {
|
2021-06-22 07:29:03 +03:00
|
|
|
|
let mut menu_items = HashMap::new();
|
2021-10-08 17:38:24 +03:00
|
|
|
|
let menu = to_wry_menu(&mut menu_items, menu);
|
2021-06-22 07:29:03 +03:00
|
|
|
|
window_builder.inner = window_builder.inner.with_menu(menu);
|
2021-10-08 17:38:24 +03:00
|
|
|
|
Some(menu_items)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
2021-06-22 07:29:03 +03:00
|
|
|
|
};
|
2021-06-04 19:51:15 +03:00
|
|
|
|
let window = window_builder.inner.build(event_loop).unwrap();
|
2021-06-23 17:29:30 +03:00
|
|
|
|
|
2021-06-06 00:20:16 +03:00
|
|
|
|
if window_builder.center {
|
2021-09-30 02:52:05 +03:00
|
|
|
|
let _ = center_window(&window, window.inner_size());
|
2021-06-06 00:20:16 +03:00
|
|
|
|
}
|
2021-04-29 01:56:05 +03:00
|
|
|
|
let mut webview_builder = WebViewBuilder::new(window)
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|e| Error::CreateWebview(Box::new(e)))?
|
2021-04-29 01:56:05 +03:00
|
|
|
|
.with_url(&url)
|
2021-05-12 16:55:12 +03:00
|
|
|
|
.unwrap() // safe to unwrap because we validate the URL beforehand
|
|
|
|
|
.with_transparent(is_window_transparent);
|
2022-03-13 17:28:16 +03:00
|
|
|
|
if webview_attributes.file_drop_handler_enabled {
|
|
|
|
|
webview_builder = webview_builder.with_file_drop_handler(create_file_drop_handler(&context));
|
|
|
|
|
}
|
2022-01-09 17:29:29 +03:00
|
|
|
|
if let Some(handler) = ipc_handler {
|
|
|
|
|
webview_builder = webview_builder.with_ipc_handler(create_ipc_handler(
|
2021-06-20 20:12:28 +03:00
|
|
|
|
context,
|
|
|
|
|
label.clone(),
|
2021-10-08 17:38:24 +03:00
|
|
|
|
menu_ids,
|
2021-12-09 18:22:12 +03:00
|
|
|
|
js_event_listeners,
|
2021-06-20 20:12:28 +03:00
|
|
|
|
handler,
|
|
|
|
|
));
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
2021-08-24 17:40:10 +03:00
|
|
|
|
for (scheme, protocol) in uri_scheme_protocols {
|
|
|
|
|
webview_builder = webview_builder.with_custom_protocol(scheme, move |wry_request| {
|
|
|
|
|
protocol(&HttpRequestWrapper::from(wry_request).0)
|
|
|
|
|
.map(|tauri_response| HttpResponseWrapper::from(tauri_response).0)
|
2021-06-16 04:04:06 +03:00
|
|
|
|
.map_err(|_| wry::Error::InitScriptError)
|
2021-04-29 01:56:05 +03:00
|
|
|
|
});
|
|
|
|
|
}
|
2021-08-02 05:54:10 +03:00
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
for script in webview_attributes.initialization_scripts {
|
|
|
|
|
webview_builder = webview_builder.with_initialization_script(&script);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-29 22:28:37 +03:00
|
|
|
|
let mut web_context = web_context.lock().expect("poisoned WebContext store");
|
|
|
|
|
let is_first_context = web_context.is_empty();
|
|
|
|
|
let automation_enabled = std::env::var("TAURI_AUTOMATION").as_deref() == Ok("true");
|
|
|
|
|
let web_context = match web_context.entry(
|
|
|
|
|
// force a unique WebContext when automation is false;
|
|
|
|
|
// the context must be stored on the HashMap because it must outlive the WebView on macOS
|
|
|
|
|
if automation_enabled {
|
|
|
|
|
webview_attributes.data_directory.clone()
|
|
|
|
|
} else {
|
|
|
|
|
// random unique key
|
|
|
|
|
Some(Uuid::new_v4().to_hyphenated().to_string().into())
|
|
|
|
|
},
|
|
|
|
|
) {
|
|
|
|
|
Occupied(occupied) => occupied.into_mut(),
|
|
|
|
|
Vacant(vacant) => {
|
|
|
|
|
let mut web_context = WebContext::new(webview_attributes.data_directory);
|
|
|
|
|
web_context.set_allows_automation(if automation_enabled {
|
|
|
|
|
is_first_context
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
});
|
|
|
|
|
vacant.insert(web_context)
|
|
|
|
|
}
|
2021-08-02 05:54:10 +03:00
|
|
|
|
};
|
2021-10-23 16:05:38 +03:00
|
|
|
|
|
|
|
|
|
if webview_attributes.clipboard {
|
|
|
|
|
webview_builder.webview.clipboard = true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-07 16:04:33 +03:00
|
|
|
|
#[cfg(any(debug_assertions, feature = "devtools"))]
|
|
|
|
|
{
|
2022-03-28 16:20:38 +03:00
|
|
|
|
webview_builder = webview_builder.with_devtools(true);
|
2022-02-07 16:04:33 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-29 22:28:37 +03:00
|
|
|
|
let webview = webview_builder
|
|
|
|
|
.with_web_context(web_context)
|
|
|
|
|
.build()
|
|
|
|
|
.map_err(|e| Error::CreateWebview(Box::new(e)))?;
|
2021-08-02 05:54:10 +03:00
|
|
|
|
|
2022-03-15 18:59:37 +03:00
|
|
|
|
webview_id_map.insert(webview.window().id(), window_id);
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
{
|
2022-03-28 16:20:38 +03:00
|
|
|
|
let controller = webview.controller();
|
|
|
|
|
let proxy_ = proxy.clone();
|
|
|
|
|
let mut token = EventRegistrationToken::default();
|
|
|
|
|
unsafe {
|
|
|
|
|
controller.GotFocus(
|
|
|
|
|
FocusChangedEventHandler::create(Box::new(move |_, _| {
|
|
|
|
|
let _ = proxy_.send_event(Message::Webview(
|
|
|
|
|
window_id,
|
|
|
|
|
WebviewMessage::WebviewEvent(WebviewEvent::Focused(true)),
|
|
|
|
|
));
|
|
|
|
|
Ok(())
|
|
|
|
|
})),
|
|
|
|
|
&mut token,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
.unwrap();
|
|
|
|
|
unsafe {
|
|
|
|
|
controller.LostFocus(
|
|
|
|
|
FocusChangedEventHandler::create(Box::new(move |_, _| {
|
|
|
|
|
let _ = proxy.send_event(Message::Webview(
|
|
|
|
|
window_id,
|
|
|
|
|
WebviewMessage::WebviewEvent(WebviewEvent::Focused(false)),
|
|
|
|
|
));
|
|
|
|
|
Ok(())
|
|
|
|
|
})),
|
|
|
|
|
&mut token,
|
|
|
|
|
)
|
2022-03-15 18:59:37 +03:00
|
|
|
|
}
|
2022-03-28 16:20:38 +03:00
|
|
|
|
.unwrap();
|
2022-03-15 18:59:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 20:35:26 +03:00
|
|
|
|
Ok(WindowWrapper {
|
2021-07-15 13:05:29 +03:00
|
|
|
|
label,
|
2021-07-29 20:35:26 +03:00
|
|
|
|
inner: WindowHandle::Webview(webview),
|
2021-06-04 19:51:15 +03:00
|
|
|
|
menu_items,
|
|
|
|
|
})
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-09 17:29:29 +03:00
|
|
|
|
/// Create a wry ipc handler from a tauri ipc handler.
|
2022-03-15 17:20:23 +03:00
|
|
|
|
fn create_ipc_handler<T: UserEvent>(
|
|
|
|
|
context: Context<T>,
|
2021-07-15 13:05:29 +03:00
|
|
|
|
label: String,
|
2021-11-25 19:27:12 +03:00
|
|
|
|
menu_ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
|
2022-02-06 23:50:02 +03:00
|
|
|
|
js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u64>>>>,
|
2022-03-15 17:20:23 +03:00
|
|
|
|
handler: WebviewIpcHandler<T, Wry<T>>,
|
2022-01-09 17:29:29 +03:00
|
|
|
|
) -> Box<dyn Fn(&Window, String) + 'static> {
|
2021-04-04 03:41:04 +03:00
|
|
|
|
Box::new(move |window, request| {
|
|
|
|
|
handler(
|
|
|
|
|
DetachedWindow {
|
|
|
|
|
dispatcher: WryDispatcher {
|
2022-03-15 18:59:37 +03:00
|
|
|
|
window_id: *context
|
|
|
|
|
.webview_id_map
|
|
|
|
|
.0
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.get(&window.id())
|
|
|
|
|
.unwrap(),
|
2021-05-08 18:11:40 +03:00
|
|
|
|
context: context.clone(),
|
2021-04-04 03:41:04 +03:00
|
|
|
|
},
|
|
|
|
|
label: label.clone(),
|
2021-10-08 17:38:24 +03:00
|
|
|
|
menu_ids: menu_ids.clone(),
|
2021-12-09 18:22:12 +03:00
|
|
|
|
js_event_listeners: js_event_listeners.clone(),
|
2021-04-04 03:41:04 +03:00
|
|
|
|
},
|
2022-01-09 17:29:29 +03:00
|
|
|
|
request,
|
2021-04-04 03:41:04 +03:00
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-13 17:28:16 +03:00
|
|
|
|
/// Create a wry file drop handler.
|
2022-03-15 17:20:23 +03:00
|
|
|
|
fn create_file_drop_handler<T: UserEvent>(
|
|
|
|
|
context: &Context<T>,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
) -> Box<dyn Fn(&Window, WryFileDropEvent) -> bool + 'static> {
|
2022-03-13 17:28:16 +03:00
|
|
|
|
let window_event_listeners = context.window_event_listeners.clone();
|
2022-03-15 18:59:37 +03:00
|
|
|
|
let webview_id_map = context.webview_id_map.clone();
|
2021-04-04 03:41:04 +03:00
|
|
|
|
Box::new(move |window, event| {
|
2022-03-13 17:28:16 +03:00
|
|
|
|
let event: FileDropEvent = FileDropEventWrapper(event).into();
|
|
|
|
|
let window_event = WindowEvent::FileDrop(event);
|
|
|
|
|
let listeners = window_event_listeners.lock().unwrap();
|
2022-03-15 18:59:37 +03:00
|
|
|
|
if let Some(window_listeners) = listeners.get(&webview_id_map.get(&window.id())) {
|
2022-03-13 17:28:16 +03:00
|
|
|
|
let listeners_map = window_listeners.lock().unwrap();
|
|
|
|
|
let has_listener = !listeners_map.is_empty();
|
|
|
|
|
for listener in listeners_map.values() {
|
|
|
|
|
listener(&window_event);
|
|
|
|
|
}
|
|
|
|
|
// block the default OS action on file drop if we had a listener
|
|
|
|
|
has_listener
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
2021-04-04 03:41:04 +03:00
|
|
|
|
})
|
|
|
|
|
}
|