1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#[cfg(feature = "system-tray")]
use crate::Tray;
use serde_repr::Deserialize_repr;
#[cfg(feature = "system-tray")]
use tauri::Manager;
use tauri::{PhysicalPosition, PhysicalSize, Result, Runtime, Window};
#[derive(Debug, Deserialize_repr)]
#[repr(u16)]
pub enum Position {
TopLeft = 0,
TopRight,
BottomLeft,
BottomRight,
TopCenter,
BottomCenter,
LeftCenter,
RightCenter,
Center,
#[cfg(feature = "system-tray")]
TrayLeft,
#[cfg(feature = "system-tray")]
TrayBottomLeft,
#[cfg(feature = "system-tray")]
TrayRight,
#[cfg(feature = "system-tray")]
TrayBottomRight,
#[cfg(feature = "system-tray")]
TrayCenter,
#[cfg(feature = "system-tray")]
TrayBottomCenter,
}
pub trait WindowExt {
fn move_window(&self, position: Position) -> Result<()>;
}
impl<R: Runtime> WindowExt for Window<R> {
fn move_window(&self, pos: Position) -> Result<()> {
use Position::*;
let screen = self.current_monitor()?.unwrap();
let screen_position = screen.position();
let screen_size = PhysicalSize::<i32> {
width: screen.size().width as i32,
height: screen.size().height as i32,
};
let window_size = PhysicalSize::<i32> {
width: self.outer_size()?.width as i32,
height: self.outer_size()?.height as i32,
};
#[cfg(feature = "system-tray")]
let (tray_position, tray_size) = self
.state::<Tray>()
.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 = "system-tray")]
TrayLeft => {
if let Some((tray_x, tray_y)) = tray_position {
PhysicalPosition {
x: tray_x,
y: tray_y - window_size.height,
}
} else {
panic!("tray position not set");
}
}
#[cfg(feature = "system-tray")]
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 = "system-tray")]
TrayRight => {
if let (Some((tray_x, tray_y)), Some((tray_width, _))) = (tray_position, tray_size) {
PhysicalPosition {
x: tray_x + tray_width,
y: tray_y - window_size.height,
}
} else {
panic!("Tray position not set");
}
}
#[cfg(feature = "system-tray")]
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 = "system-tray")]
TrayCenter => {
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 - window_size.height,
}
} else {
panic!("Tray position not set");
}
}
#[cfg(feature = "system-tray")]
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))
}
}