Skip to content

Commit

Permalink
refactor(wm): remove mutability from window and events
Browse files Browse the repository at this point in the history
We had a mut requirement on some of the Window functions which may have
been vestigial or in preparation for more state on Window objects, but
presently unused. I removed that, as the Window struct is currently just
carrying an HWND value that's essentially always immutable - there's no
advantage to ever reusing a Window struct vs. making a new one for
another HWND.

In doing so we then no longer needed to be passing in mutable events, so
I applied a little simplification of the event receiver / dispatcher to
process_event. After that it became obvious that we could just pass the
owned event directly into process_event instead, which substantially
simplifies the ownership model and lifetime for those objects.

This is small, and shouldn't create any meaningful behavioral change.
  • Loading branch information
raggi authored and LGUG2Z committed Apr 13, 2024
1 parent 5334e19 commit f56fc36
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
29 changes: 12 additions & 17 deletions komorebi/src/process_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::sync::Arc;

use color_eyre::eyre::anyhow;
use color_eyre::Result;
use crossbeam_channel::select;
use parking_lot::Mutex;

use komorebi_core::OperationDirection;
Expand Down Expand Up @@ -42,14 +41,10 @@ pub fn listen_for_events(wm: Arc<Mutex<WindowManager>>) {
std::thread::spawn(move || {
tracing::info!("listening");
loop {
select! {
recv(receiver) -> mut maybe_event => {
if let Ok(event) = maybe_event.as_mut() {
match wm.lock().process_event(event) {
Ok(()) => {},
Err(error) => tracing::error!("{}", error)
}
}
if let Ok(event) = receiver.recv() {
match wm.lock().process_event(event) {
Ok(()) => {}
Err(error) => tracing::error!("{}", error),
}
}
}
Expand All @@ -59,21 +54,21 @@ pub fn listen_for_events(wm: Arc<Mutex<WindowManager>>) {
impl WindowManager {
#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]
#[tracing::instrument(skip(self))]
pub fn process_event(&mut self, event: &mut WindowManagerEvent) -> Result<()> {
pub fn process_event(&mut self, event: WindowManagerEvent) -> Result<()> {
if self.is_paused {
tracing::trace!("ignoring while paused");
return Ok(());
}

let should_manage = event.window().should_manage(Some(*event))?;
let should_manage = event.window().should_manage(Some(event))?;

// Hide or reposition the window based on whether the target is managed.
if BORDER_ENABLED.load(Ordering::SeqCst) {
if let WindowManagerEvent::FocusChange(_, window) = event {
let border_window = Border::from(BORDER_HWND.load(Ordering::SeqCst));

if should_manage {
border_window.set_position(*window, true)?;
border_window.set_position(window, true)?;
} else {
let mut stackbar = false;
if let Ok(class) = window.class() {
Expand Down Expand Up @@ -122,7 +117,7 @@ impl WindowManager {
| WindowManagerEvent::MoveResizeEnd(_, window) => {
self.reconcile_monitors()?;

let monitor_idx = self.monitor_idx_from_window(*window)
let monitor_idx = self.monitor_idx_from_window(window)
.ok_or_else(|| anyhow!("there is no monitor associated with this window, it may have already been destroyed"))?;

// This is a hidden window apparently associated with COM support mechanisms (based
Expand Down Expand Up @@ -329,14 +324,14 @@ impl WindowManager {
if !workspace.contains_window(window.hwnd) {
match behaviour {
WindowContainerBehaviour::Create => {
workspace.new_container_for_window(*window);
workspace.new_container_for_window(window);
self.update_focused_workspace(false)?;
}
WindowContainerBehaviour::Append => {
workspace
.focused_container_mut()
.ok_or_else(|| anyhow!("there is no focused container"))?
.add_window(*window);
.add_window(window);
self.update_focused_workspace(true)?;
}
}
Expand Down Expand Up @@ -607,7 +602,7 @@ impl WindowManager {
.iter()
.any(|w| w.hwnd == window.hwnd)
{
target_window = Option::from(*window);
target_window = Option::from(window);
WindowsApi::raise_window(border.hwnd())?;
};

Expand Down Expand Up @@ -700,7 +695,7 @@ impl WindowManager {

serde_json::to_writer_pretty(&file, &known_hwnds)?;
notify_subscribers(&serde_json::to_string(&Notification {
event: NotificationEvent::WindowManager(*event),
event: NotificationEvent::WindowManager(event),
state: self.as_ref().into(),
})?)?;

Expand Down
4 changes: 2 additions & 2 deletions komorebi/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Window {
HWND(self.hwnd)
}

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

Expand All @@ -140,7 +140,7 @@ impl Window {
)
}

pub fn set_position(&mut self, layout: &Rect, top: bool) -> Result<()> {
pub fn set_position(&self, layout: &Rect, top: bool) -> Result<()> {
let rect = *layout;
WindowsApi::position_window(self.hwnd(), &rect, top)
}
Expand Down

0 comments on commit f56fc36

Please sign in to comment.