mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-22 00:21:43 +01:00
feat(wm): handle non-observable applications
OBS Studio is not observable when starting komorebi, so this changes Application.observe to set a new field is_observable to false if registering the observer fails instead of erroring out. This allows the wm initialization to continue instead of crashing.
This commit is contained in:
1 parent
565c6cd6f8
commit
8eb01713f2
3 files changed
+30
-20
No files matched your search
@@ -46,6 +46,7 @@ pub struct Application {
|
||||
element: AccessibilityUiElement,
|
||||
pub process_id: i32,
|
||||
pub observer: AccessibilityObserver,
|
||||
pub is_observable: bool,
|
||||
}
|
||||
|
||||
#[instrument(skip_all)]
|
||||
@@ -88,7 +89,7 @@ impl Drop for Application {
|
||||
// this gets called when an Application clone on a Window is dropped, so we need
|
||||
// to make sure it only invalidates the observer if the Application is no longer
|
||||
// running
|
||||
if !self.is_valid() {
|
||||
if self.is_observable && !self.is_valid() {
|
||||
tracing::info!(
|
||||
"invalidating application observer for process id {}",
|
||||
self.process_id
|
||||
@@ -108,6 +109,7 @@ impl Application {
|
||||
process_id,
|
||||
Some(application_observer_callback),
|
||||
)?),
|
||||
is_observable: true,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -117,7 +119,7 @@ impl Application {
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
pub fn observe(&self, run_loop: &CFRunLoop) -> Result<(), AccessibilityError> {
|
||||
pub fn observe(&mut self, run_loop: &CFRunLoop) {
|
||||
tracing::info!(
|
||||
"registering observer for process: {}, name: {}",
|
||||
self.process_id,
|
||||
@@ -125,12 +127,24 @@ impl Application {
|
||||
.unwrap_or_else(|| String::from("<NO NAME FOUND>"))
|
||||
);
|
||||
|
||||
AccessibilityApi::add_observer_to_run_loop(
|
||||
match AccessibilityApi::add_observer_to_run_loop(
|
||||
&self.observer,
|
||||
&self.element,
|
||||
NOTIFICATIONS,
|
||||
run_loop,
|
||||
)
|
||||
) {
|
||||
Ok(_) => {
|
||||
self.is_observable = true;
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::warn!(
|
||||
"failed to register observer for process {} ({}): {error}",
|
||||
self.process_id,
|
||||
self.name()
|
||||
.unwrap_or_else(|| String::from("<NO NAME FOUND>"))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_valid(&self) -> bool {
|
||||
|
||||
+10
-14
@@ -26,6 +26,7 @@ use objc2_core_foundation::CFString;
|
||||
use objc2_core_foundation::CGPoint;
|
||||
use objc2_core_foundation::CGRect;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::hash_map::Entry;
|
||||
use std::ffi::c_void;
|
||||
use std::ptr::NonNull;
|
||||
|
||||
@@ -82,20 +83,15 @@ impl MacosApi {
|
||||
.entry(monitor_idx)
|
||||
.or_insert_with(Vec::new);
|
||||
|
||||
let application =
|
||||
wm.applications.entry(info.owner_pid).or_insert_with(|| {
|
||||
let application = Application::new(info.owner_pid)
|
||||
.unwrap_or_else(|_| {
|
||||
panic!(
|
||||
"failed to create application from pid {}",
|
||||
info.owner_pid
|
||||
)
|
||||
});
|
||||
application
|
||||
.observe(&wm.run_loop)
|
||||
.expect("application must be observable");
|
||||
application
|
||||
});
|
||||
let application = match wm.applications.entry(info.owner_pid) {
|
||||
Entry::Occupied(entry) => entry.into_mut(),
|
||||
Entry::Vacant(vacant) => {
|
||||
let mut application = Application::new(info.owner_pid)?;
|
||||
// TODO: this ain't great, fix this OBS workaround
|
||||
application.observe(&wm.run_loop);
|
||||
vacant.insert(application)
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(window) = application.window_by_title(&info.name) {
|
||||
window.observe(&wm.run_loop)?;
|
||||
|
||||
@@ -108,8 +108,8 @@ impl WindowManager {
|
||||
match self.applications.entry(process_id) {
|
||||
Entry::Occupied(entry) => Ok(entry.into_mut()),
|
||||
Entry::Vacant(vacant) => {
|
||||
let application = Application::new(process_id)?;
|
||||
application.observe(&self.run_loop)?;
|
||||
let mut application = Application::new(process_id)?;
|
||||
application.observe(&self.run_loop);
|
||||
Ok(vacant.insert(application))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user