BlueGit

ict rusty-teams

fix(calls): share border stays with the shared window

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-10-14 · 6 weeks ago

Hash
36cd2191d76b43586d8defbd2e6650f166b19b46
Parent
56ab69a4
4 files changed +189 −22
crates/app/src/call/overlay.rs +73 −16
@@ -6,8 +6,8 @@ use gpui_kit::*;
6 6
7 7 use super::model::format_elapsed;
8 8 use super::overlay_layout::{
9 MINI_POSITION_KEY, bar_collapsed, bar_rect, border_rects, border_thickness, format_position,
10 mini_rect, mini_wanted, mini_work_area, parse_position,
9 BorderVisibility, MINI_POSITION_KEY, bar_collapsed, bar_rect, border_rects, border_thickness,
10 border_visibility, format_position, mini_rect, mini_wanted, mini_work_area, parse_position,
11 11 };
12 12 use crate::app_state::AppState;
13 13 use crate::notify::platform::{self, NativeHandle, ScreenRect, ShareGeometry, WorkArea};
@@ -31,6 +31,8 @@ struct Placed {
31 31 handle: AnyWindowHandle,
32 32 native: Option<NativeHandle>,
33 33 rect: Option<ScreenRect>,
34 anchor: Option<NativeHandle>,
35 visible: bool,
34 36 }
35 37
36 38 impl Placed {
@@ -39,12 +41,43 @@ impl Placed {
39 41 let Some(native) = self.native else {
40 42 return;
41 43 };
42 platform::place(native, rect.left, rect.top, rect.width(), rect.height());
44 match self.anchor {
45 Some(anchor) => platform::place_above(
46 native,
47 anchor,
48 rect.left,
49 rect.top,
50 rect.width(),
51 rect.height(),
52 ),
53 None => platform::place(native, rect.left, rect.top, rect.width(), rect.height()),
54 }
43 55 // WM_SIZE arrives while the app is borrowed, so GPUI drops its own resize report.
44 56 self.handle
45 57 .update(cx, |_, window, cx| window.bounds_changed(cx))
46 58 .ok();
47 59 }
60
61 fn set_visible(&mut self, visible: bool) {
62 let Some(native) = self.native else {
63 return;
64 };
65 if self.visible == visible {
66 return;
67 }
68 self.visible = visible;
69 if visible {
70 platform::show_window_inactive(native);
71 } else {
72 platform::hide_window(native);
73 }
74 }
75
76 fn restack(&self) {
77 if let (Some(native), Some(anchor)) = (self.native, self.anchor) {
78 platform::stack_above(native, anchor);
79 }
80 }
48 81 }
49 82
50 83 struct ShareSession {
@@ -267,7 +300,12 @@ impl CallWindows {
267 300 return;
268 301 };
269 302 session.source = source;
270 if refresh_geometry {
303 let shared_window = session
304 .source
305 .as_ref()
306 .filter(|source| source.kind == ShareKind::Window)
307 .map(|source| source.id as NativeHandle);
308 if refresh_geometry || shared_window.is_some() {
271 309 session.geometry = session
272 310 .source
273 311 .as_ref()
@@ -293,17 +331,29 @@ impl CallWindows {
293 331 cx.notify();
294 332 }
295 333 self.ensure_window(Role::Bar, bar_rect(work, collapsed), work.scale, true, cx);
296 match geometry {
297 Some(geometry) => {
298 let thickness = border_thickness(geometry.work.scale);
299 for (side, rect) in border_rects(geometry.target, thickness)
300 .into_iter()
301 .enumerate()
302 {
303 self.ensure_window(Role::Border(side), rect, geometry.work.scale, true, cx);
334 match border_visibility(shared_window.is_some(), geometry.is_some()) {
335 BorderVisibility::Show => {
336 let thickness = border_thickness(work.scale);
337 let rects = geometry.map(|geometry| border_rects(geometry.target, thickness));
338 for (side, rect) in rects.into_iter().flatten().enumerate() {
339 if let Some(placed) = self.windows.get_mut(&Role::Border(side)) {
340 placed.anchor = shared_window;
341 placed.set_visible(true);
342 }
343 self.ensure_window(Role::Border(side), rect, work.scale, true, cx);
344 if let Some(placed) = self.windows.get(&Role::Border(side)) {
345 placed.restack();
346 }
347 }
348 }
349 BorderVisibility::Hide => {
350 for side in 0..BORDER_SIDES {
351 if let Some(placed) = self.windows.get_mut(&Role::Border(side)) {
352 placed.set_visible(false);
353 }
304 354 }
305 355 }
306 None => self.close_borders(cx),
356 BorderVisibility::Close => self.close_borders(cx),
307 357 }
308 358 }
309 359
@@ -333,12 +383,15 @@ impl CallWindows {
333 383 (true, None) => self.open_pop_out(cx),
334 384 (false, Some(handle)) => {
335 385 self.pop_out = None;
336 handle.update(cx, |_, window, _| window.remove_window()).ok();
386 handle
387 .update(cx, |_, window, _| window.remove_window())
388 .ok();
337 389 }
338 390 (true, Some(handle)) => {
339 391 if handle.update(cx, |_, _, _| ()).is_err() {
340 392 self.pop_out = None;
341 self.app.update(cx, |state, cx| state.return_stage_from_pop_out(cx));
393 self.app
394 .update(cx, |state, cx| state.return_stage_from_pop_out(cx));
342 395 }
343 396 }
344 397 (false, None) => {}
@@ -469,13 +522,17 @@ impl CallWindows {
469 522 }
470 523 if matches!(role, Role::Border(_)) {
471 524 handle
472 .update(cx, |_, window, _| platform::make_window_click_through(window))
525 .update(cx, |_, window, _| {
526 platform::make_window_click_through(window)
527 })
473 528 .ok();
474 529 }
475 530 Placed {
476 531 handle,
477 532 native,
478 533 rect: None,
534 anchor: None,
535 visible: true,
479 536 }
480 537 });
481 538 controller.update(cx, |this, _| {
@@ -6,8 +6,8 @@ use gpui_kit::*;
6
6
7use super::model::format_elapsed;
7use super::model::format_elapsed;
8use super::overlay_layout::{
8use super::overlay_layout::{
9 MINI_POSITION_KEY, bar_collapsed, bar_rect, border_rects, border_thickness, format_position,
9 BorderVisibility, MINI_POSITION_KEY, bar_collapsed, bar_rect, border_rects, border_thickness,
10 mini_rect, mini_wanted, mini_work_area, parse_position,
10 border_visibility, format_position, mini_rect, mini_wanted, mini_work_area, parse_position,
11};
11};
12use crate::app_state::AppState;
12use crate::app_state::AppState;
13use crate::notify::platform::{self, NativeHandle, ScreenRect, ShareGeometry, WorkArea};
13use crate::notify::platform::{self, NativeHandle, ScreenRect, ShareGeometry, WorkArea};
@@ -31,6 +31,8 @@ struct Placed {
31 handle: AnyWindowHandle,
31 handle: AnyWindowHandle,
32 native: Option<NativeHandle>,
32 native: Option<NativeHandle>,
33 rect: Option<ScreenRect>,
33 rect: Option<ScreenRect>,
34 anchor: Option<NativeHandle>,
35 visible: bool,
34}
36}
35
37
36impl Placed {
38impl Placed {
@@ -39,12 +41,43 @@ impl Placed {
39 let Some(native) = self.native else {
41 let Some(native) = self.native else {
40 return;
42 return;
41 };
43 };
42 platform::place(native, rect.left, rect.top, rect.width(), rect.height());
44 match self.anchor {
45 Some(anchor) => platform::place_above(
46 native,
47 anchor,
48 rect.left,
49 rect.top,
50 rect.width(),
51 rect.height(),
52 ),
53 None => platform::place(native, rect.left, rect.top, rect.width(), rect.height()),
54 }
43 // WM_SIZE arrives while the app is borrowed, so GPUI drops its own resize report.
55 // WM_SIZE arrives while the app is borrowed, so GPUI drops its own resize report.
44 self.handle
56 self.handle
45 .update(cx, |_, window, cx| window.bounds_changed(cx))
57 .update(cx, |_, window, cx| window.bounds_changed(cx))
46 .ok();
58 .ok();
47 }
59 }
60
61 fn set_visible(&mut self, visible: bool) {
62 let Some(native) = self.native else {
63 return;
64 };
65 if self.visible == visible {
66 return;
67 }
68 self.visible = visible;
69 if visible {
70 platform::show_window_inactive(native);
71 } else {
72 platform::hide_window(native);
73 }
74 }
75
76 fn restack(&self) {
77 if let (Some(native), Some(anchor)) = (self.native, self.anchor) {
78 platform::stack_above(native, anchor);
79 }
80 }
48}
81}
49
82
50struct ShareSession {
83struct ShareSession {
@@ -267,7 +300,12 @@ impl CallWindows {
267 return;
300 return;
268 };
301 };
269 session.source = source;
302 session.source = source;
270 if refresh_geometry {
303 let shared_window = session
304 .source
305 .as_ref()
306 .filter(|source| source.kind == ShareKind::Window)
307 .map(|source| source.id as NativeHandle);
308 if refresh_geometry || shared_window.is_some() {
271 session.geometry = session
309 session.geometry = session
272 .source
310 .source
273 .as_ref()
311 .as_ref()
@@ -293,17 +331,29 @@ impl CallWindows {
293 cx.notify();
331 cx.notify();
294 }
332 }
295 self.ensure_window(Role::Bar, bar_rect(work, collapsed), work.scale, true, cx);
333 self.ensure_window(Role::Bar, bar_rect(work, collapsed), work.scale, true, cx);
296 match geometry {
334 match border_visibility(shared_window.is_some(), geometry.is_some()) {
297 Some(geometry) => {
335 BorderVisibility::Show => {
298 let thickness = border_thickness(geometry.work.scale);
336 let thickness = border_thickness(work.scale);
299 for (side, rect) in border_rects(geometry.target, thickness)
337 let rects = geometry.map(|geometry| border_rects(geometry.target, thickness));
300 .into_iter()
338 for (side, rect) in rects.into_iter().flatten().enumerate() {
301 .enumerate()
339 if let Some(placed) = self.windows.get_mut(&Role::Border(side)) {
302 {
340 placed.anchor = shared_window;
303 self.ensure_window(Role::Border(side), rect, geometry.work.scale, true, cx);
341 placed.set_visible(true);
342 }
343 self.ensure_window(Role::Border(side), rect, work.scale, true, cx);
344 if let Some(placed) = self.windows.get(&Role::Border(side)) {
345 placed.restack();
346 }
347 }
348 }
349 BorderVisibility::Hide => {
350 for side in 0..BORDER_SIDES {
351 if let Some(placed) = self.windows.get_mut(&Role::Border(side)) {
352 placed.set_visible(false);
353 }
304 }
354 }
305 }
355 }
306 None => self.close_borders(cx),
356 BorderVisibility::Close => self.close_borders(cx),
307 }
357 }
308 }
358 }
309
359
@@ -333,12 +383,15 @@ impl CallWindows {
333 (true, None) => self.open_pop_out(cx),
383 (true, None) => self.open_pop_out(cx),
334 (false, Some(handle)) => {
384 (false, Some(handle)) => {
335 self.pop_out = None;
385 self.pop_out = None;
336 handle.update(cx, |_, window, _| window.remove_window()).ok();
386 handle
387 .update(cx, |_, window, _| window.remove_window())
388 .ok();
337 }
389 }
338 (true, Some(handle)) => {
390 (true, Some(handle)) => {
339 if handle.update(cx, |_, _, _| ()).is_err() {
391 if handle.update(cx, |_, _, _| ()).is_err() {
340 self.pop_out = None;
392 self.pop_out = None;
341 self.app.update(cx, |state, cx| state.return_stage_from_pop_out(cx));
393 self.app
394 .update(cx, |state, cx| state.return_stage_from_pop_out(cx));
342 }
395 }
343 }
396 }
344 (false, None) => {}
397 (false, None) => {}
@@ -469,13 +522,17 @@ impl CallWindows {
469 }
522 }
470 if matches!(role, Role::Border(_)) {
523 if matches!(role, Role::Border(_)) {
471 handle
524 handle
472 .update(cx, |_, window, _| platform::make_window_click_through(window))
525 .update(cx, |_, window, _| {
526 platform::make_window_click_through(window)
527 })
473 .ok();
528 .ok();
474 }
529 }
475 Placed {
530 Placed {
476 handle,
531 handle,
477 native,
532 native,
478 rect: None,
533 rect: None,
534 anchor: None,
535 visible: true,
479 }
536 }
480 });
537 });
481 controller.update(cx, |this, _| {
538 controller.update(cx, |this, _| {
crates/app/src/call/overlay_layout.rs +23 −0
@@ -77,6 +77,21 @@ pub fn border_rects(target: ScreenRect, thickness: i32) -> [ScreenRect; 4] {
77 77 ]
78 78 }
79 79
80 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
81 pub enum BorderVisibility {
82 Show,
83 Hide,
84 Close,
85 }
86
87 pub fn border_visibility(window_share: bool, geometry_found: bool) -> BorderVisibility {
88 match (geometry_found, window_share) {
89 (true, _) => BorderVisibility::Show,
90 (false, true) => BorderVisibility::Hide,
91 (false, false) => BorderVisibility::Close,
92 }
93 }
94
80 95 pub fn mini_rect(work: WorkArea, saved: Option<(i32, i32)>) -> ScreenRect {
81 96 let width = scaled(MINI_WIDTH, work.scale);
82 97 let height = scaled(MINI_HEIGHT, work.scale);
@@ -209,6 +224,14 @@ mod tests {
209 224 assert_eq!(right.left, 3);
210 225 }
211 226
227 #[test]
228 fn border_hides_for_a_window_without_frame_and_closes_for_a_screen() {
229 assert_eq!(border_visibility(true, true), BorderVisibility::Show);
230 assert_eq!(border_visibility(false, true), BorderVisibility::Show);
231 assert_eq!(border_visibility(true, false), BorderVisibility::Hide);
232 assert_eq!(border_visibility(false, false), BorderVisibility::Close);
233 }
234
212 235 #[test]
213 236 fn mini_defaults_to_the_bottom_right_corner() {
214 237 let rect = mini_rect(work(1.), None);
@@ -77,6 +77,21 @@ pub fn border_rects(target: ScreenRect, thickness: i32) -> [ScreenRect; 4] {
77 ]
77 ]
78}
78}
79
79
80#[derive(Debug, Clone, Copy, PartialEq, Eq)]
81pub enum BorderVisibility {
82 Show,
83 Hide,
84 Close,
85}
86
87pub fn border_visibility(window_share: bool, geometry_found: bool) -> BorderVisibility {
88 match (geometry_found, window_share) {
89 (true, _) => BorderVisibility::Show,
90 (false, true) => BorderVisibility::Hide,
91 (false, false) => BorderVisibility::Close,
92 }
93}
94
80pub fn mini_rect(work: WorkArea, saved: Option<(i32, i32)>) -> ScreenRect {
95pub fn mini_rect(work: WorkArea, saved: Option<(i32, i32)>) -> ScreenRect {
81 let width = scaled(MINI_WIDTH, work.scale);
96 let width = scaled(MINI_WIDTH, work.scale);
82 let height = scaled(MINI_HEIGHT, work.scale);
97 let height = scaled(MINI_HEIGHT, work.scale);
@@ -209,6 +224,14 @@ mod tests {
209 assert_eq!(right.left, 3);
224 assert_eq!(right.left, 3);
210 }
225 }
211
226
227 #[test]
228 fn border_hides_for_a_window_without_frame_and_closes_for_a_screen() {
229 assert_eq!(border_visibility(true, true), BorderVisibility::Show);
230 assert_eq!(border_visibility(false, true), BorderVisibility::Show);
231 assert_eq!(border_visibility(true, false), BorderVisibility::Hide);
232 assert_eq!(border_visibility(false, false), BorderVisibility::Close);
233 }
234
212 #[test]
235 #[test]
213 fn mini_defaults_to_the_bottom_right_corner() {
236 fn mini_defaults_to_the_bottom_right_corner() {
214 let rect = mini_rect(work(1.), None);
237 let rect = mini_rect(work(1.), None);
crates/app/src/notify/platform/stub.rs +14 −0
@@ -24,6 +24,20 @@ pub fn work_area_at(_x: i32, _y: i32) -> Option<WorkArea> {
24 24
25 25 pub fn place(_handle: NativeHandle, _x: i32, _y: i32, _width: i32, _height: i32) {}
26 26
27 pub fn place_above(
28 _handle: NativeHandle,
29 _shared: NativeHandle,
30 _x: i32,
31 _y: i32,
32 _width: i32,
33 _height: i32,
34 ) {
35 }
36
37 pub fn stack_above(_handle: NativeHandle, _shared: NativeHandle) {}
38
39 pub fn show_window_inactive(_handle: NativeHandle) {}
40
27 41 pub fn exclude_from_capture(_handle: NativeHandle) {}
28 42
29 43 #[cfg(target_os = "linux")]
@@ -24,6 +24,20 @@ pub fn work_area_at(_x: i32, _y: i32) -> Option<WorkArea> {
24
24
25pub fn place(_handle: NativeHandle, _x: i32, _y: i32, _width: i32, _height: i32) {}
25pub fn place(_handle: NativeHandle, _x: i32, _y: i32, _width: i32, _height: i32) {}
26
26
27pub fn place_above(
28 _handle: NativeHandle,
29 _shared: NativeHandle,
30 _x: i32,
31 _y: i32,
32 _width: i32,
33 _height: i32,
34) {
35}
36
37pub fn stack_above(_handle: NativeHandle, _shared: NativeHandle) {}
38
39pub fn show_window_inactive(_handle: NativeHandle) {}
40
27pub fn exclude_from_capture(_handle: NativeHandle) {}
41pub fn exclude_from_capture(_handle: NativeHandle) {}
28
42
29#[cfg(target_os = "linux")]
43#[cfg(target_os = "linux")]
crates/app/src/notify/platform/windows_impl.rs +79 −6
@@ -12,7 +12,7 @@ use tray_icon::menu::{CheckMenuItem, Menu, MenuEvent, MenuItem, PredefinedMenuIt
12 12 use tray_icon::{Icon, MouseButton, MouseButtonState, TrayIcon, TrayIconBuilder, TrayIconEvent};
13 13 use windows::Win32::Foundation::{COLORREF, HWND, POINT, RECT};
14 14 use windows::Win32::Graphics::Dwm::{
15 DWMWA_BORDER_COLOR, DWMWA_COLOR_NONE, DWMWA_EXTENDED_FRAME_BOUNDS,
15 DWMWA_BORDER_COLOR, DWMWA_CLOAKED, DWMWA_COLOR_NONE, DWMWA_EXTENDED_FRAME_BOUNDS,
16 16 DWMWA_WINDOW_CORNER_PREFERENCE, DWMWCP_DONOTROUND, DwmGetWindowAttribute,
17 17 DwmSetWindowAttribute,
18 18 };
@@ -29,13 +29,14 @@ use windows::Win32::UI::Shell::{
29 29 };
30 30 use windows::Win32::UI::WindowsAndMessaging::{
31 31 CreateIconIndirect, DestroyIcon, FLASHW_STOP, FLASHW_TIMERNOFG, FLASHW_TRAY, FLASHWINFO,
32 FlashWindowEx, GWL_EXSTYLE, GWL_STYLE, GetWindowLongPtrW, GetWindowRect, HICON, HWND_TOPMOST,
33 ICONINFO, IsIconic, LWA_ALPHA, SPI_GETCLIENTAREAANIMATION, SW_HIDE, SW_RESTORE, SW_SHOW,
32 FlashWindowEx, GW_HWNDPREV, GWL_EXSTYLE, GWL_STYLE, GetWindow, GetWindowLongPtrW,
33 GetWindowRect, HICON, HWND_NOTOPMOST, HWND_TOPMOST, ICONINFO, IsIconic, IsWindowVisible,
34 LWA_ALPHA, SPI_GETCLIENTAREAANIMATION, SW_HIDE, SW_RESTORE, SW_SHOW, SW_SHOWNOACTIVATE,
34 35 SWP_FRAMECHANGED, SWP_NOACTIVATE, SWP_NOMOVE, SWP_NOOWNERZORDER, SWP_NOSIZE, SWP_NOZORDER,
35 36 SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, SetForegroundWindow, SetLayeredWindowAttributes,
36 37 SetWindowDisplayAffinity, SetWindowLongPtrW, SetWindowPos, ShowWindow, SystemParametersInfoW,
37 WDA_EXCLUDEFROMCAPTURE, WS_CAPTION, WS_EX_LAYERED, WS_EX_NOACTIVATE, WS_EX_TRANSPARENT,
38 WS_POPUP, WS_THICKFRAME,
38 WDA_EXCLUDEFROMCAPTURE, WS_CAPTION, WS_EX_LAYERED, WS_EX_NOACTIVATE, WS_EX_TOPMOST,
39 WS_EX_TRANSPARENT, WS_POPUP, WS_THICKFRAME,
39 40 };
40 41 use windows::core::{BOOL, PCWSTR, w};
41 42
@@ -191,6 +192,68 @@ pub fn place(handle: NativeHandle, x: i32, y: i32, width: i32, height: i32) {
191 192 }
192 193 }
193 194
195 fn is_topmost(window: HWND) -> bool {
196 unsafe { GetWindowLongPtrW(window, GWL_EXSTYLE) & WS_EX_TOPMOST.0 as isize != 0 }
197 }
198
199 fn insert_after_above(handle: HWND, shared: HWND) -> Option<HWND> {
200 let shared_topmost = is_topmost(shared);
201 let band = if shared_topmost {
202 HWND_TOPMOST
203 } else {
204 HWND_NOTOPMOST
205 };
206 unsafe {
207 match GetWindow(shared, GW_HWNDPREV) {
208 Ok(previous) if previous == handle => None,
209 Ok(previous) if shared_topmost || !is_topmost(previous) => Some(previous),
210 _ => Some(band),
211 }
212 }
213 }
214
215 pub fn place_above(
216 handle: NativeHandle,
217 shared: NativeHandle,
218 x: i32,
219 y: i32,
220 width: i32,
221 height: i32,
222 ) {
223 let insert_after = insert_after_above(hwnd(handle), hwnd(shared));
224 let flags = if insert_after.is_some() {
225 SWP_NOACTIVATE | SWP_NOOWNERZORDER
226 } else {
227 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER
228 };
229 unsafe {
230 let _ = SetWindowPos(hwnd(handle), insert_after, x, y, width, height, flags);
231 }
232 }
233
234 pub fn stack_above(handle: NativeHandle, shared: NativeHandle) {
235 let Some(insert_after) = insert_after_above(hwnd(handle), hwnd(shared)) else {
236 return;
237 };
238 unsafe {
239 let _ = SetWindowPos(
240 hwnd(handle),
241 Some(insert_after),
242 0,
243 0,
244 0,
245 0,
246 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE,
247 );
248 }
249 }
250
251 pub fn show_window_inactive(handle: NativeHandle) {
252 unsafe {
253 let _ = ShowWindow(hwnd(handle), SW_SHOWNOACTIVATE);
254 }
255 }
256
194 257 pub fn exclude_from_capture(handle: NativeHandle) {
195 258 unsafe {
196 259 let _ = SetWindowDisplayAffinity(hwnd(handle), WDA_EXCLUDEFROMCAPTURE);
@@ -235,7 +298,17 @@ pub fn window_rect(handle: NativeHandle) -> Option<ScreenRect> {
235 298 fn visible_frame(window: HWND) -> Option<ScreenRect> {
236 299 let mut rect = RECT::default();
237 300 unsafe {
238 if IsIconic(window).as_bool() {
301 if IsIconic(window).as_bool() || !IsWindowVisible(window).as_bool() {
302 return None;
303 }
304 let mut cloaked = 0u32;
305 let queried = DwmGetWindowAttribute(
306 window,
307 DWMWA_CLOAKED,
308 &mut cloaked as *mut _ as *mut c_void,
309 size_of::<u32>() as u32,
310 );
311 if queried.is_ok() && cloaked != 0 {
239 312 return None;
240 313 }
241 314 // Excludes the invisible resize margin that GetWindowRect includes.
@@ -12,7 +12,7 @@ use tray_icon::menu::{CheckMenuItem, Menu, MenuEvent, MenuItem, PredefinedMenuIt
12use tray_icon::{Icon, MouseButton, MouseButtonState, TrayIcon, TrayIconBuilder, TrayIconEvent};
12use tray_icon::{Icon, MouseButton, MouseButtonState, TrayIcon, TrayIconBuilder, TrayIconEvent};
13use windows::Win32::Foundation::{COLORREF, HWND, POINT, RECT};
13use windows::Win32::Foundation::{COLORREF, HWND, POINT, RECT};
14use windows::Win32::Graphics::Dwm::{
14use windows::Win32::Graphics::Dwm::{
15 DWMWA_BORDER_COLOR, DWMWA_COLOR_NONE, DWMWA_EXTENDED_FRAME_BOUNDS,
15 DWMWA_BORDER_COLOR, DWMWA_CLOAKED, DWMWA_COLOR_NONE, DWMWA_EXTENDED_FRAME_BOUNDS,
16 DWMWA_WINDOW_CORNER_PREFERENCE, DWMWCP_DONOTROUND, DwmGetWindowAttribute,
16 DWMWA_WINDOW_CORNER_PREFERENCE, DWMWCP_DONOTROUND, DwmGetWindowAttribute,
17 DwmSetWindowAttribute,
17 DwmSetWindowAttribute,
18};
18};
@@ -29,13 +29,14 @@ use windows::Win32::UI::Shell::{
29};
29};
30use windows::Win32::UI::WindowsAndMessaging::{
30use windows::Win32::UI::WindowsAndMessaging::{
31 CreateIconIndirect, DestroyIcon, FLASHW_STOP, FLASHW_TIMERNOFG, FLASHW_TRAY, FLASHWINFO,
31 CreateIconIndirect, DestroyIcon, FLASHW_STOP, FLASHW_TIMERNOFG, FLASHW_TRAY, FLASHWINFO,
32 FlashWindowEx, GWL_EXSTYLE, GWL_STYLE, GetWindowLongPtrW, GetWindowRect, HICON, HWND_TOPMOST,
32 FlashWindowEx, GW_HWNDPREV, GWL_EXSTYLE, GWL_STYLE, GetWindow, GetWindowLongPtrW,
33 ICONINFO, IsIconic, LWA_ALPHA, SPI_GETCLIENTAREAANIMATION, SW_HIDE, SW_RESTORE, SW_SHOW,
33 GetWindowRect, HICON, HWND_NOTOPMOST, HWND_TOPMOST, ICONINFO, IsIconic, IsWindowVisible,
34 LWA_ALPHA, SPI_GETCLIENTAREAANIMATION, SW_HIDE, SW_RESTORE, SW_SHOW, SW_SHOWNOACTIVATE,
34 SWP_FRAMECHANGED, SWP_NOACTIVATE, SWP_NOMOVE, SWP_NOOWNERZORDER, SWP_NOSIZE, SWP_NOZORDER,
35 SWP_FRAMECHANGED, SWP_NOACTIVATE, SWP_NOMOVE, SWP_NOOWNERZORDER, SWP_NOSIZE, SWP_NOZORDER,
35 SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, SetForegroundWindow, SetLayeredWindowAttributes,
36 SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, SetForegroundWindow, SetLayeredWindowAttributes,
36 SetWindowDisplayAffinity, SetWindowLongPtrW, SetWindowPos, ShowWindow, SystemParametersInfoW,
37 SetWindowDisplayAffinity, SetWindowLongPtrW, SetWindowPos, ShowWindow, SystemParametersInfoW,
37 WDA_EXCLUDEFROMCAPTURE, WS_CAPTION, WS_EX_LAYERED, WS_EX_NOACTIVATE, WS_EX_TRANSPARENT,
38 WDA_EXCLUDEFROMCAPTURE, WS_CAPTION, WS_EX_LAYERED, WS_EX_NOACTIVATE, WS_EX_TOPMOST,
38 WS_POPUP, WS_THICKFRAME,
39 WS_EX_TRANSPARENT, WS_POPUP, WS_THICKFRAME,
39};
40};
40use windows::core::{BOOL, PCWSTR, w};
41use windows::core::{BOOL, PCWSTR, w};
41
42
@@ -191,6 +192,68 @@ pub fn place(handle: NativeHandle, x: i32, y: i32, width: i32, height: i32) {
191 }
192 }
192}
193}
193
194
195fn is_topmost(window: HWND) -> bool {
196 unsafe { GetWindowLongPtrW(window, GWL_EXSTYLE) & WS_EX_TOPMOST.0 as isize != 0 }
197}
198
199fn insert_after_above(handle: HWND, shared: HWND) -> Option<HWND> {
200 let shared_topmost = is_topmost(shared);
201 let band = if shared_topmost {
202 HWND_TOPMOST
203 } else {
204 HWND_NOTOPMOST
205 };
206 unsafe {
207 match GetWindow(shared, GW_HWNDPREV) {
208 Ok(previous) if previous == handle => None,
209 Ok(previous) if shared_topmost || !is_topmost(previous) => Some(previous),
210 _ => Some(band),
211 }
212 }
213}
214
215pub fn place_above(
216 handle: NativeHandle,
217 shared: NativeHandle,
218 x: i32,
219 y: i32,
220 width: i32,
221 height: i32,
222) {
223 let insert_after = insert_after_above(hwnd(handle), hwnd(shared));
224 let flags = if insert_after.is_some() {
225 SWP_NOACTIVATE | SWP_NOOWNERZORDER
226 } else {
227 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER
228 };
229 unsafe {
230 let _ = SetWindowPos(hwnd(handle), insert_after, x, y, width, height, flags);
231 }
232}
233
234pub fn stack_above(handle: NativeHandle, shared: NativeHandle) {
235 let Some(insert_after) = insert_after_above(hwnd(handle), hwnd(shared)) else {
236 return;
237 };
238 unsafe {
239 let _ = SetWindowPos(
240 hwnd(handle),
241 Some(insert_after),
242 0,
243 0,
244 0,
245 0,
246 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE,
247 );
248 }
249}
250
251pub fn show_window_inactive(handle: NativeHandle) {
252 unsafe {
253 let _ = ShowWindow(hwnd(handle), SW_SHOWNOACTIVATE);
254 }
255}
256
194pub fn exclude_from_capture(handle: NativeHandle) {
257pub fn exclude_from_capture(handle: NativeHandle) {
195 unsafe {
258 unsafe {
196 let _ = SetWindowDisplayAffinity(hwnd(handle), WDA_EXCLUDEFROMCAPTURE);
259 let _ = SetWindowDisplayAffinity(hwnd(handle), WDA_EXCLUDEFROMCAPTURE);
@@ -235,7 +298,17 @@ pub fn window_rect(handle: NativeHandle) -> Option<ScreenRect> {
235fn visible_frame(window: HWND) -> Option<ScreenRect> {
298fn visible_frame(window: HWND) -> Option<ScreenRect> {
236 let mut rect = RECT::default();
299 let mut rect = RECT::default();
237 unsafe {
300 unsafe {
238 if IsIconic(window).as_bool() {
301 if IsIconic(window).as_bool() || !IsWindowVisible(window).as_bool() {
302 return None;
303 }
304 let mut cloaked = 0u32;
305 let queried = DwmGetWindowAttribute(
306 window,
307 DWMWA_CLOAKED,
308 &mut cloaked as *mut _ as *mut c_void,
309 size_of::<u32>() as u32,
310 );
311 if queried.is_ok() && cloaked != 0 {
239 return None;
312 return None;
240 }
313 }
241 // Excludes the invisible resize margin that GetWindowRect includes.
314 // Excludes the invisible resize margin that GetWindowRect includes.