fix(wm): handle native tabs (mostly)

This commit adds a global list of user-define-able applications which
use native tabs (I'll expose a config option for later). This is used to
improve handling of tabs.

I have tested with Ghostty and Finder. It's possible to add tabs with
cmd+t, close them with cmd+w, and also to drag them into their own
windows.

It's also possible to cmd+n to create a new window instance in Finder,
but when doing this with Ghostty, it doesn't emit an event (I think this
is a bug in Ghostty, since it works fine with Finder).
This commit is contained in:
LGUG2Z committed 2025-09-22 18:59:40 -07:00
1 parent b020b5bb69
commit f3b256bcbf
5 files changed
+166 -82

No files matched your search

+4
View File
@@ -116,6 +116,10 @@ lazy_static! {
})]));
static ref PERMAIGNORE_CLASSES: Arc<Mutex<Vec<String>>> =
Arc::new(Mutex::new(vec!["Chrome_RenderWidgetHostHWND".to_string(),]));
static ref TABBED_APPLICATIONS: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![
"Finder".to_string(),
"Ghostty".to_string()
]));
}
pub static DEFAULT_WORKSPACE_PADDING: AtomicI32 = AtomicI32::new(5);
+1
View File
@@ -103,6 +103,7 @@ impl Monitor {
}
}
#[tracing::instrument(skip_all)]
pub fn update_focused_workspace(&mut self, offset: Option<Rect>) -> eyre::Result<()> {
let offset = if self.work_area_offset.is_some() {
self.work_area_offset
+134 -80
View File
@@ -1,3 +1,4 @@
use crate::TABBED_APPLICATIONS;
use crate::WORKSPACE_MATCHING_RULES;
use crate::accessibility::AccessibilityApi;
use crate::accessibility::notification_constants::AccessibilityNotification;
@@ -5,6 +6,7 @@ use crate::ax_event_listener::event_tx;
use crate::core::config_generation::MatchingRule;
use crate::core::default_layout::DefaultLayout;
use crate::core::layout::Layout;
use crate::macos_api::MacosApi;
use crate::window::Window;
use crate::window_manager::WindowManager;
use crate::window_manager_event::SystemNotification;
@@ -80,106 +82,138 @@ impl WindowManager {
WindowManagerEvent::FocusChange(notification, process_id, _) => {
let application = self.application(process_id)?;
let application_name = application.name().unwrap_or_default().clone();
let mut tabbed_window = false;
if let Some(window_id) = application.main_window_id() {
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()
if matches!(
notification,
SystemNotification::Accessibility(
AccessibilityNotification::AXMainWindowChanged
)
) && let Some(element) = application.main_window()
{
self.update_focused_workspace(self.mouse_follows_focus, false)?;
}
let workspace = self.focused_workspace_mut()?;
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);
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)?;
let tabbed_applications = TABBED_APPLICATIONS.lock();
if tabbed_applications.contains(&application_name) {
for window in workspace.visible_windows().iter().flatten() {
if window.application.name().unwrap_or_default() == application_name
{
let tab_rect = MacosApi::window_rect(&element)?;
let main_rect = MacosApi::window_rect(&window.element)?;
if tab_rect == main_rect {
tracing::debug!("ignoring focus change for tabbed window");
tabbed_window = true;
}
}
} 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 {
}
}
drop(tabbed_applications);
}
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)?;
}
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);
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;
}
}
}
}
}
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
event_tx().send(WindowManagerEvent::Show(
notification,
event.process_id(),
))?
}
}
if !has_matching_workspace_rule
&& workspace.focus_container_by_window(window_id).is_err()
workspace.layer = WorkspaceLayer::Tiling;
if matches!(
self.focused_workspace()?.layout,
Layout::Default(DefaultLayout::Scrolling)
) && !self.focused_workspace()?.containers().is_empty()
{
// 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
event_tx().send(WindowManagerEvent::Show(
notification,
event.process_id(),
))?
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!(
@@ -187,15 +221,18 @@ impl WindowManager {
SystemNotification::Accessibility(
AccessibilityNotification::AXMainWindowChanged
)
) {
) && !tabbed_window
{
self.reap_invalid_windows_for_application(process_id)?;
self.update_focused_workspace(false, false)?;
}
}
// TODO: upate this to work with floating applications / rules
// TODO: update this to work with floating applications / rules
WindowManagerEvent::Show(_, process_id) => {
let mut window_id = None;
let mut window_element = None;
let mut application_name = String::new();
let mut tabbed_window = false;
let mut create = true;
{
@@ -205,11 +242,28 @@ impl WindowManager {
{
window_id = Some(wid);
window_element = Some(element.clone());
application_name = application.name().unwrap_or_default().clone();
}
}
if let (Some(window_id), Some(element)) = (window_id, &window_element) {
let workspace = self.focused_workspace()?;
let tabbed_applications = TABBED_APPLICATIONS.lock();
if tabbed_applications.contains(&application_name) {
for window in workspace.visible_windows().iter().flatten() {
if window.application.name().unwrap_or_default() == application_name {
let tab_rect = MacosApi::window_rect(element)?;
let main_rect = MacosApi::window_rect(&window.element)?;
if tab_rect == main_rect {
tabbed_window = true;
}
}
}
}
drop(tabbed_applications);
if workspace.contains_window(window_id) {
// ignore bogus show events
tracing::debug!("ignoring show event for window already on workspace");
@@ -233,7 +287,7 @@ impl WindowManager {
}
}
if create {
if create && !tabbed_window {
let application = self.application(process_id)?;
if let Some(element) = window_element
&& let Ok(window) = Window::new(element, application.clone())
+25 -2
View File
@@ -7,6 +7,7 @@ use crate::LibraryError;
use crate::MANAGE_IDENTIFIERS;
use crate::PERMAIGNORE_CLASSES;
use crate::REGEX_IDENTIFIERS;
use crate::TABBED_APPLICATIONS;
use crate::WINDOW_RESTORE_POSITIONS;
use crate::accessibility::AccessibilityApi;
use crate::accessibility::attribute_constants::kAXFocusedAttribute;
@@ -487,7 +488,18 @@ impl Window {
match self.set_point(CGPoint::new(rect.left as CGFloat, rect.top as CGFloat)) {
Ok(_) => {}
Err(error) => {
reaper::send_notification(ReaperNotification::InvalidWindow(self.id));
let mut should_reap = true;
let tabbed_applications = TABBED_APPLICATIONS.lock();
if tabbed_applications.contains(&self.application.name().unwrap_or_default())
&& self.is_valid()
{
should_reap = false;
}
if should_reap {
reaper::send_notification(ReaperNotification::InvalidWindow(self.id));
}
return Err(error);
}
}
@@ -495,7 +507,18 @@ impl Window {
match self.set_size(CGSize::new(rect.right as CGFloat, rect.bottom as CGFloat)) {
Ok(_) => Ok(()),
Err(error) => {
reaper::send_notification(ReaperNotification::InvalidWindow(self.id));
let mut should_reap = true;
let tabbed_applications = TABBED_APPLICATIONS.lock();
if tabbed_applications.contains(&self.application.name().unwrap_or_default())
&& self.is_valid()
{
should_reap = false;
}
if should_reap {
reaper::send_notification(ReaperNotification::InvalidWindow(self.id));
}
Err(error)
}
}
+2
View File
@@ -3,6 +3,8 @@ with pkgs;
mkShell {
name = "komorebi";
RUST_BACKTRACE = "full";
buildInputs = [
just
gcc