From a3c22e921610b28a404827d7fa464666cd1647b4 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Fri, 26 May 2023 00:06:34 -0300 Subject: [PATCH] fix mobile build --- examples/parent-window/src/desktop.rs | 39 +++++++++++ examples/parent-window/src/main.rs | 36 ++-------- examples/splashscreen/src/desktop.rs | 98 +++++++++++++++++++++++++++ examples/splashscreen/src/main.rs | 92 ++----------------------- 4 files changed, 145 insertions(+), 120 deletions(-) create mode 100644 examples/parent-window/src/desktop.rs create mode 100644 examples/splashscreen/src/desktop.rs diff --git a/examples/parent-window/src/desktop.rs b/examples/parent-window/src/desktop.rs new file mode 100644 index 000000000..b9dde21dc --- /dev/null +++ b/examples/parent-window/src/desktop.rs @@ -0,0 +1,39 @@ +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +use tauri::{command, window::WindowBuilder, Window, WindowUrl}; + +#[command] +async fn create_child_window(id: String, window: Window) { + let child = WindowBuilder::new(&window, id, WindowUrl::default()) + .title("Child") + .inner_size(400.0, 300.0); + + #[cfg(target_os = "macos")] + let child = child.parent_window(window.ns_window().unwrap()); + #[cfg(windows)] + let child = child.parent_window(window.hwnd().unwrap()); + + child.build().unwrap(); +} + +pub fn main() { + tauri::Builder::default() + .on_page_load(|window, _payload| { + let label = window.label().to_string(); + window.listen("clicked".to_string(), move |_payload| { + println!("got 'clicked' event on window '{label}'"); + }); + }) + .invoke_handler(tauri::generate_handler![create_child_window]) + .setup(|app| { + WindowBuilder::new(app, "main".to_string(), WindowUrl::default()) + .title("Main") + .inner_size(600.0, 400.0) + .build()?; + Ok(()) + }) + .run(tauri::build_script_context!()) + .expect("failed to run tauri application"); +} diff --git a/examples/parent-window/src/main.rs b/examples/parent-window/src/main.rs index 411008dcc..9615e547f 100644 --- a/examples/parent-window/src/main.rs +++ b/examples/parent-window/src/main.rs @@ -4,38 +4,10 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -use tauri::{command, window::WindowBuilder, Window, WindowUrl}; - -#[command] -async fn create_child_window(id: String, window: Window) { - let child = WindowBuilder::new(&window, id, WindowUrl::default()) - .title("Child") - .inner_size(400.0, 300.0); - - #[cfg(target_os = "macos")] - let child = child.parent_window(window.ns_window().unwrap()); - #[cfg(windows)] - let child = child.parent_window(window.hwnd().unwrap()); - - child.build().unwrap(); -} +#[cfg(desktop)] +mod desktop; fn main() { - tauri::Builder::default() - .on_page_load(|window, _payload| { - let label = window.label().to_string(); - window.listen("clicked".to_string(), move |_payload| { - println!("got 'clicked' event on window '{label}'"); - }); - }) - .invoke_handler(tauri::generate_handler![create_child_window]) - .setup(|app| { - WindowBuilder::new(app, "main".to_string(), WindowUrl::default()) - .title("Main") - .inner_size(600.0, 400.0) - .build()?; - Ok(()) - }) - .run(tauri::build_script_context!()) - .expect("failed to run tauri application"); + #[cfg(desktop)] + desktop::main(); } diff --git a/examples/splashscreen/src/desktop.rs b/examples/splashscreen/src/desktop.rs new file mode 100644 index 000000000..53af74290 --- /dev/null +++ b/examples/splashscreen/src/desktop.rs @@ -0,0 +1,98 @@ +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +#![cfg(desktop)] +#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] + +// Application code for a splashscreen system that waits on a Rust initialization script +mod rust { + use std::{thread::sleep, time::Duration}; + use tauri::Manager; + + // this command is here just so the example doesn't throw an error + #[tauri::command] + fn close_splashscreen() {} + + pub fn main() { + tauri::Builder::default() + .setup(|app| { + let splashscreen_window = app.get_window("splashscreen").unwrap(); + let main_window = app.get_window("main").unwrap(); + // we perform the initialization code on a new task so the app doesn't crash + tauri::async_runtime::spawn(async move { + println!("Initializing..."); + sleep(Duration::from_secs(2)); + println!("Done initializing."); + + // After it's done, close the splashscreen and display the main window + splashscreen_window.close().unwrap(); + main_window.show().unwrap(); + }); + Ok(()) + }) + .invoke_handler(tauri::generate_handler![close_splashscreen]) + .run(super::context()) + .expect("failed to run app"); + } +} + +// Application code for a splashscreen system that waits for the UI +mod ui { + use std::sync::{Arc, Mutex}; + use tauri::{Manager, State, Window}; + + // wrappers around each Window + // we use a dedicated type because Tauri can only manage a single instance of a given type + struct SplashscreenWindow(Arc>); + struct MainWindow(Arc>); + + #[tauri::command] + fn close_splashscreen( + _: Window, // force inference of P + splashscreen: State, + main: State, + ) { + // Close splashscreen + splashscreen.0.lock().unwrap().close().unwrap(); + // Show main window + main.0.lock().unwrap().show().unwrap(); + } + + pub fn main() { + let context = super::context(); + tauri::Builder::default() + .menu(if cfg!(target_os = "macos") { + tauri::Menu::os_default(&context.package_info().name) + } else { + tauri::Menu::default() + }) + .setup(|app| { + // set the splashscreen and main windows to be globally available with the tauri state API + app.manage(SplashscreenWindow(Arc::new(Mutex::new( + app.get_window("splashscreen").unwrap(), + )))); + app.manage(MainWindow(Arc::new(Mutex::new( + app.get_window("main").unwrap(), + )))); + Ok(()) + }) + .invoke_handler(tauri::generate_handler![close_splashscreen]) + .run(context) + .expect("error while running tauri application"); + } +} + +fn context() -> tauri::Context { + tauri::build_script_context!() +} + +pub fn main() { + // toggle this flag to experiment with different splashscreen usages + let ui = false; + if ui { + ui::main(); + } else { + rust::main(); + } +} diff --git a/examples/splashscreen/src/main.rs b/examples/splashscreen/src/main.rs index 295d0856e..9615e547f 100644 --- a/examples/splashscreen/src/main.rs +++ b/examples/splashscreen/src/main.rs @@ -4,94 +4,10 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -// Application code for a splashscreen system that waits on a Rust initialization script -mod rust { - use std::{thread::sleep, time::Duration}; - use tauri::Manager; - - // this command is here just so the example doesn't throw an error - #[tauri::command] - fn close_splashscreen() {} - - pub fn main() { - tauri::Builder::default() - .setup(|app| { - let splashscreen_window = app.get_window("splashscreen").unwrap(); - let main_window = app.get_window("main").unwrap(); - // we perform the initialization code on a new task so the app doesn't crash - tauri::async_runtime::spawn(async move { - println!("Initializing..."); - sleep(Duration::from_secs(2)); - println!("Done initializing."); - - // After it's done, close the splashscreen and display the main window - splashscreen_window.close().unwrap(); - main_window.show().unwrap(); - }); - Ok(()) - }) - .invoke_handler(tauri::generate_handler![close_splashscreen]) - .run(super::context()) - .expect("failed to run app"); - } -} - -// Application code for a splashscreen system that waits for the UI -mod ui { - use std::sync::{Arc, Mutex}; - use tauri::{Manager, State, Window}; - - // wrappers around each Window - // we use a dedicated type because Tauri can only manage a single instance of a given type - struct SplashscreenWindow(Arc>); - struct MainWindow(Arc>); - - #[tauri::command] - fn close_splashscreen( - _: Window, // force inference of P - splashscreen: State, - main: State, - ) { - // Close splashscreen - splashscreen.0.lock().unwrap().close().unwrap(); - // Show main window - main.0.lock().unwrap().show().unwrap(); - } - - pub fn main() { - let context = super::context(); - tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) - .setup(|app| { - // set the splashscreen and main windows to be globally available with the tauri state API - app.manage(SplashscreenWindow(Arc::new(Mutex::new( - app.get_window("splashscreen").unwrap(), - )))); - app.manage(MainWindow(Arc::new(Mutex::new( - app.get_window("main").unwrap(), - )))); - Ok(()) - }) - .invoke_handler(tauri::generate_handler![close_splashscreen]) - .run(context) - .expect("error while running tauri application"); - } -} - -fn context() -> tauri::Context { - tauri::build_script_context!() -} +#[cfg(desktop)] +mod desktop; fn main() { - // toggle this flag to experiment with different splashscreen usages - let ui = false; - if ui { - ui::main(); - } else { - rust::main(); - } + #[cfg(desktop)] + desktop::main(); }