main.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. #![cfg_attr(
  5. all(not(debug_assertions), target_os = "windows"),
  6. windows_subsystem = "windows"
  7. )]
  8. #[cfg(any(windows, target_os = "macos"))]
  9. use tauri::Manager;
  10. use tauri::{command, window, AppHandle, WindowBuilder, WindowUrl};
  11. #[command]
  12. fn create_child_window(id: String, app: AppHandle) {
  13. #[cfg(any(windows, target_os = "macos"))]
  14. let main = app.get_window("main").unwrap();
  15. let child = window::WindowBuilder::new(&app, id, WindowUrl::default())
  16. .title("Child")
  17. .inner_size(400.0, 300.0);
  18. #[cfg(target_os = "macos")]
  19. let child = child.parent_window(main.ns_window().unwrap());
  20. #[cfg(windows)]
  21. let child = child.parent_window(main.hwnd().unwrap());
  22. child.build().unwrap();
  23. }
  24. fn main() {
  25. tauri::Builder::default()
  26. .on_page_load(|window, _payload| {
  27. let label = window.label().to_string();
  28. window.listen("clicked".to_string(), move |_payload| {
  29. println!("got 'clicked' event on window '{}'", label);
  30. });
  31. })
  32. .invoke_handler(tauri::generate_handler![create_child_window])
  33. .create_window(
  34. "main".to_string(),
  35. WindowUrl::default(),
  36. |window_builder, webview_attributes| {
  37. (
  38. window_builder.title("Main").inner_size(600.0, 400.0),
  39. webview_attributes,
  40. )
  41. },
  42. )
  43. .unwrap() // safe to unwrap: window label is valid
  44. .run(tauri::generate_context!(
  45. "../../examples/parent-window/tauri.conf.json"
  46. ))
  47. .expect("failed to run tauri application");
  48. }