main.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #![allow(
  9. // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
  10. clippy::nonstandard_macro_braces,
  11. )]
  12. mod cmd;
  13. mod menu;
  14. #[cfg(target_os = "linux")]
  15. use std::path::PathBuf;
  16. use serde::Serialize;
  17. use tauri::{
  18. CustomMenuItem, Event, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, WindowBuilder,
  19. WindowUrl,
  20. };
  21. #[derive(Serialize)]
  22. struct Reply {
  23. data: String,
  24. }
  25. #[tauri::command]
  26. async fn menu_toggle(window: tauri::Window) {
  27. window.menu_handle().toggle().unwrap();
  28. }
  29. fn main() {
  30. tauri::Builder::default()
  31. .on_page_load(|window, _| {
  32. let window_ = window.clone();
  33. window.listen("js-event", move |event| {
  34. println!("got js-event with message '{:?}'", event.payload());
  35. let reply = Reply {
  36. data: "something else".to_string(),
  37. };
  38. window_
  39. .emit("rust-event", Some(reply))
  40. .expect("failed to emit");
  41. });
  42. })
  43. .menu(menu::get_menu())
  44. .on_menu_event(|event| {
  45. println!("{:?}", event.menu_item_id());
  46. })
  47. .system_tray(
  48. SystemTray::new().with_menu(
  49. SystemTrayMenu::new()
  50. .add_item(CustomMenuItem::new("toggle", "Toggle"))
  51. .add_item(CustomMenuItem::new("new", "New window"))
  52. .add_item(CustomMenuItem::new("icon_1", "Tray Icon 1"))
  53. .add_item(CustomMenuItem::new("icon_2", "Tray Icon 2")),
  54. ),
  55. )
  56. .on_system_tray_event(|app, event| match event {
  57. SystemTrayEvent::LeftClick {
  58. position: _,
  59. size: _,
  60. ..
  61. } => {
  62. let window = app.get_window("main").unwrap();
  63. window.show().unwrap();
  64. window.set_focus().unwrap();
  65. }
  66. SystemTrayEvent::MenuItemClick { id, .. } => {
  67. let item_handle = app.tray_handle().get_item(&id);
  68. match id.as_str() {
  69. "toggle" => {
  70. let window = app.get_window("main").unwrap();
  71. let new_title = if window.is_visible().unwrap() {
  72. window.hide().unwrap();
  73. "Show"
  74. } else {
  75. window.show().unwrap();
  76. "Hide"
  77. };
  78. item_handle.set_title(new_title).unwrap();
  79. }
  80. "new" => app
  81. .create_window(
  82. "new",
  83. WindowUrl::App("index.html".into()),
  84. |window_builder, webview_attributes| {
  85. (window_builder.title("Tauri"), webview_attributes)
  86. },
  87. )
  88. .unwrap(),
  89. #[cfg(target_os = "macos")]
  90. "icon_1" => {
  91. app.tray_handle().set_icon_as_template(true).unwrap();
  92. app
  93. .tray_handle()
  94. .set_icon(tauri::Icon::Raw(
  95. include_bytes!("../../../.icons/tray_icon_with_transparency.png").to_vec(),
  96. ))
  97. .unwrap();
  98. }
  99. #[cfg(target_os = "macos")]
  100. "icon_2" => {
  101. app.tray_handle().set_icon_as_template(true).unwrap();
  102. app
  103. .tray_handle()
  104. .set_icon(tauri::Icon::Raw(
  105. include_bytes!("../../../.icons/tray_icon.png").to_vec(),
  106. ))
  107. .unwrap();
  108. }
  109. #[cfg(target_os = "linux")]
  110. "icon_1" => app
  111. .tray_handle()
  112. .set_icon(tauri::Icon::File(PathBuf::from(
  113. "../../../.icons/tray_icon_with_transparency.png",
  114. )))
  115. .unwrap(),
  116. #[cfg(target_os = "linux")]
  117. "icon_2" => app
  118. .tray_handle()
  119. .set_icon(tauri::Icon::File(PathBuf::from(
  120. "../../../.icons/tray_icon.png",
  121. )))
  122. .unwrap(),
  123. #[cfg(target_os = "windows")]
  124. "icon_1" => app
  125. .tray_handle()
  126. .set_icon(tauri::Icon::Raw(
  127. include_bytes!("../../../.icons/tray_icon_with_transparency.ico").to_vec(),
  128. ))
  129. .unwrap(),
  130. #[cfg(target_os = "windows")]
  131. "icon_2" => app
  132. .tray_handle()
  133. .set_icon(tauri::Icon::Raw(
  134. include_bytes!("../../../.icons/icon.ico").to_vec(),
  135. ))
  136. .unwrap(),
  137. _ => {}
  138. }
  139. }
  140. _ => {}
  141. })
  142. .invoke_handler(tauri::generate_handler![
  143. cmd::log_operation,
  144. cmd::perform_request,
  145. menu_toggle,
  146. ])
  147. .build(tauri::generate_context!())
  148. .expect("error while building tauri application")
  149. .run(|app_handle, e| {
  150. if let Event::CloseRequested { label, api, .. } = e {
  151. api.prevent_close();
  152. let window = app_handle.get_window(&label).unwrap();
  153. window.emit("close-requested", ()).unwrap();
  154. }
  155. })
  156. }