mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-12-17 23:51:43 +03:00
refactor(core): move webview interface to the app module (#1230)
* refactor(core): move webview interface to the app module * fix: fmt
This commit is contained in:
parent
a3d6dff216
commit
54a5a13cda
@ -1,12 +1,16 @@
|
||||
use crate::ApplicationExt;
|
||||
use futures::future::BoxFuture;
|
||||
use std::marker::PhantomData;
|
||||
use tauri_api::{config::Config, private::AsTauriContext};
|
||||
|
||||
pub(crate) mod event;
|
||||
mod runner;
|
||||
mod webview;
|
||||
mod webview_manager;
|
||||
|
||||
pub use webview::{
|
||||
wry::WryApplication, ApplicationDispatcherExt, ApplicationExt, Callback, Event, Icon, Message,
|
||||
WebviewBuilderExt, WindowBuilderExt,
|
||||
};
|
||||
pub use webview_manager::{WebviewDispatcher, WebviewManager};
|
||||
|
||||
type InvokeHandler<D> =
|
||||
|
@ -7,7 +7,7 @@ use crate::api::assets::{AssetFetch, Assets};
|
||||
|
||||
use crate::{api::config::WindowUrl, ApplicationExt, WebviewBuilderExt};
|
||||
|
||||
use super::{App, WebviewDispatcher, WebviewManager};
|
||||
use super::{App, WebviewDispatcher, WebviewManager, WindowBuilderExt};
|
||||
#[cfg(embedded_server)]
|
||||
use crate::api::tcp::{get_available_port, port_is_available};
|
||||
use crate::app::Context;
|
||||
@ -268,8 +268,37 @@ fn build_webview<A: ApplicationExt + 'static>(
|
||||
let mut dispatchers = HashMap::new();
|
||||
|
||||
for window_config in application.context.config.tauri.windows.clone() {
|
||||
let window = crate::webview::WindowBuilder::from(&window_config);
|
||||
let window = webview_application.create_window(window.get())?;
|
||||
let mut window = A::WindowBuilder::new()
|
||||
.title(window_config.title.to_string())
|
||||
.width(window_config.width)
|
||||
.height(window_config.height)
|
||||
.visible(window_config.visible)
|
||||
.resizable(window_config.resizable)
|
||||
.decorations(window_config.decorations)
|
||||
.maximized(window_config.maximized)
|
||||
.fullscreen(window_config.fullscreen)
|
||||
.transparent(window_config.transparent)
|
||||
.always_on_top(window_config.always_on_top);
|
||||
if let Some(min_width) = window_config.min_width {
|
||||
window = window.min_width(min_width);
|
||||
}
|
||||
if let Some(min_height) = window_config.min_height {
|
||||
window = window.min_height(min_height);
|
||||
}
|
||||
if let Some(max_width) = window_config.max_width {
|
||||
window = window.max_width(max_width);
|
||||
}
|
||||
if let Some(max_height) = window_config.max_height {
|
||||
window = window.max_height(max_height);
|
||||
}
|
||||
if let Some(x) = window_config.x {
|
||||
window = window.x(x);
|
||||
}
|
||||
if let Some(y) = window_config.y {
|
||||
window = window.y(y);
|
||||
}
|
||||
|
||||
let window = webview_application.create_window(window)?;
|
||||
let dispatcher = webview_application.dispatcher(&window);
|
||||
dispatchers.insert(
|
||||
window_config.label.to_string(),
|
||||
|
@ -1,6 +1,6 @@
|
||||
pub(crate) mod wry;
|
||||
pub mod wry;
|
||||
|
||||
pub use crate::{api::config::WindowConfig, plugin::PluginStore};
|
||||
use crate::plugin::PluginStore;
|
||||
|
||||
/// An event to be posted to the webview event loop.
|
||||
pub enum Event {
|
||||
@ -8,45 +8,89 @@ pub enum Event {
|
||||
Run(crate::SyncTask),
|
||||
}
|
||||
|
||||
/// A icon definition.
|
||||
pub enum Icon {
|
||||
/// Icon from file path.
|
||||
File(String),
|
||||
/// Icon from raw bytes.
|
||||
Raw(Vec<u8>),
|
||||
}
|
||||
|
||||
/// Messages to dispatch to the application.
|
||||
pub enum Message {
|
||||
// webview messages
|
||||
/// Eval a script on the webview.
|
||||
EvalScript(String),
|
||||
// custom messages
|
||||
/// Custom event.
|
||||
Event(Event),
|
||||
// window messages
|
||||
/// Updates the window resizable flag.
|
||||
SetResizable(bool),
|
||||
/// Updates the window title.
|
||||
SetTitle(String),
|
||||
/// Maximizes the window.
|
||||
Maximize,
|
||||
/// Unmaximizes the window.
|
||||
Unmaximize,
|
||||
/// Minimizes the window.
|
||||
Minimize,
|
||||
/// Unminimizes the window.
|
||||
Unminimize,
|
||||
/// Shows the window.
|
||||
Show,
|
||||
/// Hides the window.
|
||||
Hide,
|
||||
/// Updates the transparency flag.
|
||||
SetTransparent(bool),
|
||||
/// Updates the hasDecorations flag.
|
||||
SetDecorations(bool),
|
||||
/// Updates the window alwaysOnTop flag.
|
||||
SetAlwaysOnTop(bool),
|
||||
/// Updates the window width.
|
||||
SetWidth(f64),
|
||||
/// Updates the window height.
|
||||
SetHeight(f64),
|
||||
Resize { width: f64, height: f64 },
|
||||
SetMinSize { min_width: f64, min_height: f64 },
|
||||
SetMaxSize { max_width: f64, max_height: f64 },
|
||||
/// Resizes the window.
|
||||
Resize {
|
||||
/// New width.
|
||||
width: f64,
|
||||
/// New height.
|
||||
height: f64,
|
||||
},
|
||||
/// Updates the window min size.
|
||||
SetMinSize {
|
||||
/// New value for the window min width.
|
||||
min_width: f64,
|
||||
/// New value for the window min height.
|
||||
min_height: f64,
|
||||
},
|
||||
/// Updates the window max size.
|
||||
SetMaxSize {
|
||||
/// New value for the window max width.
|
||||
max_width: f64,
|
||||
/// New value for the window max height.
|
||||
max_height: f64,
|
||||
},
|
||||
/// Updates the X position.
|
||||
SetX(f64),
|
||||
/// Updates the Y position.
|
||||
SetY(f64),
|
||||
SetPosition { x: f64, y: f64 },
|
||||
/// Updates the window position.
|
||||
SetPosition {
|
||||
/// New value for the window X coordinate.
|
||||
x: f64,
|
||||
/// New value for the window Y coordinate.
|
||||
y: f64,
|
||||
},
|
||||
/// Updates the window fullscreen state.
|
||||
SetFullscreen(bool),
|
||||
/// Updates the window icon.
|
||||
SetIcon(Icon),
|
||||
}
|
||||
|
||||
/// The window builder.
|
||||
pub trait WindowBuilderExt: Sized {
|
||||
/// The window type.
|
||||
type Window;
|
||||
|
||||
/// Initializes a new window builder.
|
||||
fn new() -> Self;
|
||||
|
||||
@ -98,53 +142,6 @@ pub trait WindowBuilderExt: Sized {
|
||||
|
||||
/// Whether the window should always be on top of other windows.
|
||||
fn always_on_top(self, always_on_top: bool) -> Self;
|
||||
|
||||
/// build the window.
|
||||
fn finish(self) -> crate::Result<Self::Window>;
|
||||
}
|
||||
|
||||
pub struct WindowBuilder<T>(T);
|
||||
|
||||
impl<T> WindowBuilder<T> {
|
||||
pub fn get(self) -> T {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: WindowBuilderExt> From<&WindowConfig> for WindowBuilder<T> {
|
||||
fn from(config: &WindowConfig) -> Self {
|
||||
let mut window = T::new()
|
||||
.title(config.title.to_string())
|
||||
.width(config.width)
|
||||
.height(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);
|
||||
if let Some(min_width) = config.min_width {
|
||||
window = window.min_width(min_width);
|
||||
}
|
||||
if let Some(min_height) = config.min_height {
|
||||
window = window.min_height(min_height);
|
||||
}
|
||||
if let Some(max_width) = config.max_width {
|
||||
window = window.max_width(max_width);
|
||||
}
|
||||
if let Some(max_height) = config.max_height {
|
||||
window = window.max_height(max_height);
|
||||
}
|
||||
if let Some(x) = config.x {
|
||||
window = window.x(x);
|
||||
}
|
||||
if let Some(y) = config.y {
|
||||
window = window.y(y);
|
||||
}
|
||||
|
||||
Self(window)
|
||||
}
|
||||
}
|
||||
|
||||
/// The webview builder.
|
@ -30,8 +30,6 @@ impl TryInto<wry::Icon> for Icon {
|
||||
}
|
||||
|
||||
impl WindowBuilderExt for wry::AppWindowAttributes {
|
||||
type Window = Self;
|
||||
|
||||
fn new() -> Self {
|
||||
Default::default()
|
||||
}
|
||||
@ -115,11 +113,6 @@ impl WindowBuilderExt for wry::AppWindowAttributes {
|
||||
self.always_on_top = always_on_top;
|
||||
self
|
||||
}
|
||||
|
||||
/// build the window.
|
||||
fn finish(self) -> crate::Result<Self::Window> {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// The webview builder.
|
||||
@ -270,7 +263,7 @@ impl ApplicationExt for WryApplication {
|
||||
fn create_window(&self, window_builder: Self::WindowBuilder) -> crate::Result<Self::Window> {
|
||||
let window = self
|
||||
.inner
|
||||
.create_window(window_builder.finish()?)
|
||||
.create_window(window_builder)
|
||||
.map_err(|_| crate::Error::CreateWindow)?;
|
||||
Ok(window)
|
||||
}
|
@ -1,9 +1,6 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{
|
||||
webview::{Event, Icon, Message},
|
||||
ApplicationDispatcherExt,
|
||||
};
|
||||
use super::{ApplicationDispatcherExt, Event, Icon, Message};
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
|
@ -3,7 +3,7 @@ use crate::{
|
||||
ask as ask_dialog, message as message_dialog, pick_folder, save_file, select, select_multiple,
|
||||
DialogSelection, Response,
|
||||
},
|
||||
ApplicationDispatcherExt,
|
||||
app::{ApplicationDispatcherExt, Event},
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value as JsonValue;
|
||||
@ -98,7 +98,7 @@ impl Cmd {
|
||||
.to_string();
|
||||
webview_manager
|
||||
.current_webview()?
|
||||
.send_event(crate::webview::Event::Run(Box::new(move || {
|
||||
.send_event(Event::Run(Box::new(move || {
|
||||
message_dialog(app_name, message);
|
||||
})));
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::webview::Icon;
|
||||
use crate::app::{ApplicationDispatcherExt, Icon};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -82,7 +82,7 @@ pub enum Cmd {
|
||||
}
|
||||
|
||||
impl Cmd {
|
||||
pub async fn run<D: crate::ApplicationDispatcherExt + 'static>(
|
||||
pub async fn run<D: ApplicationDispatcherExt + 'static>(
|
||||
self,
|
||||
webview_manager: &crate::WebviewManager<D>,
|
||||
) -> crate::Result<()> {
|
||||
|
@ -21,8 +21,6 @@ mod error;
|
||||
pub mod plugin;
|
||||
/// The salt helpers.
|
||||
mod salt;
|
||||
/// Webview interface.
|
||||
mod webview;
|
||||
|
||||
/// The Tauri error enum.
|
||||
pub use error::Error;
|
||||
@ -37,13 +35,10 @@ pub type SyncTask = Box<dyn FnOnce() + Send>;
|
||||
pub use app::*;
|
||||
pub use tauri_api as api;
|
||||
pub use tauri_macros::FromTauriContext;
|
||||
pub use webview::{
|
||||
ApplicationDispatcherExt, ApplicationExt, Callback, WebviewBuilderExt, WindowBuilderExt,
|
||||
};
|
||||
|
||||
/// The Tauri webview implementations.
|
||||
pub mod flavors {
|
||||
pub use super::webview::wry::WryApplication as Wry;
|
||||
pub use super::app::WryApplication as Wry;
|
||||
}
|
||||
|
||||
use std::process::Stdio;
|
||||
@ -54,7 +49,7 @@ use serde::Serialize;
|
||||
/// Synchronously executes the given task
|
||||
/// and evaluates its Result to the JS promise described by the `callback` and `error` function names.
|
||||
pub fn execute_promise_sync<
|
||||
D: ApplicationDispatcherExt + 'static,
|
||||
D: app::ApplicationDispatcherExt + 'static,
|
||||
R: Serialize,
|
||||
F: FnOnce() -> Result<R> + Send + 'static,
|
||||
>(
|
||||
@ -65,7 +60,7 @@ pub fn execute_promise_sync<
|
||||
) {
|
||||
let webview_manager_ = webview_manager.clone();
|
||||
if let Ok(dispatcher) = webview_manager.current_webview() {
|
||||
dispatcher.send_event(webview::Event::Run(Box::new(move || {
|
||||
dispatcher.send_event(app::Event::Run(Box::new(move || {
|
||||
let callback_string =
|
||||
match format_callback_result(task().map_err(|err| err.to_string()), &callback, &error) {
|
||||
Ok(js) => js,
|
||||
@ -89,7 +84,7 @@ pub fn execute_promise_sync<
|
||||
/// If the Result `is_ok()`, the callback will be the `success_callback` function name and the argument will be the Ok value.
|
||||
/// If the Result `is_err()`, the callback will be the `error_callback` function name and the argument will be the Err value.
|
||||
pub async fn execute_promise<
|
||||
D: ApplicationDispatcherExt,
|
||||
D: app::ApplicationDispatcherExt,
|
||||
R: Serialize,
|
||||
F: futures::Future<Output = Result<R>> + Send + 'static,
|
||||
>(
|
||||
@ -112,7 +107,7 @@ pub async fn execute_promise<
|
||||
}
|
||||
|
||||
/// Calls the given command and evaluates its output to the JS promise described by the `callback` and `error` function names.
|
||||
pub async fn call<D: ApplicationDispatcherExt>(
|
||||
pub async fn call<D: app::ApplicationDispatcherExt>(
|
||||
webview_manager: &crate::WebviewManager<D>,
|
||||
command: String,
|
||||
args: Vec<String>,
|
||||
|
Loading…
Reference in New Issue
Block a user