|
@@ -12,24 +12,12 @@ use web_view::{builder, Content, WebView};
|
|
|
use super::App;
|
|
|
#[cfg(feature = "embedded-server")]
|
|
|
use crate::api::tcp::{get_available_port, port_is_available};
|
|
|
-use tauri_api::config::{get, Config};
|
|
|
-
|
|
|
-#[cfg(feature = "cli")]
|
|
|
-use tauri_api::cli::get_matches;
|
|
|
+use tauri_api::config::get;
|
|
|
|
|
|
// Main entry point function for running the Webview
|
|
|
pub(crate) fn run(application: &mut App) -> crate::Result<()> {
|
|
|
- // get the tauri config struct
|
|
|
- let config = get()?;
|
|
|
-
|
|
|
- #[cfg(feature = "cli")]
|
|
|
- {
|
|
|
- let matches = get_matches(config.clone());
|
|
|
- crate::cli::set_matches(matches)?;
|
|
|
- }
|
|
|
-
|
|
|
// setup the content using the config struct depending on the compile target
|
|
|
- let main_content = setup_content(config.clone())?;
|
|
|
+ let main_content = setup_content()?;
|
|
|
|
|
|
// setup the server url for the embedded-server
|
|
|
#[cfg(feature = "embedded-server")]
|
|
@@ -44,7 +32,6 @@ pub(crate) fn run(application: &mut App) -> crate::Result<()> {
|
|
|
// build the webview
|
|
|
let webview = build_webview(
|
|
|
application,
|
|
|
- config,
|
|
|
main_content,
|
|
|
if application.splashscreen_html().is_some() {
|
|
|
Some(Content::Html(
|
|
@@ -74,12 +61,13 @@ pub(crate) fn run(application: &mut App) -> crate::Result<()> {
|
|
|
|
|
|
// setup content for dev-server
|
|
|
#[cfg(not(any(feature = "embedded-server", feature = "no-server")))]
|
|
|
-fn setup_content(config: Config) -> crate::Result<Content<String>> {
|
|
|
+fn setup_content() -> crate::Result<Content<String>> {
|
|
|
+ let config = get()?;
|
|
|
if config.build.dev_path.starts_with("http") {
|
|
|
- Ok(Content::Url(config.build.dev_path))
|
|
|
+ Ok(Content::Url(config.build.dev_path.clone()))
|
|
|
} else {
|
|
|
- let dev_dir = config.build.dev_path;
|
|
|
- let dev_path = Path::new(&dev_dir).join("index.tauri.html");
|
|
|
+ let dev_dir = &config.build.dev_path;
|
|
|
+ let dev_path = Path::new(dev_dir).join("index.tauri.html");
|
|
|
if !dev_path.exists() {
|
|
|
panic!(
|
|
|
"Couldn't find 'index.tauri.html' inside {}; did you forget to run 'tauri dev'?",
|
|
@@ -92,16 +80,21 @@ fn setup_content(config: Config) -> crate::Result<Content<String>> {
|
|
|
|
|
|
// setup content for embedded server
|
|
|
#[cfg(feature = "embedded-server")]
|
|
|
-fn setup_content(config: Config) -> crate::Result<Content<String>> {
|
|
|
- let (port, valid) = setup_port(config.clone()).expect("Unable to setup Port");
|
|
|
- let url = setup_server_url(config.clone(), valid, port).expect("Unable to setup URL");
|
|
|
+fn setup_content() -> crate::Result<Content<String>> {
|
|
|
+ let (port, valid) = setup_port()?;
|
|
|
+ let url = (if valid {
|
|
|
+ setup_server_url(port)
|
|
|
+ } else {
|
|
|
+ Err(anyhow::anyhow!("invalid port"))
|
|
|
+ })
|
|
|
+ .expect("Unable to setup URL");
|
|
|
|
|
|
Ok(Content::Url(url.to_string()))
|
|
|
}
|
|
|
|
|
|
// setup content for no-server
|
|
|
#[cfg(feature = "no-server")]
|
|
|
-fn setup_content(_: Config) -> crate::Result<Content<String>> {
|
|
|
+fn setup_content() -> crate::Result<Content<String>> {
|
|
|
let dist_dir = match option_env!("TAURI_DIST_DIR") {
|
|
|
Some(d) => d.to_string(),
|
|
|
None => env::current_dir()?
|
|
@@ -116,35 +109,33 @@ fn setup_content(_: Config) -> crate::Result<Content<String>> {
|
|
|
|
|
|
// get the port for the embedded server
|
|
|
#[cfg(feature = "embedded-server")]
|
|
|
-fn setup_port(config: Config) -> Option<(String, bool)> {
|
|
|
+fn setup_port() -> crate::Result<(String, bool)> {
|
|
|
+ let config = get()?;
|
|
|
if config.tauri.embedded_server.port == "random" {
|
|
|
match get_available_port() {
|
|
|
- Some(available_port) => Some((available_port.to_string(), true)),
|
|
|
- None => Some(("0".to_string(), false)),
|
|
|
+ Some(available_port) => Ok((available_port.to_string(), true)),
|
|
|
+ None => Ok(("0".to_string(), false)),
|
|
|
}
|
|
|
} else {
|
|
|
- let port = config.tauri.embedded_server.port;
|
|
|
+ let port = &config.tauri.embedded_server.port;
|
|
|
let port_valid = port_is_available(
|
|
|
port
|
|
|
.parse::<u16>()
|
|
|
.expect(&format!("Invalid port {}", port)),
|
|
|
);
|
|
|
- Some((port, port_valid))
|
|
|
+ Ok((port.to_string(), port_valid))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// setup the server url for embedded server
|
|
|
#[cfg(feature = "embedded-server")]
|
|
|
-fn setup_server_url(config: Config, valid: bool, port: String) -> Option<String> {
|
|
|
- if valid {
|
|
|
- let mut url = format!("{}:{}", config.tauri.embedded_server.host, port);
|
|
|
- if !url.starts_with("http") {
|
|
|
- url = format!("http://{}", url);
|
|
|
- }
|
|
|
- Some(url)
|
|
|
- } else {
|
|
|
- None
|
|
|
+fn setup_server_url(port: String) -> crate::Result<String> {
|
|
|
+ let config = get()?;
|
|
|
+ let mut url = format!("{}:{}", config.tauri.embedded_server.host, port);
|
|
|
+ if !url.starts_with("http") {
|
|
|
+ url = format!("http://{}", url);
|
|
|
}
|
|
|
+ Ok(url)
|
|
|
}
|
|
|
|
|
|
// spawn the embedded server
|
|
@@ -186,22 +177,21 @@ fn spawn_updater() -> crate::Result<()> {
|
|
|
// build the webview struct
|
|
|
fn build_webview(
|
|
|
application: &mut App,
|
|
|
- config: Config,
|
|
|
content: Content<String>,
|
|
|
splashscreen_content: Option<Content<String>>,
|
|
|
) -> crate::Result<WebView<'_, ()>> {
|
|
|
+ let config = get()?;
|
|
|
let content_clone = match content {
|
|
|
Content::Html(ref html) => Content::Html(html.clone()),
|
|
|
Content::Url(ref url) => Content::Url(url.clone()),
|
|
|
};
|
|
|
let debug = cfg!(debug_assertions);
|
|
|
- let config_clone = config.clone();
|
|
|
// get properties from config struct
|
|
|
let width = config.tauri.window.width;
|
|
|
let height = config.tauri.window.height;
|
|
|
let resizable = config.tauri.window.resizable;
|
|
|
let fullscreen = config.tauri.window.fullscreen;
|
|
|
- let title = config.tauri.window.title.into_boxed_str();
|
|
|
+ let title = config.tauri.window.title.clone().into_boxed_str();
|
|
|
|
|
|
let has_splashscreen = splashscreen_content.is_some();
|
|
|
let mut initialized_splashscreen = false;
|
|
@@ -229,9 +219,7 @@ fn build_webview(
|
|
|
webview.eval(&format!(r#"window.location.href = "{}""#, content_href))?;
|
|
|
} else {
|
|
|
let handler_error;
|
|
|
- if let Err(tauri_handle_error) =
|
|
|
- crate::endpoints::handle(webview, arg, config_clone.clone())
|
|
|
- {
|
|
|
+ if let Err(tauri_handle_error) = crate::endpoints::handle(webview, arg) {
|
|
|
let tauri_handle_error_str = tauri_handle_error.to_string();
|
|
|
if tauri_handle_error_str.contains("unknown variant") {
|
|
|
let handled_by_app = application.run_invoke_handler(webview, arg);
|
|
@@ -295,14 +283,13 @@ mod test {
|
|
|
#[cfg(not(feature = "embedded-server"))]
|
|
|
use std::{env, fs::read_to_string, path::Path};
|
|
|
|
|
|
- fn init_config() -> tauri_api::config::Config {
|
|
|
+ fn init_config() -> &'static tauri_api::config::Config {
|
|
|
tauri_api::config::get().expect("unable to setup default config")
|
|
|
}
|
|
|
|
|
|
#[test]
|
|
|
fn check_setup_content() {
|
|
|
let config = init_config();
|
|
|
- let _c = config.clone();
|
|
|
|
|
|
let tauri_dir = match option_env!("TAURI_DIR") {
|
|
|
Some(d) => d.to_string(),
|
|
@@ -313,7 +300,7 @@ mod test {
|
|
|
.expect("Unable to convert to normal String"),
|
|
|
};
|
|
|
env::set_current_dir(tauri_dir).expect("failed to change cwd");
|
|
|
- let res = super::setup_content(config);
|
|
|
+ let res = super::setup_content();
|
|
|
|
|
|
#[cfg(feature = "embedded-server")]
|
|
|
match res {
|
|
@@ -342,10 +329,10 @@ mod test {
|
|
|
|
|
|
#[cfg(not(any(feature = "embedded-server", feature = "no-server")))]
|
|
|
match res {
|
|
|
- Ok(Content::Url(dp)) => assert_eq!(dp, _c.build.dev_path),
|
|
|
+ Ok(Content::Url(dp)) => assert_eq!(dp, config.build.dev_path),
|
|
|
Ok(Content::Html(s)) => {
|
|
|
- let dev_dir = _c.build.dev_path;
|
|
|
- let dev_path = Path::new(&dev_dir).join("index.tauri.html");
|
|
|
+ let dev_dir = &config.build.dev_path;
|
|
|
+ let dev_path = Path::new(dev_dir).join("index.tauri.html");
|
|
|
assert_eq!(
|
|
|
s,
|
|
|
read_to_string(dev_path).expect("failed to read dev path")
|
|
@@ -358,11 +345,9 @@ mod test {
|
|
|
#[cfg(feature = "embedded-server")]
|
|
|
#[test]
|
|
|
fn check_setup_port() {
|
|
|
- let config = init_config();
|
|
|
-
|
|
|
- let res = super::setup_port(config);
|
|
|
+ let res = super::setup_port();
|
|
|
match res {
|
|
|
- Some((_s, _b)) => assert!(true),
|
|
|
+ Ok((_s, _b)) => assert!(true),
|
|
|
_ => assert!(false),
|
|
|
}
|
|
|
}
|
|
@@ -372,16 +357,13 @@ mod test {
|
|
|
#[cfg(feature = "embedded-server")]
|
|
|
#[test]
|
|
|
fn check_server_url(port in (any::<u32>().prop_map(|v| v.to_string()))) {
|
|
|
- let config = init_config();
|
|
|
- let valid = true;
|
|
|
-
|
|
|
let p = port.clone();
|
|
|
|
|
|
- let res = super::setup_server_url(config, valid, port);
|
|
|
+ let res = super::setup_server_url(port);
|
|
|
|
|
|
match res {
|
|
|
- Some(url) => assert!(url.contains(&p)),
|
|
|
- None => assert!(false)
|
|
|
+ Ok(url) => assert!(url.contains(&p)),
|
|
|
+ Err(_) => assert!(false)
|
|
|
}
|
|
|
}
|
|
|
}
|