mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-17 22:33:12 +01:00
fix(wm): handle destruction of 1st native tab
This commit implements some fixes (still not ideal) to handle the closing of the 1st tab of an application which uses native tabs. When the 1st tab is destroyed, the window AXUIElement gets rendered invalid, so we have to reap that window, and then emit our own Show event for the tab that is becoming the new 1st tab and set that as the window. There is an edge case now where if this happens while the window is floating, our emitted show event will result in that window being integrated into the layout rather than staying tiling, but I think this is better than having a completely janky workspace and possibly having to restart komorebi.
This commit is contained in:
1 parent
627d150241
commit
712f2d86a0
5 files changed
+144
-57
No files matched your search
@@ -187,6 +187,7 @@ impl MacosApi {
|
||||
)?)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
pub fn window_rect(element: &AXUIElement) -> Result<CGRect, AccessibilityError> {
|
||||
let mut position_receiver = std::ptr::null();
|
||||
let mut size_receiver = std::ptr::null();
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use crate::TABBED_APPLICATIONS;
|
||||
use crate::WORKSPACE_MATCHING_RULES;
|
||||
use crate::accessibility::AccessibilityApi;
|
||||
use crate::accessibility::error::AccessibilityApiError;
|
||||
use crate::accessibility::error::AccessibilityError;
|
||||
use crate::accessibility::notification_constants::AccessibilityNotification;
|
||||
use crate::ax_event_listener::event_tx;
|
||||
use crate::core::config_generation::MatchingRule;
|
||||
@@ -96,17 +98,47 @@ impl WindowManager {
|
||||
|
||||
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 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 = MacosApi::window_rect(&window.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
|
||||
event_tx().send(WindowManagerEvent::Show(
|
||||
notification,
|
||||
event.process_id(),
|
||||
))?;
|
||||
|
||||
first_tab_destroyed = true;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if first_tab_destroyed {
|
||||
self.reap_invalid_windows_for_application(process_id)?;
|
||||
}
|
||||
}
|
||||
|
||||
drop(tabbed_applications);
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
use crate::TABBED_APPLICATIONS;
|
||||
use crate::accessibility::error::AccessibilityError;
|
||||
use crate::window::Window;
|
||||
use crate::window_manager::WindowManager;
|
||||
use crossbeam_channel::Receiver;
|
||||
use crossbeam_channel::Sender;
|
||||
@@ -69,6 +72,15 @@ fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result<()>
|
||||
w.id != window_id
|
||||
});
|
||||
}
|
||||
|
||||
workspace.floating_windows_mut().retain(|w| {
|
||||
if w.id == window_id {
|
||||
should_update = true;
|
||||
tracing::info!("reaping window: {window_id}");
|
||||
}
|
||||
|
||||
w.id != window_id
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,3 +99,24 @@ fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result<()>
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn notify_on_error(
|
||||
window: &Window,
|
||||
result: Result<(), AccessibilityError>,
|
||||
) -> Result<(), AccessibilityError> {
|
||||
if let Err(_error) = &result {
|
||||
let mut should_reap = true;
|
||||
let tabbed_applications = TABBED_APPLICATIONS.lock();
|
||||
if tabbed_applications.contains(&window.application.name().unwrap_or_default())
|
||||
&& window.is_valid()
|
||||
{
|
||||
should_reap = false;
|
||||
}
|
||||
|
||||
if should_reap {
|
||||
send_notification(ReaperNotification::InvalidWindow(window.id));
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
+43
-51
@@ -7,7 +7,6 @@ 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;
|
||||
@@ -38,7 +37,6 @@ use crate::core_graphics::CoreGraphicsApi;
|
||||
use crate::hidden_frame_bottom_left;
|
||||
use crate::macos_api::MacosApi;
|
||||
use crate::reaper;
|
||||
use crate::reaper::ReaperNotification;
|
||||
use crate::window_manager_event::SystemNotification;
|
||||
use crate::window_manager_event::WindowManagerEvent;
|
||||
use color_eyre::eyre;
|
||||
@@ -348,8 +346,8 @@ impl Window {
|
||||
rect.origin.y,
|
||||
);
|
||||
|
||||
self.set_point(hidden_rect.origin)?;
|
||||
self.set_size(hidden_rect.size)?;
|
||||
self.set_point(hidden_rect.origin, true)?;
|
||||
self.set_size(hidden_rect.size, true)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -419,8 +417,8 @@ impl Window {
|
||||
.unwrap_or_else(|| String::from("<NO TITLE FOUND>"))
|
||||
);
|
||||
|
||||
self.set_point(cg_rect.origin)?;
|
||||
self.set_size(cg_rect.size)?;
|
||||
self.set_point(cg_rect.origin, true)?;
|
||||
self.set_size(cg_rect.size, true)?;
|
||||
should_remove_restore_position = true;
|
||||
}
|
||||
|
||||
@@ -485,49 +483,31 @@ impl Window {
|
||||
}
|
||||
|
||||
pub fn set_position(&self, rect: &Rect) -> Result<(), AccessibilityError> {
|
||||
match self.set_point(CGPoint::new(rect.left as CGFloat, rect.top as CGFloat)) {
|
||||
Ok(_) => {}
|
||||
Err(error) => {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
match self.set_size(CGSize::new(rect.right as CGFloat, rect.bottom as CGFloat)) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(error) => {
|
||||
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)
|
||||
}
|
||||
}
|
||||
self.set_point(
|
||||
CGPoint::new(rect.left as CGFloat, rect.top as CGFloat),
|
||||
true,
|
||||
)?;
|
||||
self.set_size(
|
||||
CGSize::new(rect.right as CGFloat, rect.bottom as CGFloat),
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn focus(&self, mouse_follows_focus: bool) -> Result<(), LibraryError> {
|
||||
unsafe {
|
||||
self.running_application()?
|
||||
.activateWithOptions(NSApplicationActivationOptions::empty());
|
||||
match self.running_application() {
|
||||
Ok(running_application) => {
|
||||
running_application
|
||||
.activateWithOptions(NSApplicationActivationOptions::empty());
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::warn!(
|
||||
"failed to get running application for {} ({:?}): {error}",
|
||||
self.application.process_id,
|
||||
self.application.name()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let cf_boolean = CFBoolean::new(true);
|
||||
let value = &**cf_boolean;
|
||||
@@ -548,22 +528,34 @@ impl Window {
|
||||
AccessibilityApi::set_attribute_cf_value(&self.element, kAXFocusedAttribute, value)
|
||||
}
|
||||
|
||||
pub fn set_point(&self, point: CGPoint) -> Result<(), AccessibilityError> {
|
||||
AccessibilityApi::set_attribute_ax_value(
|
||||
pub fn set_point(&self, point: CGPoint, should_reap: bool) -> Result<(), AccessibilityError> {
|
||||
let result = AccessibilityApi::set_attribute_ax_value(
|
||||
&self.element,
|
||||
kAXPositionAttribute,
|
||||
AXValueType::CGPoint,
|
||||
point,
|
||||
)
|
||||
);
|
||||
|
||||
if should_reap {
|
||||
reaper::notify_on_error(self, result)
|
||||
} else {
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_size(&self, size: CGSize) -> Result<(), AccessibilityError> {
|
||||
AccessibilityApi::set_attribute_ax_value(
|
||||
pub fn set_size(&self, size: CGSize, should_reap: bool) -> Result<(), AccessibilityError> {
|
||||
let result = AccessibilityApi::set_attribute_ax_value(
|
||||
&self.element,
|
||||
kAXSizeAttribute,
|
||||
AXValueType::CGSize,
|
||||
size,
|
||||
)
|
||||
);
|
||||
|
||||
if should_reap {
|
||||
reaper::notify_on_error(self, result)
|
||||
} else {
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
pub fn center(&mut self, work_area: &Rect, resize: bool) -> Result<(), AccessibilityError> {
|
||||
|
||||
@@ -287,12 +287,14 @@ impl Workspace {
|
||||
insertion_idx
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn reap_invalid_windows_for_application(
|
||||
&mut self,
|
||||
process_id: i32,
|
||||
valid_window_ids: &[u32],
|
||||
) -> eyre::Result<usize> {
|
||||
let mut invalid_window_ids = vec![];
|
||||
let mut invalid_floating_window_ids = vec![];
|
||||
for container in self.containers() {
|
||||
if let Some(focused_window) = container.focused_window()
|
||||
&& focused_window.application.process_id == process_id
|
||||
@@ -309,11 +311,33 @@ impl Workspace {
|
||||
}
|
||||
}
|
||||
|
||||
for window in self.floating_windows() {
|
||||
if window.application.process_id == process_id {
|
||||
if !valid_window_ids.contains(&window.id) {
|
||||
invalid_floating_window_ids.push(window.id);
|
||||
}
|
||||
|
||||
// if accessibility API calls fail on AXError::InvalidUIElement
|
||||
// let's try and nuke these early
|
||||
if !window.is_valid() {
|
||||
invalid_floating_window_ids.push(window.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for window_id in &invalid_window_ids {
|
||||
self.remove_window(*window_id)?;
|
||||
}
|
||||
|
||||
Ok(invalid_window_ids.len())
|
||||
for window_id in &invalid_floating_window_ids {
|
||||
self.remove_floating_window(*window_id);
|
||||
}
|
||||
|
||||
tracing::debug!(
|
||||
"reaping invalid windows {invalid_window_ids:?} {invalid_floating_window_ids:?}"
|
||||
);
|
||||
|
||||
Ok(invalid_window_ids.len() + invalid_floating_window_ids.len())
|
||||
}
|
||||
|
||||
pub fn remove_window(&mut self, window_id: u32) -> eyre::Result<Window> {
|
||||
@@ -538,8 +562,10 @@ impl Workspace {
|
||||
should_hide = true
|
||||
}
|
||||
|
||||
if should_hide {
|
||||
window.hide()?;
|
||||
if should_hide && let Err(error) = window.hide() {
|
||||
tracing::warn!(
|
||||
"failed to hide floating window, but this might be because it is already hidden: {error}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -773,7 +799,10 @@ impl Workspace {
|
||||
|
||||
pub fn remove_focused_floating_window(&mut self) -> Option<Window> {
|
||||
let window_id = MacosApi::foreground_window_id()?;
|
||||
self.remove_floating_window(window_id)
|
||||
}
|
||||
|
||||
pub fn remove_floating_window(&mut self, window_id: u32) -> Option<Window> {
|
||||
let mut idx = None;
|
||||
for (i, window) in self.floating_windows().iter().enumerate() {
|
||||
if window_id == window.id {
|
||||
|
||||
Reference in new issue
Block a user