tauri_plugin_positioner/
lib.rs1#![doc(
15 html_logo_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png",
16 html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png"
17)]
18#![cfg(not(any(target_os = "android", target_os = "ios")))]
19
20mod ext;
21
22pub use ext::*;
23use tauri::{
24 Result, Runtime,
25 plugin::{self, TauriPlugin},
26};
27
28#[cfg(feature = "tray-icon")]
29use tauri::{AppHandle, Manager, PhysicalPosition, PhysicalSize, tray::TrayIconEvent};
30
31#[cfg(feature = "tray-icon")]
32struct Tray(std::sync::Mutex<Option<(PhysicalPosition<f64>, PhysicalSize<f64>)>>);
33
34#[cfg(feature = "tray-icon")]
45pub fn on_tray_event<R: Runtime>(app: &AppHandle<R>, event: &TrayIconEvent) {
46 let (position, size) = {
47 match event {
48 TrayIconEvent::Click { rect, .. }
49 | TrayIconEvent::Enter { rect, .. }
50 | TrayIconEvent::Leave { rect, .. }
51 | TrayIconEvent::Move { rect, .. } => {
52 let size = rect.size.to_physical(1.0);
54 let position = rect.position.to_physical(1.0);
55 (position, size)
56 }
57
58 _ => return,
59 }
60 };
61
62 app.state::<Tray>()
63 .0
64 .lock()
65 .unwrap()
66 .replace((position, size));
67}
68
69#[tauri::command]
70async fn move_window<R: Runtime>(window: tauri::Window<R>, position: Position) -> Result<()> {
71 window.move_window(position)
72}
73
74#[cfg(feature = "tray-icon")]
75#[tauri::command]
76async fn move_window_constrained<R: Runtime>(
77 window: tauri::Window<R>,
78 position: Position,
79) -> Result<()> {
80 window.move_window_constrained(position)
81}
82
83#[cfg(feature = "tray-icon")]
84#[tauri::command]
85fn set_tray_icon_state<R: Runtime>(
86 app: AppHandle<R>,
87 position: PhysicalPosition<f64>,
88 size: PhysicalSize<f64>,
89) {
90 app.state::<Tray>()
91 .0
92 .lock()
93 .unwrap()
94 .replace((position, size));
95}
96
97pub fn init<R: Runtime>() -> TauriPlugin<R> {
99 let plugin = plugin::Builder::new("positioner").invoke_handler(tauri::generate_handler![
100 move_window,
101 #[cfg(feature = "tray-icon")]
102 move_window_constrained,
103 #[cfg(feature = "tray-icon")]
104 set_tray_icon_state
105 ]);
106
107 #[cfg(feature = "tray-icon")]
108 let plugin = plugin.setup(|app_handle, _api| {
109 app_handle.manage(Tray(std::sync::Mutex::new(None)));
110 Ok(())
111 });
112
113 plugin.build()
114}