Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
LGUG2Z committed Dec 4, 2024
1 parent 3781c8e commit c6f1e7c
Show file tree
Hide file tree
Showing 10 changed files with 398 additions and 213 deletions.
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ install-target target:
install:
just install-targets komorebic komorebic-no-console komorebi komorebi-bar komorebi-gui

run target:
run target $RUST_BACKTRACE="1":
cargo +stable run --bin {{ target }} --locked

warn target $RUST_LOG="warn":
Expand Down
294 changes: 234 additions & 60 deletions komorebi/src/border_manager/border.rs

Large diffs are not rendered by default.

201 changes: 67 additions & 134 deletions komorebi/src/border_manager/mod.rs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions komorebi/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ pub enum BorderImplementation {
ValueEnum,
JsonSchema,
PartialEq,
Eq,
Hash,
)]
pub enum WindowKind {
Single,
Expand Down
6 changes: 6 additions & 0 deletions komorebi/src/core/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ impl From<Rect> for RECT {
}
}

impl Rect {
pub fn is_same_size_as(&self, rhs: &Self) -> bool {
self.right == rhs.right && self.bottom == rhs.bottom
}
}

impl Rect {
/// decrease the size of self by the padding amount.
pub fn add_padding<T>(&mut self, padding: T)
Expand Down
10 changes: 10 additions & 0 deletions komorebi/src/process_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,16 @@ impl WindowManager {
}
SocketMessage::Border(enable) => {
border_manager::BORDER_ENABLED.store(enable, Ordering::SeqCst);
if !enable {
match IMPLEMENTATION.load() {
BorderImplementation::Komorebi => {
border_manager::destroy_all_borders()?;
}
BorderImplementation::Windows => {
self.remove_all_accents()?;
}
}
}
}
SocketMessage::BorderImplementation(implementation) => {
if !*WINDOWS_11 && matches!(implementation, BorderImplementation::Windows) {
Expand Down
6 changes: 0 additions & 6 deletions komorebi/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::animation::ANIMATION_ENABLED_PER_ANIMATION;
use crate::animation::ANIMATION_MANAGER;
use crate::animation::ANIMATION_STYLE_GLOBAL;
use crate::animation::ANIMATION_STYLE_PER_ANIMATION;
use crate::border_manager;
use crate::com::SetCloak;
use crate::focus_manager;
use crate::stackbar_manager;
Expand Down Expand Up @@ -193,9 +192,6 @@ impl RenderDispatcher for MovementRenderDispatcher {
}

fn pre_render(&self) -> Result<()> {
border_manager::BORDER_TEMPORARILY_DISABLED.store(true, Ordering::SeqCst);
border_manager::send_notification(Some(self.hwnd));

stackbar_manager::STACKBAR_TEMPORARILY_DISABLED.store(true, Ordering::SeqCst);
stackbar_manager::send_notification();

Expand Down Expand Up @@ -224,10 +220,8 @@ impl RenderDispatcher for MovementRenderDispatcher {
focus_manager::send_notification(self.hwnd)
}

border_manager::BORDER_TEMPORARILY_DISABLED.store(false, Ordering::SeqCst);
stackbar_manager::STACKBAR_TEMPORARILY_DISABLED.store(false, Ordering::SeqCst);

border_manager::send_notification(Some(self.hwnd));
stackbar_manager::send_notification();
transparency_manager::send_notification();
}
Expand Down
27 changes: 23 additions & 4 deletions komorebi/src/windows_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ macro_rules! as_ptr {
};
}

use crate::border_manager::Border;
pub(crate) use as_ptr;

pub enum WindowsResult<T, E> {
Expand Down Expand Up @@ -452,8 +453,22 @@ impl WindowsApi {
)
}

pub fn set_border_pos(hwnd: isize, layout: &Rect, position: isize) -> Result<()> {
let flags = { SetWindowPosition::SHOW_WINDOW | SetWindowPosition::NO_ACTIVATE };
pub fn set_border_pos(
hwnd: isize,
layout: &Rect,
position: isize,
location_change: bool,
) -> Result<()> {
let mut flags = {
SetWindowPosition::NO_SEND_CHANGING
| SetWindowPosition::NO_ACTIVATE
| SetWindowPosition::NO_REDRAW
};

if !location_change {
flags.insert(SetWindowPosition::SHOW_WINDOW);
}

Self::set_window_pos(
HWND(as_ptr!(hwnd)),
layout,
Expand Down Expand Up @@ -1090,7 +1105,11 @@ impl WindowsApi {
.process()
}

pub fn create_border_window(name: PCWSTR, instance: isize) -> Result<isize> {
pub fn create_border_window(
name: PCWSTR,
instance: isize,
border: *const Border,
) -> Result<isize> {
unsafe {
let hwnd = CreateWindowExW(
WS_EX_TOOLWINDOW | WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_NOACTIVATE,
Expand All @@ -1104,7 +1123,7 @@ impl WindowsApi {
None,
None,
HINSTANCE(as_ptr!(instance)),
None,
Some(border as _),
)?;

SetLayeredWindowAttributes(hwnd, COLORREF(0), 0, LWA_COLORKEY)?;
Expand Down
59 changes: 52 additions & 7 deletions komorebi/src/windows_callbacks.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
use std::collections::VecDeque;

use windows::Win32::Foundation::BOOL;
use windows::Win32::Foundation::HWND;
use windows::Win32::Foundation::LPARAM;
use windows::Win32::UI::Accessibility::HWINEVENTHOOK;

use crate::border_manager;
use crate::container::Container;
use crate::window::RuleDebug;
use crate::window::Window;
use crate::window_manager_event::WindowManagerEvent;
use crate::windows_api::WindowsApi;
use crate::winevent::WinEvent;
use crate::winevent_listener;
use windows::Win32::Foundation::BOOL;
use windows::Win32::Foundation::HWND;
use windows::Win32::Foundation::LPARAM;
use windows::Win32::Foundation::WPARAM;
use windows::Win32::UI::Accessibility::HWINEVENTHOOK;
use windows::Win32::UI::WindowsAndMessaging::GetWindowLongW;
use windows::Win32::UI::WindowsAndMessaging::PostMessageW;
use windows::Win32::UI::WindowsAndMessaging::SendNotifyMessageW;
use windows::Win32::UI::WindowsAndMessaging::EVENT_OBJECT_LOCATIONCHANGE;
use windows::Win32::UI::WindowsAndMessaging::GWL_EXSTYLE;
use windows::Win32::UI::WindowsAndMessaging::GWL_STYLE;
use windows::Win32::UI::WindowsAndMessaging::OBJID_WINDOW;
use windows::Win32::UI::WindowsAndMessaging::WS_CHILD;
use windows::Win32::UI::WindowsAndMessaging::WS_EX_NOACTIVATE;
use windows::Win32::UI::WindowsAndMessaging::WS_EX_TOOLWINDOW;

pub extern "system" fn enum_window(hwnd: HWND, lparam: LPARAM) -> BOOL {
let containers = unsafe { &mut *(lparam.0 as *mut VecDeque<Container>) };
Expand Down Expand Up @@ -60,6 +71,15 @@ pub extern "system" fn alt_tab_windows(hwnd: HWND, lparam: LPARAM) -> BOOL {
true.into()
}

pub fn has_filtered_style(hwnd: HWND) -> bool {
let style = unsafe { GetWindowLongW(hwnd, GWL_STYLE) as u32 };
let ex_style = unsafe { GetWindowLongW(hwnd, GWL_EXSTYLE) as u32 };

style & WS_CHILD.0 != 0
|| ex_style & WS_EX_TOOLWINDOW.0 != 0
|| ex_style & WS_EX_NOACTIVATE.0 != 0
}

pub extern "system" fn win_event_hook(
_h_win_event_hook: HWINEVENTHOOK,
event: u32,
Expand All @@ -69,8 +89,7 @@ pub extern "system" fn win_event_hook(
_id_event_thread: u32,
_dwms_event_time: u32,
) {
// OBJID_WINDOW
if id_object != 0 {
if id_object != OBJID_WINDOW.0 {
return;
}

Expand All @@ -81,6 +100,32 @@ pub extern "system" fn win_event_hook(
Err(_) => return,
};

// border
if matches!(winevent, WinEvent::ObjectLocationChange) && !has_filtered_style(hwnd) {
let border_window = border_manager::window_border(hwnd.0 as isize);

if let Some(border) = border_window {
unsafe {
let _ = SendNotifyMessageW(
border.hwnd(),
EVENT_OBJECT_LOCATIONCHANGE,
WPARAM(0),
LPARAM(hwnd.0 as isize),
);
}
}
}

if matches!(winevent, WinEvent::ObjectReorder | WinEvent::ObjectFocus) {
let border_window = border_manager::window_border(hwnd.0 as isize);

if let Some(border) = border_window {
unsafe {
let _ = PostMessageW(border.hwnd(), event, WPARAM(0), LPARAM(hwnd.0 as isize));
}
}
}

let event_type = match WindowManagerEvent::from_win_event(winevent, window) {
None => {
tracing::trace!(
Expand Down
4 changes: 3 additions & 1 deletion komorebi/src/winevent_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use windows::Win32::UI::WindowsAndMessaging::TranslateMessage;
use windows::Win32::UI::WindowsAndMessaging::EVENT_MAX;
use windows::Win32::UI::WindowsAndMessaging::EVENT_MIN;
use windows::Win32::UI::WindowsAndMessaging::MSG;
use windows::Win32::UI::WindowsAndMessaging::WINEVENT_OUTOFCONTEXT;
use windows::Win32::UI::WindowsAndMessaging::WINEVENT_SKIPOWNPROCESS;

use crate::window_manager_event::WindowManagerEvent;
use crate::windows_callbacks;
Expand All @@ -31,7 +33,7 @@ pub fn start() {
Some(windows_callbacks::win_event_hook),
0,
0,
0,
WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS,
)
};

Expand Down

0 comments on commit c6f1e7c

Please sign in to comment.