Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,9 +674,18 @@ fn spawn_taskbar_watchdog() {
if let Some(s) = state.as_ref() {
let tray_hwnd = s.tray_notify_hwnd.map(|h| h.to_hwnd());
let target_hwnd = controller_for(s.hwnd.to_hwnd());
// A secondary taskbar has no TrayNotifyWnd. Its clock is only
// measured, and the first sample follows the initial placement.
let measured_tray = || -> Option<RECT> {
let taskbar = s.taskbar_hwnd?.to_hwnd();
let bounds = native_interop::get_taskbar_rect(taskbar)?;
taskbar_collision::measured_tray(taskbar, bounds)
};
(
target_hwnd,
tray_hwnd.and_then(native_interop::get_window_rect_safe),
tray_hwnd
.and_then(native_interop::get_window_rect_safe)
.or_else(measured_tray),
)
} else {
(HWND::default(), None)
Expand Down Expand Up @@ -955,8 +964,7 @@ fn restored_dock_rect(state: &AppState, taskbar: HWND, taskbar_rect: RECT) -> Op
let mut placement = surface.placement.clone();
placement.offset_x = offsets.offset_x;
placement.offset_y = offsets.offset_y;
let tray = native_interop::find_child_window(taskbar, "TrayNotifyWnd")
.and_then(native_interop::get_window_rect_safe);
let tray = positioning::taskbar_tray_rect(&placement, taskbar, taskbar_rect);
Some(positioning::surface_screen_rect(
&placement,
frame.width,
Expand Down
53 changes: 53 additions & 0 deletions src/window/placement_regression_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,59 @@ fn smart_anchoring_fractional_dpi_scaling() {
assert_eq!(rect.left, 1000);
}

#[test]
fn drag_docked_widget_stays_clear_of_a_secondary_taskbar_clock() {
// A secondary taskbar has no TrayNotifyWnd; its clock is only measured.
let monitor = RECT {
left: 3840,
top: 561,
right: 5760,
bottom: 1641,
};
let taskbar = RECT {
top: 1593,
..monitor
};
let clock = RECT {
left: 5682,
right: 5752,
..taskbar
};
let occupancy = collision_fixture(taskbar, Some(clock), Some(4203));
let rect_for = |placement: &theme_engine::Placement, width, measured: Option<RECT>| {
let tray = positioning::placement_tray_rect(placement, None, || measured);
positioning::surface_screen_rect(placement, width, 46, 1.0, monitor, Some(taskbar), tray)
};
let edges = |rect: RECT| (rect.left, rect.right);

// Dropped 1436px along the bar, where it fits beside the clock.
let dragged = positioning::taskbar_dock_placement(1, 1436, 1.0, true);
let measured = occupancy.reserved_extent();
assert_eq!(measured, Some(clock));
assert_eq!(edges(rect_for(&dragged, 310, measured)), (5276, 5586));
// Enabling another provider widens it: it grows away from the clock.
assert_eq!(edges(rect_for(&dragged, 502, measured)), (5180, 5682));
// Before the first sample only the end of the bar is known.
assert_eq!(edges(rect_for(&dragged, 502, None)), (5258, 5760));

// A TrayNotifyWnd is used as is, without consulting the measurement.
let tray_window = RECT {
left: 5600,
..taskbar
};
assert_eq!(
positioning::placement_tray_rect(&dragged, Some(tray_window), || unreachable!()),
Some(tray_window)
);
// Tray-referenced placements keep anchoring to the end of the bar.
let snapped = positioning::dock_placement(1, 0, 1.0, true);
assert_eq!(
positioning::placement_tray_rect(&snapped, None, || unreachable!()),
None
);
assert_eq!(edges(rect_for(&snapped, 310, measured)), (5450, 5760));
}

// Fixture for the traditional leading-apps/trailing-tray arrangement. The
// production detector also supports independent groups and leading-side gaps.
fn collision_fixture(
Expand Down
42 changes: 35 additions & 7 deletions src/window/positioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,7 @@ pub(super) fn position_custom_theme_internal(
let taskbar = native_interop::taskbar_for_monitor(&taskbars, &displays, display.handle);
let width = scaled_theme_dimension(theme.canvas.width.max(1), scale);
let height = scaled_theme_dimension(theme.canvas.height.max(1), scale);
let tray = taskbar
.and_then(|tb| native_interop::find_child_window(tb.hwnd, "TrayNotifyWnd"))
.and_then(native_interop::get_window_rect_safe);
let tray = taskbar.and_then(|tb| taskbar_tray_rect(&theme.placement, tb.hwnd, tb.rect));
let rect = surface_screen_rect(
&theme.placement,
width,
Expand Down Expand Up @@ -952,6 +950,39 @@ pub(super) fn system_tray_reference(taskbar: RECT, tray: Option<RECT>) -> RECT {
})
}

/// Drag-docked placements stay clear of the notification area; authored
/// placements keep their exact position.
fn clamps_to_tray(placement: &theme_engine::Placement) -> bool {
placement.clamp_taskbar_drag
&& placement.reference.region == ReferenceRegion::Taskbar
&& placement.nest == SurfaceNest::Taskbar
}

/// The notification area a placement is positioned against. A secondary
/// taskbar has no TrayNotifyWnd, so the clock a drag-docked widget must stay
/// clear of is known only from the measured occupancy. Tray-referenced
/// placements still anchor to the end of such a bar.
pub(super) fn placement_tray_rect(
placement: &theme_engine::Placement,
tray_window: Option<RECT>,
measured_tray: impl FnOnce() -> Option<RECT>,
) -> Option<RECT> {
tray_window.or_else(|| clamps_to_tray(placement).then(measured_tray).flatten())
}

pub(super) fn taskbar_tray_rect(
placement: &theme_engine::Placement,
taskbar: HWND,
taskbar_rect: RECT,
) -> Option<RECT> {
placement_tray_rect(
placement,
native_interop::find_child_window(taskbar, "TrayNotifyWnd")
.and_then(native_interop::get_window_rect_safe),
|| taskbar_collision::measured_tray(taskbar, taskbar_rect),
)
}

pub(super) fn surface_screen_rect(
placement: &theme_engine::Placement,
width: i32,
Expand Down Expand Up @@ -984,10 +1015,7 @@ pub(super) fn surface_screen_rect(
vertical_anchor_factor(placement.surface_vertical.unwrap_or(placement.vertical)),
(placement.offset_y as f64 * scale).round() as i32,
);
let (x, y) = if placement.clamp_taskbar_drag
&& placement.reference.region == ReferenceRegion::Taskbar
&& placement.nest == SurfaceNest::Taskbar
{
let (x, y) = if clamps_to_tray(placement) {
if let Some(tb) = taskbar {
if native_interop::is_taskbar_horizontal(tb) {
let max_x = tray
Expand Down
83 changes: 81 additions & 2 deletions src/window/taskbar_collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ use windows::Win32::UI::Accessibility::*;

const MAX_SAMPLE_AGE: Duration = Duration::from_secs(6);
static OCCUPANCY: Mutex<Vec<(SendHwnd, Occupancy, Instant)>> = Mutex::new(Vec::new());
/// Notification area last measured on each taskbar, with the taskbar bounds it
/// was measured in. Unlike OCCUPANCY it outlives a failed or hidden sample: an
/// unreadable bar is unknown, not proof that its clock is gone.
static MEASURED_TRAYS: Mutex<Vec<(SendHwnd, RECT, RECT)>> = Mutex::new(Vec::new());

#[derive(Clone, Debug)]
pub(super) struct Occupancy {
Expand Down Expand Up @@ -44,6 +48,17 @@ impl Occupancy {
.any(|fixed| intersection(item, *fixed) == Some(item))
}

/// Extent of the fixed notification-area controls. A secondary taskbar
/// has no TrayNotifyWnd, so its clock is known only from this measurement.
pub fn reserved_extent(&self) -> Option<RECT> {
self.reserved.iter().copied().reduce(|a, b| RECT {
left: a.left.min(b.left),
top: a.top.min(b.top),
right: a.right.max(b.right),
bottom: a.bottom.max(b.bottom),
})
}

pub fn overlaps_app_controls(&self, widget: RECT) -> bool {
self.occupied
.iter()
Expand Down Expand Up @@ -145,6 +160,31 @@ fn find_sample(
.map(|(_, sample, _)| sample.clone())
}

fn measured_trays() -> MutexGuard<'static, Vec<(SendHwnd, RECT, RECT)>> {
MEASURED_TRAYS.lock().unwrap_or_else(|e| e.into_inner())
}

/// The notification area measured on a taskbar that still has these bounds.
pub(super) fn measured_tray(hwnd: HWND, bounds: RECT) -> Option<RECT> {
find_tray(&measured_trays(), hwnd, bounds)
}

fn find_tray(trays: &[(SendHwnd, RECT, RECT)], hwnd: HWND, bounds: RECT) -> Option<RECT> {
trays
.iter()
.find(|(handle, measured_in, _)| handle.to_hwnd() == hwnd && *measured_in == bounds)
.map(|(_, _, tray)| *tray)
}

/// Only a successful sample replaces a taskbar's entry, including with "no
/// notification area" after the clock is turned off.
fn record_tray(trays: &mut Vec<(SendHwnd, RECT, RECT)>, hwnd: HWND, sample: &Occupancy) {
trays.retain(|(handle, _, _)| handle.to_hwnd() != hwnd);
if let Some(tray) = sample.reserved_extent() {
trays.push((SendHwnd::from_hwnd(hwnd), sample.bounds, tray));
}
}

// Some Win10/third-party shells expose task buttons through legacy UIA proxies.
// Use control roles, not localized titles or Windows-11-only class names.
fn occupies_space(role: UIA_CONTROLTYPE_ID) -> bool {
Expand Down Expand Up @@ -319,8 +359,17 @@ pub(super) fn spawn_reader() {
match Reader::new() {
Ok(reader) => loop {
let mut next = Vec::new();
for taskbar in native_interop::find_taskbars() {
match reader.read(taskbar) {
let taskbars = native_interop::find_taskbars();
// Forget taskbars that no longer exist (Explorer restart).
measured_trays().retain(|(handle, _, _)| {
taskbars.iter().any(|tb| tb.hwnd == handle.to_hwnd())
});
for taskbar in taskbars {
let sample = reader.read(taskbar);
if let Ok(Some(sample)) = &sample {
record_tray(&mut measured_trays(), taskbar.hwnd, sample);
}
match sample {
Ok(Some(sample)) => next.push((
SendHwnd::from_hwnd(taskbar.hwnd),
sample,
Expand Down Expand Up @@ -534,6 +583,36 @@ mod tests {
assert!(find_sample(&[], hwnd, bounds, now).is_none());
}

#[test]
fn measured_tray_follows_the_latest_sample_of_the_same_taskbar_geometry() {
let hwnd = HWND(std::ptr::dangling_mut());
let bounds = rect(3840, 1593, 5760, 1641);
let clock = rect(5682, 1593, 5752, 1641);
let mut sample = layout(bounds, vec![rect(3895, 1593, 4203, 1641), clock]);
assert_eq!(sample.reserved_extent(), None);
sample.reserved.push(clock);
let mut trays = Vec::new();
record_tray(&mut trays, hwnd, &sample);
assert_eq!(find_tray(&trays, hwnd, bounds), Some(clock));
// A moved (auto-hidden, rescaled) or different taskbar is unknown.
assert_eq!(find_tray(&trays, hwnd, rect(3840, 1639, 5760, 1641)), None);
assert_eq!(find_tray(&trays, HWND(2usize as *mut _), bounds), None);

// Several notification-area controls are covered as one extent.
sample.reserved.push(rect(5600, 1593, 5682, 1641));
record_tray(&mut trays, hwnd, &sample);
assert_eq!(
find_tray(&trays, hwnd, bounds),
Some(rect(5600, 1593, 5752, 1641))
);
assert_eq!(trays.len(), 1);

// A sample without a clock (turned off in Settings) clears the entry.
sample.reserved.clear();
record_tray(&mut trays, hwnd, &sample);
assert_eq!(find_tray(&trays, hwnd, bounds), None);
}

#[test]
fn tray_expansion_does_not_trigger_app_collision_while_task_buttons_do() {
let bounds = rect(0, 0, 1920, 48);
Expand Down