Skip to content

Commit

Permalink
fix(workspaces): let set_foreground_window fail
Browse files Browse the repository at this point in the history
When hiding/restoring windows as part of workspace switching, calls to
SetForegroundWindow fail, and if they fail, other hidden windows get
lost forever in hidden mode when the error is returned up the call chain
using the ? operator.

This commit separates out the focus() calls from the loops restoring
windows when switching workspaces, and also ensures that calls to
SetForegroundWindow will log an error, but ultimately continue to the
end of the focus() function call.
  • Loading branch information
LGUG2Z committed Jul 31, 2021
1 parent b867db1 commit 7356892
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 9 additions & 1 deletion komorebi/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,15 @@ impl Window {
WindowsApi::attach_thread_input(current_thread_id, window_thread_id, true)?;

// Raise Window to foreground
WindowsApi::set_foreground_window(self.hwnd())?;
match WindowsApi::set_foreground_window(self.hwnd()) {
Ok(_) => {}
Err(error) => {
tracing::error!(
"could not set as foreground window, but continuing execution of focus(): {}",
error
);
}
};

// Center cursor in Window
WindowsApi::center_cursor_in_rect(&WindowsApi::window_rect(self.hwnd())?)?;
Expand Down
8 changes: 7 additions & 1 deletion komorebi/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,22 @@ impl Workspace {

pub fn restore(&mut self) -> Result<()> {
let idx = self.focused_container_idx();
let mut to_focus = None;
for (i, container) in self.containers_mut().iter_mut().enumerate() {
if let Some(window) = container.visible_window_mut() {
window.restore();

if idx == i {
window.focus()?;
to_focus = Option::from(window);
}
}
}

// Do this here to make sure that an error doesn't stop the restoration of other windows
if let Some(window) = to_focus {
window.focus()?;
}

Ok(())
}

Expand Down

0 comments on commit 7356892

Please sign in to comment.