Skip to main content

tauri_plugin_positioner/
lib.rs

1// Copyright 2021 Jonas Kruckenberg
2// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
3// SPDX-License-Identifier: Apache-2.0
4// SPDX-License-Identifier: MIT
5
6//! A plugin for Tauri that helps position your windows at well-known locations.
7//!
8//! ## Cargo features
9//!
10//! - **tray-icon**: Enables tray-icon-relative positions.
11//!
12//!   Note: This requires attaching the Tauri plugin, *even* when using the trait extension only.
13
14#![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/// Records the tray icon's latest position and size so that the `Tray*` [`Position`] variants
35/// (e.g. [`Position::TrayLeft`]) can be resolved by [`WindowExt::move_window`] and
36/// [`WindowExt::move_window_constrained`].
37///
38/// Call this from your tray icon's event handler. Only [`TrayIconEvent::Click`],
39/// [`TrayIconEvent::Enter`], [`TrayIconEvent::Leave`] and [`TrayIconEvent::Move`] carry the
40/// icon's position and update the tracked value; other events are ignored. Until this has been
41/// called at least once, moving a window to a `Tray*` position fails.
42///
43/// Requires the `tray-icon` feature.
44#[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                // tray-icon emits PhysicalSize so the scale factor should not matter.
53                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
97/// The Tauri plugin that exposes [`WindowExt::move_window`] to the webview.
98pub 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}