Experiment with bundle identifier being attached to generated subclass names (Issue #63)

This commit is contained in:
Ryan McGrath 2022-10-13 17:15:52 -04:00
parent 4d1e0ddb9d
commit 215eee4ae2
No known key found for this signature in database
GPG key ID: DA6CBD9233593DEA

View file

@ -4,13 +4,37 @@ use std::sync::{Arc, RwLock};
use lazy_static::lazy_static;
use objc::{class, msg_send, sel, sel_impl};
use objc::declare::ClassDecl;
use objc::runtime::{objc_getClass, Class};
use objc::runtime::{objc_getClass, Class, Object};
lazy_static! {
static ref CLASSES: ClassMap = ClassMap::new();
}
/// A temporary method for testing; this will get cleaned up if it's worth bringing in permanently.
///
/// (and probably not repeatedly queried...)
///
/// This accounts for code not running in a standard bundle, and returns `None` if the bundle
/// identifier is nil.
fn get_bundle_id() -> Option<String> {
let identifier: *mut Object = unsafe {
let bundle: *mut Object = msg_send![class!(NSBundle), mainBundle];
msg_send![bundle, bundleIdentifier]
};
if identifier == crate::foundation::nil {
return None;
}
let identifier = crate::foundation::NSString::retain(identifier).to_string()
.replace(".", "_")
.replace("-", "_");
Some(identifier)
}
/// Represents an entry in a `ClassMap`. We store an optional superclass_name for debugging
/// purposes; it's an `Option` to make the logic of loading a class type where we don't need to
/// care about the superclass type simpler.
@ -119,7 +143,10 @@ where
// If we can't find the class anywhere, then we'll attempt to load the superclass and register
// our new class type.
if let Some(superclass) = CLASSES.load(superclass_name, None) {
let objc_subclass_name = format!("{}_{}", subclass_name, superclass_name);
let objc_subclass_name = match get_bundle_id() {
Some(bundle_id) => format!("{}_{}_{}", subclass_name, superclass_name, bundle_id),
None => format!("{}_{}", subclass_name, superclass_name)
};
match ClassDecl::new(&objc_subclass_name, unsafe { &*superclass }) {
Some(mut decl) => {