mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-09-28 21:51:47 +02:00
fix(window-state): compare position with available monitors bounds (#48)
* remember monitor data * check only if the position exists Co-authored-by: amrbashir <amr.bashir2015@gmail.com>
This commit is contained in:
@@ -5,7 +5,8 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tauri::{
|
use tauri::{
|
||||||
plugin::{Builder as PluginBuilder, TauriPlugin},
|
plugin::{Builder as PluginBuilder, TauriPlugin},
|
||||||
LogicalSize, Manager, PhysicalPosition, RunEvent, Runtime, Window, WindowEvent,
|
LogicalSize, Manager, Monitor, PhysicalPosition, PhysicalSize, RunEvent, Runtime, Window,
|
||||||
|
WindowEvent,
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
@@ -58,7 +59,6 @@ struct WindowMetadata {
|
|||||||
visible: bool,
|
visible: bool,
|
||||||
decorated: bool,
|
decorated: bool,
|
||||||
fullscreen: bool,
|
fullscreen: bool,
|
||||||
monitor: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct WindowStateCache(Arc<Mutex<HashMap<String, WindowMetadata>>>);
|
struct WindowStateCache(Arc<Mutex<HashMap<String, WindowMetadata>>>);
|
||||||
@@ -105,12 +105,11 @@ impl<R: Runtime> WindowExt for Window<R> {
|
|||||||
// restore position to saved value if saved monitor exists
|
// restore position to saved value if saved monitor exists
|
||||||
// otherwise, let the OS decide where to place the window
|
// otherwise, let the OS decide where to place the window
|
||||||
for m in self.available_monitors()? {
|
for m in self.available_monitors()? {
|
||||||
if m.name().map(ToString::to_string).unwrap_or_default() == state.monitor {
|
if m.contains((state.x, state.y).into()) {
|
||||||
self.set_position(PhysicalPosition {
|
self.set_position(PhysicalPosition {
|
||||||
x: state.x,
|
x: state.x,
|
||||||
y: state.y,
|
y: state.y,
|
||||||
})?;
|
})?;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,12 +130,6 @@ impl<R: Runtime> WindowExt for Window<R> {
|
|||||||
let visible = self.is_visible().unwrap_or(true);
|
let visible = self.is_visible().unwrap_or(true);
|
||||||
let decorated = self.is_decorated().unwrap_or(true);
|
let decorated = self.is_decorated().unwrap_or(true);
|
||||||
let fullscreen = self.is_fullscreen().unwrap_or(false);
|
let fullscreen = self.is_fullscreen().unwrap_or(false);
|
||||||
let monitor = self
|
|
||||||
.current_monitor()?
|
|
||||||
.unwrap()
|
|
||||||
.name()
|
|
||||||
.map(ToString::to_string)
|
|
||||||
.unwrap_or_default();
|
|
||||||
c.insert(
|
c.insert(
|
||||||
self.label().into(),
|
self.label().into(),
|
||||||
WindowMetadata {
|
WindowMetadata {
|
||||||
@@ -148,7 +141,6 @@ impl<R: Runtime> WindowExt for Window<R> {
|
|||||||
visible,
|
visible,
|
||||||
decorated,
|
decorated,
|
||||||
fullscreen,
|
fullscreen,
|
||||||
monitor,
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -235,8 +227,6 @@ impl Builder {
|
|||||||
state.maximized = is_maximized;
|
state.maximized = is_maximized;
|
||||||
|
|
||||||
if let Some(monitor) = window_clone.current_monitor().unwrap() {
|
if let Some(monitor) = window_clone.current_monitor().unwrap() {
|
||||||
state.monitor =
|
|
||||||
monitor.name().map(ToString::to_string).unwrap_or_default();
|
|
||||||
let monitor_position = monitor.position();
|
let monitor_position = monitor.position();
|
||||||
// save only window positions that are inside the current monitor
|
// save only window positions that are inside the current monitor
|
||||||
if position.x > monitor_position.x
|
if position.x > monitor_position.x
|
||||||
@@ -288,3 +278,19 @@ impl Builder {
|
|||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trait MonitorExt {
|
||||||
|
fn contains(&self, position: PhysicalPosition<i32>) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MonitorExt for Monitor {
|
||||||
|
fn contains(&self, position: PhysicalPosition<i32>) -> bool {
|
||||||
|
let PhysicalPosition { x, y } = *self.position();
|
||||||
|
let PhysicalSize { width, height } = *self.size();
|
||||||
|
|
||||||
|
x < position.x as _
|
||||||
|
&& position.x < (x + width as i32)
|
||||||
|
&& y < position.y as _
|
||||||
|
&& position.y < (y + height as i32)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user