Skip to content

Commit

Permalink
[WIP] Update to objc2 0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DataTriny committed Dec 21, 2023
1 parent be024d5 commit 023b8d5
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 107 deletions.
52 changes: 16 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions platforms/macos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ default-target = "x86_64-apple-darwin"
[dependencies]
accesskit = { version = "0.12.1", path = "../../common" }
accesskit_consumer = { version = "0.16.1", path = "../../consumer" }
objc2 = "> 0.3.0-beta.4, <= 0.3.0-beta.5"
objc2 = "0.5.0"
once_cell = "1.13.0"

[dependencies.icrate]
version = "<= 0.0.2"
version = "0.1.0"
features = [
"AppKit",
"AppKit_NSAccessibilityElement",
Expand Down
6 changes: 3 additions & 3 deletions platforms/macos/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

use accesskit::{ActionHandler, TreeUpdate};
use accesskit_consumer::{FilterResult, Tree};
use icrate::objc2::rc::{Id, WeakId};
use icrate::{
AppKit::NSView,
Foundation::{MainThreadMarker, NSArray, NSObject, NSPoint},
};
use objc2::rc::{Id, Shared, WeakId};
use std::{ffi::c_void, ptr::null_mut, rc::Rc};

use crate::{
Expand Down Expand Up @@ -40,7 +40,7 @@ impl Adapter {
action_handler: Box<dyn ActionHandler>,
) -> Self {
let view = unsafe { Id::retain(view as *mut NSView) }.unwrap();
let view = WeakId::new(&view);
let view = WeakId::from_id(&view);
let tree = Tree::new(initial_state, is_view_focused);
let mtm = MainThreadMarker::new().unwrap();
Self {
Expand Down Expand Up @@ -83,7 +83,7 @@ impl Adapter {
self.context.get_or_create_platform_node(node.id()),
))
})
.collect::<Vec<Id<NSObject, Shared>>>()
.collect::<Vec<Id<NSObject>>>()
};
let array = NSArray::from_vec(platform_nodes);
Id::autorelease_return(array)
Expand Down
11 changes: 4 additions & 7 deletions platforms/macos/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

use accesskit::{ActionHandler, ActionRequest, NodeId};
use accesskit_consumer::Tree;
use icrate::objc2::rc::{Id, WeakId};
use icrate::{AppKit::*, Foundation::MainThreadMarker};
use objc2::rc::{Id, Shared, WeakId};
use std::{cell::RefCell, collections::HashMap, rc::Rc};

use crate::node::PlatformNode;
Expand All @@ -15,7 +15,7 @@ pub(crate) struct Context {
pub(crate) view: WeakId<NSView>,
pub(crate) tree: RefCell<Tree>,
pub(crate) action_handler: RefCell<Box<dyn ActionHandler>>,
platform_nodes: RefCell<HashMap<NodeId, Id<PlatformNode, Shared>>>,
platform_nodes: RefCell<HashMap<NodeId, Id<PlatformNode>>>,
_mtm: MainThreadMarker,
}

Expand All @@ -35,10 +35,7 @@ impl Context {
})
}

pub(crate) fn get_or_create_platform_node(
self: &Rc<Self>,
id: NodeId,
) -> Id<PlatformNode, Shared> {
pub(crate) fn get_or_create_platform_node(self: &Rc<Self>, id: NodeId) -> Id<PlatformNode> {
let mut platform_nodes = self.platform_nodes.borrow_mut();
if let Some(result) = platform_nodes.get(&id) {
return result.clone();
Expand All @@ -49,7 +46,7 @@ impl Context {
result
}

pub(crate) fn remove_platform_node(&self, id: NodeId) -> Option<Id<PlatformNode, Shared>> {
pub(crate) fn remove_platform_node(&self, id: NodeId) -> Option<Id<PlatformNode>> {
let mut platform_nodes = self.platform_nodes.borrow_mut();
platform_nodes.remove(&id)
}
Expand Down
4 changes: 2 additions & 2 deletions platforms/macos/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

use accesskit::{Live, NodeId, Role};
use accesskit_consumer::{DetachedNode, FilterResult, Node, TreeChangeHandler, TreeState};
use icrate::objc2::{msg_send, runtime::AnyObject, Message};
use icrate::{
AppKit::*,
Foundation::{NSMutableDictionary, NSNumber, NSString},
};
use objc2::{msg_send, runtime::Object, Message};
use std::{collections::HashSet, rc::Rc};

use crate::{
Expand Down Expand Up @@ -87,7 +87,7 @@ impl QueuedEvent {
}
};

let mut user_info = NSMutableDictionary::<_, Object>::new();
let mut user_info = NSMutableDictionary::<_, AnyObject>::new();
let text = NSString::from_str(&text);
set_object_for_key(&mut user_info, &*text, unsafe {
NSAccessibilityAnnouncementKey
Expand Down
42 changes: 23 additions & 19 deletions platforms/macos/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ use icrate::{
},
};
use objc2::{
declare::{Ivar, IvarDrop},
declare_class, msg_send_id,
rc::{Id, Owned, Shared},
runtime::{Object, Sel},
sel, ClassType,
mutability::InteriorMutable,
rc::Id,
runtime::{AnyObject, Sel},
sel, ClassType, DeclaredClass,
};
use std::{
ptr::null_mut,
Expand Down Expand Up @@ -325,29 +325,34 @@ impl<'a> NodeWrapper<'a> {
}
}

pub struct BoxedData {
struct BoxedData {
context: Weak<Context>,
node_id: NodeId,
}

struct PlatformNodeIvars {
boxed: Box<BoxedData>,
}

declare_class!(
pub(crate) struct PlatformNode {
pub(crate) struct PlatformNode;
// SAFETY: This is set in `PlatformNode::new` immediately after
// the object is created.
boxed: IvarDrop<Box<BoxedData>, "_boxed">,
}

mod ivars;

unsafe impl ClassType for PlatformNode {
#[inherits(NSObject)]
type Super = NSAccessibilityElement;
type Mutability = InteriorMutable;
const NAME: &'static str = "AccessKitNode";
}

impl DeclaredClass for PlatformNode {
type Ivars = PlatformNodeIvars;
}

unsafe impl PlatformNode {
#[method(accessibilityParent)]
fn parent(&self) -> *mut Object {
fn parent(&self) -> *mut AnyObject {
self.resolve_with_context(|node, context| {
if let Some(parent) = node.filtered_parent(&filter) {
Id::autorelease_return(context.get_or_create_platform_node(parent.id()))
Expand Down Expand Up @@ -775,13 +780,12 @@ declare_class!(
);

impl PlatformNode {
pub(crate) fn new(context: Weak<Context>, node_id: NodeId) -> Id<Self, Shared> {
let boxed = Box::new(BoxedData { context, node_id });
unsafe {
let mut object: Id<Self, Owned> = msg_send_id![Self::class(), new];
Ivar::write(&mut object.boxed, boxed);
object.into()
}
pub(crate) fn new(context: Weak<Context>, node_id: NodeId) -> Id<Self> {
let this = Self::alloc().set_ivars(PlatformNodeIvars {
boxed: Box::new(BoxedData { context, node_id }),
});

unsafe { msg_send_id![super(this), init] }
}

fn resolve_with_context<F, T>(&self, f: F) -> Option<T>
Expand All @@ -807,7 +811,7 @@ impl PlatformNode {
let platform_nodes = node
.filtered_children(filter)
.map(|child| context.get_or_create_platform_node(child.id()))
.collect::<Vec<Id<PlatformNode, Shared>>>();
.collect::<Vec<Id<PlatformNode>>>();
Id::autorelease_return(NSArray::from_vec(platform_nodes))
})
.unwrap_or_else(null_mut)
Expand Down
18 changes: 7 additions & 11 deletions platforms/macos/src/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@

use icrate::AppKit::NSWindow;
use objc2::{
declare::MethodImplementation,
encode::{
Encode, Encoding,
__unstable::{EncodeArguments, EncodeReturn},
},
encode::{Encode, EncodeArguments, EncodeReturn, Encoding},
ffi::class_addMethod,
msg_send,
runtime::{Bool, Class, Object, Sel},
runtime::{AnyClass, AnyObject, Bool, MethodImplementation, Sel},
sel, Message,
};
use std::{ffi::CString, ptr::null_mut};

extern "C" fn focus_forwarder(this: &NSWindow, _cmd: Sel) -> *mut Object {
extern "C" fn focus_forwarder(this: &NSWindow, _cmd: Sel) -> *mut AnyObject {
unsafe {
this.contentView().map_or_else(null_mut, |view| {
msg_send![&*view, accessibilityFocusedUIElement]
Expand All @@ -40,10 +36,10 @@ extern "C" fn focus_forwarder(this: &NSWindow, _cmd: Sel) -> *mut Object {
/// 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();
let class = AnyClass::get(class_name).unwrap();
unsafe {
add_method(
class as *const Class as *mut Class,
class as *const AnyClass as *mut AnyClass,
sel!(accessibilityFocusedUIElement),
focus_forwarder as unsafe extern "C" fn(_, _) -> _,
)
Expand All @@ -53,7 +49,7 @@ pub unsafe fn add_focus_forwarder_to_window_class(class_name: &str) {
// The rest of this file is copied from objc2 with only minor adaptations,
// to allow a method to be added to an existing class.

unsafe fn add_method<T, F>(class: *mut Class, sel: Sel, func: F)
unsafe fn add_method<T, F>(class: *mut AnyClass, sel: Sel, func: F)
where
T: Message + ?Sized,
F: MethodImplementation<Callee = T>,
Expand Down Expand Up @@ -87,7 +83,7 @@ fn count_args(sel: Sel) -> usize {

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);
let mut types = format!("{}{}{}", ret, <*mut AnyObject>::ENCODING, Sel::ENCODING);
for enc in args {
use core::fmt::Write;
write!(&mut types, "{}", enc).unwrap();
Expand Down
Loading

0 comments on commit 023b8d5

Please sign in to comment.