Skip to content

Commit

Permalink
fix(cfg,komorebi): remove all uses of invisible borders
Browse files Browse the repository at this point in the history
This makes the borders pixel-perfect, and border_overflow can be
disabled on all applications.

Unfortunately this also means we lose the corner rounding, so that may
need to be done differently or the offsets refined in some way to
address that.
  • Loading branch information
raggi authored and LGUG2Z committed Feb 26, 2024
1 parent 344e6ad commit 4affefa
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 158 deletions.
32 changes: 0 additions & 32 deletions komorebi/src/border.rs
@@ -1,5 +1,4 @@
use std::sync::atomic::Ordering;
use std::time::Duration;

use color_eyre::Result;
use windows::core::PCWSTR;
Expand All @@ -12,17 +11,12 @@ use windows::Win32::UI::WindowsAndMessaging::CS_VREDRAW;
use windows::Win32::UI::WindowsAndMessaging::MSG;
use windows::Win32::UI::WindowsAndMessaging::WNDCLASSW;

use komorebi_core::Rect;

use crate::window::should_act;
use crate::window::Window;
use crate::windows_callbacks;
use crate::WindowsApi;
use crate::BORDER_HWND;
use crate::BORDER_OFFSET;
use crate::BORDER_OVERFLOW_IDENTIFIERS;
use crate::BORDER_RECT;
use crate::REGEX_IDENTIFIERS;
use crate::TRANSPARENCY_COLOUR;
use crate::WINDOWS_11;

Expand Down Expand Up @@ -68,7 +62,6 @@ impl Border {
unsafe {
while GetMessageW(&mut message, border.hwnd(), 0, 0).into() {
DispatchMessageW(&message);
std::thread::sleep(Duration::from_millis(10));
}
}

Expand Down Expand Up @@ -100,7 +93,6 @@ impl Border {
pub fn set_position(
self,
window: Window,
invisible_borders: &Rect,
activate: bool,
) -> Result<()> {
if self.hwnd == 0 {
Expand All @@ -111,30 +103,6 @@ impl Border {
}

let mut rect = WindowsApi::window_rect(window.hwnd())?;
rect.top -= invisible_borders.bottom;
rect.bottom += invisible_borders.bottom;

let border_overflows = BORDER_OVERFLOW_IDENTIFIERS.lock();
let regex_identifiers = REGEX_IDENTIFIERS.lock();

let title = &window.title()?;
let exe_name = &window.exe()?;
let class = &window.class()?;

let should_expand_border = should_act(
title,
exe_name,
class,
&border_overflows,
&regex_identifiers,
);

if should_expand_border {
rect.left -= invisible_borders.left;
rect.top -= invisible_borders.top;
rect.right += invisible_borders.right;
rect.bottom += invisible_borders.bottom;
}

let border_offset = BORDER_OFFSET.lock();
if let Some(border_offset) = *border_offset {
Expand Down
3 changes: 1 addition & 2 deletions komorebi/src/monitor.rs
Expand Up @@ -195,7 +195,6 @@ impl Monitor {
pub fn update_focused_workspace(
&mut self,
offset: Option<Rect>,
invisible_borders: &Rect,
) -> Result<()> {
let work_area = *self.work_area_size();
let offset = if self.work_area_offset().is_some() {
Expand All @@ -206,7 +205,7 @@ impl Monitor {

self.focused_workspace_mut()
.ok_or_else(|| anyhow!("there is no workspace"))?
.update(&work_area, offset, invisible_borders)?;
.update(&work_area, offset)?;

Ok(())
}
Expand Down
17 changes: 5 additions & 12 deletions komorebi/src/process_command.rs
Expand Up @@ -305,7 +305,6 @@ impl WindowManager {
});
}

let invisible_borders = self.invisible_borders;
let offset = self.work_area_offset;

let mut hwnds_to_purge = vec![];
Expand Down Expand Up @@ -348,7 +347,7 @@ impl WindowManager {
.ok_or_else(|| anyhow!("there is no focused workspace"))?
.remove_window(hwnd)?;

monitor.update_focused_workspace(offset, &invisible_borders)?;
monitor.update_focused_workspace(offset)?;
}
}
SocketMessage::FocusedWorkspaceContainerPadding(adjustment) => {
Expand Down Expand Up @@ -1094,10 +1093,7 @@ impl WindowManager {
SocketMessage::UnmanageFocusedWindow => {
self.unmanage_focused_window()?;
}
SocketMessage::InvisibleBorders(rect) => {
self.invisible_borders = rect;
self.retile_all(false)?;
}
SocketMessage::InvisibleBorders(_rect) => {}
SocketMessage::WorkAreaOffset(rect) => {
self.work_area_offset = Option::from(rect);
self.retile_all(false)?;
Expand Down Expand Up @@ -1390,9 +1386,6 @@ impl WindowManager {
| SocketMessage::FocusWorkspaceNumber(_) => {
let foreground = WindowsApi::foreground_window()?;
let foreground_window = Window { hwnd: foreground };
let mut rect = WindowsApi::window_rect(foreground_window.hwnd())?;
rect.top -= self.invisible_borders.bottom;
rect.bottom += self.invisible_borders.bottom;

let monocle = BORDER_COLOUR_MONOCLE.load(Ordering::SeqCst);
if monocle != 0 && self.focused_workspace()?.monocle_container().is_some() {
Expand All @@ -1409,7 +1402,7 @@ impl WindowManager {
}

let border = Border::from(BORDER_HWND.load(Ordering::SeqCst));
border.set_position(foreground_window, &self.invisible_borders, false)?;
border.set_position(foreground_window, false)?;
}
SocketMessage::TogglePause => {
let is_paused = self.is_paused;
Expand All @@ -1419,7 +1412,7 @@ impl WindowManager {
border.hide()?;
} else {
let focused = self.focused_window()?;
border.set_position(*focused, &self.invisible_borders, true)?;
border.set_position(*focused, true)?;
focused.focus(false)?;
}
}
Expand All @@ -1429,7 +1422,7 @@ impl WindowManager {

if tiling_enabled {
let focused = self.focused_window()?;
border.set_position(*focused, &self.invisible_borders, true)?;
border.set_position(*focused, true)?;
focused.focus(false)?;
} else {
border.hide()?;
Expand Down
25 changes: 5 additions & 20 deletions komorebi/src/process_event.rs
Expand Up @@ -109,7 +109,6 @@ impl WindowManager {
_ => {}
}

let invisible_borders = self.invisible_borders;
let offset = self.work_area_offset;

for (i, monitor) in self.monitors_mut().iter_mut().enumerate() {
Expand All @@ -123,7 +122,7 @@ impl WindowManager {
for (j, workspace) in monitor.workspaces_mut().iter_mut().enumerate() {
let reaped_orphans = workspace.reap_orphans()?;
if reaped_orphans.0 > 0 || reaped_orphans.1 > 0 {
workspace.update(&work_area, offset, &invisible_borders)?;
workspace.update(&work_area, offset)?;
tracing::info!(
"reaped {} orphan window(s) and {} orphaned container(s) on monitor: {}, workspace: {}",
reaped_orphans.0,
Expand Down Expand Up @@ -334,7 +333,6 @@ impl WindowManager {
.ok_or_else(|| anyhow!("cannot get monitor idx from current position"))?;

let new_window_behaviour = self.window_container_behaviour;
let invisible_borders = self.invisible_borders;

let workspace = self.focused_workspace_mut()?;
if !workspace
Expand All @@ -344,7 +342,7 @@ impl WindowManager {
{
let focused_container_idx = workspace.focused_container_idx();

let mut new_position = WindowsApi::window_rect(window.hwnd())?;
let new_position = WindowsApi::window_rect(window.hwnd())?;

let old_position = *workspace
.latest_layout()
Expand All @@ -369,12 +367,6 @@ impl WindowManager {
}
}

// Adjust for the invisible borders
new_position.left += invisible_borders.left;
new_position.top += invisible_borders.top;
new_position.right -= invisible_borders.right;
new_position.bottom -= invisible_borders.bottom;

let resize = Rect {
left: new_position.left - old_position.left,
top: new_position.top - old_position.top,
Expand All @@ -385,9 +377,7 @@ impl WindowManager {
// If we have moved across the monitors, use that override, otherwise determine
// if a move has taken place by ruling out a resize
let is_move = moved_across_monitors
|| resize.right == 0 && resize.bottom == 0
|| resize.right.abs() == invisible_borders.right
&& resize.bottom.abs() == invisible_borders.bottom;
|| resize.right == 0 && resize.bottom == 0;

if is_move {
tracing::info!("moving with mouse");
Expand Down Expand Up @@ -595,15 +585,10 @@ impl WindowManager {
}

if let Some(target_window) = target_window {
let window = target_window;
let mut rect = WindowsApi::window_rect(window.hwnd())?;
rect.top -= self.invisible_borders.bottom;
rect.bottom += self.invisible_borders.bottom;

let activate = BORDER_HIDDEN.load(Ordering::SeqCst);

WindowsApi::invalidate_border_rect()?;
border.set_position(target_window, &self.invisible_borders, activate)?;
border.set_position(target_window, activate)?;

if activate {
BORDER_HIDDEN.store(false, Ordering::SeqCst);
Expand All @@ -616,7 +601,7 @@ impl WindowManager {

// If we unmanaged a window, it shouldn't be immediately hidden behind managed windows
if let WindowManagerEvent::Unmanage(window) = event {
window.center(&self.focused_monitor_work_area()?, &invisible_borders)?;
window.center(&self.focused_monitor_work_area()?)?;
}

// If there are no more windows on the workspace, we shouldn't show the border window
Expand Down
23 changes: 1 addition & 22 deletions komorebi/src/static_config.rs
Expand Up @@ -298,13 +298,6 @@ pub struct StaticConfig {
impl From<&WindowManager> for StaticConfig {
#[allow(clippy::too_many_lines)]
fn from(value: &WindowManager) -> Self {
let default_invisible_borders = Rect {
left: 7,
top: 0,
right: 14,
bottom: 7,
};

let mut monitors = vec![];
for m in value.monitors() {
monitors.push(MonitorConfig::from(m));
Expand Down Expand Up @@ -374,11 +367,7 @@ impl From<&WindowManager> for StaticConfig {
};

Self {
invisible_borders: if value.invisible_borders == default_invisible_borders {
None
} else {
Option::from(value.invisible_borders)
},
invisible_borders: None,
resize_delta: Option::from(value.resize_delta),
window_container_behaviour: Option::from(value.window_container_behaviour),
cross_monitor_move_behaviour: Option::from(value.cross_monitor_move_behaviour),
Expand Down Expand Up @@ -745,12 +734,6 @@ impl StaticConfig {
incoming_events: incoming,
command_listener: listener,
is_paused: false,
invisible_borders: value.invisible_borders.unwrap_or(Rect {
left: 7,
top: 0,
right: 14,
bottom: 7,
}),
virtual_desktop_id: current_virtual_desktop(),
work_area_offset: value.global_work_area_offset,
window_container_behaviour: value
Expand Down Expand Up @@ -897,10 +880,6 @@ impl StaticConfig {
wm.hide_border()?;
}

if let Some(val) = value.invisible_borders {
wm.invisible_borders = val;
}

if let Some(val) = value.window_container_behaviour {
wm.window_container_behaviour = val;
}
Expand Down
31 changes: 2 additions & 29 deletions komorebi/src/window.rs
Expand Up @@ -32,7 +32,6 @@ use crate::styles::WindowStyle;
use crate::window_manager_event::WindowManagerEvent;
use crate::windows_api::WindowsApi;
use crate::ALT_FOCUS_HACK;
use crate::BORDER_OVERFLOW_IDENTIFIERS;
use crate::FLOAT_IDENTIFIERS;
use crate::HIDDEN_HWNDS;
use crate::HIDING_BEHAVIOUR;
Expand Down Expand Up @@ -129,7 +128,7 @@ impl Window {
HWND(self.hwnd)
}

pub fn center(&mut self, work_area: &Rect, invisible_borders: &Rect) -> Result<()> {
pub fn center(&mut self, work_area: &Rect) -> Result<()> {
let half_width = work_area.right / 2;
let half_weight = work_area.bottom / 2;

Expand All @@ -140,42 +139,16 @@ impl Window {
right: half_width,
bottom: half_weight,
},
invisible_borders,
true,
)
}

pub fn set_position(
&mut self,
layout: &Rect,
invisible_borders: &Rect,
top: bool,
) -> Result<()> {
let mut rect = *layout;

let border_overflows = BORDER_OVERFLOW_IDENTIFIERS.lock();
let regex_identifiers = REGEX_IDENTIFIERS.lock();

let title = &self.title()?;
let class = &self.class()?;
let exe_name = &self.exe()?;

let should_remove_border = !should_act(
title,
exe_name,
class,
&border_overflows,
&regex_identifiers,
);

if should_remove_border {
// Remove the invisible borders
rect.left -= invisible_borders.left;
rect.top -= invisible_borders.top;
rect.right += invisible_borders.right;
rect.bottom += invisible_borders.bottom;
}

let rect = *layout;
WindowsApi::position_window(self.hwnd(), &rect, top)
}

Expand Down

0 comments on commit 4affefa

Please sign in to comment.