fix(wm): be more discerning with input event listener

This commit fixes a couple of subtle bugs. The InputEventListener will
now only emit a custom Show event if the CGEvent was a LeftMouseUp.

AXDialog subroles have been added to PERMAIGNORE_CLASSES, and an
additional check against PERMAIGNORE_CLASSES has been added in the Show
handler because apparently windows can change their roles after they are
created.

I had also missed a line to make sure we focus windows in a base case in
the FocusChange handler, which has now been added.
This commit is contained in:
LGUG2Z committed 2025-09-24 21:23:26 -07:00
1 parent e1be883dab
commit 3a24b51111
3 files changed
+122 -95

No files matched your search

+11 -2
View File
@@ -25,12 +25,20 @@ pub struct InputEventListener {
extern "C-unwind" fn callback(
_: CGEventTapProxy,
_event_type: CGEventType,
event_type: CGEventType,
mut event_ref: NonNull<CGEvent>,
_listener: *mut c_void,
) -> *mut CGEvent {
// this should cover both clicking the close button and closing out something
// by using cmd+w or cmd+q since some apps don't send events on close
reaper::send_notification(ReaperNotification::MouseUpKeyUp);
if let Some(element) = MacosApi::foreground_window() {
// this one is only really for when people "drag" a tab out of one window
// to create another window - we wanna make sure it gets handled because
// events don't get sent sometimes
if event_type == CGEventType::LeftMouseUp
&& let Some(element) = MacosApi::foreground_window()
{
let mut pid = 0;
unsafe {
element.pid(NonNull::from_mut(&mut pid));
@@ -46,6 +54,7 @@ extern "C-unwind" fn callback(
window_manager_event_listener::send_notification(event);
}
}
unsafe { event_ref.as_mut() }
}
+1 -1
View File
@@ -117,7 +117,7 @@ lazy_static! {
matching_strategy: Option::from(MatchingStrategy::Equals),
})]));
static ref PERMAIGNORE_CLASSES: Arc<Mutex<Vec<String>>> =
Arc::new(Mutex::new(vec!["Chrome_RenderWidgetHostHWND".to_string(),]));
Arc::new(Mutex::new(vec!["AXDialog".to_string(),]));
static ref TABBED_APPLICATIONS: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![
"Finder".to_string(),
"Ghostty".to_string()
+110 -92
View File
@@ -1,6 +1,7 @@
use crate::FLOATING_APPLICATIONS;
use crate::Notification;
use crate::NotificationEvent;
use crate::PERMAIGNORE_CLASSES;
use crate::REGEX_IDENTIFIERS;
use crate::TABBED_APPLICATIONS;
use crate::WORKSPACE_MATCHING_RULES;
@@ -8,6 +9,7 @@ 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::core::WindowContainerBehaviour;
use crate::core::config_generation::MatchingRule;
use crate::core::default_layout::DefaultLayout;
@@ -94,6 +96,12 @@ impl WindowManager {
event.process_id(),
event.notification(),
);
} else {
tracing::trace!(
"processing event: {event} for process {} with notification {}",
event.process_id(),
event.notification(),
);
}
#[allow(clippy::useless_asref)]
@@ -108,11 +116,13 @@ impl WindowManager {
let application_name = application.name().unwrap_or_default().clone();
let mut tabbed_window = false;
if let Some(window_id) = application.main_window_id() {
if matches!(
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()
{
@@ -173,120 +183,117 @@ impl WindowManager {
}
drop(tabbed_applications);
}
if !tabbed_window {
let is_known = self.known_window_ids.get(&window_id).cloned();
let mut is_on_current_workspace = false;
if !tabbed_window {
let is_known = self.known_window_ids.get(&window_id).cloned();
let mut is_on_current_workspace = false;
// 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)?;
}
// 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 workspace = self.focused_workspace_mut()?;
if workspace.contains_window(window_id) {
is_on_current_workspace = true;
}
let workspace = self.focused_workspace_mut()?;
if workspace.contains_window(window_id) {
is_on_current_workspace = true;
}
let floating_window_idx = workspace
.floating_windows()
.iter()
.position(|w| w.id == window_id);
let floating_window_idx = workspace
.floating_windows()
.iter()
.position(|w| w.id == window_id);
match floating_window_idx {
None => {
// if let Some(w) = workspace.maximized_window() {
// if w.hwnd == window_id {
// return Ok(());
// }
// }
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
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;
}
}
}
}
}
}
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,
),
event.process_id(),
Some(window_id),
)
if !has_matching_workspace_rule
&& workspace.focus_container_by_window(window_id).is_err()
{
window_manager_event_listener::send_notification(
event,
);
// 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,
),
event.process_id(),
Some(window_id),
)
{
window_manager_event_listener::send_notification(
event,
);
}
}
} else {
workspace.focus_container_by_window(window_id)?;
}
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)?;
}
}
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)?;
Some(idx) => {
if let Some(_window) = workspace.floating_windows().get(idx) {
workspace.layer = WorkspaceLayer::Floating;
}
}
}
Some(idx) => {
if let Some(_window) = workspace.floating_windows().get(idx) {
workspace.layer = WorkspaceLayer::Floating;
}
// maybe we don't need a separate reconciliator module?? this works pretty well!
if !is_on_current_workspace && let Some((m_idx, w_idx)) = is_known {
self.focus_monitor(m_idx)?;
self.focus_workspace(w_idx)?;
}
}
// maybe we don't need a separate reconciliator module?? this works pretty well!
if !is_on_current_workspace && let Some((m_idx, w_idx)) = is_known {
self.focus_monitor(m_idx)?;
self.focus_workspace(w_idx)?;
}
}
}
if matches!(
notification,
@@ -371,6 +378,17 @@ impl WindowManager {
}
}
// this happens sometimes because of the mouse event from input listener which emits a show
if let Some(element) = &window_element {
let subrole = AdhocWindow::subrole(element).unwrap_or_default();
if PERMAIGNORE_CLASSES.lock().contains(&subrole) {
tracing::debug!(
"ignoring AXDialog window that got past the initial filter"
);
create = false
}
}
if create && !tabbed_window {
let application = self.application(process_id)?;
if let Some(element) = window_element