fix(wm): delegate all cmd+tab focus changes to ws reconciliator

Mainly a cleanup PR - there were some subtle bugs introduced trying to
exclude the ManualNotification for cmd+tab focus changes, but since this
is all being delegated to the workspace reconciliator now, we can remove
those confusing matches! guards and that variant all together.
This commit is contained in:
LGUG2Z committed 2025-10-10 09:56:09 -07:00
1 parent 141eedfd60
commit aa718bf6e9
3 files changed
+135 -146

No files matched your search

+11 -3
View File
@@ -107,7 +107,7 @@ pub fn listen_for_commands(wm: Arc<Mutex<WindowManager>>) {
}
impl WindowManager {
#[tracing::instrument(skip(self, reply))]
#[tracing::instrument(skip(self, reply, message))]
pub fn process_command(
&mut self,
message: SocketMessage,
@@ -121,7 +121,11 @@ impl WindowManager {
return Ok(());
}
tracing::info!("processing command: {message}");
if matches!(message, SocketMessage::Theme(_)) {
tracing::trace!("processing command: {message}");
} else {
tracing::info!("processing command: {message}");
}
#[allow(clippy::useless_asref)]
// We don't have From implemented for &mut WindowManager
@@ -1730,7 +1734,11 @@ impl WindowManager {
border_manager::send_notification(None, None);
tracing::info!("processed");
if matches!(message, SocketMessage::Theme(_)) {
tracing::trace!("processed command: {message}");
} else {
tracing::info!("processed command: {message}");
}
Ok(())
}
+119 -138
View File
@@ -10,7 +10,6 @@ use crate::accessibility::AccessibilityApi;
use crate::accessibility::error::AccessibilityApiError;
use crate::accessibility::error::AccessibilityError;
use crate::accessibility::notification_constants::AccessibilityNotification;
use crate::app_kit_notification_constants::AppKitWorkspaceNotification;
use crate::border_manager;
use crate::core::Sizing;
use crate::core::WindowContainerBehaviour;
@@ -178,16 +177,6 @@ impl WindowManager {
self.enforce_workspace_rules()?;
match event {
WindowManagerEvent::FocusChange(
SystemNotification::Manual(ManualNotification::FocusOnCmdTab),
_,
Some(window_id),
) => {
let focused_workspace = self.focused_workspace_mut()?;
let container_idx = focused_workspace.container_idx_for_window(window_id);
focused_workspace.focus_container(container_idx.unwrap_or_default());
self.update_focused_workspace(self.mouse_follows_focus, true)?;
}
WindowManagerEvent::FocusChange(notification, process_id, _) => {
let application = self.application(process_id)?;
let application_name = application.name().unwrap_or_default().clone();
@@ -196,35 +185,27 @@ impl WindowManager {
let mut needs_reconciliation = false;
if let Some(window_id) = application.main_window_id()
&& matches!(
notification,
SystemNotification::Accessibility(
AccessibilityNotification::AXMainWindowChanged
) | SystemNotification::AppKitWorkspace(
AppKitWorkspaceNotification::NSWorkspaceDidActivateApplicationNotification
)
) && let Some(element) = application.main_window()
{
let workspace = self.focused_workspace_mut()?;
&& let Some(element) = application.main_window()
{
let workspace = self.focused_workspace_mut()?;
let tabbed_applications = TABBED_APPLICATIONS.lock();
if tabbed_applications.contains(&application_name) {
let mut first_tab_destroyed = false;
let tabbed_applications = TABBED_APPLICATIONS.lock();
if tabbed_applications.contains(&application_name) {
let mut first_tab_destroyed = false;
for window in workspace.visible_windows() {
if let Some(window) = window
&& window.application.name().unwrap_or_default()
== application_name
{
let tab_rect = MacosApi::window_rect(&element)?;
let main_rect = match MacosApi::window_rect(&window.element) {
Ok(rect) => rect,
Err(AccessibilityError::Api(
AccessibilityApiError::InvalidUIElement,
)) => {
// this means we have closed the 1st tab, so we need this window object to be reaped
// and for the new 1st tab to be the key element of the window struct
if let Some(event) =
for window in workspace.visible_windows() {
if let Some(window) = window
&& window.application.name().unwrap_or_default() == application_name
{
let tab_rect = MacosApi::window_rect(&element)?;
let main_rect = match MacosApi::window_rect(&window.element) {
Ok(rect) => rect,
Err(AccessibilityError::Api(
AccessibilityApiError::InvalidUIElement,
)) => {
// this means we have closed the 1st tab, so we need this window object to be reaped
// and for the new 1st tab to be the key element of the window struct
if let Some(event) =
WindowManagerEvent::from_system_notification(
SystemNotification::Manual(
ManualNotification::ShowOnFocusChangeFirstTabDestroyed,
@@ -238,102 +219,103 @@ impl WindowManager {
);
}
first_tab_destroyed = true;
first_tab_destroyed = true;
tracing::debug!(
"first tab of a native tabbed app was destroyed; reaping window and sending a new show event"
);
tracing::debug!(
"first tab of a native tabbed app was destroyed; reaping window and sending a new show event"
);
tab_rect
}
Err(error) => return Err(error.into()),
};
if tab_rect == main_rect {
tracing::debug!("ignoring focus change for tabbed window");
tabbed_window = true;
tab_rect
}
}
}
Err(error) => return Err(error.into()),
};
if first_tab_destroyed {
self.reap_invalid_windows_for_application(process_id)?;
if tab_rect == main_rect {
tracing::debug!("ignoring focus change for tabbed window");
tabbed_window = true;
}
}
}
drop(tabbed_applications);
if first_tab_destroyed {
self.reap_invalid_windows_for_application(process_id)?;
}
}
if !tabbed_window {
let is_known = self.known_window_ids.get(&window_id).cloned();
let mut is_on_current_workspace = false;
drop(tabbed_applications);
// TODO: figure out if this applies on macOS too
// don't want to trigger the full workspace updates when there are no managed
// containers - this makes floating windows on empty workspaces go into very
// annoying focus change loops which prevents users from interacting with them
if !matches!(
self.focused_workspace()?.layout,
Layout::Default(DefaultLayout::Scrolling)
) && !self.focused_workspace()?.containers().is_empty() {
self.update_focused_workspace(self.mouse_follows_focus, false)?;
}
if !tabbed_window {
let is_known = self.known_window_ids.get(&window_id).cloned();
let mut is_on_current_workspace = false;
let workspace = self.focused_workspace_mut()?;
if workspace.contains_window(window_id) {
is_on_current_workspace = true;
}
// TODO: figure out if this applies on macOS too
// don't want to trigger the full workspace updates when there are no managed
// containers - this makes floating windows on empty workspaces go into very
// annoying focus change loops which prevents users from interacting with them
if !matches!(
self.focused_workspace()?.layout,
Layout::Default(DefaultLayout::Scrolling)
) && !self.focused_workspace()?.containers().is_empty()
{
self.update_focused_workspace(self.mouse_follows_focus, false)?;
}
let floating_window_idx = workspace
.floating_windows()
.iter()
.position(|w| w.id == window_id);
let workspace = self.focused_workspace_mut()?;
if workspace.contains_window(window_id) {
is_on_current_workspace = true;
}
match floating_window_idx {
None => {
// if let Some(w) = workspace.maximized_window() {
// if w.hwnd == window_id {
// return Ok(());
// }
// }
let floating_window_idx = workspace
.floating_windows()
.iter()
.position(|w| w.id == window_id);
if let Some(monocle) = &workspace.monocle_container {
if let Some(window) = monocle.focused_window() {
window.focus(false)?;
}
} else if !is_on_current_workspace && is_known.is_none() {
// thanks, I hate it - need to do this so that we don't mess up
// the workspace rules, but also don't miss events from dumb
// apps like notes, mail etc.
let mut has_matching_workspace_rule = false;
let workspace_rules = WORKSPACE_MATCHING_RULES.lock();
for rule in &*workspace_rules {
match &rule.matching_rule {
MatchingRule::Simple(r) => {
if r.id.trim_end_matches(".exe") == application_name
match floating_window_idx {
None => {
// if let Some(w) = workspace.maximized_window() {
// if w.hwnd == window_id {
// return Ok(());
// }
// }
if let Some(monocle) = &workspace.monocle_container {
if let Some(window) = monocle.focused_window() {
window.focus(false)?;
}
} else if !is_on_current_workspace && is_known.is_none() {
// thanks, I hate it - need to do this so that we don't mess up
// the workspace rules, but also don't miss events from dumb
// apps like notes, mail etc.
let mut has_matching_workspace_rule = false;
let workspace_rules = WORKSPACE_MATCHING_RULES.lock();
for rule in &*workspace_rules {
match &rule.matching_rule {
MatchingRule::Simple(r) => {
if r.id.trim_end_matches(".exe") == application_name
{
has_matching_workspace_rule = true;
}
}
// TODO: this is pretty coarse
MatchingRule::Composite(rules) => {
for r in rules {
if r.id.trim_end_matches(".exe")
== application_name
{
has_matching_workspace_rule = true;
}
}
// TODO: this is pretty coarse
MatchingRule::Composite(rules) => {
for r in rules {
if r.id.trim_end_matches(".exe")
== application_name
{
has_matching_workspace_rule = true;
}
}
}
}
}
}
if !has_matching_workspace_rule
&& workspace.focus_container_by_window(window_id).is_err()
{
// if this fails, the app was probably open but windowless when komorebi
// launched, so the window hasn't been registered - we should treat it
// as a "Show" event
if let Some(event) =
if !has_matching_workspace_rule
&& workspace.focus_container_by_window(window_id).is_err()
{
// if this fails, the app was probably open but windowless when komorebi
// launched, so the window hasn't been registered - we should treat it
// as a "Show" event
if let Some(event) =
WindowManagerEvent::from_system_notification(
SystemNotification::Manual(
ManualNotification::ShowOnFocusChangeWindowlessAppRestored,
@@ -347,37 +329,36 @@ impl WindowManager {
);
should_switch_workspace_layer_to_tiling = false;
}
}
} else if is_on_current_workspace {
workspace.focus_container_by_window(window_id)?;
}
if should_switch_workspace_layer_to_tiling {
workspace.layer = WorkspaceLayer::Tiling;
}
if matches!(self.focused_workspace()?.layout, Layout::Default(DefaultLayout::Scrolling))
&& !self.focused_workspace()?.containers().is_empty() {
self.update_focused_workspace(self.mouse_follows_focus, false)?;
}
} else if is_on_current_workspace {
workspace.focus_container_by_window(window_id)?;
}
Some(idx) => {
if let Some(_window) = workspace.floating_windows().get(idx) {
workspace.layer = WorkspaceLayer::Floating;
}
if should_switch_workspace_layer_to_tiling {
workspace.layer = WorkspaceLayer::Tiling;
}
if matches!(
self.focused_workspace()?.layout,
Layout::Default(DefaultLayout::Scrolling)
) && !self.focused_workspace()?.containers().is_empty()
{
self.update_focused_workspace(self.mouse_follows_focus, false)?;
}
}
if !is_on_current_workspace && let Some((m_idx, w_idx)) = is_known
&& matches!(
notification,
SystemNotification::AppKitWorkspace(AppKitWorkspaceNotification::NSWorkspaceDidActivateApplicationNotification)
) {
workspace_reconciliator::send_notification(m_idx, w_idx, event);
needs_reconciliation = true;
Some(idx) => {
if let Some(_window) = workspace.floating_windows().get(idx) {
workspace.layer = WorkspaceLayer::Floating;
}
}
}
if !is_on_current_workspace && let Some((m_idx, w_idx)) = is_known {
workspace_reconciliator::send_notification(m_idx, w_idx, event);
needs_reconciliation = true;
}
}
}
if matches!(
notification,
+5 -5
View File
@@ -24,7 +24,6 @@ pub enum ManualNotification {
ShowOnFocusChangeWindowlessAppRestored,
Manage,
Unmanage,
FocusOnCmdTab,
}
#[derive(Debug, Copy, Clone, Serialize, Deserialize, Display)]
@@ -60,10 +59,11 @@ impl WindowManagerEvent {
)
| SystemNotification::AppKitWorkspace(
AppKitWorkspaceNotification::NSWorkspaceDidActivateApplicationNotification,
)
| SystemNotification::Manual(ManualNotification::FocusOnCmdTab) => Some(
WindowManagerEvent::FocusChange(notification, process_id, window_id),
),
) => Some(WindowManagerEvent::FocusChange(
notification,
process_id,
window_id,
)),
SystemNotification::Accessibility(AccessibilityNotification::AXWindowCreated)
| SystemNotification::Accessibility(AccessibilityNotification::AXApplicationShown)
| SystemNotification::AppKitWorkspace(