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-05-21 22:16:05 +03:00
|
|
|
|
Dispatch, Error, Icon, Params, Result, RunIteration, Runtime, RuntimeHandle,
|
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")]
|
|
|
|
|
use tauri_runtime::SystemTrayEvent;
|
2021-05-21 22:16:05 +03:00
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
use winapi::shared::windef::HWND;
|
2021-05-10 19:27:42 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
use wry::application::platform::system_tray::SystemTrayBuilder;
|
2021-05-21 22:16:05 +03:00
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
use wry::application::platform::windows::WindowBuilderExtWindows;
|
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-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-04-29 01:56:05 +03:00
|
|
|
|
window::{Fullscreen, Icon as WindowIcon, Window, WindowBuilder as WryWindowBuilder, WindowId},
|
|
|
|
|
},
|
|
|
|
|
webview::{
|
|
|
|
|
FileDropEvent as WryFileDropEvent, RpcRequest as WryRpcRequest, RpcResponse, WebView,
|
|
|
|
|
WebViewBuilder,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-21 21:43:11 +03:00
|
|
|
|
use std::{
|
|
|
|
|
collections::HashMap,
|
|
|
|
|
convert::TryFrom,
|
2021-05-19 03:46:21 +03:00
|
|
|
|
fs::read,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
sync::{
|
2021-05-04 04:06:50 +03:00
|
|
|
|
mpsc::{channel, Receiver, Sender},
|
2021-05-21 22:16:05 +03:00
|
|
|
|
Arc, Mutex, MutexGuard,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
},
|
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-04-29 01:56:05 +03:00
|
|
|
|
type CreateWebviewHandler =
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Box<dyn FnOnce(&EventLoopWindowTarget<Message>) -> Result<WebView> + Send>;
|
2021-05-04 04:06:50 +03:00
|
|
|
|
type MainThreadTask = Box<dyn FnOnce() + Send>;
|
2021-05-06 06:43:02 +03:00
|
|
|
|
type WindowEventHandler = Box<dyn Fn(&WindowEvent) + Send>;
|
|
|
|
|
type WindowEventListeners = Arc<Mutex<HashMap<Uuid, WindowEventHandler>>>;
|
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))
|
|
|
|
|
}
|
|
|
|
|
_ => 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-05-10 00:43:50 +03:00
|
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
|
pub struct WindowBuilderWrapper(WryWindowBuilder);
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
.always_on_top(config.always_on_top);
|
|
|
|
|
|
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-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-05-09 14:15:37 +03:00
|
|
|
|
fn menu<I: MenuId>(self, menu: Vec<Menu<I>>) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(
|
|
|
|
|
self.0.with_menu(
|
|
|
|
|
menu
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|m| MenuWrapper::from(m).0)
|
|
|
|
|
.collect::<Vec<WryMenu>>(),
|
|
|
|
|
),
|
|
|
|
|
)
|
2021-05-08 18:11:40 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
fn position(self, x: f64, y: f64) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(self.0.with_position(WryLogicalPosition::new(x, y)))
|
2021-02-12 03:50:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
fn inner_size(self, width: f64, height: f64) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(self.0.with_inner_size(WryLogicalSize::new(width, height)))
|
2021-02-12 03:50:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
fn min_inner_size(self, min_width: f64, min_height: f64) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(
|
|
|
|
|
self
|
|
|
|
|
.0
|
|
|
|
|
.with_min_inner_size(WryLogicalSize::new(min_width, min_height)),
|
|
|
|
|
)
|
2021-02-12 03:50:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
fn max_inner_size(self, max_width: f64, max_height: f64) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(
|
|
|
|
|
self
|
|
|
|
|
.0
|
|
|
|
|
.with_max_inner_size(WryLogicalSize::new(max_width, max_height)),
|
|
|
|
|
)
|
2021-02-12 03:50:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
fn resizable(self, resizable: bool) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(self.0.with_resizable(resizable))
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
fn title<S: Into<String>>(self, title: S) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(self.0.with_title(title.into()))
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
fn fullscreen(self, fullscreen: bool) -> Self {
|
|
|
|
|
if fullscreen {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(self.0.with_fullscreen(Some(Fullscreen::Borderless(None))))
|
2021-04-29 01:56:05 +03:00
|
|
|
|
} else {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(self.0.with_fullscreen(None))
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
2021-02-12 03:50:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-30 23:41:28 +03:00
|
|
|
|
fn focus(self) -> Self {
|
|
|
|
|
Self(self.0.with_focus())
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
fn maximized(self, maximized: bool) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(self.0.with_maximized(maximized))
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
fn visible(self, visible: bool) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(self.0.with_visible(visible))
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
fn transparent(self, transparent: bool) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(self.0.with_transparent(transparent))
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
fn decorations(self, decorations: bool) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(self.0.with_decorations(decorations))
|
2021-02-08 17:19:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 01:56:05 +03:00
|
|
|
|
fn always_on_top(self, always_on_top: bool) -> Self {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
Self(self.0.with_always_on_top(always_on_top))
|
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)]
|
|
|
|
|
fn parent_window(self, parent: HWND) -> Self {
|
|
|
|
|
Self(self.0.with_parent_window(parent))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
fn owner_window(self, owner: HWND) -> Self {
|
|
|
|
|
Self(self.0.with_owner_window(owner))
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
fn icon(self, icon: Icon) -> Result<Self> {
|
|
|
|
|
Ok(Self(
|
|
|
|
|
self.0.with_window_icon(Some(WryIcon::try_from(icon)?.0)),
|
|
|
|
|
))
|
2021-02-23 17:57:58 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn has_icon(&self) -> bool {
|
2021-05-10 00:43:50 +03:00
|
|
|
|
self.0.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-05-10 00:43:50 +03:00
|
|
|
|
self.0.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)]
|
|
|
|
|
struct Hwnd(*mut std::ffi::c_void);
|
|
|
|
|
#[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-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-04-29 01:56:05 +03:00
|
|
|
|
SetResizable(bool),
|
|
|
|
|
SetTitle(String),
|
|
|
|
|
Maximize,
|
|
|
|
|
Unmaximize,
|
|
|
|
|
Minimize,
|
|
|
|
|
Unminimize,
|
|
|
|
|
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),
|
|
|
|
|
DragWindow,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
enum WebviewMessage {
|
|
|
|
|
EvaluateScript(String),
|
2021-05-07 16:58:44 +03:00
|
|
|
|
Print,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
enum Message {
|
|
|
|
|
Window(WindowId, WindowMessage),
|
|
|
|
|
Webview(WindowId, WebviewMessage),
|
|
|
|
|
CreateWebview(Arc<Mutex<Option<CreateWebviewHandler>>>, Sender<WindowId>),
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 20:42:08 +03:00
|
|
|
|
#[derive(Clone)]
|
2021-05-08 18:11:40 +03:00
|
|
|
|
struct DispatcherContext {
|
2021-04-29 01:56:05 +03:00
|
|
|
|
proxy: EventLoopProxy<Message>,
|
2021-05-04 04:06:50 +03:00
|
|
|
|
task_tx: Sender<MainThreadTask>,
|
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-05-06 02:15:08 +03:00
|
|
|
|
macro_rules! dispatcher_getter {
|
|
|
|
|
($self: ident, $message: expr) => {{
|
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
$self
|
2021-05-08 18:11:40 +03:00
|
|
|
|
.context
|
2021-05-06 02:15:08 +03:00
|
|
|
|
.proxy
|
|
|
|
|
.send_event(Message::Window($self.window_id, $message(tx)))
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)?;
|
2021-05-06 02:15:08 +03:00
|
|
|
|
rx.recv().unwrap()
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
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-05-04 04:06:50 +03:00
|
|
|
|
.task_tx
|
|
|
|
|
.send(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()
|
|
|
|
|
.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()
|
|
|
|
|
.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-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)]
|
|
|
|
|
fn hwnd(&self) -> Result<*mut std::ffi::c_void> {
|
|
|
|
|
Ok(dispatcher_getter!(self, WindowMessage::Hwnd).0)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 16:58:44 +03:00
|
|
|
|
// Setters
|
|
|
|
|
|
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-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-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(
|
|
|
|
|
Arc::new(Mutex::new(Some(Box::new(move |event_loop| {
|
2021-05-08 18:11:40 +03:00
|
|
|
|
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-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-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-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-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-04-29 01:56:05 +03:00
|
|
|
|
event_loop: EventLoop<Message>,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
webviews: Arc<Mutex<HashMap<WindowId, WebView>>>,
|
2021-05-04 04:06:50 +03:00
|
|
|
|
task_tx: Sender<MainThreadTask>,
|
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-05-10 19:27:42 +03:00
|
|
|
|
#[cfg(feature = "system-tray")]
|
2021-05-09 14:15:37 +03:00
|
|
|
|
system_tray_event_listeners: SystemTrayEventListeners,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
task_rx: Arc<Receiver<MainThreadTask>>,
|
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(
|
|
|
|
|
Arc::new(Mutex::new(Some(Box::new(move |event_loop| {
|
|
|
|
|
create_webview(event_loop, dispatcher_context, pending)
|
|
|
|
|
})))),
|
|
|
|
|
tx,
|
|
|
|
|
))
|
|
|
|
|
.map_err(|_| Error::FailedToSendMessage)?;
|
|
|
|
|
let window_id = rx.recv().unwrap();
|
|
|
|
|
let dispatcher = WryDispatcher {
|
|
|
|
|
window_id,
|
|
|
|
|
context: self.dispatcher_context.clone(),
|
|
|
|
|
};
|
|
|
|
|
Ok(DetachedWindow { label, dispatcher })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-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-05-04 04:06:50 +03:00
|
|
|
|
let (task_tx, task_rx) = channel();
|
2021-04-29 01:56:05 +03:00
|
|
|
|
Ok(Self {
|
|
|
|
|
event_loop,
|
|
|
|
|
webviews: Default::default(),
|
2021-05-04 04:06:50 +03:00
|
|
|
|
task_tx,
|
2021-05-21 22:16:05 +03:00
|
|
|
|
task_rx: Arc::new(task_rx),
|
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-05-21 22:16:05 +03:00
|
|
|
|
system_tray_event_listeners: 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 {
|
|
|
|
|
proxy: self.event_loop.create_proxy(),
|
|
|
|
|
task_tx: self.task_tx.clone(),
|
|
|
|
|
window_event_listeners: self.window_event_listeners.clone(),
|
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
menu_event_listeners: self.menu_event_listeners.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 {
|
|
|
|
|
proxy: proxy.clone(),
|
|
|
|
|
task_tx: self.task_tx.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-04-29 01:56:05 +03:00
|
|
|
|
window_id: webview.window().id(),
|
2021-05-08 18:11:40 +03:00
|
|
|
|
context: DispatcherContext {
|
|
|
|
|
proxy,
|
|
|
|
|
task_tx: self.task_tx.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-04-04 03:41:04 +03:00
|
|
|
|
};
|
|
|
|
|
|
2021-05-09 14:15:37 +03:00
|
|
|
|
self
|
|
|
|
|
.webviews
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.insert(webview.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-05-09 14:15:37 +03:00
|
|
|
|
fn system_tray<I: MenuId>(
|
|
|
|
|
&self,
|
2021-05-11 14:38:08 +03:00
|
|
|
|
icon: Icon,
|
2021-05-10 00:43:50 +03:00
|
|
|
|
menu_items: Vec<SystemTrayMenuItem<I>>,
|
|
|
|
|
) -> Result<()> {
|
2021-05-11 14:38:08 +03:00
|
|
|
|
// todo: fix this interface in Tao to an enum similar to Icon
|
|
|
|
|
|
|
|
|
|
// we expect the code that passes the Icon enum to have already checked the platform.
|
|
|
|
|
let icon = match icon {
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
Icon::File(path) => path,
|
|
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "linux"))]
|
|
|
|
|
Icon::Raw(bytes) => bytes,
|
|
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
Icon::Raw(_) => {
|
|
|
|
|
panic!("linux requires the system menu icon to be a file path, not bytes.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "linux"))]
|
|
|
|
|
Icon::File(_) => {
|
|
|
|
|
panic!("non-linux system menu icons must be bytes, not a file path",)
|
|
|
|
|
}
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
2021-05-09 14:15:37 +03:00
|
|
|
|
|
2021-05-10 00:43:50 +03:00
|
|
|
|
SystemTrayBuilder::new(
|
|
|
|
|
icon,
|
|
|
|
|
menu_items
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|m| MenuItemWrapper::from(m).0)
|
|
|
|
|
.collect(),
|
|
|
|
|
)
|
|
|
|
|
.build(&self.event_loop)
|
|
|
|
|
.map_err(|e| Error::SystemTray(Box::new(e)))?;
|
2021-05-09 14:15:37 +03:00
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
.system_tray_event_listeners
|
|
|
|
|
.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"))]
|
|
|
|
|
fn run_iteration(&mut self) -> RunIteration {
|
|
|
|
|
use wry::application::platform::run_return::EventLoopExtRunReturn;
|
|
|
|
|
let webviews = self.webviews.clone();
|
|
|
|
|
let task_rx = self.task_rx.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")]
|
|
|
|
|
let system_tray_event_listeners = self.system_tray_event_listeners.clone();
|
|
|
|
|
|
|
|
|
|
let mut iteration = RunIteration::default();
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
webviews: webviews.lock().expect("poisoned webview collection"),
|
|
|
|
|
task_rx: task_rx.clone(),
|
|
|
|
|
window_event_listeners: window_event_listeners.clone(),
|
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
menu_event_listeners: menu_event_listeners.clone(),
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
system_tray_event_listeners: system_tray_event_listeners.clone(),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
iteration
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 07:23:15 +03:00
|
|
|
|
fn run(self) {
|
2021-05-21 22:16:05 +03:00
|
|
|
|
let webviews = self.webviews.clone();
|
2021-05-04 04:06:50 +03:00
|
|
|
|
let task_rx = self.task_rx;
|
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-05-09 14:15:37 +03:00
|
|
|
|
let system_tray_event_listeners = self.system_tray_event_listeners;
|
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 {
|
|
|
|
|
webviews: webviews.lock().expect("poisoned webview collection"),
|
|
|
|
|
task_rx: task_rx.clone(),
|
|
|
|
|
window_event_listeners: window_event_listeners.clone(),
|
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
menu_event_listeners: menu_event_listeners.clone(),
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
system_tray_event_listeners: system_tray_event_listeners.clone(),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 01:56:05 +03:00
|
|
|
|
|
2021-05-21 22:16:05 +03:00
|
|
|
|
struct EventLoopIterationContext<'a> {
|
|
|
|
|
webviews: MutexGuard<'a, HashMap<WindowId, WebView>>,
|
|
|
|
|
task_rx: Arc<Receiver<MainThreadTask>>,
|
|
|
|
|
window_event_listeners: WindowEventListeners,
|
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
menu_event_listeners: MenuEventListeners,
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
system_tray_event_listeners: SystemTrayEventListeners,
|
|
|
|
|
}
|
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 {
|
|
|
|
|
mut webviews,
|
|
|
|
|
task_rx,
|
|
|
|
|
window_event_listeners,
|
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
menu_event_listeners,
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
system_tray_event_listeners,
|
|
|
|
|
} = context;
|
|
|
|
|
*control_flow = ControlFlow::Wait;
|
|
|
|
|
|
|
|
|
|
for (_, w) in webviews.iter() {
|
|
|
|
|
if let Err(e) = w.evaluate_script() {
|
|
|
|
|
eprintln!("{}", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while let Ok(task) = task_rx.try_recv() {
|
|
|
|
|
task();
|
|
|
|
|
}
|
2021-05-04 04:06:50 +03:00
|
|
|
|
|
2021-05-21 22:16:05 +03:00
|
|
|
|
match event {
|
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
|
|
Event::MenuEvent {
|
|
|
|
|
menu_id,
|
|
|
|
|
origin: MenuType::Menubar,
|
|
|
|
|
} => {
|
|
|
|
|
let event = MenuEvent {
|
|
|
|
|
menu_item_id: menu_id.0,
|
|
|
|
|
};
|
|
|
|
|
for handler in menu_event_listeners.lock().unwrap().values() {
|
|
|
|
|
handler(&event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
|
|
Event::MenuEvent {
|
|
|
|
|
menu_id,
|
|
|
|
|
origin: MenuType::SystemTray,
|
|
|
|
|
} => {
|
|
|
|
|
let event = SystemTrayEvent {
|
|
|
|
|
menu_item_id: menu_id.0,
|
|
|
|
|
};
|
|
|
|
|
for handler in system_tray_event_listeners.lock().unwrap().values() {
|
|
|
|
|
handler(&event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Event::WindowEvent { event, window_id } => {
|
|
|
|
|
if let Some(event) = WindowEventWrapper::from(&event).0 {
|
|
|
|
|
for handler in window_event_listeners.lock().unwrap().values() {
|
|
|
|
|
handler(&event);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 01:56:05 +03:00
|
|
|
|
match event {
|
2021-05-21 22:16:05 +03:00
|
|
|
|
WryWindowEvent::CloseRequested => {
|
|
|
|
|
webviews.remove(&window_id);
|
|
|
|
|
if webviews.is_empty() {
|
|
|
|
|
*control_flow = ControlFlow::Exit;
|
2021-05-08 18:11:40 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
WryWindowEvent::Resized(_) => {
|
|
|
|
|
if let Err(e) = webviews[&window_id].resize() {
|
|
|
|
|
eprintln!("{}", e);
|
2021-05-09 14:15:37 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Event::UserEvent(message) => match message {
|
|
|
|
|
Message::Window(id, window_message) => {
|
|
|
|
|
if let Some(webview) = webviews.get_mut(&id) {
|
|
|
|
|
let window = webview.window();
|
|
|
|
|
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-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;
|
|
|
|
|
tx.send(Hwnd(window.hwnd())).unwrap()
|
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
// Setters
|
|
|
|
|
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::Show => window.set_visible(true),
|
|
|
|
|
WindowMessage::Hide => window.set_visible(false),
|
|
|
|
|
WindowMessage::Close => {
|
|
|
|
|
webviews.remove(&id);
|
2021-05-06 06:43:02 +03:00
|
|
|
|
if webviews.is_empty() {
|
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
|
}
|
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));
|
|
|
|
|
}
|
|
|
|
|
WindowMessage::DragWindow => {
|
|
|
|
|
let _ = window.drag_window();
|
|
|
|
|
}
|
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) => {
|
|
|
|
|
let _ = webview.dispatch_script(&script);
|
2021-04-29 01:56:05 +03:00
|
|
|
|
}
|
2021-05-21 22:16:05 +03:00
|
|
|
|
WebviewMessage::Print => {
|
|
|
|
|
let _ = webview.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()
|
|
|
|
|
};
|
|
|
|
|
match handler(event_loop) {
|
|
|
|
|
Ok(webview) => {
|
|
|
|
|
let window_id = webview.window().id();
|
|
|
|
|
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-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-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-05-10 00:43:50 +03:00
|
|
|
|
) -> Result<WebView> {
|
2021-04-29 01:56:05 +03:00
|
|
|
|
let PendingWindow {
|
|
|
|
|
webview_attributes,
|
2021-05-09 21:19:37 +03:00
|
|
|
|
window_builder,
|
2021-04-29 01:56:05 +03:00
|
|
|
|
rpc_handler,
|
|
|
|
|
file_drop_handler,
|
|
|
|
|
label,
|
|
|
|
|
url,
|
|
|
|
|
..
|
|
|
|
|
} = pending;
|
|
|
|
|
|
2021-05-12 16:55:12 +03:00
|
|
|
|
let is_window_transparent = window_builder.0.window.transparent;
|
2021-05-10 00:43:50 +03:00
|
|
|
|
let window = window_builder.0.build(event_loop).unwrap();
|
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-05-08 18:11:40 +03:00
|
|
|
|
webview_builder =
|
|
|
|
|
webview_builder.with_file_drop_handler(create_file_drop_handler(context, label, 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| {
|
|
|
|
|
protocol(url).map_err(|_| wry::Error::InitScriptError)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if let Some(data_directory) = webview_attributes.data_directory {
|
|
|
|
|
webview_builder = webview_builder.with_data_directory(data_directory);
|
|
|
|
|
}
|
|
|
|
|
for script in webview_attributes.initialization_scripts {
|
|
|
|
|
webview_builder = webview_builder.with_initialization_script(&script);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
webview_builder
|
|
|
|
|
.build()
|
2021-05-10 00:43:50 +03:00
|
|
|
|
.map_err(|e| Error::CreateWebview(Box::new(e)))
|
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(),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
}
|