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::{
|
|
|
|
|
monitor::Monitor,
|
2021-05-09 21:19:37 +03:00
|
|
|
|
webview::{
|
|
|
|
|
FileDropEvent, FileDropHandler, RpcRequest, WebviewRpcHandler, WindowBuilder, WindowBuilderBase,
|
|
|
|
|
},
|
|
|
|
|
window::{
|
|
|
|
|
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
|
2021-05-10 19:27:42 +03:00
|
|
|
|
DetachedWindow, PendingWindow, WindowEvent,
|
2021-04-04 03:41:04 +03:00
|
|
|
|
},
|
2021-06-21 19:32:22 +03:00
|
|
|
|
ClipboardManager, Dispatch, Error, GlobalShortcutManager, Icon, Params, Result, RunEvent,
|
|
|
|
|
RunIteration, Runtime, RuntimeHandle, UserAttentionType,
|
2021-02-17 17:15:04 +03:00
|
|
|
|
};
|
2021-04-29 01:56:05 +03:00
|
|
|
|
|
2021-05-10 19:27:42 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
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)]
|
|
|
|
|
use winapi::shared::windef::HWND;
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
use wry::application::platform::windows::WindowBuilderExtWindows;
|
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-05-06 06:43:02 +03:00
|
|
|
|
event::{Event, WindowEvent as WryWindowEvent},
|
2021-04-29 01:56:05 +03:00
|
|
|
|
event_loop::{ControlFlow, EventLoop, EventLoopProxy, EventLoopWindowTarget},
|
2021-05-06 02:15:08 +03:00
|
|
|
|
monitor::MonitorHandle,
|
2021-06-21 18:29:26 +03:00
|
|
|
|
platform::global_shortcut::{GlobalShortcut, ShortcutManager as WryShortcutManager},
|
2021-06-21 05:42:38 +03:00
|
|
|
|
window::{
|
|
|
|
|
Fullscreen, Icon as WindowIcon, UserAttentionType as WryUserAttentionType, Window,
|
|
|
|
|
WindowBuilder as WryWindowBuilder, WindowId,
|
|
|
|
|
},
|
2021-04-29 01:56:05 +03:00
|
|
|
|
},
|
|
|
|
|
webview::{
|
2021-06-16 04:04:06 +03:00
|
|
|
|
FileDropEvent as WryFileDropEvent, RpcRequest as WryRpcRequest, RpcResponse, WebContext,
|
|
|
|
|
WebView, WebViewBuilder,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-21 21:43:11 +03:00
|
|
|
|
use std::{
|
2021-06-23 22:30:40 +03:00
|
|
|
|
collections::HashMap,
|
2021-04-21 21:43:11 +03:00
|
|
|
|
convert::TryFrom,
|
2021-05-19 03:46:21 +03:00
|
|
|
|
fs::read,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
sync::{
|
2021-06-16 17:07:41 +03:00
|
|
|
|
atomic::{AtomicBool, Ordering},
|
2021-06-05 04:23:03 +03:00
|
|
|
|
mpsc::{channel, Sender},
|
2021-05-21 22:16:05 +03:00
|
|
|
|
Arc, Mutex, MutexGuard,
|
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
|
|
|
|
|
2021-05-10 19:27:42 +03:00
|
|
|
|
#[cfg(any(feature = "menu", feature = "system-tray"))]
|
|
|
|
|
mod menu;
|
|
|
|
|
#[cfg(any(feature = "menu", feature = "system-tray"))]
|
|
|
|
|
use menu::*;
|
|
|
|
|
|
2021-06-16 04:04:06 +03:00
|
|
|
|
mod mime_type;
|
|
|
|
|
use mime_type::MimeType;
|
|
|
|
|
|
2021-06-05 04:23:03 +03:00
|
|
|
|
type MainTask = Arc<Mutex<Option<Box<dyn FnOnce() + Send>>>>;
|
2021-06-23 22:30:40 +03:00
|
|
|
|
type CreateWebviewHandler =
|
|
|
|
|
Box<dyn FnOnce(&EventLoopWindowTarget<Message>) -> Result<WebviewWrapper> + Send>;
|
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>>>;
|
|
|
|
|
type WindowEventListeners = Arc<Mutex<HashMap<WindowId, WindowEventListenersMap>>>;
|
2021-06-21 18:29:26 +03:00
|
|
|
|
type GlobalShortcutListeners = Arc<Mutex<HashMap<AcceleratorId, Box<dyn Fn() + Send>>>>;
|
|
|
|
|
|
|
|
|
|
macro_rules! dispatcher_getter {
|
|
|
|
|
($self: ident, $message: expr) => {{
|
|
|
|
|
if current_thread().id() == $self.context.main_thread_id
|
|
|
|
|
&& !$self.context.is_event_loop_running.load(Ordering::Relaxed)
|
|
|
|
|
{
|
|
|
|
|
panic!("This API cannot be called when the event loop is not running");
|
|
|
|
|
}
|
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
$self
|
|
|
|
|
.context
|
|
|
|
|
.proxy
|
|
|
|
|
.send_event(Message::Window($self.window_id, $message(tx)))
|
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)?;
|
|
|
|
|
rx.recv().unwrap()
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 19:32:22 +03:00
|
|
|
|
macro_rules! getter {
|
2021-06-21 18:29:26 +03:00
|
|
|
|
($self: ident, $rx: expr, $message: expr) => {{
|
|
|
|
|
if current_thread().id() == $self.context.main_thread_id
|
|
|
|
|
&& !$self.context.is_event_loop_running.load(Ordering::Relaxed)
|
|
|
|
|
{
|
|
|
|
|
panic!("This API cannot be called when the event loop is not running");
|
|
|
|
|
}
|
|
|
|
|
$self
|
|
|
|
|
.context
|
|
|
|
|
.proxy
|
|
|
|
|
.send_event($message)
|
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)?;
|
|
|
|
|
$rx.recv().unwrap()
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2021-06-21 19:32:22 +03:00
|
|
|
|
struct EventLoopContext {
|
2021-06-21 18:29:26 +03:00
|
|
|
|
main_thread_id: ThreadId,
|
|
|
|
|
is_event_loop_running: Arc<AtomicBool>,
|
|
|
|
|
proxy: EventLoopProxy<Message>,
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 19:32:22 +03:00
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
struct GlobalShortcutWrapper(GlobalShortcut);
|
|
|
|
|
|
|
|
|
|
unsafe impl Send for GlobalShortcutWrapper {}
|
|
|
|
|
|
2021-06-21 18:29:26 +03:00
|
|
|
|
/// Wrapper around [`WryShortcutManager`].
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct GlobalShortcutManagerHandle {
|
2021-06-21 19:32:22 +03:00
|
|
|
|
context: EventLoopContext,
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl GlobalShortcutManager for GlobalShortcutManagerHandle {
|
|
|
|
|
fn is_registered(&self, accelerator: &str) -> Result<bool> {
|
|
|
|
|
let (tx, rx) = channel();
|
2021-06-21 19:32:22 +03:00
|
|
|
|
Ok(getter!(
|
2021-06-21 18:29:26 +03:00
|
|
|
|
self,
|
|
|
|
|
rx,
|
|
|
|
|
Message::GlobalShortcut(GlobalShortcutMessage::IsRegistered(
|
|
|
|
|
accelerator.parse().expect("invalid accelerator"),
|
|
|
|
|
tx
|
|
|
|
|
))
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
)?;
|
|
|
|
|
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))
|
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-06-21 19:32:22 +03:00
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct ClipboardManagerWrapper {
|
|
|
|
|
context: EventLoopContext,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ClipboardManager for ClipboardManagerWrapper {
|
|
|
|
|
fn read_text(&self) -> Result<Option<String>> {
|
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
Ok(getter!(
|
|
|
|
|
self,
|
|
|
|
|
rx,
|
|
|
|
|
Message::Clipboard(ClipboardMessage::ReadText(tx))
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn write_text<T: Into<String>>(&mut self, text: T) -> Result<()> {
|
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
getter!(
|
|
|
|
|
self,
|
|
|
|
|
rx,
|
|
|
|
|
Message::Clipboard(ClipboardMessage::WriteText(text.into(), tx))
|
|
|
|
|
);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
/// Wrapper around a [`wry::application::window::Icon`] that can be created from an [`Icon`].
|
|
|
|
|
pub struct WryIcon(WindowIcon);
|
2021-02-14 03:35:55 +03:00
|
|
|
|
|
2021-05-19 03:46:21 +03:00
|
|
|
|
fn icon_err<E: std::error::Error + Send + 'static>(e: E) -> Error {
|
|
|
|
|
Error::InvalidIcon(Box::new(e))
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-04 03:41:04 +03:00
|
|
|
|
impl TryFrom<Icon> for WryIcon {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
type Error = Error;
|
|
|
|
|
fn try_from(icon: Icon) -> std::result::Result<Self, Self::Error> {
|
2021-05-19 03:46:21 +03:00
|
|
|
|
let image_bytes = match icon {
|
|
|
|
|
Icon::File(path) => read(path).map_err(icon_err)?,
|
|
|
|
|
Icon::Raw(raw) => raw,
|
2021-05-10 00:43:50 +03:00
|
|
|
|
_ => unimplemented!(),
|
2021-02-14 03:35:55 +03:00
|
|
|
|
};
|
2021-05-19 03:46:21 +03:00
|
|
|
|
let extension = infer::get(&image_bytes)
|
|
|
|
|
.expect("could not determine icon extension")
|
|
|
|
|
.extension();
|
|
|
|
|
match extension {
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
"ico" => {
|
|
|
|
|
let icon_dir = ico::IconDir::read(std::io::Cursor::new(image_bytes)).map_err(icon_err)?;
|
|
|
|
|
let entry = &icon_dir.entries()[0];
|
|
|
|
|
let icon = WindowIcon::from_rgba(
|
|
|
|
|
entry.decode().map_err(icon_err)?.rgba_data().to_vec(),
|
|
|
|
|
entry.width(),
|
|
|
|
|
entry.height(),
|
|
|
|
|
)
|
|
|
|
|
.map_err(icon_err)?;
|
|
|
|
|
Ok(Self(icon))
|
|
|
|
|
}
|
2021-06-01 19:07:48 +03:00
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
"png" => {
|
|
|
|
|
let decoder = png::Decoder::new(std::io::Cursor::new(image_bytes));
|
|
|
|
|
let (info, mut reader) = decoder.read_info().map_err(icon_err)?;
|
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
|
while let Ok(Some(row)) = reader.next_row() {
|
|
|
|
|
buffer.extend(row);
|
|
|
|
|
}
|
|
|
|
|
let icon = WindowIcon::from_rgba(buffer, info.width, info.height).map_err(icon_err)?;
|
|
|
|
|
Ok(Self(icon))
|
|
|
|
|
}
|
2021-05-19 03:46:21 +03:00
|
|
|
|
_ => panic!(
|
|
|
|
|
"image `{}` extension not supported; please file a Tauri feature request",
|
|
|
|
|
extension
|
|
|
|
|
),
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
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>);
|
|
|
|
|
|
|
|
|
|
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::CloseRequested => WindowEvent::CloseRequested,
|
|
|
|
|
WryWindowEvent::Destroyed => WindowEvent::Destroyed,
|
|
|
|
|
WryWindowEvent::Focused(focused) => WindowEvent::Focused(*focused),
|
|
|
|
|
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
|
|
|
|
},
|
|
|
|
|
_ => return Self(None),
|
|
|
|
|
};
|
|
|
|
|
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)]
|
|
|
|
|
struct UserAttentionTypeWrapper(WryUserAttentionType);
|
|
|
|
|
|
|
|
|
|
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-06-04 19:51:15 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
2021-06-22 07:29:03 +03:00
|
|
|
|
menu: Menu<u16>,
|
2021-06-04 19:51:15 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// safe since `menu_items` are read only here
|
|
|
|
|
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)
|
|
|
|
|
.decorations(config.decorations)
|
|
|
|
|
.maximized(config.maximized)
|
|
|
|
|
.fullscreen(config.fullscreen)
|
|
|
|
|
.transparent(config.transparent)
|
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
|
|
|
|
|
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-05-31 00:43:28 +03:00
|
|
|
|
if config.focus {
|
|
|
|
|
window = window.focus();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
window
|
2021-02-12 03:50:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 19:27:42 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn menu<I: MenuId>(mut self, menu: Menu<I>) -> Self {
|
2021-06-22 07:29:03 +03:00
|
|
|
|
self.menu = convert_menu_id(Menu::new(), 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-06-04 19:51:15 +03:00
|
|
|
|
fn focus(mut self) -> Self {
|
|
|
|
|
self.inner = self.inner.with_focus();
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn icon(mut self, icon: Icon) -> Result<Self> {
|
|
|
|
|
self.inner = self
|
|
|
|
|
.inner
|
|
|
|
|
.with_window_icon(Some(WryIcon::try_from(icon)?.0));
|
|
|
|
|
Ok(self)
|
2021-02-23 17:57:58 +03:00
|
|
|
|
}
|
|
|
|
|
|
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-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-05-10 19:27:42 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
2021-05-08 18:11:40 +03:00
|
|
|
|
fn has_menu(&self) -> bool {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
self.inner.window.window_menu.is_some()
|
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 RpcRequestWrapper(WryRpcRequest);
|
|
|
|
|
|
|
|
|
|
impl From<RpcRequestWrapper> for RpcRequest {
|
|
|
|
|
fn from(request: RpcRequestWrapper) -> Self {
|
2021-03-07 05:19:12 +03:00
|
|
|
|
Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
command: request.0.method,
|
|
|
|
|
params: request.0.params,
|
2021-03-07 05:19:12 +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),
|
|
|
|
|
WryFileDropEvent::Cancelled => FileDropEvent::Cancelled,
|
2021-03-13 03:02:36 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-21 22:53:46 +03:00
|
|
|
|
#[cfg(windows)]
|
2021-06-27 17:02:17 +03:00
|
|
|
|
struct Hwnd(HWND);
|
2021-05-21 22:53:46 +03:00
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
unsafe impl Send for Hwnd {}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
enum WindowMessage {
|
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
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
IsMenuVisible(Sender<bool>),
|
2021-05-06 02:15:08 +03:00
|
|
|
|
CurrentMonitor(Sender<Option<MonitorHandle>>),
|
|
|
|
|
PrimaryMonitor(Sender<Option<MonitorHandle>>),
|
|
|
|
|
AvailableMonitors(Sender<Vec<MonitorHandle>>),
|
2021-05-21 22:53:46 +03:00
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
Hwnd(Sender<Hwnd>),
|
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
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
ShowMenu,
|
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
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,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
SetIcon(WindowIcon),
|
2021-05-31 02:48:21 +03:00
|
|
|
|
SetSkipTaskbar(bool),
|
2021-04-29 01:56:05 +03:00
|
|
|
|
DragWindow,
|
2021-06-04 19:51:15 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
2021-06-21 18:29:26 +03:00
|
|
|
|
UpdateMenuItem(u16, menu::MenuUpdate),
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
enum WebviewMessage {
|
|
|
|
|
EvaluateScript(String),
|
2021-05-07 16:58:44 +03:00
|
|
|
|
Print,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub(crate) enum TrayMessage {
|
2021-06-21 18:29:26 +03:00
|
|
|
|
UpdateItem(u16, menu::MenuUpdate),
|
2021-06-04 19:51:15 +03:00
|
|
|
|
UpdateIcon(Icon),
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
Remove,
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 18:29:26 +03:00
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub(crate) enum GlobalShortcutMessage {
|
|
|
|
|
IsRegistered(Accelerator, Sender<bool>),
|
|
|
|
|
Register(Accelerator, Sender<Result<GlobalShortcutWrapper>>),
|
|
|
|
|
Unregister(GlobalShortcutWrapper, Sender<Result<()>>),
|
|
|
|
|
UnregisterAll(Sender<Result<()>>),
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 19:32:22 +03:00
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub(crate) enum ClipboardMessage {
|
|
|
|
|
WriteText(String, Sender<()>),
|
|
|
|
|
ReadText(Sender<Option<String>>),
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
#[derive(Clone)]
|
2021-06-04 19:51:15 +03:00
|
|
|
|
pub(crate) enum Message {
|
2021-06-05 04:23:03 +03:00
|
|
|
|
Task(MainTask),
|
2021-04-29 01:56:05 +03:00
|
|
|
|
Window(WindowId, WindowMessage),
|
|
|
|
|
Webview(WindowId, WebviewMessage),
|
2021-06-04 19:51:15 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
Tray(TrayMessage),
|
2021-04-29 01:56:05 +03:00
|
|
|
|
CreateWebview(Arc<Mutex<Option<CreateWebviewHandler>>>, Sender<WindowId>),
|
2021-06-21 18:29:26 +03:00
|
|
|
|
GlobalShortcut(GlobalShortcutMessage),
|
2021-06-21 19:32:22 +03:00
|
|
|
|
Clipboard(ClipboardMessage),
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 20:42:08 +03:00
|
|
|
|
#[derive(Clone)]
|
2021-05-08 18:11:40 +03:00
|
|
|
|
struct DispatcherContext {
|
2021-06-16 17:07:41 +03:00
|
|
|
|
main_thread_id: ThreadId,
|
|
|
|
|
is_event_loop_running: Arc<AtomicBool>,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
proxy: EventLoopProxy<Message>,
|
2021-05-06 06:43:02 +03:00
|
|
|
|
window_event_listeners: WindowEventListeners,
|
2021-05-10 19:27:42 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
2021-05-08 18:11:40 +03:00
|
|
|
|
menu_event_listeners: MenuEventListeners,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The Tauri [`Dispatch`] for [`Wry`].
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct WryDispatcher {
|
|
|
|
|
window_id: WindowId,
|
|
|
|
|
context: DispatcherContext,
|
2021-04-04 03:41:04 +03:00
|
|
|
|
}
|
2021-02-17 17:15:04 +03:00
|
|
|
|
|
2021-04-04 03:41:04 +03:00
|
|
|
|
impl Dispatch for WryDispatcher {
|
|
|
|
|
type Runtime = Wry;
|
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-05-04 04:06:50 +03:00
|
|
|
|
self
|
2021-05-08 18:11:40 +03:00
|
|
|
|
.context
|
2021-06-05 04:23:03 +03:00
|
|
|
|
.proxy
|
|
|
|
|
.send_event(Message::Task(Arc::new(Mutex::new(Some(Box::new(f))))))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
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-10 19:27:42 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
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
|
|
|
|
|
}
|
|
|
|
|
|
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> {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
Ok(dispatcher_getter!(self, WindowMessage::ScaleFactor))
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn inner_position(&self) -> Result<PhysicalPosition<i32>> {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
dispatcher_getter!(self, WindowMessage::InnerPosition)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn outer_position(&self) -> Result<PhysicalPosition<i32>> {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
dispatcher_getter!(self, WindowMessage::OuterPosition)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn inner_size(&self) -> Result<PhysicalSize<u32>> {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
Ok(dispatcher_getter!(self, WindowMessage::InnerSize))
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn outer_size(&self) -> Result<PhysicalSize<u32>> {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
Ok(dispatcher_getter!(self, WindowMessage::OuterSize))
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn is_fullscreen(&self) -> Result<bool> {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
Ok(dispatcher_getter!(self, WindowMessage::IsFullscreen))
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn is_maximized(&self) -> Result<bool> {
|
2021-05-06 02:15:08 +03:00
|
|
|
|
Ok(dispatcher_getter!(self, WindowMessage::IsMaximized))
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-30 23:16:07 +03:00
|
|
|
|
/// Gets the window’s current decoration state.
|
|
|
|
|
fn is_decorated(&self) -> Result<bool> {
|
|
|
|
|
Ok(dispatcher_getter!(self, WindowMessage::IsDecorated))
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-30 23:23:52 +03:00
|
|
|
|
/// Gets the window’s current resizable state.
|
|
|
|
|
fn is_resizable(&self) -> Result<bool> {
|
|
|
|
|
Ok(dispatcher_getter!(self, WindowMessage::IsResizable))
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 00:06:24 +03:00
|
|
|
|
fn is_visible(&self) -> Result<bool> {
|
|
|
|
|
Ok(dispatcher_getter!(self, WindowMessage::IsVisible))
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-16 04:04:44 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
fn is_menu_visible(&self) -> Result<bool> {
|
|
|
|
|
Ok(dispatcher_getter!(self, WindowMessage::IsMenuVisible))
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn current_monitor(&self) -> Result<Option<Monitor>> {
|
|
|
|
|
Ok(
|
|
|
|
|
dispatcher_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>> {
|
|
|
|
|
Ok(
|
|
|
|
|
dispatcher_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(
|
|
|
|
|
dispatcher_getter!(self, WindowMessage::AvailableMonitors)
|
|
|
|
|
.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-05-21 22:53:46 +03:00
|
|
|
|
#[cfg(windows)]
|
2021-06-27 17:02:17 +03:00
|
|
|
|
fn hwnd(&self) -> Result<HWND> {
|
2021-05-21 22:53:46 +03:00
|
|
|
|
Ok(dispatcher_getter!(self, WindowMessage::Hwnd).0)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 16:58:44 +03:00
|
|
|
|
// Setters
|
|
|
|
|
|
2021-06-06 00:20:16 +03:00
|
|
|
|
fn center(&self) -> Result<()> {
|
|
|
|
|
dispatcher_getter!(self, WindowMessage::Center)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn print(&self) -> Result<()> {
|
2021-05-07 16:58:44 +03:00
|
|
|
|
self
|
2021-05-08 18:11:40 +03:00
|
|
|
|
.context
|
2021-05-07 16:58:44 +03:00
|
|
|
|
.proxy
|
|
|
|
|
.send_event(Message::Webview(self.window_id, WebviewMessage::Print))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
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<()> {
|
|
|
|
|
self
|
|
|
|
|
.context
|
|
|
|
|
.proxy
|
|
|
|
|
.send_event(Message::Window(
|
|
|
|
|
self.window_id,
|
|
|
|
|
WindowMessage::RequestUserAttention(request_type.map(Into::into)),
|
|
|
|
|
))
|
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
|
|
|
|
}
|
|
|
|
|
|
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-05-11 04:26:18 +03:00
|
|
|
|
fn create_window<P: Params<Runtime = Self::Runtime>>(
|
2021-04-04 03:41:04 +03:00
|
|
|
|
&mut self,
|
2021-05-11 04:26:18 +03:00
|
|
|
|
pending: PendingWindow<P>,
|
|
|
|
|
) -> Result<DetachedWindow<P>> {
|
2021-04-29 01:56:05 +03:00
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
let label = pending.label.clone();
|
2021-05-08 18:11:40 +03:00
|
|
|
|
let context = self.context.clone();
|
2021-06-23 17:29:30 +03:00
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
self
|
2021-05-08 18:11:40 +03:00
|
|
|
|
.context
|
2021-04-29 01:56:05 +03:00
|
|
|
|
.proxy
|
|
|
|
|
.send_event(Message::CreateWebview(
|
2021-06-23 22:30:40 +03:00
|
|
|
|
Arc::new(Mutex::new(Some(Box::new(move |event_loop| {
|
|
|
|
|
create_webview(event_loop, context, pending)
|
|
|
|
|
})))),
|
2021-04-29 01:56:05 +03:00
|
|
|
|
tx,
|
|
|
|
|
))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)?;
|
2021-04-29 01:56:05 +03:00
|
|
|
|
let window_id = rx.recv().unwrap();
|
2021-06-23 17:29:30 +03:00
|
|
|
|
|
2021-04-04 03:41:04 +03:00
|
|
|
|
let dispatcher = WryDispatcher {
|
2021-04-29 01:56:05 +03:00
|
|
|
|
window_id,
|
2021-05-08 18:11:40 +03:00
|
|
|
|
context: self.context.clone(),
|
2021-04-04 03:41:04 +03:00
|
|
|
|
};
|
|
|
|
|
Ok(DetachedWindow { label, dispatcher })
|
2021-02-17 17:15:04 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn set_resizable(&self, resizable: bool) -> Result<()> {
|
2021-02-16 20:42:08 +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::SetResizable(resizable),
|
|
|
|
|
))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
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-02-08 17:19:22 +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::SetTitle(title.into()),
|
|
|
|
|
))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn maximize(&self) -> Result<()> {
|
2021-02-08 17:19:22 +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::Maximize))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn unmaximize(&self) -> Result<()> {
|
2021-02-16 20:42:08 +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::Unmaximize))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn minimize(&self) -> Result<()> {
|
2021-02-16 20:42:08 +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::Minimize))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
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-02-16 20:42:08 +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::Unminimize))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
2021-02-08 17:19:22 +03:00
|
|
|
|
|
2021-06-16 04:04:44 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
fn show_menu(&self) -> Result<()> {
|
|
|
|
|
self
|
|
|
|
|
.context
|
|
|
|
|
.proxy
|
|
|
|
|
.send_event(Message::Window(self.window_id, WindowMessage::ShowMenu))
|
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
fn hide_menu(&self) -> Result<()> {
|
|
|
|
|
self
|
|
|
|
|
.context
|
|
|
|
|
.proxy
|
|
|
|
|
.send_event(Message::Window(self.window_id, WindowMessage::HideMenu))
|
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn show(&self) -> Result<()> {
|
2021-04-04 03:41:04 +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::Show))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn hide(&self) -> Result<()> {
|
2021-04-04 03:41:04 +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::Hide))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn close(&self) -> Result<()> {
|
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-02-16 20:42:08 +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::SetDecorations(decorations),
|
|
|
|
|
))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
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-02-16 20:42:08 +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::SetAlwaysOnTop(always_on_top),
|
|
|
|
|
))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn set_size(&self, size: Size) -> Result<()> {
|
2021-02-16 20:42:08 +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,
|
2021-05-06 02:15:08 +03:00
|
|
|
|
WindowMessage::SetSize(size),
|
2021-04-29 01:56:05 +03:00
|
|
|
|
))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
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-02-16 20:42:08 +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,
|
2021-05-06 02:15:08 +03:00
|
|
|
|
WindowMessage::SetMinSize(size),
|
2021-04-29 01:56:05 +03:00
|
|
|
|
))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
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-02-16 20:42:08 +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,
|
2021-05-06 02:15:08 +03:00
|
|
|
|
WindowMessage::SetMaxSize(size),
|
2021-04-29 01:56:05 +03:00
|
|
|
|
))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn set_position(&self, position: Position) -> Result<()> {
|
2021-02-16 20:42:08 +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,
|
2021-05-06 02:15:08 +03:00
|
|
|
|
WindowMessage::SetPosition(position),
|
2021-04-29 01:56:05 +03:00
|
|
|
|
))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn set_fullscreen(&self, fullscreen: bool) -> Result<()> {
|
2021-02-16 20:42:08 +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::SetFullscreen(fullscreen),
|
|
|
|
|
))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-30 23:29:04 +03:00
|
|
|
|
fn set_focus(&self) -> Result<()> {
|
|
|
|
|
self
|
|
|
|
|
.context
|
|
|
|
|
.proxy
|
|
|
|
|
.send_event(Message::Window(self.window_id, WindowMessage::SetFocus))
|
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn set_icon(&self, icon: Icon) -> Result<()> {
|
2021-04-29 01:56:05 +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::SetIcon(WryIcon::try_from(icon)?.0),
|
|
|
|
|
))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
2021-02-16 20:42:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 02:48:21 +03:00
|
|
|
|
fn set_skip_taskbar(&self, skip: bool) -> Result<()> {
|
|
|
|
|
self
|
|
|
|
|
.context
|
|
|
|
|
.proxy
|
|
|
|
|
.send_event(Message::Window(
|
|
|
|
|
self.window_id,
|
|
|
|
|
WindowMessage::SetSkipTaskbar(skip),
|
|
|
|
|
))
|
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn start_dragging(&self) -> Result<()> {
|
2021-02-16 20:42:08 +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::DragWindow))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
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-02-16 20:42:08 +03:00
|
|
|
|
self
|
2021-05-08 18:11:40 +03:00
|
|
|
|
.context
|
2021-04-29 01:56:05 +03:00
|
|
|
|
.proxy
|
|
|
|
|
.send_event(Message::Webview(
|
|
|
|
|
self.window_id,
|
|
|
|
|
WebviewMessage::EvaluateScript(script.into()),
|
|
|
|
|
))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
2021-06-04 19:51:15 +03:00
|
|
|
|
|
|
|
|
|
#[cfg(feature = "menu")]
|
2021-06-21 18:29:26 +03:00
|
|
|
|
fn update_menu_item(&self, id: u16, update: menu::MenuUpdate) -> Result<()> {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
self
|
|
|
|
|
.context
|
|
|
|
|
.proxy
|
|
|
|
|
.send_event(Message::Window(
|
|
|
|
|
self.window_id,
|
|
|
|
|
WindowMessage::UpdateMenuItem(id, update),
|
|
|
|
|
))
|
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
#[derive(Clone, Default)]
|
|
|
|
|
struct TrayContext {
|
|
|
|
|
tray: Arc<Mutex<Option<Arc<Mutex<WrySystemTray>>>>>,
|
|
|
|
|
listeners: SystemTrayEventListeners,
|
|
|
|
|
items: SystemTrayItems,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct WebviewWrapper {
|
2021-06-20 20:12:28 +03:00
|
|
|
|
label: String,
|
2021-06-04 19:51:15 +03:00
|
|
|
|
inner: WebView,
|
|
|
|
|
#[cfg(feature = "menu")]
|
2021-06-21 18:29:26 +03:00
|
|
|
|
menu_items: HashMap<u16, WryCustomMenuItem>,
|
2021-06-16 04:04:44 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
is_menu_visible: AtomicBool,
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
/// A Tauri [`Runtime`] wrapper around wry.
|
2021-04-04 03:41:04 +03:00
|
|
|
|
pub struct Wry {
|
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>>,
|
|
|
|
|
global_shortcut_manager_handle: GlobalShortcutManagerHandle,
|
2021-06-21 19:32:22 +03:00
|
|
|
|
clipboard_manager: Arc<Mutex<Clipboard>>,
|
|
|
|
|
clipboard_manager_handle: ClipboardManagerWrapper,
|
2021-06-16 17:07:41 +03:00
|
|
|
|
is_event_loop_running: Arc<AtomicBool>,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
event_loop: EventLoop<Message>,
|
2021-06-04 19:51:15 +03:00
|
|
|
|
webviews: Arc<Mutex<HashMap<WindowId, WebviewWrapper>>>,
|
2021-05-06 06:43:02 +03:00
|
|
|
|
window_event_listeners: WindowEventListeners,
|
2021-05-10 19:27:42 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 22:49:01 +03:00
|
|
|
|
/// A handle to the Wry runtime.
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct WryHandle {
|
|
|
|
|
dispatcher_context: DispatcherContext,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl RuntimeHandle for WryHandle {
|
|
|
|
|
type Runtime = Wry;
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
fn create_window<P: Params<Runtime = Self::Runtime>>(
|
|
|
|
|
&self,
|
|
|
|
|
pending: PendingWindow<P>,
|
|
|
|
|
) -> Result<DetachedWindow<P>> {
|
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
let label = pending.label.clone();
|
|
|
|
|
let dispatcher_context = self.dispatcher_context.clone();
|
|
|
|
|
self
|
|
|
|
|
.dispatcher_context
|
|
|
|
|
.proxy
|
|
|
|
|
.send_event(Message::CreateWebview(
|
2021-06-23 22:30:40 +03:00
|
|
|
|
Arc::new(Mutex::new(Some(Box::new(move |event_loop| {
|
|
|
|
|
create_webview(event_loop, dispatcher_context, pending)
|
|
|
|
|
})))),
|
2021-05-18 22:49:01 +03:00
|
|
|
|
tx,
|
|
|
|
|
))
|
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)?;
|
|
|
|
|
let window_id = rx.recv().unwrap();
|
2021-06-23 17:29:30 +03:00
|
|
|
|
|
2021-05-18 22:49:01 +03:00
|
|
|
|
let dispatcher = WryDispatcher {
|
|
|
|
|
window_id,
|
|
|
|
|
context: self.dispatcher_context.clone(),
|
|
|
|
|
};
|
|
|
|
|
Ok(DetachedWindow { label, dispatcher })
|
|
|
|
|
}
|
2021-06-04 19:51:15 +03:00
|
|
|
|
|
|
|
|
|
#[cfg(all(windows, feature = "system-tray"))]
|
|
|
|
|
fn remove_system_tray(&self) -> Result<()> {
|
|
|
|
|
self
|
|
|
|
|
.dispatcher_context
|
|
|
|
|
.proxy
|
|
|
|
|
.send_event(Message::Tray(TrayMessage::Remove))
|
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)
|
|
|
|
|
}
|
2021-05-18 22:49:01 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-04 03:41:04 +03:00
|
|
|
|
impl Runtime for Wry {
|
2021-02-08 17:19:22 +03:00
|
|
|
|
type Dispatcher = WryDispatcher;
|
2021-05-18 22:49:01 +03:00
|
|
|
|
type Handle = WryHandle;
|
2021-06-21 18:29:26 +03:00
|
|
|
|
type GlobalShortcutManager = GlobalShortcutManagerHandle;
|
2021-06-21 19:32:22 +03:00
|
|
|
|
type ClipboardManager = ClipboardManagerWrapper;
|
2021-06-04 19:51:15 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
type TrayHandler = SystemTrayHandle;
|
2021-02-08 17:19:22 +03:00
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn new() -> Result<Self> {
|
2021-04-29 01:56:05 +03:00
|
|
|
|
let event_loop = EventLoop::<Message>::with_user_event();
|
2021-06-21 18:29:26 +03:00
|
|
|
|
let proxy = event_loop.create_proxy();
|
|
|
|
|
let main_thread_id = current_thread().id();
|
|
|
|
|
let is_event_loop_running = Arc::new(AtomicBool::default());
|
2021-06-21 19:32:22 +03:00
|
|
|
|
|
|
|
|
|
let event_loop_context = EventLoopContext {
|
|
|
|
|
main_thread_id,
|
|
|
|
|
is_event_loop_running: is_event_loop_running.clone(),
|
|
|
|
|
proxy,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let global_shortcut_manager = WryShortcutManager::new(&event_loop);
|
|
|
|
|
let global_shortcut_listeners = GlobalShortcutListeners::default();
|
|
|
|
|
let clipboard_manager = Clipboard::new();
|
|
|
|
|
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,
|
|
|
|
|
global_shortcut_manager: Arc::new(Mutex::new(global_shortcut_manager)),
|
|
|
|
|
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-06-21 19:32:22 +03:00
|
|
|
|
clipboard_manager: Arc::new(Mutex::new(clipboard_manager)),
|
|
|
|
|
clipboard_manager_handle,
|
2021-06-21 18:29:26 +03:00
|
|
|
|
is_event_loop_running,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
event_loop,
|
|
|
|
|
webviews: Default::default(),
|
2021-05-06 06:43:02 +03:00
|
|
|
|
window_event_listeners: Default::default(),
|
2021-05-10 19:27:42 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
2021-05-08 18:11:40 +03:00
|
|
|
|
menu_event_listeners: Default::default(),
|
2021-05-10 19:27:42 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
2021-06-04 19:51:15 +03:00
|
|
|
|
tray_context: Default::default(),
|
2021-04-29 01:56:05 +03:00
|
|
|
|
})
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 22:49:01 +03:00
|
|
|
|
fn handle(&self) -> Self::Handle {
|
|
|
|
|
WryHandle {
|
|
|
|
|
dispatcher_context: DispatcherContext {
|
2021-06-16 17:07:41 +03:00
|
|
|
|
main_thread_id: self.main_thread_id,
|
|
|
|
|
is_event_loop_running: self.is_event_loop_running.clone(),
|
2021-05-18 22:49:01 +03:00
|
|
|
|
proxy: self.event_loop.create_proxy(),
|
|
|
|
|
window_event_listeners: self.window_event_listeners.clone(),
|
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
menu_event_listeners: self.menu_event_listeners.clone(),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 04:26:18 +03:00
|
|
|
|
fn create_window<P: Params<Runtime = Self>>(
|
2021-05-09 14:15:37 +03:00
|
|
|
|
&self,
|
2021-05-11 04:26:18 +03:00
|
|
|
|
pending: PendingWindow<P>,
|
|
|
|
|
) -> Result<DetachedWindow<P>> {
|
2021-04-29 01:56:05 +03:00
|
|
|
|
let label = pending.label.clone();
|
|
|
|
|
let proxy = self.event_loop.create_proxy();
|
2021-05-04 04:06:50 +03:00
|
|
|
|
let webview = create_webview(
|
|
|
|
|
&self.event_loop,
|
2021-05-08 18:11:40 +03:00
|
|
|
|
DispatcherContext {
|
2021-06-16 17:07:41 +03:00
|
|
|
|
main_thread_id: self.main_thread_id,
|
|
|
|
|
is_event_loop_running: self.is_event_loop_running.clone(),
|
2021-05-08 18:11:40 +03:00
|
|
|
|
proxy: proxy.clone(),
|
|
|
|
|
window_event_listeners: self.window_event_listeners.clone(),
|
2021-05-10 19:27:42 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
2021-05-08 18:11:40 +03:00
|
|
|
|
menu_event_listeners: self.menu_event_listeners.clone(),
|
|
|
|
|
},
|
2021-05-04 04:06:50 +03:00
|
|
|
|
pending,
|
|
|
|
|
)?;
|
2021-04-04 03:41:04 +03:00
|
|
|
|
|
|
|
|
|
let dispatcher = WryDispatcher {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
window_id: webview.inner.window().id(),
|
2021-05-08 18:11:40 +03:00
|
|
|
|
context: DispatcherContext {
|
2021-06-16 17:07:41 +03:00
|
|
|
|
main_thread_id: self.main_thread_id,
|
|
|
|
|
is_event_loop_running: self.is_event_loop_running.clone(),
|
2021-05-08 18:11:40 +03:00
|
|
|
|
proxy,
|
|
|
|
|
window_event_listeners: self.window_event_listeners.clone(),
|
2021-05-10 19:27:42 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
2021-05-08 18:11:40 +03:00
|
|
|
|
menu_event_listeners: self.menu_event_listeners.clone(),
|
|
|
|
|
},
|
2021-04-04 03:41:04 +03:00
|
|
|
|
};
|
|
|
|
|
|
2021-05-09 14:15:37 +03:00
|
|
|
|
self
|
|
|
|
|
.webviews
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
2021-06-04 19:51:15 +03:00
|
|
|
|
.insert(webview.inner.window().id(), webview);
|
2021-04-29 01:56:05 +03:00
|
|
|
|
|
2021-04-04 03:41:04 +03:00
|
|
|
|
Ok(DetachedWindow { label, dispatcher })
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 14:38:08 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
2021-06-04 19:51:15 +03:00
|
|
|
|
fn system_tray<I: MenuId>(&self, system_tray: SystemTray<I>) -> Result<Self::TrayHandler> {
|
|
|
|
|
let icon = system_tray
|
|
|
|
|
.icon
|
|
|
|
|
.expect("tray icon not set")
|
|
|
|
|
.into_tray_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-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()
|
|
|
|
|
.insert(id, Box::new(f));
|
2021-05-09 14:15:37 +03:00
|
|
|
|
id
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-21 22:16:05 +03:00
|
|
|
|
#[cfg(any(target_os = "windows", target_os = "macos"))]
|
2021-06-20 20:12:28 +03:00
|
|
|
|
fn run_iteration<F: Fn(RunEvent) + 'static>(&mut self, callback: F) -> RunIteration {
|
2021-05-21 22:16:05 +03:00
|
|
|
|
use wry::application::platform::run_return::EventLoopExtRunReturn;
|
|
|
|
|
let webviews = self.webviews.clone();
|
|
|
|
|
let window_event_listeners = self.window_event_listeners.clone();
|
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
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();
|
|
|
|
|
|
2021-06-16 17:07:41 +03:00
|
|
|
|
self.is_event_loop_running.store(true, Ordering::Relaxed);
|
2021-05-21 22:16:05 +03:00
|
|
|
|
self
|
|
|
|
|
.event_loop
|
|
|
|
|
.run_return(|event, event_loop, control_flow| {
|
|
|
|
|
if let Event::MainEventsCleared = &event {
|
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
|
}
|
|
|
|
|
iteration = handle_event_loop(
|
|
|
|
|
event,
|
|
|
|
|
event_loop,
|
|
|
|
|
control_flow,
|
|
|
|
|
EventLoopIterationContext {
|
2021-06-20 20:12:28 +03:00
|
|
|
|
callback: &callback,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
webviews: webviews.lock().expect("poisoned webview collection"),
|
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-05-21 22:16:05 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
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-06-16 17:07:41 +03:00
|
|
|
|
self.is_event_loop_running.store(false, Ordering::Relaxed);
|
2021-05-21 22:16:05 +03:00
|
|
|
|
|
|
|
|
|
iteration
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-20 20:12:28 +03:00
|
|
|
|
fn run<F: Fn(RunEvent) + 'static>(self, callback: F) {
|
2021-06-16 17:07:41 +03:00
|
|
|
|
self.is_event_loop_running.store(true, Ordering::Relaxed);
|
2021-05-21 22:16:05 +03:00
|
|
|
|
let webviews = self.webviews.clone();
|
2021-05-06 06:43:02 +03:00
|
|
|
|
let window_event_listeners = self.window_event_listeners.clone();
|
2021-05-10 19:27:42 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
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-06-20 20:12:28 +03:00
|
|
|
|
callback: &callback,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
webviews: webviews.lock().expect("poisoned webview collection"),
|
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-05-21 22:16:05 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
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-04-29 01:56:05 +03:00
|
|
|
|
|
2021-05-21 22:16:05 +03:00
|
|
|
|
struct EventLoopIterationContext<'a> {
|
2021-06-20 20:12:28 +03:00
|
|
|
|
callback: &'a (dyn Fn(RunEvent) + 'static),
|
2021-06-04 19:51:15 +03:00
|
|
|
|
webviews: MutexGuard<'a, HashMap<WindowId, WebviewWrapper>>,
|
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>>,
|
2021-06-30 16:38:22 +03:00
|
|
|
|
global_shortcut_manager_handle: &'a GlobalShortcutManagerHandle,
|
2021-06-21 19:32:22 +03:00
|
|
|
|
clipboard_manager: Arc<Mutex<Clipboard>>,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
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-05-21 22:16:05 +03:00
|
|
|
|
fn handle_event_loop(
|
|
|
|
|
event: Event<Message>,
|
|
|
|
|
event_loop: &EventLoopWindowTarget<Message>,
|
|
|
|
|
control_flow: &mut ControlFlow,
|
|
|
|
|
context: EventLoopIterationContext<'_>,
|
|
|
|
|
) -> RunIteration {
|
|
|
|
|
let EventLoopIterationContext {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
callback,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
mut webviews,
|
|
|
|
|
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
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
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;
|
|
|
|
|
*control_flow = ControlFlow::Wait;
|
|
|
|
|
|
|
|
|
|
match event {
|
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
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
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-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-06-23 17:29:30 +03:00
|
|
|
|
let listeners = menu_event_listeners.lock().unwrap();
|
|
|
|
|
let window_menu_event_listeners = listeners.get(&window_id).cloned().unwrap_or_default();
|
|
|
|
|
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-05-21 22:16:05 +03:00
|
|
|
|
} => {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
let event = SystemTrayEvent::MenuItemClick(menu_id.0);
|
|
|
|
|
for handler in tray_context.listeners.lock().unwrap().values() {
|
|
|
|
|
handler(&event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
Event::TrayEvent {
|
|
|
|
|
bounds,
|
|
|
|
|
event,
|
|
|
|
|
position: _cursor_position,
|
|
|
|
|
} => {
|
|
|
|
|
let (position, size) = (
|
|
|
|
|
PhysicalPositionWrapper(bounds.position).into(),
|
|
|
|
|
PhysicalSizeWrapper(bounds.size).into(),
|
|
|
|
|
);
|
|
|
|
|
let event = match event {
|
|
|
|
|
TrayEvent::LeftClick => SystemTrayEvent::LeftClick { position, size },
|
|
|
|
|
TrayEvent::RightClick => SystemTrayEvent::RightClick { position, size },
|
|
|
|
|
TrayEvent::DoubleClick => SystemTrayEvent::DoubleClick { 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Event::WindowEvent { event, window_id } => {
|
|
|
|
|
if let Some(event) = WindowEventWrapper::from(&event).0 {
|
2021-06-23 22:20:09 +03:00
|
|
|
|
for handler in window_event_listeners
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.get(&window_id)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.values()
|
|
|
|
|
{
|
2021-05-21 22:16:05 +03:00
|
|
|
|
handler(&event);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 01:56:05 +03:00
|
|
|
|
match event {
|
2021-05-21 22:16:05 +03:00
|
|
|
|
WryWindowEvent::CloseRequested => {
|
2021-06-23 17:29:30 +03:00
|
|
|
|
on_window_close(
|
|
|
|
|
callback,
|
|
|
|
|
window_id,
|
|
|
|
|
&mut webviews,
|
|
|
|
|
control_flow,
|
2021-06-23 18:05:58 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
menu_event_listeners.clone(),
|
2021-06-23 17:29:30 +03:00
|
|
|
|
);
|
2021-05-08 18:11:40 +03:00
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
WryWindowEvent::Resized(_) => {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
if let Err(e) = webviews[&window_id].inner.resize() {
|
2021-05-21 22:16:05 +03:00
|
|
|
|
eprintln!("{}", e);
|
2021-05-09 14:15:37 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Event::UserEvent(message) => match message {
|
2021-06-05 04:23:03 +03:00
|
|
|
|
Message::Task(task) => {
|
|
|
|
|
if let Some(task) = task.lock().unwrap().take() {
|
|
|
|
|
task();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
Message::Window(id, window_message) => {
|
|
|
|
|
if let Some(webview) = webviews.get_mut(&id) {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
let window = webview.inner.window();
|
2021-05-21 22:16:05 +03:00
|
|
|
|
match window_message {
|
|
|
|
|
// 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
|
|
|
|
|
.send(PhysicalSizeWrapper(window.inner_size()).into())
|
|
|
|
|
.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(),
|
2021-05-30 23:16:07 +03:00
|
|
|
|
WindowMessage::IsDecorated(tx) => tx.send(window.is_decorated()).unwrap(),
|
2021-05-30 23:23:52 +03:00
|
|
|
|
WindowMessage::IsResizable(tx) => tx.send(window.is_resizable()).unwrap(),
|
2021-05-31 00:06:24 +03:00
|
|
|
|
WindowMessage::IsVisible(tx) => tx.send(window.is_visible()).unwrap(),
|
2021-06-16 04:04:44 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
WindowMessage::IsMenuVisible(tx) => tx
|
|
|
|
|
.send(webview.is_menu_visible.load(Ordering::Relaxed))
|
|
|
|
|
.unwrap(),
|
2021-05-21 22:16:05 +03:00
|
|
|
|
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()
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
2021-05-21 22:53:46 +03:00
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
WindowMessage::Hwnd(tx) => {
|
|
|
|
|
use wry::application::platform::windows::WindowExtWindows;
|
2021-06-27 17:02:17 +03:00
|
|
|
|
tx.send(Hwnd(window.hwnd() as HWND)).unwrap()
|
2021-05-21 22:53:46 +03:00
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
// Setters
|
2021-06-06 00:20:16 +03:00
|
|
|
|
WindowMessage::Center(tx) => {
|
|
|
|
|
tx.send(center_window(window)).unwrap();
|
|
|
|
|
}
|
2021-06-21 05:42:38 +03:00
|
|
|
|
WindowMessage::RequestUserAttention(request_type) => {
|
|
|
|
|
window.request_user_attention(request_type.map(|r| r.0));
|
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
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),
|
2021-06-16 04:04:44 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
WindowMessage::ShowMenu => {
|
|
|
|
|
window.show_menu();
|
|
|
|
|
webview.is_menu_visible.store(true, Ordering::Relaxed);
|
|
|
|
|
}
|
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
WindowMessage::HideMenu => {
|
|
|
|
|
window.hide_menu();
|
|
|
|
|
webview.is_menu_visible.store(false, Ordering::Relaxed);
|
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
WindowMessage::Show => window.set_visible(true),
|
|
|
|
|
WindowMessage::Hide => window.set_visible(false),
|
|
|
|
|
WindowMessage::Close => {
|
2021-06-23 22:20:09 +03:00
|
|
|
|
for handler in window_event_listeners
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.get(&window.id())
|
|
|
|
|
.unwrap()
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.values()
|
|
|
|
|
{
|
|
|
|
|
handler(&WindowEvent::CloseRequested);
|
|
|
|
|
}
|
2021-06-23 17:29:30 +03:00
|
|
|
|
on_window_close(
|
|
|
|
|
callback,
|
|
|
|
|
id,
|
|
|
|
|
&mut webviews,
|
|
|
|
|
control_flow,
|
2021-06-23 18:05:58 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
menu_event_listeners.clone(),
|
2021-06-23 17:29:30 +03:00
|
|
|
|
);
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
2021-05-21 22:16:05 +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)
|
2021-05-06 06:43:02 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-30 23:29:04 +03:00
|
|
|
|
WindowMessage::SetFocus => {
|
|
|
|
|
window.set_focus();
|
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
WindowMessage::SetIcon(icon) => {
|
|
|
|
|
window.set_window_icon(Some(icon));
|
|
|
|
|
}
|
2021-05-31 02:48:21 +03:00
|
|
|
|
WindowMessage::SetSkipTaskbar(skip) => {
|
|
|
|
|
window.set_skip_taskbar(skip);
|
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
WindowMessage::DragWindow => {
|
|
|
|
|
let _ = window.drag_window();
|
|
|
|
|
}
|
2021-06-04 19:51:15 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
WindowMessage::UpdateMenuItem(id, update) => {
|
|
|
|
|
let item = webview
|
|
|
|
|
.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-04-29 01:56:05 +03:00
|
|
|
|
}
|
2021-05-06 06:43:02 +03:00
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
}
|
|
|
|
|
Message::Webview(id, webview_message) => {
|
|
|
|
|
if let Some(webview) = webviews.get_mut(&id) {
|
|
|
|
|
match webview_message {
|
|
|
|
|
WebviewMessage::EvaluateScript(script) => {
|
2021-06-16 04:04:06 +03:00
|
|
|
|
if let Err(e) = webview.inner.evaluate_script(&script) {
|
|
|
|
|
eprintln!("{}", e);
|
|
|
|
|
}
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
WebviewMessage::Print => {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
let _ = webview.inner.print();
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Message::CreateWebview(handler, sender) => {
|
|
|
|
|
let handler = {
|
|
|
|
|
let mut lock = handler.lock().expect("poisoned create webview handler");
|
|
|
|
|
std::mem::take(&mut *lock).unwrap()
|
|
|
|
|
};
|
2021-06-23 22:30:40 +03:00
|
|
|
|
match handler(event_loop) {
|
2021-05-21 22:16:05 +03:00
|
|
|
|
Ok(webview) => {
|
2021-06-04 19:51:15 +03:00
|
|
|
|
let window_id = webview.inner.window().id();
|
2021-05-21 22:16:05 +03:00
|
|
|
|
webviews.insert(window_id, webview);
|
|
|
|
|
sender.send(window_id).unwrap();
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
Err(e) => {
|
|
|
|
|
eprintln!("{}", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
2021-06-04 19:51:15 +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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
TrayMessage::UpdateIcon(icon) => {
|
|
|
|
|
if let Some(tray) = &*tray_context.tray.lock().unwrap() {
|
|
|
|
|
tray.lock().unwrap().set_icon(icon.into_tray_icon());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
TrayMessage::Remove => {
|
|
|
|
|
if let Some(tray) = tray_context.tray.lock().unwrap().as_ref() {
|
|
|
|
|
use wry::application::platform::windows::SystemTrayExtWindows;
|
|
|
|
|
tray.lock().unwrap().remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-06-21 18:29:26 +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(),
|
|
|
|
|
},
|
2021-06-21 19:32:22 +03:00
|
|
|
|
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(),
|
|
|
|
|
},
|
2021-05-21 22:16:05 +03:00
|
|
|
|
},
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RunIteration {
|
|
|
|
|
webview_count: webviews.len(),
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-04 03:41:04 +03:00
|
|
|
|
|
2021-06-20 20:12:28 +03:00
|
|
|
|
fn on_window_close<'a>(
|
|
|
|
|
callback: &'a (dyn Fn(RunEvent) + 'static),
|
|
|
|
|
window_id: WindowId,
|
|
|
|
|
webviews: &mut MutexGuard<'a, HashMap<WindowId, WebviewWrapper>>,
|
|
|
|
|
control_flow: &mut ControlFlow,
|
2021-06-23 19:03:28 +03:00
|
|
|
|
#[cfg(feature = "menu")] menu_event_listeners: MenuEventListeners,
|
2021-06-20 20:12:28 +03:00
|
|
|
|
) {
|
|
|
|
|
if let Some(webview) = webviews.remove(&window_id) {
|
2021-06-23 18:05:58 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
2021-06-23 17:29:30 +03:00
|
|
|
|
menu_event_listeners.lock().unwrap().remove(&window_id);
|
2021-06-20 20:12:28 +03:00
|
|
|
|
callback(RunEvent::WindowClose(webview.label));
|
|
|
|
|
}
|
|
|
|
|
if webviews.is_empty() {
|
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
|
callback(RunEvent::Exit);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 00:20:16 +03:00
|
|
|
|
fn center_window(window: &Window) -> Result<()> {
|
|
|
|
|
if let Some(monitor) = window.current_monitor() {
|
|
|
|
|
let screen_size = monitor.size();
|
|
|
|
|
let window_size = window.inner_size();
|
|
|
|
|
let x = (screen_size.width - window_size.width) / 2;
|
|
|
|
|
let y = (screen_size.height - window_size.height) / 2;
|
|
|
|
|
window.set_outer_position(WryPhysicalPosition::new(x, y));
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
Err(Error::FailedToGetMonitor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 04:26:18 +03:00
|
|
|
|
fn create_webview<P: Params<Runtime = Wry>>(
|
2021-04-29 01:56:05 +03:00
|
|
|
|
event_loop: &EventLoopWindowTarget<Message>,
|
2021-05-08 18:11:40 +03:00
|
|
|
|
context: DispatcherContext,
|
2021-05-11 04:26:18 +03:00
|
|
|
|
pending: PendingWindow<P>,
|
2021-06-04 19:51:15 +03:00
|
|
|
|
) -> Result<WebviewWrapper> {
|
2021-06-22 07:29:03 +03:00
|
|
|
|
#[allow(unused_mut)]
|
2021-04-29 01:56:05 +03:00
|
|
|
|
let PendingWindow {
|
|
|
|
|
webview_attributes,
|
2021-06-22 07:29:03 +03:00
|
|
|
|
mut window_builder,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
rpc_handler,
|
|
|
|
|
file_drop_handler,
|
|
|
|
|
label,
|
|
|
|
|
url,
|
|
|
|
|
..
|
|
|
|
|
} = pending;
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
let is_window_transparent = window_builder.inner.window.transparent;
|
|
|
|
|
#[cfg(feature = "menu")]
|
2021-06-22 07:29:03 +03:00
|
|
|
|
let menu_items = {
|
|
|
|
|
let mut menu_items = HashMap::new();
|
|
|
|
|
let menu = to_wry_menu(&mut menu_items, window_builder.menu);
|
|
|
|
|
window_builder.inner = window_builder.inner.with_menu(menu);
|
|
|
|
|
menu_items
|
|
|
|
|
};
|
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-23 22:20:09 +03:00
|
|
|
|
context
|
|
|
|
|
.window_event_listeners
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.insert(window.id(), WindowEventListenersMap::default());
|
|
|
|
|
|
2021-06-23 17:29:30 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
context
|
|
|
|
|
.menu_event_listeners
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.insert(window.id(), WindowMenuEventListeners::default());
|
|
|
|
|
|
2021-06-06 00:20:16 +03:00
|
|
|
|
if window_builder.center {
|
|
|
|
|
let _ = center_window(&window);
|
|
|
|
|
}
|
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);
|
2021-04-29 01:56:05 +03:00
|
|
|
|
if let Some(handler) = rpc_handler {
|
2021-05-08 18:11:40 +03:00
|
|
|
|
webview_builder =
|
|
|
|
|
webview_builder.with_rpc_handler(create_rpc_handler(context.clone(), label.clone(), handler));
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
|
|
|
|
if let Some(handler) = file_drop_handler {
|
2021-06-20 20:12:28 +03:00
|
|
|
|
webview_builder = webview_builder.with_file_drop_handler(create_file_drop_handler(
|
|
|
|
|
context,
|
|
|
|
|
label.clone(),
|
|
|
|
|
handler,
|
|
|
|
|
));
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
|
|
|
|
for (scheme, protocol) in webview_attributes.uri_scheme_protocols {
|
|
|
|
|
webview_builder = webview_builder.with_custom_protocol(scheme, move |_window, url| {
|
2021-06-16 04:04:06 +03:00
|
|
|
|
protocol(url)
|
|
|
|
|
.map(|data| {
|
|
|
|
|
let mime_type = MimeType::parse(&data, url);
|
|
|
|
|
(data, mime_type)
|
|
|
|
|
})
|
|
|
|
|
.map_err(|_| wry::Error::InitScriptError)
|
2021-04-29 01:56:05 +03:00
|
|
|
|
});
|
|
|
|
|
}
|
2021-06-23 22:30:40 +03:00
|
|
|
|
let context = WebContext::new(webview_attributes.data_directory);
|
|
|
|
|
webview_builder = webview_builder.with_web_context(&context);
|
2021-04-29 01:56:05 +03:00
|
|
|
|
for script in webview_attributes.initialization_scripts {
|
|
|
|
|
webview_builder = webview_builder.with_initialization_script(&script);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 19:51:15 +03:00
|
|
|
|
let webview = webview_builder
|
2021-04-29 01:56:05 +03:00
|
|
|
|
.build()
|
2021-06-04 19:51:15 +03:00
|
|
|
|
.map_err(|e| Error::CreateWebview(Box::new(e)))?;
|
|
|
|
|
|
|
|
|
|
Ok(WebviewWrapper {
|
2021-06-20 20:12:28 +03:00
|
|
|
|
label: format!("{}", label),
|
2021-06-04 19:51:15 +03:00
|
|
|
|
inner: webview,
|
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
menu_items,
|
2021-06-16 04:04:44 +03:00
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
is_menu_visible: AtomicBool::new(true),
|
2021-06-04 19:51:15 +03:00
|
|
|
|
})
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-04 03:41:04 +03:00
|
|
|
|
/// Create a wry rpc handler from a tauri rpc handler.
|
2021-05-11 04:26:18 +03:00
|
|
|
|
fn create_rpc_handler<P: Params<Runtime = Wry>>(
|
2021-05-08 18:11:40 +03:00
|
|
|
|
context: DispatcherContext,
|
2021-05-11 04:26:18 +03:00
|
|
|
|
label: P::Label,
|
|
|
|
|
handler: WebviewRpcHandler<P>,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
) -> Box<dyn Fn(&Window, WryRpcRequest) -> Option<RpcResponse> + 'static> {
|
2021-04-04 03:41:04 +03:00
|
|
|
|
Box::new(move |window, request| {
|
|
|
|
|
handler(
|
|
|
|
|
DetachedWindow {
|
|
|
|
|
dispatcher: WryDispatcher {
|
2021-04-29 01:56:05 +03:00
|
|
|
|
window_id: window.id(),
|
2021-05-08 18:11:40 +03:00
|
|
|
|
context: context.clone(),
|
2021-04-04 03:41:04 +03:00
|
|
|
|
},
|
|
|
|
|
label: label.clone(),
|
|
|
|
|
},
|
2021-05-10 00:43:50 +03:00
|
|
|
|
RpcRequestWrapper(request).into(),
|
2021-04-04 03:41:04 +03:00
|
|
|
|
);
|
|
|
|
|
None
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a wry file drop handler from a tauri file drop handler.
|
2021-05-11 04:26:18 +03:00
|
|
|
|
fn create_file_drop_handler<P: Params<Runtime = Wry>>(
|
2021-05-08 18:11:40 +03:00
|
|
|
|
context: DispatcherContext,
|
2021-05-11 04:26:18 +03:00
|
|
|
|
label: P::Label,
|
|
|
|
|
handler: FileDropHandler<P>,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
) -> Box<dyn Fn(&Window, WryFileDropEvent) -> bool + 'static> {
|
2021-04-04 03:41:04 +03:00
|
|
|
|
Box::new(move |window, event| {
|
|
|
|
|
handler(
|
2021-05-10 00:43:50 +03:00
|
|
|
|
FileDropEventWrapper(event).into(),
|
2021-04-04 03:41:04 +03:00
|
|
|
|
DetachedWindow {
|
|
|
|
|
dispatcher: WryDispatcher {
|
2021-04-29 01:56:05 +03:00
|
|
|
|
window_id: window.id(),
|
2021-05-08 18:11:40 +03:00
|
|
|
|
context: context.clone(),
|
2021-04-04 03:41:04 +03:00
|
|
|
|
},
|
|
|
|
|
label: label.clone(),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
}
|