From 60b9893a72c8e8ff3fb482f35d3bc38d35266df5 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Wed, 24 Jul 2019 21:16:28 -0300 Subject: [PATCH 1/7] feat(proton) configure webview from quasar provided config.json --- .gitignore | 4 ++++ lib/rust/src/config.rs | 16 ++++++++++++++++ lib/rust/src/lib.rs | 1 + templates/rust/src/main.rs | 26 ++++++++++++++------------ 4 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 lib/rust/src/config.rs diff --git a/.gitignore b/.gitignore index aee53c804f3..7c4ba406d13 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,7 @@ typings/ debug.log package-lock.json .vscode/settings.json + +# Quasar output +bundle.json +config.json \ No newline at end of file diff --git a/lib/rust/src/config.rs b/lib/rust/src/config.rs new file mode 100644 index 00000000000..96d081accea --- /dev/null +++ b/lib/rust/src/config.rs @@ -0,0 +1,16 @@ +#[derive(Deserialize)] +#[serde(tag = "cmd", rename_all = "camelCase")] +pub struct Config { + #[serde(default)] + pub width: i32, + #[serde(default)] + pub height: i32, + #[serde(default)] + pub resizable: bool, + #[serde(default)] + pub title: String, +} + +pub fn get() -> Config { + serde_json::from_str(include_str!("../../../config.json")).unwrap() +} diff --git a/lib/rust/src/lib.rs b/lib/rust/src/lib.rs index 7aa56685d61..027294b4ccb 100644 --- a/lib/rust/src/lib.rs +++ b/lib/rust/src/lib.rs @@ -16,6 +16,7 @@ pub mod rpc; pub mod tcp; pub mod updater; pub mod version; +pub mod config; use proton_ui::WebView; diff --git a/templates/rust/src/main.rs b/templates/rust/src/main.rs index 8248d4d8412..cf5500b0e66 100755 --- a/templates/rust/src/main.rs +++ b/templates/rust/src/main.rs @@ -69,17 +69,16 @@ fn main() { debug = cfg!(debug_assertions); #[cfg(feature = "serverless")] { - fn inline_style(s: &str) -> String { + fn inline_style(s: &str) -> String { format!(r#""#, s) - } - - fn inline_script(s: &str) -> String { + } + fn inline_script(s: &str) -> String { format!(r#""#, s) - } - let html = format!(r#"{styles}
{scripts}"#, - styles = inline_style(include_str!("../target/compiled-web/css/app.css")), - scripts = inline_script(include_str!("../target/compiled-web/js/app.js")), - ); + } + let html = format!(r#"{styles}
{scripts}"#, + styles = inline_style(include_str!("../target/compiled-web/css/app.css")), + scripts = inline_script(include_str!("../target/compiled-web/js/app.js")), + ); content = proton_ui::Content::Html(html); } #[cfg(not(feature = "serverless"))] @@ -93,10 +92,13 @@ fn main() { } } + let config = proton::config::get(); + let webview = proton_ui::builder() - .title("MyApp - Serverless") - .size(800, 600) // TODO:Resolution is fixed right now, change this later to be dynamic - .resizable(true) + .title(&config.title) + .content(content) + .size(config.width, config.height) + .resizable(config.resizable) .debug(debug) .user_data(()) .invoke_handler(|webview, arg| { From ab0e6050ae7c51f286a69c2fa0f8b33051ff0d4f Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Wed, 31 Jul 2019 19:18:51 -0300 Subject: [PATCH 2/7] feat(config) window config --- lib/rust/src/api/cmd.rs | 5 +---- lib/rust/src/config.rs | 44 ++++++++++++++++++++++++++++++++------ lib/rust/src/lib.rs | 2 +- templates/rust/src/main.rs | 7 +++--- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/lib/rust/src/api/cmd.rs b/lib/rust/src/api/cmd.rs index 4257eca6d28..d6655e2ee1d 100644 --- a/lib/rust/src/api/cmd.rs +++ b/lib/rust/src/api/cmd.rs @@ -54,8 +54,5 @@ pub enum Cmd { once: bool, }, #[cfg(any(feature = "all-api", feature = "emit"))] - Emit { - event: String, - payload: String, - }, + Emit { event: String, payload: String }, } diff --git a/lib/rust/src/config.rs b/lib/rust/src/config.rs index 96d081accea..7cf5a17bba4 100644 --- a/lib/rust/src/config.rs +++ b/lib/rust/src/config.rs @@ -1,16 +1,48 @@ #[derive(Deserialize)] -#[serde(tag = "cmd", rename_all = "camelCase")] -pub struct Config { - #[serde(default)] +#[serde(tag = "window", rename_all = "camelCase")] +pub struct WindowConfig { + #[serde(default = "default_width")] pub width: i32, - #[serde(default)] + #[serde(default = "default_height")] pub height: i32, - #[serde(default)] + #[serde(default = "default_resizable")] pub resizable: bool, - #[serde(default)] + #[serde(default = "default_title")] pub title: String, } +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Config { + #[serde(default = "default_window")] + pub window: WindowConfig, +} + +fn default_width() -> i32 { + 800 +} + +fn default_height() -> i32 { + 600 +} + +fn default_resizable() -> bool { + true +} + +fn default_title() -> String { + "Quasar Proton App".to_string() +} + +fn default_window() -> WindowConfig { + return WindowConfig { + width: default_width(), + height: default_height(), + resizable: default_resizable(), + title: default_title(), + }; +} + pub fn get() -> Config { serde_json::from_str(include_str!("../../../config.json")).unwrap() } diff --git a/lib/rust/src/lib.rs b/lib/rust/src/lib.rs index 3cf1e5c2059..b37fd3409ad 100644 --- a/lib/rust/src/lib.rs +++ b/lib/rust/src/lib.rs @@ -9,6 +9,7 @@ extern crate lazy_static; pub mod api; pub mod command; +pub mod config; pub mod dir; pub mod event; pub mod file; @@ -21,7 +22,6 @@ pub mod salt; pub mod tcp; pub mod updater; pub mod version; -pub mod config; use proton_ui::WebView; diff --git a/templates/rust/src/main.rs b/templates/rust/src/main.rs index 7febd7feb20..a7f19fb6082 100755 --- a/templates/rust/src/main.rs +++ b/templates/rust/src/main.rs @@ -96,10 +96,9 @@ fn main() { let config = proton::config::get(); let webview = proton_ui::builder() - .title(&config.title) - .content(content) - .size(config.width, config.height) - .resizable(config.resizable) + .title(&config.window.title) + .size(config.window.width, config.window.height) + .resizable(config.window.resizable) .debug(debug) .user_data(()) .invoke_handler(|webview, arg| { From d334636be7dea97dae27918b206382d24bc9f865 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Wed, 31 Jul 2019 20:01:37 -0300 Subject: [PATCH 3/7] feat(config) rename serverless to embeddedServer --- templates/rust/Cargo.toml | 2 +- templates/rust/src/main.rs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/templates/rust/Cargo.toml b/templates/rust/Cargo.toml index 8ef1ebcfad0..1316409c8e9 100755 --- a/templates/rust/Cargo.toml +++ b/templates/rust/Cargo.toml @@ -26,7 +26,7 @@ includedir_codegen = "0.5.0" [features] dev = [] # has no explicit dependencies -serverless = [] # has no explicit dependencies +embedded-server = [] # has no explicit dependencies [package.metadata.bundle] identifier = "com.quasar.dev" diff --git a/templates/rust/src/main.rs b/templates/rust/src/main.rs index a7f19fb6082..9690fffe304 100755 --- a/templates/rust/src/main.rs +++ b/templates/rust/src/main.rs @@ -17,13 +17,13 @@ extern crate tiny_http; use clap::{App, Arg}; #[cfg(not(feature = "dev"))] -#[cfg(not(feature = "serverless"))] +#[cfg(feature = "embedded-server")] use std::thread; mod cmd; #[cfg(not(feature = "dev"))] -#[cfg(not(feature = "serverless"))] +#[cfg(feature = "embedded-server")] mod server; fn main() { @@ -67,7 +67,7 @@ fn main() { #[cfg(not(feature = "dev"))] { debug = cfg!(debug_assertions); - #[cfg(feature = "serverless")] + #[cfg(not(feature = "embedded-server"))] { fn inline_style(s: &str) -> String { format!(r#""#, s) @@ -82,7 +82,7 @@ fn main() { ); content = proton_ui::Content::Html(html); } - #[cfg(not(feature = "serverless"))] + #[cfg(feature = "embedded-server")] { if let Some(available_port) = proton::tcp::get_available_port() { _server_url = format!("{}:{}", "127.0.0.1", available_port); @@ -165,7 +165,7 @@ fn main() { #[cfg(not(feature = "dev"))] { - #[cfg(not(feature = "serverless"))] + #[cfg(feature = "embedded-server")] { thread::spawn(move || { let server = tiny_http::Server::http(_server_url).unwrap(); From 25f3ddca434ce567caf106f90df473ab66065c08 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Wed, 31 Jul 2019 20:48:51 -0300 Subject: [PATCH 4/7] feat(config) embedded server host and port config --- lib/rust/src/config.rs | 40 +++++++++++++++++++++++++++++++------- templates/rust/src/main.rs | 34 ++++++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/lib/rust/src/config.rs b/lib/rust/src/config.rs index 7cf5a17bba4..e09fbc2ea19 100644 --- a/lib/rust/src/config.rs +++ b/lib/rust/src/config.rs @@ -11,13 +11,6 @@ pub struct WindowConfig { pub title: String, } -#[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct Config { - #[serde(default = "default_window")] - pub window: WindowConfig, -} - fn default_width() -> i32 { 800 } @@ -43,6 +36,39 @@ fn default_window() -> WindowConfig { }; } +#[derive(Deserialize)] +#[serde(tag = "embeddedServer", rename_all = "camelCase")] +pub struct EmbeddedServerConfig { + #[serde(default = "default_host")] + pub host: String, + #[serde(default = "default_port")] + pub port: String +} + +fn default_host() -> String { + "http://127.0.0.1".to_string() +} + +fn default_port() -> String { + "random".to_string() +} + +fn default_embedded_server() -> EmbeddedServerConfig { + EmbeddedServerConfig { + host: default_host(), + port: default_port() + } +} + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Config { + #[serde(default = "default_window")] + pub window: WindowConfig, + #[serde(default = "default_embedded_server")] + pub embedded_server: EmbeddedServerConfig +} + pub fn get() -> Config { serde_json::from_str(include_str!("../../../config.json")).unwrap() } diff --git a/templates/rust/src/main.rs b/templates/rust/src/main.rs index 9690fffe304..5ca9ca7165c 100755 --- a/templates/rust/src/main.rs +++ b/templates/rust/src/main.rs @@ -29,7 +29,9 @@ mod server; fn main() { let debug; let content; - let _server_url: String; + let config; + #[cfg(feature = "embedded-server")] + let server_url: String; #[cfg(feature = "updater")] { @@ -66,6 +68,7 @@ fn main() { #[cfg(not(feature = "dev"))] { + config = proton::config::get(); debug = cfg!(debug_assertions); #[cfg(not(feature = "embedded-server"))] { @@ -84,17 +87,32 @@ fn main() { } #[cfg(feature = "embedded-server")] { - if let Some(available_port) = proton::tcp::get_available_port() { - _server_url = format!("{}:{}", "127.0.0.1", available_port); - content = proton_ui::Content::Url(format!("http://{}", _server_url)); + let port; + let port_valid; + if config.embedded_server.port == "random" { + match proton::tcp::get_available_port() { + Some(available_port) => { + port = available_port.to_string(); + port_valid = true; + } + None => { + port = "0".to_string(); + port_valid = false; + } + } } else { - panic!("Could not find an open port"); + port = config.embedded_server.port; + port_valid = proton::tcp::port_is_available(port.parse::().expect(&format!("Invalid port {}", port))); + } + if port_valid { + server_url = format!("{}:{}", config.embedded_server.host, port); + content = proton_ui::Content::Url(server_url.clone()); + } else { + panic!(format!("Port {} is not valid or not open", port)); } } } - let config = proton::config::get(); - let webview = proton_ui::builder() .title(&config.window.title) .size(config.window.width, config.window.height) @@ -168,7 +186,7 @@ fn main() { #[cfg(feature = "embedded-server")] { thread::spawn(move || { - let server = tiny_http::Server::http(_server_url).unwrap(); + let server = tiny_http::Server::http(server_url.clone()).expect(&format!("Could not start embedded server with the specified url: {}", server_url)); for request in server.incoming_requests() { let mut url = request.url().to_string(); if url == "/" { From ab57f938ab4f92fe50866657da5931146cf9c636 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Wed, 31 Jul 2019 23:21:02 -0300 Subject: [PATCH 5/7] feat(template) q-app inlines assets on output index.html --- templates/rust/src/main.rs | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/templates/rust/src/main.rs b/templates/rust/src/main.rs index 5ca9ca7165c..96834b7c34d 100755 --- a/templates/rust/src/main.rs +++ b/templates/rust/src/main.rs @@ -72,18 +72,7 @@ fn main() { debug = cfg!(debug_assertions); #[cfg(not(feature = "embedded-server"))] { - fn inline_style(s: &str) -> String { - format!(r#""#, s) - } - - fn inline_script(s: &str) -> String { - format!(r#""#, s) - } - let html = format!(r#"{styles}
{scripts}"#, - styles = inline_style(include_str!("../target/compiled-web/css/app.css")), - scripts = inline_script(include_str!("../target/compiled-web/js/app.js")), - ); - content = proton_ui::Content::Html(html); + content = proton_ui::Content::Html(include_str!("../target/compiled-web/index.html")); } #[cfg(feature = "embedded-server")] { From b1792c7712818eb5f79a05022fe5b72e59b78ff6 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Sat, 3 Aug 2019 23:51:51 -0300 Subject: [PATCH 6/7] feat(proton) move server code to lib --- lib/rust/Cargo.toml | 5 +++++ lib/rust/build.rs | 10 ++++++++++ lib/rust/src/lib.rs | 10 ++++------ lib/rust/src/server.rs | 26 ++++++++++++++++++++++++++ templates/rust/Cargo.toml | 7 ------- templates/rust/src/main.rs | 7 +------ 6 files changed, 46 insertions(+), 19 deletions(-) create mode 100644 lib/rust/build.rs create mode 100644 lib/rust/src/server.rs diff --git a/lib/rust/Cargo.toml b/lib/rust/Cargo.toml index 9e31112902a..171467c7bf6 100644 --- a/lib/rust/Cargo.toml +++ b/lib/rust/Cargo.toml @@ -29,6 +29,11 @@ sysinfo = "0.9" webbrowser = "0.5.1" uuid = { version = "0.7", features = ["v4"] } lazy_static = "1.3.0" +includedir = "0.5.0" +tiny_http = "0.6" + +[build-dependencies] +includedir_codegen = "0.5.0" [features] all-api = [] diff --git a/lib/rust/build.rs b/lib/rust/build.rs new file mode 100644 index 00000000000..07829cd86e6 --- /dev/null +++ b/lib/rust/build.rs @@ -0,0 +1,10 @@ +extern crate includedir_codegen; + +use includedir_codegen::Compression; + +fn main() { + includedir_codegen::start("ASSETS") + .dir("./target/compiled-web", Compression::Gzip) + .build("data.rs") + .unwrap(); +} diff --git a/lib/rust/src/lib.rs b/lib/rust/src/lib.rs index b37fd3409ad..2cd6a499dba 100644 --- a/lib/rust/src/lib.rs +++ b/lib/rust/src/lib.rs @@ -7,6 +7,9 @@ mod macros; #[macro_use] extern crate lazy_static; +extern crate includedir; +extern crate phf; + pub mod api; pub mod command; pub mod config; @@ -22,6 +25,7 @@ pub mod salt; pub mod tcp; pub mod updater; pub mod version; +pub mod server; use proton_ui::WebView; @@ -37,12 +41,6 @@ pub fn spawn () + Send + 'static>(what: F) { }); } -pub fn run_async () + Send + 'static>(what: F) { - POOL.with(|thread| { - thread.execute(move || what()); - }); -} - pub fn execute_promise Result + Send + 'static>( webview: &mut WebView<'_, T>, what: F, diff --git a/lib/rust/src/server.rs b/lib/rust/src/server.rs new file mode 100644 index 00000000000..5a7534d1597 --- /dev/null +++ b/lib/rust/src/server.rs @@ -0,0 +1,26 @@ +use tiny_http::{Header, Response}; + +include!(concat!(env!("OUT_DIR"), "/data.rs")); + +pub fn asset_response(path: &str) -> Response>> { + let asset = ASSETS + .get(&format!("./target/compiled-web{}", path)) + .unwrap() + .into_owned(); + let mut response = Response::from_data(asset); + let header; + + if path.ends_with(".svg") { + header = Header::from_bytes(&b"Content-Type"[..], &b"image/svg+xml"[..]).unwrap(); + } else if path.ends_with(".css") { + header = Header::from_bytes(&b"Content-Type"[..], &b"text/css"[..]).unwrap(); + } else if path.ends_with(".html") { + header = Header::from_bytes(&b"Content-Type"[..], &b"text/html"[..]).unwrap(); + } else { + header = Header::from_bytes(&b"Content-Type"[..], &b"appication/octet-stream"[..]).unwrap(); + } + + response.add_header(header); + + response +} diff --git a/templates/rust/Cargo.toml b/templates/rust/Cargo.toml index 1316409c8e9..35124456790 100755 --- a/templates/rust/Cargo.toml +++ b/templates/rust/Cargo.toml @@ -5,8 +5,6 @@ description = "A Quasar Proton App" author = [] license = "" repository = "" -build = "build.rs" -include = ["data"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -17,13 +15,8 @@ serde = "1.0" serde_derive = "1.0" tiny_http = "0.6" clap = {version = "2.33", features = ["yaml"]} -phf = "0.7.21" -includedir = "0.5.0" proton = { path = "../../proton/lib/rust" } -[build-dependencies] -includedir_codegen = "0.5.0" - [features] dev = [] # has no explicit dependencies embedded-server = [] # has no explicit dependencies diff --git a/templates/rust/src/main.rs b/templates/rust/src/main.rs index 96834b7c34d..c698f8052a1 100755 --- a/templates/rust/src/main.rs +++ b/templates/rust/src/main.rs @@ -5,11 +5,6 @@ extern crate proton; extern crate proton_ui; extern crate serde_json; -#[cfg(not(feature = "dev"))] -extern crate includedir; -#[cfg(not(feature = "dev"))] -extern crate phf; - #[cfg(not(feature = "dev"))] extern crate tiny_http; @@ -181,7 +176,7 @@ fn main() { if url == "/" { url = "/index.html".to_string(); } - request.respond(server::asset_response(&url)).unwrap(); + request.respond(proton::server::asset_response(&url)).unwrap(); } }); } From 35074eae53c81b5660c4b4ab5f186d8ace3ccf84 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Wed, 14 Aug 2019 18:28:42 -0300 Subject: [PATCH 7/7] fix(template) misc fixes --- lib/rust/src/config.rs | 2 +- templates/rust/build.rs | 10 ---------- templates/rust/src/main.rs | 19 +++++++------------ templates/rust/src/server.rs | 26 -------------------------- 4 files changed, 8 insertions(+), 49 deletions(-) delete mode 100755 templates/rust/build.rs delete mode 100644 templates/rust/src/server.rs diff --git a/lib/rust/src/config.rs b/lib/rust/src/config.rs index e09fbc2ea19..84408b62a86 100644 --- a/lib/rust/src/config.rs +++ b/lib/rust/src/config.rs @@ -24,7 +24,7 @@ fn default_resizable() -> bool { } fn default_title() -> String { - "Quasar Proton App".to_string() + "Quasar Tauri App".to_string() } fn default_window() -> WindowConfig { diff --git a/templates/rust/build.rs b/templates/rust/build.rs deleted file mode 100755 index 07829cd86e6..00000000000 --- a/templates/rust/build.rs +++ /dev/null @@ -1,10 +0,0 @@ -extern crate includedir_codegen; - -use includedir_codegen::Compression; - -fn main() { - includedir_codegen::start("ASSETS") - .dir("./target/compiled-web", Compression::Gzip) - .build("data.rs") - .unwrap(); -} diff --git a/templates/rust/src/main.rs b/templates/rust/src/main.rs index 1f0404acf7e..c58d340e507 100755 --- a/templates/rust/src/main.rs +++ b/templates/rust/src/main.rs @@ -17,14 +17,10 @@ use std::thread; mod cmd; -#[cfg(not(feature = "dev"))] -#[cfg(feature = "embedded-server")] -mod server; - fn main() { let debug; let content; - let config; + let config = tauri::config::get(); #[cfg(feature = "embedded-server")] let server_url: String; @@ -63,18 +59,17 @@ fn main() { #[cfg(not(feature = "dev"))] { - config = proton::config::get(); debug = cfg!(debug_assertions); #[cfg(not(feature = "embedded-server"))] { - content = proton_ui::Content::Html(include_str!("../target/compiled-web/index.html")); + content = tauri_ui::Content::Html(include_str!("../target/compiled-web/index.html")); } #[cfg(feature = "embedded-server")] { let port; let port_valid; if config.embedded_server.port == "random" { - match proton::tcp::get_available_port() { + match tauri::tcp::get_available_port() { Some(available_port) => { port = available_port.to_string(); port_valid = true; @@ -86,18 +81,18 @@ fn main() { } } else { port = config.embedded_server.port; - port_valid = proton::tcp::port_is_available(port.parse::().expect(&format!("Invalid port {}", port))); + port_valid = tauri::tcp::port_is_available(port.parse::().expect(&format!("Invalid port {}", port))); } if port_valid { server_url = format!("{}:{}", config.embedded_server.host, port); - content = proton_ui::Content::Url(server_url.clone()); + content = tauri_ui::Content::Url(server_url.clone()); } else { panic!(format!("Port {} is not valid or not open", port)); } } } - let webview = proton_ui::builder() + let webview = tauri_ui::builder() .title(&config.window.title) .size(config.window.width, config.window.height) .resizable(config.window.resizable) @@ -176,7 +171,7 @@ fn main() { if url == "/" { url = "/index.html".to_string(); } - request.respond(proton::server::asset_response(&url)).unwrap(); + request.respond(tauri::server::asset_response(&url)).unwrap(); } }); } diff --git a/templates/rust/src/server.rs b/templates/rust/src/server.rs deleted file mode 100644 index 5a7534d1597..00000000000 --- a/templates/rust/src/server.rs +++ /dev/null @@ -1,26 +0,0 @@ -use tiny_http::{Header, Response}; - -include!(concat!(env!("OUT_DIR"), "/data.rs")); - -pub fn asset_response(path: &str) -> Response>> { - let asset = ASSETS - .get(&format!("./target/compiled-web{}", path)) - .unwrap() - .into_owned(); - let mut response = Response::from_data(asset); - let header; - - if path.ends_with(".svg") { - header = Header::from_bytes(&b"Content-Type"[..], &b"image/svg+xml"[..]).unwrap(); - } else if path.ends_with(".css") { - header = Header::from_bytes(&b"Content-Type"[..], &b"text/css"[..]).unwrap(); - } else if path.ends_with(".html") { - header = Header::from_bytes(&b"Content-Type"[..], &b"text/html"[..]).unwrap(); - } else { - header = Header::from_bytes(&b"Content-Type"[..], &b"appication/octet-stream"[..]).unwrap(); - } - - response.add_header(header); - - response -}