Skip to content

Commit

Permalink
refactor(wm): use from trait to construct windows
Browse files Browse the repository at this point in the history
  • Loading branch information
LGUG2Z committed Jun 4, 2024
1 parent 280aebf commit 133311b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
12 changes: 3 additions & 9 deletions komorebi/src/process_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,10 @@ impl WindowManager {
WindowsApi::left_click();
}
SocketMessage::Close => {
Window {
hwnd: WindowsApi::foreground_window()?,
}
.close()?;
Window::from(WindowsApi::foreground_window()?).close()?;
}
SocketMessage::Minimize => {
Window {
hwnd: WindowsApi::foreground_window()?,
}
.minimize();
Window::from(WindowsApi::foreground_window()?).minimize();
}
SocketMessage::ToggleFloat => self.toggle_float()?,
SocketMessage::ToggleMonocle => self.toggle_monocle()?,
Expand Down Expand Up @@ -1342,7 +1336,7 @@ impl WindowManager {
self.update_focused_workspace(false, false)?;
}
SocketMessage::DebugWindow(hwnd) => {
let window = Window { hwnd };
let window = Window::from(hwnd);
let mut rule_debug = RuleDebug::default();
let _ = window.should_manage(None, &mut rule_debug);
let schema = serde_json::to_string_pretty(&rule_debug)?;
Expand Down
6 changes: 6 additions & 0 deletions komorebi/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ impl From<isize> for Window {
}
}

impl From<HWND> for Window {
fn from(value: HWND) -> Self {
Self { hwnd: value.0 }
}
}

#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone, Serialize, JsonSchema)]
pub struct WindowDetails {
Expand Down
18 changes: 7 additions & 11 deletions komorebi/src/window_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ impl WindowManager {

// Hide the window we are about to remove if it is on the currently focused workspace
if op.is_origin(focused_monitor_idx, focused_workspace_idx) {
Window { hwnd: op.hwnd }.hide();
Window::from(op.hwnd).hide();
should_update_focused_workspace = true;
}

Expand Down Expand Up @@ -550,7 +550,7 @@ impl WindowManager {
.get_mut(op.target_workspace_idx)
.ok_or_else(|| anyhow!("there is no workspace with that index"))?;

target_workspace.new_container_for_window(Window { hwnd: op.hwnd });
target_workspace.new_container_for_window(Window::from(op.hwnd));
}

// Only re-tile the focused workspace if we need to
Expand Down Expand Up @@ -598,14 +598,14 @@ impl WindowManager {
#[tracing::instrument(skip(self))]
pub fn manage_focused_window(&mut self) -> Result<()> {
let hwnd = WindowsApi::foreground_window()?;
let event = WindowManagerEvent::Manage(Window { hwnd });
let event = WindowManagerEvent::Manage(Window::from(hwnd));
Ok(winevent_listener::event_tx().send(event)?)
}

#[tracing::instrument(skip(self))]
pub fn unmanage_focused_window(&mut self) -> Result<()> {
let hwnd = WindowsApi::foreground_window()?;
let event = WindowManagerEvent::Unmanage(Window { hwnd });
let event = WindowManagerEvent::Unmanage(Window::from(hwnd));
Ok(winevent_listener::event_tx().send(event)?)
}

Expand Down Expand Up @@ -633,15 +633,13 @@ impl WindowManager {
return Ok(());
}

let event = WindowManagerEvent::Raise(Window { hwnd });
let event = WindowManagerEvent::Raise(Window::from(hwnd));
self.has_pending_raise_op = true;
winevent_listener::event_tx().send(event)?;
} else {
tracing::debug!(
"not raising unknown window: {}",
Window {
hwnd: WindowsApi::window_at_cursor_pos()?
}
Window::from(WindowsApi::window_at_cursor_pos()?)
);
}

Expand Down Expand Up @@ -765,9 +763,7 @@ impl WindowManager {
window.focus(self.mouse_follows_focus)?;
}
} else {
let desktop_window = Window {
hwnd: WindowsApi::desktop_window()?,
};
let desktop_window = Window::from(WindowsApi::desktop_window()?);

let rect = self.focused_monitor_size()?;
WindowsApi::center_cursor_in_rect(&rect)?;
Expand Down
6 changes: 3 additions & 3 deletions komorebi/src/windows_callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub extern "system" fn enum_window(hwnd: HWND, lparam: LPARAM) -> BOOL {
let is_maximized = WindowsApi::is_zoomed(hwnd);

if is_visible && is_window && !is_minimized {
let window = Window { hwnd: hwnd.0 };
let window = Window::from(hwnd);

if let Ok(should_manage) = window.should_manage(None, &mut RuleDebug::default()) {
if should_manage {
Expand All @@ -48,7 +48,7 @@ pub extern "system" fn alt_tab_windows(hwnd: HWND, lparam: LPARAM) -> BOOL {
let is_minimized = WindowsApi::is_iconic(hwnd);

if is_visible && is_window && !is_minimized {
let window = Window { hwnd: hwnd.0 };
let window = Window::from(hwnd);

if let Ok(should_manage) = window.should_manage(None, &mut RuleDebug::default()) {
if should_manage {
Expand All @@ -74,7 +74,7 @@ pub extern "system" fn win_event_hook(
return;
}

let window = Window { hwnd: hwnd.0 };
let window = Window::from(hwnd);

let winevent = match WinEvent::try_from(event) {
Ok(event) => event,
Expand Down

0 comments on commit 133311b

Please sign in to comment.