|
| 1 | +diff --git a/vendor/gpui-pre-macos/src/window.rs b/vendor/gpui-pre-macos/src/window.rs |
| 2 | +index 1ba661c..4ddfce9 100644 |
| 3 | +--- a/vendor/gpui-pre-macos/src/window.rs |
| 4 | ++++ b/vendor/gpui-pre-macos/src/window.rs |
| 5 | +@@ -65,7 +65,7 @@ use parking_lot::Mutex; |
| 6 | + use raw_window_handle as rwh; |
| 7 | + use smallvec::SmallVec; |
| 8 | + use std::{ |
| 9 | +- cell::Cell, |
| 10 | ++ cell::{Cell, RefCell}, |
| 11 | + ffi::{CStr, CString, c_void}, |
| 12 | + mem, |
| 13 | + ops::Range, |
| 14 | +@@ -294,6 +294,19 @@ unsafe fn build_classes() { |
| 15 | + sel!(characterIndexForPoint:), |
| 16 | + character_index_for_point as extern "C" fn(&Object, Sel, NSPoint) -> u64, |
| 17 | + ); |
| 18 | ++ |
| 19 | ++ decl.add_method( |
| 20 | ++ sel!(accessibilityChildren), |
| 21 | ++ accessibility_children as extern "C" fn(&Object, Sel) -> id, |
| 22 | ++ ); |
| 23 | ++ decl.add_method( |
| 24 | ++ sel!(accessibilityFocusedUIElement), |
| 25 | ++ accessibility_focused_ui_element as extern "C" fn(&Object, Sel) -> id, |
| 26 | ++ ); |
| 27 | ++ decl.add_method( |
| 28 | ++ sel!(accessibilityHitTest:), |
| 29 | ++ accessibility_hit_test as extern "C" fn(&Object, Sel, NSPoint) -> id, |
| 30 | ++ ); |
| 31 | + decl.register() |
| 32 | + }; |
| 33 | + BLURRED_VIEW_CLASS = { |
| 34 | +@@ -713,7 +726,11 @@ struct MacWindowState { |
| 35 | + toggle_tab_bar_callback: Option<Box<dyn FnMut()>>, |
| 36 | + activated_least_once: bool, |
| 37 | + closed: Arc<AtomicBool>, |
| 38 | +- accesskit_adapter: Option<accesskit_macos::SubclassingAdapter>, |
| 39 | ++ // Kept behind an `Rc<RefCell<_>>` so the `GPUIView` accessibility methods can clone it |
| 40 | ++ // out and release the window state lock before calling into AccessKit. AppKit may |
| 41 | ++ // query accessibility while the lock is already held further up the stack, and |
| 42 | ++ // raising AccessKit events can synchronously trigger more queries. |
| 43 | ++ accesskit: Option<Rc<RefCell<AccessKitState>>>, |
| 44 | + // The parent window if this window is a sheet (Dialog kind) |
| 45 | + sheet_parent: Option<id>, |
| 46 | + } |
| 47 | +@@ -1153,7 +1170,7 @@ impl MacWindow { |
| 48 | + toggle_tab_bar_callback: None, |
| 49 | + activated_least_once: false, |
| 50 | + closed: Arc::new(AtomicBool::new(false)), |
| 51 | +- accesskit_adapter: None, |
| 52 | ++ accesskit: None, |
| 53 | + sheet_parent: None, |
| 54 | + })); |
| 55 | + let mut window = Self(state, marker); |
| 56 | +@@ -1385,15 +1402,10 @@ impl MacWindow { |
| 57 | + impl Drop for MacWindow { |
| 58 | + fn drop(&mut self) { |
| 59 | + let mut this = self.0.lock(); |
| 60 | +- // `accesskit_macos::SubclassingAdapter::for_window` strong-retains the |
| 61 | +- // window's content view, and that content view keeps the `GPUIView` it |
| 62 | +- // hosts alive. Together with the `Arc<Mutex<MacWindowState>>` parked in |
| 63 | +- // both views' `windowState` ivar, that forms |
| 64 | +- // `MacWindowState -> adapter -> content view -> GPUIView -> MacWindowState`, |
| 65 | +- // a cycle the delegate/`frame_source` teardown below cannot break. Drop |
| 66 | +- // the adapter here so the native view, its `CAMetalLayer` and the |
| 67 | +- // renderer's command queue are actually released with the window. |
| 68 | +- drop(this.accesskit_adapter.take()); |
| 69 | ++ // The `GPUIView` (and with it this state) can outlive the GPUI window until AppKit |
| 70 | ++ // finishes closing it. Accessibility queries arriving in the meantime fall back to |
| 71 | ++ // `NSView`'s defaults instead of serving a tree for a window GPUI no longer updates. |
| 72 | ++ this.accesskit.take(); |
| 73 | + this.renderer.destroy(); |
| 74 | + let window = this.native_window; |
| 75 | + let sheet_parent = this.sheet_parent.take(); |
| 76 | +@@ -2385,23 +2397,30 @@ impl PlatformWindow for MacWindow { |
| 77 | + }; |
| 78 | + let action_handler = A11yActionHandler(callbacks.action); |
| 79 | + |
| 80 | ++ // `GPUIView` implements the `NSView` accessibility methods itself and forwards them to |
| 81 | ++ // this adapter. `accesskit_macos::SubclassingAdapter` is not used because restoring the |
| 82 | ++ // view's class when it is dropped corrupts the KVO isa-swizzling AppKit's Touch Bar |
| 83 | ++ // support has applied to the view, making AppKit throw while the window closes. |
| 84 | + let adapter = unsafe { |
| 85 | +- accesskit_macos::SubclassingAdapter::for_window( |
| 86 | +- lock.native_window as *mut c_void, |
| 87 | +- activation_handler, |
| 88 | +- action_handler, |
| 89 | +- ) |
| 90 | ++ accesskit_macos::Adapter::new(lock.native_view.as_ptr().cast(), false, action_handler) |
| 91 | + }; |
| 92 | + |
| 93 | +- lock.accesskit_adapter = Some(adapter); |
| 94 | ++ lock.accesskit = Some(Rc::new(RefCell::new(AccessKitState { |
| 95 | ++ adapter, |
| 96 | ++ activation_handler, |
| 97 | ++ }))); |
| 98 | + } |
| 99 | + |
| 100 | + fn a11y_tree_update(&self, tree_update: accesskit::TreeUpdate) { |
| 101 | +- let events = { |
| 102 | +- let mut lock = self.0.lock(); |
| 103 | +- lock.accesskit_adapter |
| 104 | +- .as_mut() |
| 105 | +- .and_then(|adapter| adapter.update_if_active(|| tree_update)) |
| 106 | ++ let Some(accesskit) = self.0.lock().accesskit.clone() else { |
| 107 | ++ return; |
| 108 | ++ }; |
| 109 | ++ let events = match accesskit.try_borrow_mut() { |
| 110 | ++ Ok(mut accesskit) => accesskit.adapter.update_if_active(|| tree_update), |
| 111 | ++ Err(_) => { |
| 112 | ++ log::error!("dropped accessibility tree update during an accessibility query"); |
| 113 | ++ None |
| 114 | ++ } |
| 115 | + }; |
| 116 | + if let Some(events) = events { |
| 117 | + events.raise(); |
| 118 | +@@ -2413,6 +2432,11 @@ impl PlatformWindow for MacWindow { |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | ++struct AccessKitState { |
| 123 | ++ adapter: accesskit_macos::Adapter, |
| 124 | ++ activation_handler: A11yActivationHandler, |
| 125 | ++} |
| 126 | ++ |
| 127 | + struct A11yActivationHandler { |
| 128 | + callback: Box<dyn Fn() -> Option<accesskit::TreeUpdate> + Send + 'static>, |
| 129 | + } |
| 130 | +@@ -2509,6 +2533,54 @@ extern "C" fn dealloc_view(this: &Object, _: Sel) { |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | ++/// Runs `f` with the view's AccessKit state, or returns `None` when accessibility is disabled, |
| 135 | ++/// not initialized yet, or already torn down. Also returns `None` instead of deadlocking or |
| 136 | ++/// panicking when the query arrives while GPUI holds the window state lock or from within |
| 137 | ++/// AccessKit itself. |
| 138 | ++fn with_accesskit_state<R>(view: &Object, f: impl FnOnce(&mut AccessKitState) -> R) -> Option<R> { |
| 139 | ++ let window_state = unsafe { get_window_state(view) }; |
| 140 | ++ let accesskit = window_state.try_lock()?.accesskit.clone()?; |
| 141 | ++ let mut accesskit = accesskit.try_borrow_mut().ok()?; |
| 142 | ++ Some(f(&mut accesskit)) |
| 143 | ++} |
| 144 | ++ |
| 145 | ++extern "C" fn accessibility_children(this: &Object, _: Sel) -> id { |
| 146 | ++ with_accesskit_state(this, |state| { |
| 147 | ++ state |
| 148 | ++ .adapter |
| 149 | ++ .view_children(&mut state.activation_handler) |
| 150 | ++ .cast::<Object>() |
| 151 | ++ }) |
| 152 | ++ .unwrap_or_else(|| unsafe { msg_send![super(this, class!(NSView)), accessibilityChildren] }) |
| 153 | ++} |
| 154 | ++ |
| 155 | ++extern "C" fn accessibility_focused_ui_element(this: &Object, _: Sel) -> id { |
| 156 | ++ with_accesskit_state(this, |state| { |
| 157 | ++ state |
| 158 | ++ .adapter |
| 159 | ++ .focus(&mut state.activation_handler) |
| 160 | ++ .cast::<Object>() |
| 161 | ++ }) |
| 162 | ++ .unwrap_or_else(|| unsafe { |
| 163 | ++ msg_send![super(this, class!(NSView)), accessibilityFocusedUIElement] |
| 164 | ++ }) |
| 165 | ++} |
| 166 | ++ |
| 167 | ++extern "C" fn accessibility_hit_test(this: &Object, _: Sel, point: NSPoint) -> id { |
| 168 | ++ with_accesskit_state(this, |state| { |
| 169 | ++ state |
| 170 | ++ .adapter |
| 171 | ++ .hit_test( |
| 172 | ++ accesskit_macos::NSPoint::new(point.x, point.y), |
| 173 | ++ &mut state.activation_handler, |
| 174 | ++ ) |
| 175 | ++ .cast::<Object>() |
| 176 | ++ }) |
| 177 | ++ .unwrap_or_else(|| unsafe { |
| 178 | ++ msg_send![super(this, class!(NSView)), accessibilityHitTest: point] |
| 179 | ++ }) |
| 180 | ++} |
| 181 | ++ |
| 182 | + fn add_mouse_tracking_area(native_view: &Objc2NSView) { |
| 183 | + let options = NSTrackingAreaOptions::MouseEnteredAndExited |
| 184 | + | NSTrackingAreaOptions::MouseMoved |
| 185 | +@@ -3165,12 +3237,14 @@ extern "C" fn window_did_change_key_status(this: &Object, selector: Sel, _: id) |
| 186 | + let executor = lock.foreground_executor.clone(); |
| 187 | + drop(lock); |
| 188 | + |
| 189 | +- let a11y_events = { |
| 190 | +- let mut lock = window_state.lock(); |
| 191 | +- lock.accesskit_adapter |
| 192 | +- .as_mut() |
| 193 | +- .and_then(|adapter| adapter.update_view_focus_state(is_active)) |
| 194 | +- }; |
| 195 | ++ let accesskit = window_state.lock().accesskit.clone(); |
| 196 | ++ let a11y_events = accesskit.and_then(|accesskit| match accesskit.try_borrow_mut() { |
| 197 | ++ Ok(mut accesskit) => accesskit.adapter.update_view_focus_state(is_active), |
| 198 | ++ Err(_) => { |
| 199 | ++ log::error!("dropped accessibility focus change during an accessibility query"); |
| 200 | ++ None |
| 201 | ++ } |
| 202 | ++ }); |
| 203 | + if let Some(events) = a11y_events { |
| 204 | + events.raise(); |
| 205 | + } |
0 commit comments