From 4db626354c8e29e37bedcf94787a8dd36ce21c55 Mon Sep 17 00:00:00 2001 From: Jakob Westhoff Date: Wed, 20 Nov 2024 01:05:37 +0100 Subject: [PATCH] feat(positioner): add `moveWindowConstrained` for tray-icon positions (#2076) --- .../positioner-move-window-constrained-js.md | 6 + .../positioner-move-window-constrained.md | 6 + plugins/positioner/api-iife.js | 2 +- plugins/positioner/build.rs | 6 +- plugins/positioner/guest-js/index.ts | 13 + .../commands/move_window_constrained.toml | 13 + .../permissions/autogenerated/reference.md | 29 +- plugins/positioner/permissions/default.toml | 6 +- .../permissions/schemas/schema.json | 10 + plugins/positioner/src/ext.rs | 387 +++++++++++------- plugins/positioner/src/lib.rs | 11 + 11 files changed, 339 insertions(+), 150 deletions(-) create mode 100644 .changes/positioner-move-window-constrained-js.md create mode 100644 .changes/positioner-move-window-constrained.md create mode 100644 plugins/positioner/permissions/autogenerated/commands/move_window_constrained.toml diff --git a/.changes/positioner-move-window-constrained-js.md b/.changes/positioner-move-window-constrained-js.md new file mode 100644 index 000000000..ee1e53c1b --- /dev/null +++ b/.changes/positioner-move-window-constrained-js.md @@ -0,0 +1,6 @@ +--- +"positioner-js": minor +--- + +Add `moveWindowConstrained` function that is similar to `moveWindow` but constrains the window to the screen dimensions in case of tray icon positions. + diff --git a/.changes/positioner-move-window-constrained.md b/.changes/positioner-move-window-constrained.md new file mode 100644 index 000000000..a446bf96a --- /dev/null +++ b/.changes/positioner-move-window-constrained.md @@ -0,0 +1,6 @@ +--- +"positioner": minor +--- + +Add `WindowExt::move_window_constrained` method that is similar to `WindowExt::move_window` but constrains the window to the screen dimensions in case of tray icon positions. + diff --git a/plugins/positioner/api-iife.js b/plugins/positioner/api-iife.js index 0b78572d3..dda217783 100644 --- a/plugins/positioner/api-iife.js +++ b/plugins/positioner/api-iife.js @@ -1 +1 @@ -if("__TAURI__"in window){var __TAURI_PLUGIN_POSITIONER__=function(t){"use strict";async function o(t,o={},e){return window.__TAURI_INTERNALS__.invoke(t,o,e)}var e;return"function"==typeof SuppressedError&&SuppressedError,t.Position=void 0,(e=t.Position||(t.Position={}))[e.TopLeft=0]="TopLeft",e[e.TopRight=1]="TopRight",e[e.BottomLeft=2]="BottomLeft",e[e.BottomRight=3]="BottomRight",e[e.TopCenter=4]="TopCenter",e[e.BottomCenter=5]="BottomCenter",e[e.LeftCenter=6]="LeftCenter",e[e.RightCenter=7]="RightCenter",e[e.Center=8]="Center",e[e.TrayLeft=9]="TrayLeft",e[e.TrayBottomLeft=10]="TrayBottomLeft",e[e.TrayRight=11]="TrayRight",e[e.TrayBottomRight=12]="TrayBottomRight",e[e.TrayCenter=13]="TrayCenter",e[e.TrayBottomCenter=14]="TrayBottomCenter",t.handleIconState=async function(t){await o("plugin:positioner|set_tray_icon_state",{position:t.rect.position,size:t.rect.size})},t.moveWindow=async function(t){await o("plugin:positioner|move_window",{position:t})},t}({});Object.defineProperty(window.__TAURI__,"positioner",{value:__TAURI_PLUGIN_POSITIONER__})} +if("__TAURI__"in window){var __TAURI_PLUGIN_POSITIONER__=function(t){"use strict";async function o(t,o={},e){return window.__TAURI_INTERNALS__.invoke(t,o,e)}var e;return"function"==typeof SuppressedError&&SuppressedError,t.Position=void 0,(e=t.Position||(t.Position={}))[e.TopLeft=0]="TopLeft",e[e.TopRight=1]="TopRight",e[e.BottomLeft=2]="BottomLeft",e[e.BottomRight=3]="BottomRight",e[e.TopCenter=4]="TopCenter",e[e.BottomCenter=5]="BottomCenter",e[e.LeftCenter=6]="LeftCenter",e[e.RightCenter=7]="RightCenter",e[e.Center=8]="Center",e[e.TrayLeft=9]="TrayLeft",e[e.TrayBottomLeft=10]="TrayBottomLeft",e[e.TrayRight=11]="TrayRight",e[e.TrayBottomRight=12]="TrayBottomRight",e[e.TrayCenter=13]="TrayCenter",e[e.TrayBottomCenter=14]="TrayBottomCenter",t.handleIconState=async function(t){await o("plugin:positioner|set_tray_icon_state",{position:t.rect.position,size:t.rect.size})},t.moveWindow=async function(t){await o("plugin:positioner|move_window",{position:t})},t.moveWindowConstrained=async function(t){await o("plugin:positioner|move_window_constrained",{position:t})},t}({});Object.defineProperty(window.__TAURI__,"positioner",{value:__TAURI_PLUGIN_POSITIONER__})} diff --git a/plugins/positioner/build.rs b/plugins/positioner/build.rs index 896c81aad..830c61fb4 100644 --- a/plugins/positioner/build.rs +++ b/plugins/positioner/build.rs @@ -2,7 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT -const COMMANDS: &[&str] = &["move_window", "set_tray_icon_state"]; +const COMMANDS: &[&str] = &[ + "move_window", + "move_window_constrained", + "set_tray_icon_state", +]; fn main() { tauri_plugin::Builder::new(COMMANDS) diff --git a/plugins/positioner/guest-js/index.ts b/plugins/positioner/guest-js/index.ts index d8f6204fa..74d0295ec 100644 --- a/plugins/positioner/guest-js/index.ts +++ b/plugins/positioner/guest-js/index.ts @@ -39,6 +39,19 @@ export async function moveWindow(to: Position): Promise { }) } +/** + * Moves the `Window` to the given {@link Position} using `WindowExt.move_window_constrained()` + * + * This move operation constrains the window to the screen dimensions in case of + * tray-icon positions. + * @param to The (tray) {@link Position} to move to. + */ +export async function moveWindowConstrained(to: Position): Promise { + await invoke('plugin:positioner|move_window_constrained', { + position: to + }) +} + export async function handleIconState(event: TrayIconEvent): Promise { await invoke('plugin:positioner|set_tray_icon_state', { position: event.rect.position, diff --git a/plugins/positioner/permissions/autogenerated/commands/move_window_constrained.toml b/plugins/positioner/permissions/autogenerated/commands/move_window_constrained.toml new file mode 100644 index 000000000..803439905 --- /dev/null +++ b/plugins/positioner/permissions/autogenerated/commands/move_window_constrained.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-move-window-constrained" +description = "Enables the move_window_constrained command without any pre-configured scope." +commands.allow = ["move_window_constrained"] + +[[permission]] +identifier = "deny-move-window-constrained" +description = "Denies the move_window_constrained command without any pre-configured scope." +commands.deny = ["move_window_constrained"] diff --git a/plugins/positioner/permissions/autogenerated/reference.md b/plugins/positioner/permissions/autogenerated/reference.md index 8dfe0466e..72182c026 100644 --- a/plugins/positioner/permissions/autogenerated/reference.md +++ b/plugins/positioner/permissions/autogenerated/reference.md @@ -3,7 +3,8 @@ Allows the moveWindow and handleIconState APIs - `allow-move-window` -- `set-tray-icon-state` +- `allow-move-window-constrained` +- `allow-set-tray-icon-state` ## Permission Table @@ -43,6 +44,32 @@ Denies the move_window command without any pre-configured scope. +`positioner:allow-move-window-constrained` + + + + +Enables the move_window_constrained command without any pre-configured scope. + + + + + + + +`positioner:deny-move-window-constrained` + + + + +Denies the move_window_constrained command without any pre-configured scope. + + + + + + + `positioner:allow-set-tray-icon-state` diff --git a/plugins/positioner/permissions/default.toml b/plugins/positioner/permissions/default.toml index bd8dc25e5..167417c3b 100644 --- a/plugins/positioner/permissions/default.toml +++ b/plugins/positioner/permissions/default.toml @@ -1,4 +1,8 @@ "$schema" = "schemas/schema.json" [default] description = "Allows the moveWindow and handleIconState APIs" -permissions = ["allow-move-window", "set-tray-icon-state"] +permissions = [ + "allow-move-window", + "allow-move-window-constrained", + "allow-set-tray-icon-state", +] diff --git a/plugins/positioner/permissions/schemas/schema.json b/plugins/positioner/permissions/schemas/schema.json index c4d169668..ccf551569 100644 --- a/plugins/positioner/permissions/schemas/schema.json +++ b/plugins/positioner/permissions/schemas/schema.json @@ -304,6 +304,16 @@ "type": "string", "const": "deny-move-window" }, + { + "description": "Enables the move_window_constrained command without any pre-configured scope.", + "type": "string", + "const": "allow-move-window-constrained" + }, + { + "description": "Denies the move_window_constrained command without any pre-configured scope.", + "type": "string", + "const": "deny-move-window-constrained" + }, { "description": "Enables the set_tray_icon_state command without any pre-configured scope.", "type": "string", diff --git a/plugins/positioner/src/ext.rs b/plugins/positioner/src/ext.rs index 4688cd51c..b3d405ea2 100644 --- a/plugins/positioner/src/ext.rs +++ b/plugins/positioner/src/ext.rs @@ -8,6 +8,8 @@ use crate::Tray; use serde_repr::Deserialize_repr; #[cfg(feature = "tray-icon")] use tauri::Manager; +#[cfg(feature = "tray-icon")] +use tauri::Monitor; use tauri::{PhysicalPosition, PhysicalSize, Result, Runtime, WebviewWindow, Window}; /// Well known window positions. @@ -41,172 +43,265 @@ pub enum Position { pub trait WindowExt { /// Moves the [`Window`] to the given [`Position`] /// - /// All positions are relative to the **current** screen. + /// All (non-tray) positions are relative to the **current** screen. fn move_window(&self, position: Position) -> Result<()>; + #[cfg(feature = "tray-icon")] + /// Moves the [`Window`] to the given [`Position`] while constraining Tray Positions to the dimensions of the screen. + /// + /// All non-tray positions will not be constrained by this method. + /// + /// This method allows you to position your Tray Windows without having them + /// cut off on the screen borders. + fn move_window_constrained(&self, position: Position) -> Result<()>; } impl WindowExt for WebviewWindow { fn move_window(&self, pos: Position) -> Result<()> { self.as_ref().window().move_window(pos) } + + #[cfg(feature = "tray-icon")] + fn move_window_constrained(&self, position: Position) -> Result<()> { + self.as_ref().window().move_window_constrained(position) + } } + impl WindowExt for Window { + #[cfg(feature = "tray-icon")] + fn move_window_constrained(&self, position: Position) -> Result<()> { + // Diverge to basic move_window, if the position is not a tray position + if !matches!( + position, + Position::TrayLeft + | Position::TrayBottomLeft + | Position::TrayRight + | Position::TrayBottomRight + | Position::TrayCenter + | Position::TrayBottomCenter + ) { + return self.move_window(position); + } + + let window_position = calculate_position(self, position)?; + let monitor = get_monitor_for_tray_icon(self)?; + if let Some(monitor) = monitor { + let monitor_size = monitor.size(); + let monitor_position = monitor.position(); + let window_size = self.outer_size()?; + + let right_border_monitor = monitor_position.x as f64 + monitor_size.width as f64; + let left_border_monitor = monitor_position.x as f64; + let right_border_window = window_position.x as f64 + window_size.width as f64; + let left_border_window = window_position.x as f64; + + let constrained_x = if left_border_window < left_border_monitor { + left_border_monitor + } else if right_border_window > right_border_monitor { + right_border_monitor - window_size.width as f64 + } else { + window_position.x as f64 + }; + + let bottom_border_monitor = monitor_position.y as f64 + monitor_size.height as f64; + let top_border_monitor = monitor_position.y as f64; + let bottom_border_window = window_position.y as f64 + window_size.height as f64; + let top_border_window = window_position.y as f64; + + let constrained_y = if top_border_window < top_border_monitor { + top_border_monitor + } else if bottom_border_window > bottom_border_monitor { + bottom_border_monitor - window_size.height as f64 + } else { + window_position.y as f64 + }; + + self.set_position(PhysicalPosition::new(constrained_x, constrained_y))?; + } else { + // Fallback on non constrained positioning + self.set_position(window_position)?; + } + + Ok(()) + } + fn move_window(&self, pos: Position) -> Result<()> { - use Position::*; - - let screen = self.current_monitor()?.unwrap(); - let screen_position = screen.position(); - let screen_size = PhysicalSize:: { - width: screen.size().width as i32, - height: screen.size().height as i32, - }; - let window_size = PhysicalSize:: { - width: self.outer_size()?.width as i32, - height: self.outer_size()?.height as i32, - }; + let position = calculate_position(self, pos)?; + self.set_position(position) + } +} + +#[cfg(feature = "tray-icon")] +/// Retrieve the monitor, where the tray icon is located on. +fn get_monitor_for_tray_icon(window: &Window) -> Result> { + let tray_position = window + .state::() + .0 + .lock() + .unwrap() + .map(|(pos, _)| pos) + .unwrap_or_default(); + + window.monitor_from_point(tray_position.x, tray_position.y) +} + +/// Calculate the top-left position of the window based on the given +/// [`Position`]. +fn calculate_position( + window: &Window, + pos: Position, +) -> Result> { + use Position::*; + + let screen = window.current_monitor()?.unwrap(); + // Only use the screen_position for the Tray independent positioning, + // because a tray event may not be called on the currently active monitor. + let screen_position = screen.position(); + let screen_size = PhysicalSize:: { + width: screen.size().width as i32, + height: screen.size().height as i32, + }; + let window_size = PhysicalSize:: { + width: window.outer_size()?.width as i32, + height: window.outer_size()?.height as i32, + }; + #[cfg(feature = "tray-icon")] + let (tray_position, tray_size) = window + .state::() + .0 + .lock() + .unwrap() + .map(|(pos, size)| { + ( + Some((pos.x as i32, pos.y as i32)), + Some((size.width as i32, size.height as i32)), + ) + }) + .unwrap_or_default(); + + let physical_pos = match pos { + TopLeft => *screen_position, + TopRight => PhysicalPosition { + x: screen_position.x + (screen_size.width - window_size.width), + y: screen_position.y, + }, + BottomLeft => PhysicalPosition { + x: screen_position.x, + y: screen_size.height - (window_size.height - screen_position.y), + }, + BottomRight => PhysicalPosition { + x: screen_position.x + (screen_size.width - window_size.width), + y: screen_size.height - (window_size.height - screen_position.y), + }, + TopCenter => PhysicalPosition { + x: screen_position.x + ((screen_size.width / 2) - (window_size.width / 2)), + y: screen_position.y, + }, + BottomCenter => PhysicalPosition { + x: screen_position.x + ((screen_size.width / 2) - (window_size.width / 2)), + y: screen_size.height - (window_size.height - screen_position.y), + }, + LeftCenter => PhysicalPosition { + x: screen_position.x, + y: screen_position.y + (screen_size.height / 2) - (window_size.height / 2), + }, + RightCenter => PhysicalPosition { + x: screen_position.x + (screen_size.width - window_size.width), + y: screen_position.y + (screen_size.height / 2) - (window_size.height / 2), + }, + Center => PhysicalPosition { + x: screen_position.x + ((screen_size.width / 2) - (window_size.width / 2)), + y: screen_position.y + (screen_size.height / 2) - (window_size.height / 2), + }, #[cfg(feature = "tray-icon")] - let (tray_position, tray_size) = self - .state::() - .0 - .lock() - .unwrap() - .map(|(pos, size)| { - ( - Some((pos.x as i32, pos.y as i32)), - Some((size.width as i32, size.height as i32)), - ) - }) - .unwrap_or_default(); - - let physical_pos = match pos { - TopLeft => *screen_position, - TopRight => PhysicalPosition { - x: screen_position.x + (screen_size.width - window_size.width), - y: screen_position.y, - }, - BottomLeft => PhysicalPosition { - x: screen_position.x, - y: screen_size.height - (window_size.height - screen_position.y), - }, - BottomRight => PhysicalPosition { - x: screen_position.x + (screen_size.width - window_size.width), - y: screen_size.height - (window_size.height - screen_position.y), - }, - TopCenter => PhysicalPosition { - x: screen_position.x + ((screen_size.width / 2) - (window_size.width / 2)), - y: screen_position.y, - }, - BottomCenter => PhysicalPosition { - x: screen_position.x + ((screen_size.width / 2) - (window_size.width / 2)), - y: screen_size.height - (window_size.height - screen_position.y), - }, - LeftCenter => PhysicalPosition { - x: screen_position.x, - y: screen_position.y + (screen_size.height / 2) - (window_size.height / 2), - }, - RightCenter => PhysicalPosition { - x: screen_position.x + (screen_size.width - window_size.width), - y: screen_position.y + (screen_size.height / 2) - (window_size.height / 2), - }, - Center => PhysicalPosition { - x: screen_position.x + ((screen_size.width / 2) - (window_size.width / 2)), - y: screen_position.y + (screen_size.height / 2) - (window_size.height / 2), - }, - #[cfg(feature = "tray-icon")] - TrayLeft => { - if let (Some((tray_x, tray_y)), Some((_, _tray_height))) = - (tray_position, tray_size) - { - let y = tray_y - window_size.height; - // Choose y value based on the target OS - #[cfg(target_os = "windows")] - let y = if y < 0 { tray_y + _tray_height } else { y }; - - #[cfg(target_os = "macos")] - let y = if y < 0 { tray_y } else { y }; - - PhysicalPosition { x: tray_x, y } - } else { - panic!("Tray position not set"); - } + TrayLeft => { + if let (Some((tray_x, tray_y)), Some((_, _tray_height))) = (tray_position, tray_size) { + let y = tray_y - window_size.height; + // Choose y value based on the target OS + #[cfg(target_os = "windows")] + let y = if y < 0 { tray_y + _tray_height } else { y }; + + #[cfg(target_os = "macos")] + let y = if y < 0 { tray_y } else { y }; + + PhysicalPosition { x: tray_x, y } + } else { + panic!("Tray position not set"); } - #[cfg(feature = "tray-icon")] - TrayBottomLeft => { - if let Some((tray_x, tray_y)) = tray_position { - PhysicalPosition { - x: tray_x, - y: tray_y, - } - } else { - panic!("Tray position not set"); + } + #[cfg(feature = "tray-icon")] + TrayBottomLeft => { + if let Some((tray_x, tray_y)) = tray_position { + PhysicalPosition { + x: tray_x, + y: tray_y, } + } else { + panic!("Tray position not set"); } - #[cfg(feature = "tray-icon")] - TrayRight => { - if let (Some((tray_x, tray_y)), Some((tray_width, _tray_height))) = - (tray_position, tray_size) - { - let y = tray_y - window_size.height; - // Choose y value based on the target OS - #[cfg(target_os = "windows")] - let y = if y < 0 { tray_y + _tray_height } else { y }; - - #[cfg(target_os = "macos")] - let y = if y < 0 { tray_y } else { y }; - - PhysicalPosition { - x: tray_x + tray_width, - y, - } - } else { - panic!("Tray position not set"); + } + #[cfg(feature = "tray-icon")] + TrayRight => { + if let (Some((tray_x, tray_y)), Some((tray_width, _tray_height))) = + (tray_position, tray_size) + { + let y = tray_y - window_size.height; + // Choose y value based on the target OS + #[cfg(target_os = "windows")] + let y = if y < 0 { tray_y + _tray_height } else { y }; + + #[cfg(target_os = "macos")] + let y = if y < 0 { tray_y } else { y }; + + PhysicalPosition { + x: tray_x + tray_width, + y, } + } else { + panic!("Tray position not set"); } - #[cfg(feature = "tray-icon")] - TrayBottomRight => { - if let (Some((tray_x, tray_y)), Some((tray_width, _))) = (tray_position, tray_size) - { - PhysicalPosition { - x: tray_x + tray_width, - y: tray_y, - } - } else { - panic!("Tray position not set"); + } + #[cfg(feature = "tray-icon")] + TrayBottomRight => { + if let (Some((tray_x, tray_y)), Some((tray_width, _))) = (tray_position, tray_size) { + PhysicalPosition { + x: tray_x + tray_width, + y: tray_y, } + } else { + panic!("Tray position not set"); } - #[cfg(feature = "tray-icon")] - TrayCenter => { - if let (Some((tray_x, tray_y)), Some((tray_width, _tray_height))) = - (tray_position, tray_size) - { - let x = tray_x + tray_width / 2 - window_size.width / 2; - let y = tray_y - window_size.height; - // Choose y value based on the target OS - #[cfg(target_os = "windows")] - let y = if y < 0 { tray_y + _tray_height } else { y }; - - #[cfg(target_os = "macos")] - let y = if y < 0 { tray_y } else { y }; - - PhysicalPosition { x, y } - } else { - panic!("Tray position not set"); - } + } + #[cfg(feature = "tray-icon")] + TrayCenter => { + if let (Some((tray_x, tray_y)), Some((tray_width, _tray_height))) = + (tray_position, tray_size) + { + let x = tray_x + tray_width / 2 - window_size.width / 2; + let y = tray_y - window_size.height; + // Choose y value based on the target OS + #[cfg(target_os = "windows")] + let y = if y < 0 { tray_y + _tray_height } else { y }; + + #[cfg(target_os = "macos")] + let y = if y < 0 { tray_y } else { y }; + + PhysicalPosition { x, y } + } else { + panic!("Tray position not set"); } - #[cfg(feature = "tray-icon")] - TrayBottomCenter => { - if let (Some((tray_x, tray_y)), Some((tray_width, _))) = (tray_position, tray_size) - { - PhysicalPosition { - x: tray_x + (tray_width / 2) - (window_size.width / 2), - y: tray_y, - } - } else { - panic!("Tray position not set"); + } + #[cfg(feature = "tray-icon")] + TrayBottomCenter => { + if let (Some((tray_x, tray_y)), Some((tray_width, _))) = (tray_position, tray_size) { + PhysicalPosition { + x: tray_x + (tray_width / 2) - (window_size.width / 2), + y: tray_y, } + } else { + panic!("Tray position not set"); } - }; + } + }; - self.set_position(tauri::Position::Physical(physical_pos)) - } + Ok(physical_pos) } diff --git a/plugins/positioner/src/lib.rs b/plugins/positioner/src/lib.rs index df5eef18b..59d0c3c13 100644 --- a/plugins/positioner/src/lib.rs +++ b/plugins/positioner/src/lib.rs @@ -61,6 +61,15 @@ async fn move_window(window: tauri::Window, position: Position) - window.move_window(position) } +#[cfg(feature = "tray-icon")] +#[tauri::command] +async fn move_window_constrained( + window: tauri::Window, + position: Position, +) -> Result<()> { + window.move_window_constrained(position) +} + #[cfg(feature = "tray-icon")] #[tauri::command] fn set_tray_icon_state( @@ -80,6 +89,8 @@ pub fn init() -> TauriPlugin { let plugin = plugin::Builder::new("positioner").invoke_handler(tauri::generate_handler![ move_window, #[cfg(feature = "tray-icon")] + move_window_constrained, + #[cfg(feature = "tray-icon")] set_tray_icon_state ]);