tauri/core/tauri-runtime/src/flavors/wry.rs

1120 lines
32 KiB
Rust
Raw Normal View History

feat(license): SPDX Headers (#1449) * chore(licenses): api Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(licenses): scripts Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): cli/core Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): cli/tauri-bundler Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): workflows Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): require license_template in rust Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-api Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-build Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-codegen Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-macros Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-updater Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-utils Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): examples Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): cli/tauri.js Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): changefile Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): place both licenses in root Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): package.json SPDX Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): SPDX everywhere Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * fix(tauri.js): tests more time for ubuntu Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): commons conservancy language Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): add spdx file Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * fix(license): clippy Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): language Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com>
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
//! The [`wry`] Tauri [`Runtime`].
use crate::{
menu::{CustomMenuItem, Menu, MenuId, MenuItem, SystemTrayMenuItem},
webview::{
FileDropEvent, FileDropHandler, RpcRequest, WebviewRpcHandler, WindowBuilder, WindowBuilderBase,
},
window::{
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
DetachedWindow, MenuEvent, PendingWindow, WindowEvent,
},
Dispatch, Icon, Monitor, Params, Runtime, SystemTrayEvent,
};
2021-04-29 01:56:05 +03:00
use image::{GenericImageView, Pixel};
use tauri_utils::config::WindowConfig;
use uuid::Uuid;
2021-04-29 01:56:05 +03:00
use wry::{
application::{
dpi::{
LogicalPosition as WryLogicalPosition, LogicalSize as WryLogicalSize,
PhysicalPosition as WryPhysicalPosition, PhysicalSize as WryPhysicalSize,
Position as WryPosition, Size as WrySize,
},
event::{Event, WindowEvent as WryWindowEvent},
2021-04-29 01:56:05 +03:00
event_loop::{ControlFlow, EventLoop, EventLoopProxy, EventLoopWindowTarget},
2021-05-08 18:11:40 +03:00
menu::{
CustomMenu as WryCustomMenu, Menu as WryMenu, MenuId as WryMenuId, MenuItem as WryMenuItem,
MenuType,
},
monitor::MonitorHandle,
platform::system_tray::SystemTrayBuilder,
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,
},
};
use std::{
collections::HashMap,
convert::TryFrom,
2021-04-29 01:56:05 +03:00
sync::{
mpsc::{channel, Receiver, Sender},
2021-04-29 01:56:05 +03:00
Arc, Mutex,
},
};
2021-04-29 01:56:05 +03:00
type CreateWebviewHandler =
Box<dyn FnOnce(&EventLoopWindowTarget<Message>) -> crate::Result<WebView> + Send>;
type MainThreadTask = Box<dyn FnOnce() + Send>;
type WindowEventHandler = Box<dyn Fn(&WindowEvent) + Send>;
type WindowEventListeners = Arc<Mutex<HashMap<Uuid, WindowEventHandler>>>;
2021-05-08 18:11:40 +03:00
type MenuEventHandler = Box<dyn Fn(&MenuEvent) + Send>;
type MenuEventListeners = Arc<Mutex<HashMap<Uuid, MenuEventHandler>>>;
type SystemTrayEventHandler = Box<dyn Fn(&SystemTrayEvent) + Send>;
type SystemTrayEventListeners = HashMap<Uuid, SystemTrayEventHandler>;
2021-04-29 01:56:05 +03:00
#[repr(C)]
#[derive(Debug)]
struct PixelValue {
r: u8,
g: u8,
b: u8,
a: u8,
}
const PIXEL_SIZE: usize = std::mem::size_of::<PixelValue>();
/// 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
impl TryFrom<Icon> for WryIcon {
2021-02-14 03:35:55 +03:00
type Error = crate::Error;
fn try_from(icon: Icon) -> Result<Self, Self::Error> {
2021-04-29 01:56:05 +03:00
let image = match icon {
Icon::File(path) => image::open(path).map_err(|e| crate::Error::InvalidIcon(Box::new(e)))?,
Icon::Raw(raw) => {
image::load_from_memory(&raw).map_err(|e| crate::Error::InvalidIcon(Box::new(e)))?
2021-02-14 03:35:55 +03:00
}
};
2021-04-29 01:56:05 +03:00
let (width, height) = image.dimensions();
let mut rgba = Vec::with_capacity((width * height) as usize * PIXEL_SIZE);
for (_, _, pixel) in image.pixels() {
rgba.extend_from_slice(&pixel.to_rgba().0);
}
let icon = WindowIcon::from_rgba(rgba, width, height)
.map_err(|e| crate::Error::InvalidIcon(Box::new(e)))?;
Ok(Self(icon))
2021-02-14 03:35:55 +03:00
}
}
struct WindowEventWrapper(Option<WindowEvent>);
impl<'a> From<&WryWindowEvent<'a>> for WindowEventWrapper {
fn from(event: &WryWindowEvent<'a>) -> Self {
let event = match event {
WryWindowEvent::Resized(size) => WindowEvent::Resized((*size).into()),
WryWindowEvent::Moved(position) => WindowEvent::Moved((*position).into()),
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,
new_inner_size: (**new_inner_size).into(),
},
_ => return Self(None),
};
Self(Some(event))
}
}
impl From<MonitorHandle> for Monitor {
fn from(monitor: MonitorHandle) -> Monitor {
Self {
name: monitor.name(),
position: monitor.position().into(),
size: monitor.size().into(),
scale_factor: monitor.scale_factor(),
}
}
}
impl<T> From<WryPhysicalPosition<T>> for PhysicalPosition<T> {
fn from(position: WryPhysicalPosition<T>) -> Self {
Self {
x: position.x,
y: position.y,
}
}
}
impl<T> From<PhysicalPosition<T>> for WryPhysicalPosition<T> {
fn from(position: PhysicalPosition<T>) -> Self {
Self {
x: position.x,
y: position.y,
}
}
}
impl<T> From<LogicalPosition<T>> for WryLogicalPosition<T> {
fn from(position: LogicalPosition<T>) -> Self {
Self {
x: position.x,
y: position.y,
}
}
}
impl<T> From<WryPhysicalSize<T>> for PhysicalSize<T> {
fn from(size: WryPhysicalSize<T>) -> Self {
Self {
width: size.width,
height: size.height,
}
}
}
impl<T> From<PhysicalSize<T>> for WryPhysicalSize<T> {
fn from(size: PhysicalSize<T>) -> Self {
Self {
width: size.width,
height: size.height,
}
}
}
impl<T> From<LogicalSize<T>> for WryLogicalSize<T> {
fn from(size: LogicalSize<T>) -> Self {
Self {
width: size.width,
height: size.height,
}
}
}
impl From<Size> for WrySize {
fn from(size: Size) -> Self {
match size {
Size::Logical(s) => Self::Logical(s.into()),
Size::Physical(s) => Self::Physical(s.into()),
}
}
}
impl From<Position> for WryPosition {
fn from(position: Position) -> Self {
match position {
Position::Logical(s) => Self::Logical(s.into()),
Position::Physical(s) => Self::Physical(s.into()),
}
}
}
impl<I: MenuId> From<CustomMenuItem<I>> for WryCustomMenu {
fn from(item: CustomMenuItem<I>) -> Self {
2021-05-08 18:11:40 +03:00
Self {
id: WryMenuId(item.id_value()),
2021-05-08 18:11:40 +03:00
name: item.name,
keyboard_accelerators: None,
}
}
}
impl<I: MenuId> From<MenuItem<I>> for WryMenuItem {
fn from(item: MenuItem<I>) -> Self {
2021-05-08 18:11:40 +03:00
match item {
MenuItem::Custom(custom) => Self::Custom(custom.into()),
MenuItem::About(v) => Self::About(v),
MenuItem::Hide => Self::Hide,
MenuItem::Services => Self::Services,
MenuItem::HideOthers => Self::HideOthers,
MenuItem::ShowAll => Self::ShowAll,
MenuItem::CloseWindow => Self::CloseWindow,
MenuItem::Quit => Self::Quit,
MenuItem::Copy => Self::Copy,
MenuItem::Cut => Self::Cut,
MenuItem::Undo => Self::Undo,
MenuItem::Redo => Self::Redo,
MenuItem::SelectAll => Self::SelectAll,
MenuItem::Paste => Self::Paste,
MenuItem::EnterFullScreen => Self::EnterFullScreen,
MenuItem::Minimize => Self::Minimize,
MenuItem::Zoom => Self::Zoom,
MenuItem::Separator => Self::Separator,
}
}
}
impl<I: MenuId> From<Menu<I>> for WryMenu {
fn from(menu: Menu<I>) -> Self {
2021-05-08 18:11:40 +03:00
Self {
title: menu.title,
items: menu.items.into_iter().map(Into::into).collect(),
}
}
}
impl<I: MenuId> From<SystemTrayMenuItem<I>> for WryMenuItem {
fn from(item: SystemTrayMenuItem<I>) -> Self {
match item {
SystemTrayMenuItem::Custom(custom) => Self::Custom(custom.into()),
SystemTrayMenuItem::Separator => Self::Separator,
}
}
}
2021-04-29 01:56:05 +03:00
impl WindowBuilderBase for WryWindowBuilder {}
impl WindowBuilder for WryWindowBuilder {
fn new() -> Self {
Default::default()
}
fn with_config(config: WindowConfig) -> Self {
2021-04-29 01:56:05 +03:00
let mut window = WryWindowBuilder::new()
.title(config.title.to_string())
2021-04-29 01:56:05 +03:00
.inner_size(config.width, config.height)
.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-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-04-29 01:56:05 +03:00
if let (Some(x), Some(y)) = (config.x, config.y) {
window = window.position(x, y);
}
2021-04-29 01:56:05 +03:00
window
}
fn menu<I: MenuId>(self, menu: Vec<Menu<I>>) -> Self {
2021-05-08 18:11:40 +03:00
self.with_menu(menu.into_iter().map(Into::into).collect::<Vec<WryMenu>>())
}
2021-04-29 01:56:05 +03:00
fn position(self, x: f64, y: f64) -> Self {
self.with_position(WryLogicalPosition::new(x, y))
}
2021-04-29 01:56:05 +03:00
fn inner_size(self, width: f64, height: f64) -> Self {
self.with_inner_size(WryLogicalSize::new(width, height))
}
2021-04-29 01:56:05 +03:00
fn min_inner_size(self, min_width: f64, min_height: f64) -> Self {
self.with_min_inner_size(WryLogicalSize::new(min_width, min_height))
}
2021-04-29 01:56:05 +03:00
fn max_inner_size(self, max_width: f64, max_height: f64) -> Self {
self.with_max_inner_size(WryLogicalSize::new(max_width, max_height))
}
2021-04-29 01:56:05 +03:00
fn resizable(self, resizable: bool) -> Self {
self.with_resizable(resizable)
}
2021-04-29 01:56:05 +03:00
fn title<S: Into<String>>(self, title: S) -> Self {
self.with_title(title.into())
}
2021-04-29 01:56:05 +03:00
fn fullscreen(self, fullscreen: bool) -> Self {
if fullscreen {
self.with_fullscreen(Some(Fullscreen::Borderless(None)))
} else {
self.with_fullscreen(None)
}
}
2021-04-29 01:56:05 +03:00
fn maximized(self, maximized: bool) -> Self {
self.with_maximized(maximized)
}
2021-04-29 01:56:05 +03:00
fn visible(self, visible: bool) -> Self {
self.with_visible(visible)
}
2021-04-29 01:56:05 +03:00
fn transparent(self, transparent: bool) -> Self {
self.with_transparent(transparent)
}
2021-04-29 01:56:05 +03:00
fn decorations(self, decorations: bool) -> Self {
self.with_decorations(decorations)
}
2021-04-29 01:56:05 +03:00
fn always_on_top(self, always_on_top: bool) -> Self {
self.with_always_on_top(always_on_top)
}
2021-02-16 20:42:08 +03:00
2021-04-29 01:56:05 +03:00
fn icon(self, icon: Icon) -> crate::Result<Self> {
Ok(self.with_window_icon(Some(WryIcon::try_from(icon)?.0)))
}
fn has_icon(&self) -> bool {
2021-04-29 01:56:05 +03:00
self.window.window_icon.is_some()
2021-02-16 20:42:08 +03:00
}
2021-05-08 18:11:40 +03:00
fn has_menu(&self) -> bool {
self.window.window_menu.is_some()
}
}
2021-04-29 01:56:05 +03:00
impl From<WryRpcRequest> for RpcRequest {
fn from(request: WryRpcRequest) -> Self {
Self {
command: request.method,
params: request.params,
}
}
}
2021-04-29 01:56:05 +03:00
impl From<WryFileDropEvent> for FileDropEvent {
fn from(event: WryFileDropEvent) -> Self {
match event {
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-04-29 01:56:05 +03:00
#[derive(Debug, Clone)]
enum WindowMessage {
// Getters
ScaleFactor(Sender<f64>),
InnerPosition(Sender<crate::Result<PhysicalPosition<i32>>>),
OuterPosition(Sender<crate::Result<PhysicalPosition<i32>>>),
InnerSize(Sender<PhysicalSize<u32>>),
OuterSize(Sender<PhysicalSize<u32>>),
IsFullscreen(Sender<bool>),
IsMaximized(Sender<bool>),
CurrentMonitor(Sender<Option<MonitorHandle>>),
PrimaryMonitor(Sender<Option<MonitorHandle>>),
AvailableMonitors(Sender<Vec<MonitorHandle>>),
// Setters
2021-04-29 01:56:05 +03:00
SetResizable(bool),
SetTitle(String),
Maximize,
Unmaximize,
Minimize,
Unminimize,
Show,
Hide,
Close,
SetDecorations(bool),
SetAlwaysOnTop(bool),
SetSize(Size),
SetMinSize(Option<Size>),
SetMaxSize(Option<Size>),
SetPosition(Position),
2021-04-29 01:56:05 +03:00
SetFullscreen(bool),
SetIcon(WindowIcon),
DragWindow,
}
#[derive(Debug, Clone)]
enum WebviewMessage {
EvaluateScript(String),
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>,
task_tx: Sender<MainThreadTask>,
window_event_listeners: WindowEventListeners,
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,
}
macro_rules! dispatcher_getter {
($self: ident, $message: expr) => {{
let (tx, rx) = channel();
$self
2021-05-08 18:11:40 +03:00
.context
.proxy
.send_event(Message::Window($self.window_id, $message(tx)))
.map_err(|_| crate::Error::FailedToSendMessage)?;
rx.recv().unwrap()
}};
}
macro_rules! window_result_getter {
($window: ident, $tx: ident, $call: ident) => {
$tx
.send(
$window
.$call()
.map(Into::into)
.map_err(|_| crate::Error::FailedToSendMessage),
)
.unwrap()
};
}
macro_rules! window_getter {
($window: ident, $tx: ident, $call: ident) => {
$tx.send($window.$call().into()).unwrap()
};
}
impl Dispatch for WryDispatcher {
type Runtime = Wry;
2021-04-29 01:56:05 +03:00
type WindowBuilder = WryWindowBuilder;
fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()> {
self
2021-05-08 18:11:40 +03:00
.context
.task_tx
.send(Box::new(f))
.map_err(|_| crate::Error::FailedToSendMessage)
}
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
.window_event_listeners
.lock()
.unwrap()
.insert(id, Box::new(f));
id
}
2021-05-08 18:11:40 +03:00
fn on_menu_event<F: Fn(&MenuEvent) + Send + 'static>(&self, f: F) -> Uuid {
let id = Uuid::new_v4();
self
.context
.menu_event_listeners
.lock()
.unwrap()
.insert(id, Box::new(f));
id
}
// Getters
fn scale_factor(&self) -> crate::Result<f64> {
Ok(dispatcher_getter!(self, WindowMessage::ScaleFactor))
}
fn inner_position(&self) -> crate::Result<PhysicalPosition<i32>> {
dispatcher_getter!(self, WindowMessage::InnerPosition)
}
fn outer_position(&self) -> crate::Result<PhysicalPosition<i32>> {
dispatcher_getter!(self, WindowMessage::OuterPosition)
}
fn inner_size(&self) -> crate::Result<PhysicalSize<u32>> {
Ok(dispatcher_getter!(self, WindowMessage::InnerSize))
}
fn outer_size(&self) -> crate::Result<PhysicalSize<u32>> {
Ok(dispatcher_getter!(self, WindowMessage::OuterSize))
}
fn is_fullscreen(&self) -> crate::Result<bool> {
Ok(dispatcher_getter!(self, WindowMessage::IsFullscreen))
}
fn is_maximized(&self) -> crate::Result<bool> {
Ok(dispatcher_getter!(self, WindowMessage::IsMaximized))
}
fn current_monitor(&self) -> crate::Result<Option<Monitor>> {
Ok(dispatcher_getter!(self, WindowMessage::CurrentMonitor).map(Into::into))
}
fn primary_monitor(&self) -> crate::Result<Option<Monitor>> {
Ok(dispatcher_getter!(self, WindowMessage::PrimaryMonitor).map(Into::into))
}
fn available_monitors(&self) -> crate::Result<Vec<Monitor>> {
Ok(
dispatcher_getter!(self, WindowMessage::AvailableMonitors)
.into_iter()
.map(Into::into)
.collect(),
)
}
// Setters
fn print(&self) -> crate::Result<()> {
self
2021-05-08 18:11:40 +03:00
.context
.proxy
.send_event(Message::Webview(self.window_id, WebviewMessage::Print))
.map_err(|_| crate::Error::FailedToSendMessage)
}
fn create_window<M: Params<Runtime = Self::Runtime>>(
&mut self,
pending: PendingWindow<M>,
) -> crate::Result<DetachedWindow<M>> {
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,
))
.map_err(|_| crate::Error::FailedToSendMessage)?;
let window_id = rx.recv().unwrap();
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(),
};
Ok(DetachedWindow { label, dispatcher })
}
2021-02-16 20:42:08 +03:00
fn set_resizable(&self, resizable: bool) -> crate::Result<()> {
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-02-16 20:42:08 +03:00
.map_err(|_| crate::Error::FailedToSendMessage)
}
2021-02-16 20:42:08 +03:00
fn set_title<S: Into<String>>(&self, title: S) -> crate::Result<()> {
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-02-16 20:42:08 +03:00
.map_err(|_| crate::Error::FailedToSendMessage)
}
2021-02-16 20:42:08 +03:00
fn maximize(&self) -> crate::Result<()> {
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-02-16 20:42:08 +03:00
.map_err(|_| crate::Error::FailedToSendMessage)
}
2021-02-16 20:42:08 +03:00
fn unmaximize(&self) -> crate::Result<()> {
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-02-16 20:42:08 +03:00
.map_err(|_| crate::Error::FailedToSendMessage)
}
2021-02-16 20:42:08 +03:00
fn minimize(&self) -> crate::Result<()> {
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-02-16 20:42:08 +03:00
.map_err(|_| crate::Error::FailedToSendMessage)
}
2021-02-16 20:42:08 +03:00
fn unminimize(&self) -> crate::Result<()> {
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-02-16 20:42:08 +03:00
.map_err(|_| crate::Error::FailedToSendMessage)
}
2021-02-16 20:42:08 +03:00
fn show(&self) -> crate::Result<()> {
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))
.map_err(|_| crate::Error::FailedToSendMessage)
}
2021-02-16 20:42:08 +03:00
fn hide(&self) -> crate::Result<()> {
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))
.map_err(|_| crate::Error::FailedToSendMessage)
2021-02-16 20:42:08 +03:00
}
fn close(&self) -> crate::Result<()> {
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))
.map_err(|_| crate::Error::FailedToSendMessage)
}
2021-02-16 20:42:08 +03:00
fn set_decorations(&self, decorations: bool) -> crate::Result<()> {
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-02-16 20:42:08 +03:00
.map_err(|_| crate::Error::FailedToSendMessage)
}
fn set_always_on_top(&self, always_on_top: bool) -> crate::Result<()> {
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-02-16 20:42:08 +03:00
.map_err(|_| crate::Error::FailedToSendMessage)
}
fn set_size(&self, size: Size) -> crate::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::SetSize(size),
2021-04-29 01:56:05 +03:00
))
2021-02-16 20:42:08 +03:00
.map_err(|_| crate::Error::FailedToSendMessage)
}
fn set_min_size(&self, size: Option<Size>) -> crate::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::SetMinSize(size),
2021-04-29 01:56:05 +03:00
))
2021-02-16 20:42:08 +03:00
.map_err(|_| crate::Error::FailedToSendMessage)
}
fn set_max_size(&self, size: Option<Size>) -> crate::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::SetMaxSize(size),
2021-04-29 01:56:05 +03:00
))
2021-02-16 20:42:08 +03:00
.map_err(|_| crate::Error::FailedToSendMessage)
}
fn set_position(&self, position: Position) -> crate::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::SetPosition(position),
2021-04-29 01:56:05 +03:00
))
2021-02-16 20:42:08 +03:00
.map_err(|_| crate::Error::FailedToSendMessage)
}
fn set_fullscreen(&self, fullscreen: bool) -> crate::Result<()> {
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),
))
.map_err(|_| crate::Error::FailedToSendMessage)
}
fn set_icon(&self, icon: Icon) -> crate::Result<()> {
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-02-16 20:42:08 +03:00
.map_err(|_| crate::Error::FailedToSendMessage)
}
2021-04-29 01:56:05 +03:00
fn start_dragging(&self) -> crate::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-02-16 20:42:08 +03:00
.map_err(|_| crate::Error::FailedToSendMessage)
}
fn eval_script<S: Into<String>>(&self, script: S) -> crate::Result<()> {
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-02-16 20:42:08 +03:00
.map_err(|_| crate::Error::FailedToSendMessage)
}
}
2021-04-29 01:56:05 +03:00
/// A Tauri [`Runtime`] wrapper around wry.
pub struct Wry {
2021-04-29 01:56:05 +03:00
event_loop: EventLoop<Message>,
webviews: Mutex<HashMap<WindowId, WebView>>,
task_tx: Sender<MainThreadTask>,
window_event_listeners: WindowEventListeners,
2021-05-08 18:11:40 +03:00
menu_event_listeners: MenuEventListeners,
system_tray_event_listeners: SystemTrayEventListeners,
task_rx: Receiver<MainThreadTask>,
}
impl Runtime for Wry {
type Dispatcher = WryDispatcher;
fn new() -> crate::Result<Self> {
2021-04-29 01:56:05 +03:00
let event_loop = EventLoop::<Message>::with_user_event();
let (task_tx, task_rx) = channel();
2021-04-29 01:56:05 +03:00
Ok(Self {
event_loop,
webviews: Default::default(),
task_tx,
task_rx,
window_event_listeners: Default::default(),
2021-05-08 18:11:40 +03:00
menu_event_listeners: Default::default(),
system_tray_event_listeners: HashMap::default(),
2021-04-29 01:56:05 +03:00
})
}
fn create_window<M: Params<Runtime = Self>>(
&self,
pending: PendingWindow<M>,
) -> crate::Result<DetachedWindow<M>> {
2021-04-29 01:56:05 +03:00
let label = pending.label.clone();
let proxy = self.event_loop.create_proxy();
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(),
menu_event_listeners: self.menu_event_listeners.clone(),
},
pending,
)?;
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(),
menu_event_listeners: self.menu_event_listeners.clone(),
},
};
self
.webviews
.lock()
.unwrap()
.insert(webview.window().id(), webview);
2021-04-29 01:56:05 +03:00
Ok(DetachedWindow { label, dispatcher })
}
#[cfg(target_os = "linux")]
fn system_tray<I: MenuId>(
&self,
icon: std::path::PathBuf,
menu: Vec<SystemTrayMenuItem<I>>,
) -> crate::Result<()> {
SystemTrayBuilder::new(icon, menu.into_iter().map(Into::into).collect())
.build(&self.event_loop)
.map_err(|e| crate::Error::SystemTray(Box::new(e)))?;
Ok(())
}
#[cfg(not(target_os = "linux"))]
fn system_tray<I: MenuId>(
&self,
icon: Vec<u8>,
menu: Vec<SystemTrayMenuItem<I>>,
) -> crate::Result<()> {
SystemTrayBuilder::new(icon, menu.into_iter().map(Into::into).collect())
.build(&self.event_loop)
.map_err(|e| crate::Error::SystemTray(Box::new(e)))?;
Ok(())
}
fn on_system_tray_event<F: Fn(&SystemTrayEvent) + Send + 'static>(&mut self, f: F) -> Uuid {
let id = Uuid::new_v4();
self.system_tray_event_listeners.insert(id, Box::new(f));
id
}
fn run(self) {
let mut webviews = {
let mut lock = self.webviews.lock().expect("poisoned webview collection");
std::mem::take(&mut *lock)
};
let task_rx = self.task_rx;
let window_event_listeners = self.window_event_listeners.clone();
2021-05-08 18:11:40 +03:00
let menu_event_listeners = self.menu_event_listeners.clone();
let system_tray_event_listeners = self.system_tray_event_listeners;
2021-04-29 01:56:05 +03:00
self.event_loop.run(move |event, event_loop, control_flow| {
*control_flow = ControlFlow::Wait;
2021-04-29 01:56:05 +03:00
for (_, w) in webviews.iter() {
if let Err(e) = w.evaluate_script() {
eprintln!("{}", e);
}
}
while let Ok(task) = task_rx.try_recv() {
task();
}
2021-04-29 01:56:05 +03:00
match event {
2021-05-08 18:11:40 +03:00
Event::MenuEvent {
menu_id,
origin: MenuType::Menubar,
} => {
let event = MenuEvent {
menu_item_id: menu_id.0,
2021-05-08 18:11:40 +03:00
};
for handler in menu_event_listeners.lock().unwrap().values() {
handler(&event);
}
}
Event::MenuEvent {
menu_id,
origin: MenuType::SystemTray,
} => {
let event = SystemTrayEvent {
menu_item_id: menu_id.0,
};
for handler in system_tray_event_listeners.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 {
WryWindowEvent::CloseRequested => {
webviews.remove(&window_id);
if webviews.is_empty() {
*control_flow = ControlFlow::Exit;
}
2021-04-29 01:56:05 +03:00
}
WryWindowEvent::Resized(_) => {
if let Err(e) = webviews[&window_id].resize() {
eprintln!("{}", e);
}
}
_ => {}
2021-04-29 01:56:05 +03:00
}
}
2021-04-29 01:56: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) => window_getter!(window, tx, scale_factor),
WindowMessage::InnerPosition(tx) => {
window_result_getter!(window, tx, inner_position)
}
WindowMessage::OuterPosition(tx) => {
window_result_getter!(window, tx, outer_position)
}
WindowMessage::InnerSize(tx) => window_getter!(window, tx, inner_size),
WindowMessage::OuterSize(tx) => window_getter!(window, tx, outer_size),
WindowMessage::IsFullscreen(tx) => tx.send(window.fullscreen().is_some()).unwrap(),
WindowMessage::IsMaximized(tx) => window_getter!(window, tx, is_maximized),
WindowMessage::CurrentMonitor(tx) => window_getter!(window, tx, current_monitor),
WindowMessage::PrimaryMonitor(tx) => window_getter!(window, tx, primary_monitor),
WindowMessage::AvailableMonitors(tx) => {
tx.send(window.available_monitors().collect()).unwrap()
}
// Setters
2021-04-29 01:56: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),
WindowMessage::Show => window.set_visible(true),
WindowMessage::Hide => window.set_visible(false),
WindowMessage::Close => {
webviews.remove(&id);
if webviews.is_empty() {
*control_flow = ControlFlow::Exit;
}
}
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(WrySize::from(size));
2021-04-29 01:56:05 +03:00
}
WindowMessage::SetMinSize(size) => {
window.set_min_inner_size(size.map(WrySize::from));
2021-04-29 01:56:05 +03:00
}
WindowMessage::SetMaxSize(size) => {
window.set_max_inner_size(size.map(WrySize::from));
2021-04-29 01:56:05 +03:00
}
WindowMessage::SetPosition(position) => {
window.set_outer_position(WryPosition::from(position))
2021-04-29 01:56:05 +03:00
}
WindowMessage::SetFullscreen(fullscreen) => {
if fullscreen {
window.set_fullscreen(Some(Fullscreen::Borderless(None)))
} else {
window.set_fullscreen(None)
}
}
WindowMessage::SetIcon(icon) => {
window.set_window_icon(Some(icon));
}
WindowMessage::DragWindow => {
let _ = window.drag_window();
2021-04-29 01:56: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);
}
WebviewMessage::Print => {
let _ = webview.print();
}
2021-04-29 01:56: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();
}
Err(e) => {
eprintln!("{}", e);
}
}
}
},
_ => (),
}
})
}
}
2021-04-29 01:56:05 +03:00
fn create_webview<M: Params<Runtime = Wry>>(
event_loop: &EventLoopWindowTarget<Message>,
2021-05-08 18:11:40 +03:00
context: DispatcherContext,
2021-04-29 01:56:05 +03:00
pending: PendingWindow<M>,
) -> crate::Result<WebView> {
let PendingWindow {
webview_attributes,
window_builder,
2021-04-29 01:56:05 +03:00
rpc_handler,
file_drop_handler,
label,
url,
..
} = pending;
let window = window_builder.build(event_loop).unwrap();
2021-04-29 01:56:05 +03:00
let mut webview_builder = WebViewBuilder::new(window)
.map_err(|e| crate::Error::CreateWebview(Box::new(e)))?
2021-04-29 01:56:05 +03:00
.with_url(&url)
.unwrap(); // safe to unwrap because we validate the URL beforehand
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()
.map_err(|e| crate::Error::CreateWebview(Box::new(e)))
2021-04-29 01:56:05 +03:00
}
/// Create a wry rpc handler from a tauri rpc handler.
fn create_rpc_handler<M: Params<Runtime = Wry>>(
2021-05-08 18:11:40 +03:00
context: DispatcherContext,
label: M::Label,
handler: WebviewRpcHandler<M>,
2021-04-29 01:56:05 +03:00
) -> Box<dyn Fn(&Window, WryRpcRequest) -> Option<RpcResponse> + 'static> {
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(),
},
label: label.clone(),
},
request.into(),
);
None
})
}
/// Create a wry file drop handler from a tauri file drop handler.
fn create_file_drop_handler<M: Params<Runtime = Wry>>(
2021-05-08 18:11:40 +03:00
context: DispatcherContext,
label: M::Label,
handler: FileDropHandler<M>,
2021-04-29 01:56:05 +03:00
) -> Box<dyn Fn(&Window, WryFileDropEvent) -> bool + 'static> {
Box::new(move |window, event| {
handler(
event.into(),
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(),
},
label: label.clone(),
},
)
})
}