diff --git a/src/window.rs b/src/window.rs index e8144667..1f825780 100644 --- a/src/window.rs +++ b/src/window.rs @@ -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 { + 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) @@ -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, diff --git a/src/window/placement_regression_tests.rs b/src/window/placement_regression_tests.rs index 5e7dec5d..46786f61 100644 --- a/src/window/placement_regression_tests.rs +++ b/src/window/placement_regression_tests.rs @@ -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| { + 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( diff --git a/src/window/positioning.rs b/src/window/positioning.rs index e5c86999..8fdc3669 100644 --- a/src/window/positioning.rs +++ b/src/window/positioning.rs @@ -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, @@ -952,6 +950,39 @@ pub(super) fn system_tray_reference(taskbar: RECT, tray: Option) -> 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, + measured_tray: impl FnOnce() -> Option, +) -> Option { + 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 { + 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, @@ -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 diff --git a/src/window/taskbar_collision.rs b/src/window/taskbar_collision.rs index 9496bff1..7c4f30e4 100644 --- a/src/window/taskbar_collision.rs +++ b/src/window/taskbar_collision.rs @@ -13,6 +13,10 @@ use windows::Win32::UI::Accessibility::*; const MAX_SAMPLE_AGE: Duration = Duration::from_secs(6); static OCCUPANCY: Mutex> = 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> = Mutex::new(Vec::new()); #[derive(Clone, Debug)] pub(super) struct Occupancy { @@ -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 { + 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() @@ -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 { + find_tray(&measured_trays(), hwnd, bounds) +} + +fn find_tray(trays: &[(SendHwnd, RECT, RECT)], hwnd: HWND, bounds: RECT) -> Option { + 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 { @@ -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, @@ -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);