diff --git a/docs/usage/scrolling-layout.md b/docs/usage/scrolling-layout.md index 3e9f443a..cefdef41 100644 --- a/docs/usage/scrolling-layout.md +++ b/docs/usage/scrolling-layout.md @@ -161,9 +161,13 @@ on screen. It keeps its size, so nothing has to be resized to bring it back. Wit A window that is partly in view keeps its size too, and overhangs the edge of the work area. +Windows are seen on whichever monitor they overlap, so with more than one monitor this needs care. If another monitor +is next to the edge that a window would rest at or overhang, a window that has scrolled out of view is hidden instead +of parked, and a window that is partly in view is made narrower to fit its own work area. Each monitor has a strip of +its own, and focus and move commands carry on to the next monitor when they reach the end of one. + ## Limits -- The Scrolling layout is only supported on a single monitor. - Switching to another layout and back keeps the widths, but a column whose windows were moved past each other in the other layout is made again from the order that they are in. - Windows resting at the edges have borders too, so you may see a thin line of one at the edge of the screen. diff --git a/komorebi/src/scrolling.rs b/komorebi/src/scrolling.rs index b1683831..aa9d393a 100644 --- a/komorebi/src/scrolling.rs +++ b/komorebi/src/scrolling.rs @@ -68,6 +68,94 @@ pub(crate) fn parked(rect: Rect, visibility: Visibility, work_area: &Rect) -> Re } } +/// Whether two rectangles have any area in common. +fn overlaps(a: &Rect, b: &Rect) -> bool { + a.left < b.left + b.right + && b.left < a.left + a.right + && a.top < b.top + b.bottom + && b.top < a.top + a.bottom +} + +/// The monitors other than the one that has the work area, which is the one that contains it. +pub(crate) fn other_monitors(work_area: &Rect, monitors: &[Rect]) -> Vec { + monitors + .iter() + .filter(|monitor| { + !(monitor.left <= work_area.left + && monitor.top <= work_area.top + && monitor.left + monitor.right >= work_area.left + work_area.right + && monitor.top + monitor.bottom >= work_area.top + work_area.bottom) + }) + .copied() + .collect() +} + +/// Cut a rectangle down to what is inside the work area. +pub(crate) fn clamp_to_work_area(rect: &mut Rect, work_area: &Rect) { + if rect.left < work_area.left { + rect.right -= work_area.left - rect.left; + rect.left = work_area.left; + } + + let right_edge = work_area.left + work_area.right; + if rect.left + rect.right > right_edge { + rect.right = right_edge - rect.left; + } + + if rect.top < work_area.top { + rect.bottom -= work_area.top - rect.top; + rect.top = work_area.top; + } + + let bottom_edge = work_area.top + work_area.bottom; + if rect.top + rect.bottom > bottom_edge { + rect.bottom = bottom_edge - rect.top; + } +} + +/// Where a window rests, and whether it has to be hidden to rest there, when `others` are the +/// other monitors. +/// +/// A window is seen on whichever monitor it overlaps. One that rests or overhangs past an edge that +/// another monitor is next to would be seen there, so a window that scrolled out of view is hidden +/// instead of parked, and a window that is partly in view is made narrower to fit the work area. +/// Without a monitor next to it, the window is where [`parked`] puts it. +pub(crate) fn rest( + rect: Rect, + visibility: Visibility, + work_area: &Rect, + others: &[Rect], +) -> (Rect, bool) { + let at = parked(rect, visibility, work_area); + let seen_elsewhere = others.iter().any(|monitor| overlaps(&at, monitor)); + + match visibility { + Visibility::Visible => (at, false), + Visibility::Clipped if seen_elsewhere => { + let mut clamped = at; + clamp_to_work_area(&mut clamped, work_area); + (clamped, false) + } + Visibility::OffLeft | Visibility::OffRight if seen_elsewhere => (at, true), + _ => (at, false), + } +} + +/// Whether a window that is about to be put where the strip says should be shown first, because it +/// was hidden when it went out of view past an edge with another monitor next to it. +/// +/// Only the window that is showing in its container is: the others are the tabs at the back of a +/// stack, which are hidden because they are at the back, and showing them would put them in front +/// of the one that is in front, and the system would take the one that was shown for the one that +/// was chosen. +pub(crate) fn should_restore_when_placed( + window: isize, + focused_in_container: Option, + hidden: &[isize], +) -> bool { + focused_in_container == Some(window) && hidden.contains(&window) +} + /// Whether windows that are moved are animated to where they go. pub(crate) fn animations_enabled() -> bool { ANIMATION_ENABLED_GLOBAL.load(Ordering::SeqCst) @@ -109,6 +197,8 @@ impl Workspace { let excess = container .windows() .iter() + // a window that is hidden has not been where it was told to be + .filter(|window| !crate::HIDDEN_HWNDS.lock().contains(&window.hwnd)) .filter_map(|window| WindowsApi::window_rect(window.hwnd).ok()) .map(|actual| actual.right - placed.right) .max() @@ -783,6 +873,74 @@ mod tests { ); } + fn monitor(left: i32) -> Rect { + Rect { + left, + top: 0, + right: 1400, + bottom: 900, + } + } + + #[test] + fn the_monitor_with_the_work_area_is_not_one_of_the_others() { + let monitors = [monitor(-1400), monitor(0), monitor(1400)]; + let others = other_monitors(&AREA, &monitors); + + assert_eq!(others, vec![monitor(-1400), monitor(1400)]); + } + + #[test] + fn a_window_that_would_be_seen_on_a_neighbouring_monitor_is_hidden_or_made_narrower() { + let work_area = Rect { + left: 0, + top: 0, + right: 1400, + bottom: 900, + }; + let others = [monitor(1400)]; + let wide = Rect { + left: -1000, + top: 0, + right: 400, + bottom: 900, + }; + + // out to the left, with nothing there, it rests at the edge + let (rect, hidden) = rest(wide, Visibility::OffLeft, &work_area, &others); + assert!(!hidden); + assert_eq!(rect.left + rect.right, PARKED_VISIBLE_PIXELS); + + // out to the right, where there is a monitor, it is hidden + let off_right = Rect { left: 2000, ..wide }; + let (_, hidden) = rest(off_right, Visibility::OffRight, &work_area, &others); + assert!(hidden); + + // partly in view at the right edge it is cut down to the work area + let clipped = Rect { left: 1200, ..wide }; + let (rect, hidden) = rest(clipped, Visibility::Clipped, &work_area, &others); + assert!(!hidden); + assert_eq!(rect.left + rect.right, 1400); + + // and without a monitor there it is left as it is + let (rect, _) = rest(clipped, Visibility::Clipped, &work_area, &[]); + assert_eq!(rect, clipped); + } + + #[test] + fn only_the_window_at_the_front_of_a_stack_is_shown_when_it_is_placed() { + let hidden = [10, 11]; + + // the one at the front, if it was hidden, is shown + assert!(should_restore_when_placed(10, Some(10), &hidden)); + // the one at the back stays hidden, whatever is hidden + assert!(!should_restore_when_placed(11, Some(10), &hidden)); + // one that is not hidden has nothing to be shown from + assert!(!should_restore_when_placed(12, Some(12), &hidden)); + // and a container with no window in front shows none + assert!(!should_restore_when_placed(10, None, &hidden)); + } + #[test] fn expanding_fills_the_room_that_the_columns_in_view_leave() { let mut workspace = workspace(3, 2); diff --git a/komorebi/src/static_config.rs b/komorebi/src/static_config.rs index 2fa67419..c66c2fa7 100644 --- a/komorebi/src/static_config.rs +++ b/komorebi/src/static_config.rs @@ -1419,8 +1419,6 @@ impl StaticConfig { workspace_matching_rules.clear(); drop(workspace_matching_rules); - let monitor_count = wm.monitors().len(); - let offset = wm.work_area_offset; for (i, monitor) in wm.monitors_mut().iter_mut().enumerate() { let preferred_config_idx = { @@ -1469,15 +1467,6 @@ impl StaticConfig { monitor.update_workspaces_globals(offset); for (j, ws) in monitor.workspaces_mut().iter_mut().enumerate() { if let Some(workspace_config) = monitor_config.workspaces.get_mut(j) { - if monitor_count > 1 - && matches!(workspace_config.layout, Some(DefaultLayout::Scrolling)) - { - tracing::warn!( - "scrolling layout is only supported for a single monitor; falling back to columns layout" - ); - workspace_config.layout = Some(DefaultLayout::Columns); - } - ws.load_static_config(workspace_config, value.layout_defaults.as_ref())?; } } @@ -1786,7 +1775,6 @@ impl StaticConfig { }; let monitor_count = wm.monitors().len(); - for i in 0..monitor_count { wm.update_focused_workspace_by_monitor_idx(i)?; let ws_idx = wm.focused_workspace_idx_for_monitor_idx(i)?; diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 7b89f3dc..b96e8ca5 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -3316,16 +3316,8 @@ impl WindowManager { pub fn change_workspace_layout_default(&mut self, layout: DefaultLayout) -> eyre::Result<()> { tracing::info!("changing layout"); - let monitor_count = self.monitors().len(); let workspace = self.focused_workspace_mut()?; - if monitor_count > 1 && matches!(layout, DefaultLayout::Scrolling) { - tracing::warn!( - "scrolling layout is only supported for a single monitor; not changing layout" - ); - return Ok(()); - } - match &workspace.layout { Layout::Default(_) => {} Layout::Custom(layout) => { diff --git a/komorebi/src/windows_api.rs b/komorebi/src/windows_api.rs index 1589d2b4..183b41cf 100644 --- a/komorebi/src/windows_api.rs +++ b/komorebi/src/windows_api.rs @@ -262,6 +262,22 @@ impl WindowsApi { .process() } + /// The rectangle of every monitor that is attached. + pub fn monitor_rects() -> Vec { + let mut rects: Vec = vec![]; + + if Self::enum_display_monitors( + Some(windows_callbacks::enum_monitor_rects), + &raw mut rects as isize, + ) + .is_err() + { + tracing::warn!("could not enumerate the monitors"); + } + + rects + } + pub fn valid_hmonitors() -> eyre::Result> { Ok(win32_display_data::connected_displays_all() .flatten() diff --git a/komorebi/src/windows_callbacks.rs b/komorebi/src/windows_callbacks.rs index 5a11547f..3094f338 100644 --- a/komorebi/src/windows_callbacks.rs +++ b/komorebi/src/windows_callbacks.rs @@ -2,6 +2,7 @@ use std::collections::VecDeque; use crate::border_manager; use crate::container::Container; +use crate::core::Rect; use crate::window::RuleDebug; use crate::window::Window; use crate::window_manager_event::WindowManagerEvent; @@ -10,7 +11,10 @@ use crate::winevent::WinEvent; use crate::winevent_listener; use windows::Win32::Foundation::HWND; use windows::Win32::Foundation::LPARAM; +use windows::Win32::Foundation::RECT; use windows::Win32::Foundation::WPARAM; +use windows::Win32::Graphics::Gdi::HDC; +use windows::Win32::Graphics::Gdi::HMONITOR; use windows::Win32::UI::Accessibility::HWINEVENTHOOK; use windows::Win32::UI::WindowsAndMessaging::GWL_EXSTYLE; use windows::Win32::UI::WindowsAndMessaging::GWL_STYLE; @@ -22,6 +26,22 @@ use windows::Win32::UI::WindowsAndMessaging::WS_EX_NOACTIVATE; use windows::Win32::UI::WindowsAndMessaging::WS_EX_TOOLWINDOW; use windows_core::BOOL; +/// Collects the rectangle of every monitor, as the system sees them. +pub extern "system" fn enum_monitor_rects( + hmonitor: HMONITOR, + _hdc: HDC, + _rect: *mut RECT, + lparam: LPARAM, +) -> BOOL { + let rects = unsafe { &mut *(lparam.0 as *mut Vec) }; + + if let Ok(info) = WindowsApi::monitor_info_w(hmonitor) { + rects.push(Rect::from(info.monitorInfo.rcMonitor)); + } + + true.into() +} + pub extern "system" fn enum_window(hwnd: HWND, lparam: LPARAM) -> BOOL { let containers = unsafe { &mut *(lparam.0 as *mut VecDeque) }; diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index 5a07c81f..622ac589 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -11,6 +11,7 @@ use crate::DATA_DIR; use crate::DEFAULT_CONTAINER_PADDING; use crate::DEFAULT_WORKSPACE_PADDING; use crate::FloatingLayerBehaviour; +use crate::HIDDEN_HWNDS; use crate::INITIAL_CONFIGURATION_LOADED; use crate::KomorebiTheme; use crate::NO_TITLEBAR; @@ -730,15 +731,24 @@ impl Workspace { self.layout_options, self.layout_options_rules.len(), ); + let mut hidden = vec![]; let mut layouts = if self.is_scrolling() { + let others = crate::scrolling::other_monitors( + &self.globals.work_area, + &WindowsApi::monitor_rects(), + ); + self.layout_scrolling(&adjusted_work_area, container_padding) .into_iter() .map(|placement| { - crate::scrolling::parked( + let (rect, hide) = crate::scrolling::rest( placement.rect, placement.visibility, &adjusted_work_area, - ) + &others, + ); + hidden.push(hide); + rect }) .collect() } else { @@ -805,7 +815,20 @@ impl Workspace { WindowsApi::restore_window(window.hwnd); } } - window.set_position(layout, false)?; + if hidden.get(i).copied().unwrap_or_default() { + // it is seen on another monitor wherever it rests here + window.hide(); + } else { + if crate::scrolling::should_restore_when_placed( + window.hwnd, + container.focused_window().map(|w| w.hwnd), + &HIDDEN_HWNDS.lock(), + ) { + window.restore(); + } + + window.set_position(layout, false)?; + } } } }