-
Notifications
You must be signed in to change notification settings - Fork 66
feat: Workaround for libraries that put the macOS keyboard focus on the window rather than the content view #266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2023 The AccessKit Authors. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (found in | ||
// the LICENSE-APACHE file) or the MIT license (found in | ||
// the LICENSE-MIT file), at your option. | ||
|
||
use objc2::{ | ||
declare::MethodImplementation, | ||
encode::{Encode, EncodeArguments, Encoding}, | ||
ffi::class_addMethod, | ||
msg_send, | ||
runtime::{Bool, Class, Object, Sel}, | ||
sel, Message, | ||
}; | ||
use std::{ffi::CString, ptr::null_mut}; | ||
|
||
use crate::appkit::NSWindow; | ||
|
||
extern "C" fn focus_forwarder(this: &NSWindow, _cmd: Sel) -> *mut Object { | ||
this.content_view().map_or_else(null_mut, |view| unsafe { | ||
msg_send![&*view, accessibilityFocusedUIElement] | ||
}) | ||
} | ||
|
||
/// Modifies the specified class, which must be a subclass of `NSWindow`, | ||
/// to include an `accessibilityFocusedUIElement` method that calls | ||
/// the corresponding method on the window's content view. This is needed | ||
/// for windowing libraries such as SDL that place the keyboard focus | ||
/// directly on the window rather than the content view. | ||
/// | ||
/// # Safety | ||
/// | ||
/// This function is declared unsafe because the caller must ensure that the | ||
/// code for this crate is never unloaded from the application process, | ||
/// since it's not possible to reverse this operation. It's safest | ||
/// if this crate is statically linked into the application's main executable. | ||
/// Also, this function assumes that the specified class is a subclass | ||
/// of `NSWindow`. | ||
pub unsafe fn add_focus_forwarder_to_window_class(class_name: &str) { | ||
let class = Class::get(class_name).unwrap(); | ||
unsafe { | ||
add_method( | ||
class as *const Class as *mut Class, | ||
sel!(accessibilityFocusedUIElement), | ||
focus_forwarder as unsafe extern "C" fn(_, _) -> _, | ||
) | ||
}; | ||
} | ||
|
||
// The rest of this file is copied from objc2 with only minor adaptations, | ||
// to allow a method to be added to an existing class. | ||
mwcampbell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
unsafe fn add_method<T, F>(class: *mut Class, sel: Sel, func: F) | ||
where | ||
T: Message + ?Sized, | ||
F: MethodImplementation<Callee = T>, | ||
{ | ||
let encs = F::Args::ENCODINGS; | ||
let sel_args = count_args(sel); | ||
assert_eq!( | ||
sel_args, | ||
encs.len(), | ||
"Selector {:?} accepts {} arguments, but function accepts {}", | ||
sel, | ||
sel_args, | ||
encs.len(), | ||
); | ||
|
||
let types = method_type_encoding(&F::Ret::ENCODING, encs); | ||
let success = Bool::from_raw(unsafe { | ||
class_addMethod( | ||
class as *mut _, | ||
sel.as_ptr(), | ||
Some(func.__imp()), | ||
types.as_ptr(), | ||
) | ||
}); | ||
assert!(success.as_bool(), "Failed to add method {:?}", sel); | ||
} | ||
|
||
fn count_args(sel: Sel) -> usize { | ||
sel.name().chars().filter(|&c| c == ':').count() | ||
} | ||
|
||
fn method_type_encoding(ret: &Encoding, args: &[Encoding]) -> CString { | ||
// First two arguments are always self and the selector | ||
let mut types = format!("{}{}{}", ret, <*mut Object>::ENCODING, Sel::ENCODING); | ||
for enc in args { | ||
use core::fmt::Write; | ||
write!(&mut types, "{}", enc).unwrap(); | ||
} | ||
CString::new(types).unwrap() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.