fix(calls): share border stops flickering while the shared window is in front
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -37,19 +37,17 @@ struct Placed {
37
37
38
38
impl Placed {
39
39
fn place(&mut self, rect: ScreenRect, cx: &mut App) {
40
if self.rect == Some(rect) {
41
return;
42
}
40
43
self.rect = Some(rect);
41
44
let Some(native) = self.native else {
42
45
return;
43
46
};
44
47
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
),
48
Some(_) => {
49
platform::place_rect(native, rect.left, rect.top, rect.width(), rect.height())
50
}
53
51
None => platform::place(native, rect.left, rect.top, rect.width(), rect.height()),
54
52
}
55
53
// WM_SIZE arrives while the app is borrowed, so GPUI drops its own resize report.
@@ -72,12 +70,6 @@ impl Placed {
72
70
platform::hide_window(native);
73
71
}
74
72
}
75
76
fn restack(&self) {
77
if let (Some(native), Some(anchor)) = (self.native, self.anchor) {
78
platform::stack_above(native, anchor);
79
}
80
}
81
73
}
82
74
83
75
struct ShareSession {
@@ -341,10 +333,8 @@ impl CallWindows {
341
333
placed.set_visible(true);
342
334
}
343
335
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
336
}
337
self.restack_borders(shared_window);
348
338
}
349
339
BorderVisibility::Hide => {
350
340
for side in 0..BORDER_SIDES {
@@ -466,6 +456,16 @@ impl CallWindows {
466
456
}
467
457
}
468
458
459
fn restack_borders(&self, shared_window: Option<NativeHandle>) {
460
let Some(shared) = shared_window else {
461
return;
462
};
463
let group: Vec<NativeHandle> = (0..BORDER_SIDES)
464
.filter_map(|side| self.windows.get(&Role::Border(side))?.native)
465
.collect();
466
platform::stack_group_above(shared, &group);
467
}
468
469
469
fn ensure_window(
470
470
&mut self,
471
471
role: Role,
@@ -37,19 +37,17 @@ struct Placed {37
37
38
impl Placed {38
impl Placed {39
fn place(&mut self, rect: ScreenRect, cx: &mut App) {39
fn place(&mut self, rect: ScreenRect, cx: &mut App) {40
if self.rect == Some(rect) {41
return;42
}40
self.rect = Some(rect);43
self.rect = Some(rect);41
let Some(native) = self.native else {44
let Some(native) = self.native else {42
return;45
return;43
};46
};44
match self.anchor {47
match self.anchor {45
Some(anchor) => platform::place_above(48
Some(_) => {46
native,49
platform::place_rect(native, rect.left, rect.top, rect.width(), rect.height())47
anchor,50
}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()),51
None => platform::place(native, rect.left, rect.top, rect.width(), rect.height()),54
}52
}55
// WM_SIZE arrives while the app is borrowed, so GPUI drops its own resize report.53
// WM_SIZE arrives while the app is borrowed, so GPUI drops its own resize report.@@ -72,12 +70,6 @@ impl Placed {72
platform::hide_window(native);70
platform::hide_window(native);73
}71
}74
}72
}75
76
fn restack(&self) {77
if let (Some(native), Some(anchor)) = (self.native, self.anchor) {78
platform::stack_above(native, anchor);79
}80
}81
}73
}82
74
83
struct ShareSession {75
struct ShareSession {@@ -341,10 +333,8 @@ impl CallWindows {341
placed.set_visible(true);333
placed.set_visible(true);342
}334
}343
self.ensure_window(Role::Border(side), rect, work.scale, true, cx);335
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
}336
}337
self.restack_borders(shared_window);348
}338
}349
BorderVisibility::Hide => {339
BorderVisibility::Hide => {350
for side in 0..BORDER_SIDES {340
for side in 0..BORDER_SIDES {@@ -466,6 +456,16 @@ impl CallWindows {466
}456
}467
}457
}468
458
459
fn restack_borders(&self, shared_window: Option<NativeHandle>) {460
let Some(shared) = shared_window else {461
return;462
};463
let group: Vec<NativeHandle> = (0..BORDER_SIDES)464
.filter_map(|side| self.windows.get(&Role::Border(side))?.native)465
.collect();466
platform::stack_group_above(shared, &group);467
}468
469
fn ensure_window(469
fn ensure_window(470
&mut self,470
&mut self,471
role: Role,471
role: Role,@@ -12,6 +12,9 @@ pub use stub::*;
12
12
13
13
pub type NativeHandle = isize;
14
14
15
#[cfg_attr(not(windows), allow(dead_code))]
16
mod stack_plan;
17
15
18
#[derive(Debug, Clone, Copy, PartialEq)]
16
19
pub struct WorkArea {
17
20
pub left: i32,
@@ -12,6 +12,9 @@ pub use stub::*;12
12
13
pub type NativeHandle = isize;13
pub type NativeHandle = isize;14
14
15
#[cfg_attr(not(windows), allow(dead_code))]16
mod stack_plan;17
15
#[derive(Debug, Clone, Copy, PartialEq)]18
#[derive(Debug, Clone, Copy, PartialEq)]16
pub struct WorkArea {19
pub struct WorkArea {17
pub left: i32,20
pub left: i32,@@ -0,0 +1,67 @@
1
use super::NativeHandle;
2
3
#[derive(Debug, Clone, PartialEq, Eq)]
4
pub enum StackPlan {
5
Keep,
6
Rebuild { after: Option<NativeHandle> },
7
}
8
9
pub fn stack_plan(above: &[NativeHandle], group: &[NativeHandle]) -> StackPlan {
10
let border_count = above
11
.iter()
12
.take_while(|handle| group.contains(handle))
13
.count();
14
let covers_group = group
15
.iter()
16
.all(|handle| above[..border_count].contains(handle));
17
if covers_group {
18
StackPlan::Keep
19
} else {
20
StackPlan::Rebuild {
21
after: above.get(border_count).copied(),
22
}
23
}
24
}
25
26
#[cfg(test)]
27
mod tests {
28
use super::*;
29
30
const GROUP: [NativeHandle; 4] = [1, 2, 3, 4];
31
32
#[test]
33
fn rotated_group_directly_above_is_kept() {
34
assert_eq!(stack_plan(&[3, 1, 4, 2, 99], &GROUP), StackPlan::Keep);
35
assert_eq!(stack_plan(&[1, 2, 3, 4], &GROUP), StackPlan::Keep);
36
}
37
38
#[test]
39
fn foreign_window_between_rebuilds_after_it() {
40
assert_eq!(
41
stack_plan(&[1, 2, 99], &GROUP),
42
StackPlan::Rebuild { after: Some(99) }
43
);
44
assert_eq!(
45
stack_plan(&[99, 1, 2, 3, 4], &GROUP),
46
StackPlan::Rebuild { after: Some(99) }
47
);
48
}
49
50
#[test]
51
fn nothing_above_rebuilds_with_band_rule() {
52
assert_eq!(stack_plan(&[], &GROUP), StackPlan::Rebuild { after: None });
53
}
54
55
#[test]
56
fn shared_topmost_with_missing_borders_rebuilds_with_band_rule() {
57
assert_eq!(
58
stack_plan(&[2, 4], &GROUP),
59
StackPlan::Rebuild { after: None }
60
);
61
}
62
63
#[test]
64
fn empty_group_is_kept() {
65
assert_eq!(stack_plan(&[99], &[]), StackPlan::Keep);
66
}
67
}
@@ -0,0 +1,67 @@1
use super::NativeHandle;2
3
#[derive(Debug, Clone, PartialEq, Eq)]4
pub enum StackPlan {5
Keep,6
Rebuild { after: Option<NativeHandle> },7
}8
9
pub fn stack_plan(above: &[NativeHandle], group: &[NativeHandle]) -> StackPlan {10
let border_count = above11
.iter()12
.take_while(|handle| group.contains(handle))13
.count();14
let covers_group = group15
.iter()16
.all(|handle| above[..border_count].contains(handle));17
if covers_group {18
StackPlan::Keep19
} else {20
StackPlan::Rebuild {21
after: above.get(border_count).copied(),22
}23
}24
}25
26
#[cfg(test)]27
mod tests {28
use super::*;29
30
const GROUP: [NativeHandle; 4] = [1, 2, 3, 4];31
32
#[test]33
fn rotated_group_directly_above_is_kept() {34
assert_eq!(stack_plan(&[3, 1, 4, 2, 99], &GROUP), StackPlan::Keep);35
assert_eq!(stack_plan(&[1, 2, 3, 4], &GROUP), StackPlan::Keep);36
}37
38
#[test]39
fn foreign_window_between_rebuilds_after_it() {40
assert_eq!(41
stack_plan(&[1, 2, 99], &GROUP),42
StackPlan::Rebuild { after: Some(99) }43
);44
assert_eq!(45
stack_plan(&[99, 1, 2, 3, 4], &GROUP),46
StackPlan::Rebuild { after: Some(99) }47
);48
}49
50
#[test]51
fn nothing_above_rebuilds_with_band_rule() {52
assert_eq!(stack_plan(&[], &GROUP), StackPlan::Rebuild { after: None });53
}54
55
#[test]56
fn shared_topmost_with_missing_borders_rebuilds_with_band_rule() {57
assert_eq!(58
stack_plan(&[2, 4], &GROUP),59
StackPlan::Rebuild { after: None }60
);61
}62
63
#[test]64
fn empty_group_is_kept() {65
assert_eq!(stack_plan(&[99], &[]), StackPlan::Keep);66
}67
}@@ -24,17 +24,9 @@ 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
}
27
pub fn place_rect(_handle: NativeHandle, _x: i32, _y: i32, _width: i32, _height: i32) {}
36
28
37
pub fn stack_above(_handle: NativeHandle, _shared: NativeHandle) {}
29
pub fn stack_group_above(_shared: NativeHandle, _group: &[NativeHandle]) {}
38
30
39
31
pub fn show_window_inactive(_handle: NativeHandle) {}
40
32
@@ -24,17 +24,9 @@ pub fn work_area_at(_x: i32, _y: i32) -> Option<WorkArea> {24
24
25
pub fn place(_handle: NativeHandle, _x: i32, _y: i32, _width: i32, _height: i32) {}25
pub fn place(_handle: NativeHandle, _x: i32, _y: i32, _width: i32, _height: i32) {}26
26
27
pub fn place_above(27
pub fn place_rect(_handle: NativeHandle, _x: i32, _y: i32, _width: i32, _height: i32) {}28
_handle: NativeHandle,29
_shared: NativeHandle,30
_x: i32,31
_y: i32,32
_width: i32,33
_height: i32,34
) {35
}36
28
37
pub fn stack_above(_handle: NativeHandle, _shared: NativeHandle) {}29
pub fn stack_group_above(_shared: NativeHandle, _group: &[NativeHandle]) {}38
30
39
pub fn show_window_inactive(_handle: NativeHandle) {}31
pub fn show_window_inactive(_handle: NativeHandle) {}40
32
@@ -40,6 +40,7 @@ use windows::Win32::UI::WindowsAndMessaging::{
40
40
};
41
41
use windows::core::{BOOL, PCWSTR, w};
42
42
43
use super::stack_plan::{StackPlan, stack_plan};
43
44
use super::{NativeHandle, ScreenRect, ShareGeometry, TrayCommand, WorkArea};
44
45
use crate::notify::badge::{BADGE_SIZE, Badge, render_badge};
45
46
use crate::notify::ring_tone::{Ringtone, SAMPLE_RATE, ringback_cycle};
@@ -196,58 +197,64 @@ fn is_topmost(window: HWND) -> bool {
196
197
unsafe { GetWindowLongPtrW(window, GWL_EXSTYLE) & WS_EX_TOPMOST.0 as isize != 0 }
197
198
}
198
199
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
}
200
fn band_after(shared_topmost: bool, previous: Option<HWND>) -> HWND {
201
match previous {
202
Some(previous) if shared_topmost || !is_topmost(previous) => previous,
203
_ if shared_topmost => HWND_TOPMOST,
204
_ => HWND_NOTOPMOST,
212
205
}
213
206
}
214
207
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);
208
fn windows_above(shared: HWND, group: &[NativeHandle]) -> Vec<NativeHandle> {
209
let mut above = Vec::new();
210
let mut current = shared;
211
while let Ok(previous) = unsafe { GetWindow(current, GW_HWNDPREV) } {
212
above.push(previous.0 as NativeHandle);
213
if !group.contains(&(previous.0 as NativeHandle)) {
214
break;
215
}
216
current = previous;
231
217
}
218
above
232
219
}
233
220
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
};
221
pub fn place_rect(handle: NativeHandle, x: i32, y: i32, width: i32, height: i32) {
238
222
unsafe {
239
223
let _ = SetWindowPos(
240
224
hwnd(handle),
241
Some(insert_after),
242
0,
243
0,
244
0,
245
0,
246
SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE,
225
None,
226
x,
227
y,
228
width,
229
height,
230
SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER,
247
231
);
248
232
}
249
233
}
250
234
235
pub fn stack_group_above(shared: NativeHandle, group: &[NativeHandle]) {
236
let shared = hwnd(shared);
237
let above = windows_above(shared, group);
238
let StackPlan::Rebuild { after } = stack_plan(&above, group) else {
239
return;
240
};
241
let mut insert_after = band_after(is_topmost(shared), after.map(hwnd));
242
for handle in group {
243
unsafe {
244
let _ = SetWindowPos(
245
hwnd(*handle),
246
Some(insert_after),
247
0,
248
0,
249
0,
250
0,
251
SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE,
252
);
253
}
254
insert_after = hwnd(*handle);
255
}
256
}
257
251
258
pub fn show_window_inactive(handle: NativeHandle) {
252
259
unsafe {
253
260
let _ = ShowWindow(hwnd(handle), SW_SHOWNOACTIVATE);
@@ -40,6 +40,7 @@ use windows::Win32::UI::WindowsAndMessaging::{40
};40
};41
use windows::core::{BOOL, PCWSTR, w};41
use windows::core::{BOOL, PCWSTR, w};42
42
43
use super::stack_plan::{StackPlan, stack_plan};43
use super::{NativeHandle, ScreenRect, ShareGeometry, TrayCommand, WorkArea};44
use super::{NativeHandle, ScreenRect, ShareGeometry, TrayCommand, WorkArea};44
use crate::notify::badge::{BADGE_SIZE, Badge, render_badge};45
use crate::notify::badge::{BADGE_SIZE, Badge, render_badge};45
use crate::notify::ring_tone::{Ringtone, SAMPLE_RATE, ringback_cycle};46
use crate::notify::ring_tone::{Ringtone, SAMPLE_RATE, ringback_cycle};@@ -196,58 +197,64 @@ fn is_topmost(window: HWND) -> bool {196
unsafe { GetWindowLongPtrW(window, GWL_EXSTYLE) & WS_EX_TOPMOST.0 as isize != 0 }197
unsafe { GetWindowLongPtrW(window, GWL_EXSTYLE) & WS_EX_TOPMOST.0 as isize != 0 }197
}198
}198
199
199
fn insert_after_above(handle: HWND, shared: HWND) -> Option<HWND> {200
fn band_after(shared_topmost: bool, previous: Option<HWND>) -> HWND {200
let shared_topmost = is_topmost(shared);201
match previous {201
let band = if shared_topmost {202
Some(previous) if shared_topmost || !is_topmost(previous) => previous,202
HWND_TOPMOST203
_ if shared_topmost => HWND_TOPMOST,203
} else {204
_ => HWND_NOTOPMOST,204
HWND_NOTOPMOST205
};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
}205
}213
}206
}214
207
215
pub fn place_above(208
fn windows_above(shared: HWND, group: &[NativeHandle]) -> Vec<NativeHandle> {216
handle: NativeHandle,209
let mut above = Vec::new();217
shared: NativeHandle,210
let mut current = shared;218
x: i32,211
while let Ok(previous) = unsafe { GetWindow(current, GW_HWNDPREV) } {219
y: i32,212
above.push(previous.0 as NativeHandle);220
width: i32,213
if !group.contains(&(previous.0 as NativeHandle)) {221
height: i32,214
break;222
) {215
}223
let insert_after = insert_after_above(hwnd(handle), hwnd(shared));216
current = previous;224
let flags = if insert_after.is_some() {225
SWP_NOACTIVATE | SWP_NOOWNERZORDER226
} else {227
SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER228
};229
unsafe {230
let _ = SetWindowPos(hwnd(handle), insert_after, x, y, width, height, flags);231
}217
}218
above232
}219
}233
220
234
pub fn stack_above(handle: NativeHandle, shared: NativeHandle) {221
pub fn place_rect(handle: NativeHandle, x: i32, y: i32, width: i32, height: i32) {235
let Some(insert_after) = insert_after_above(hwnd(handle), hwnd(shared)) else {236
return;237
};238
unsafe {222
unsafe {239
let _ = SetWindowPos(223
let _ = SetWindowPos(240
hwnd(handle),224
hwnd(handle),241
Some(insert_after),225
None,242
0,226
x,243
0,227
y,244
0,228
width,245
0,229
height,246
SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE,230
SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER,247
);231
);248
}232
}249
}233
}250
234
235
pub fn stack_group_above(shared: NativeHandle, group: &[NativeHandle]) {236
let shared = hwnd(shared);237
let above = windows_above(shared, group);238
let StackPlan::Rebuild { after } = stack_plan(&above, group) else {239
return;240
};241
let mut insert_after = band_after(is_topmost(shared), after.map(hwnd));242
for handle in group {243
unsafe {244
let _ = SetWindowPos(245
hwnd(*handle),246
Some(insert_after),247
0,248
0,249
0,250
0,251
SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE,252
);253
}254
insert_after = hwnd(*handle);255
}256
}257
251
pub fn show_window_inactive(handle: NativeHandle) {258
pub fn show_window_inactive(handle: NativeHandle) {252
unsafe {259
unsafe {253
let _ = ShowWindow(hwnd(handle), SW_SHOWNOACTIVATE);260
let _ = ShowWindow(hwnd(handle), SW_SHOWNOACTIVATE);