rust_macios/appkit/
ns_nib.rs

1use objc::{msg_send, sel, sel_impl};
2
3use crate::{
4    foundation::{NSArray, NSBundle, NSData, NSDictionary, NSString, NSURL},
5    object,
6    objective_c_runtime::{
7        id, nil,
8        traits::{FromId, PNSObject},
9    },
10    utils::to_bool,
11};
12
13use super::interface_impl;
14
15pub type NSNibName = NSString;
16
17object! {
18    /// An object that manages an app’s menus.
19    unsafe pub struct NSNib;
20}
21
22#[interface_impl(NSObject)]
23impl NSNib {
24    /* Initializing a Nib
25     */
26
27    /// Returns an [`NSNib`] object initialized to the nib file at the specified URL.
28    #[deprecated = "use [`init_with_nib_data_bundle`] instead."]
29    #[method]
30    pub fn init_with_contents_of_url(&mut self, nib_file_url: NSURL) -> id {
31        unsafe { msg_send![self.m_self(), initWithContentsOfURL: nib_file_url] }
32    }
33
34    /// Returns an [`NSNib`] object initialized to the nib file in the specified bundle.
35    #[method]
36    pub fn init_with_nib_named_bundle(&mut self, nib_name: NSNibName, bundle: &NSBundle) -> Self
37    where
38        Self: Sized + FromId,
39    {
40        unsafe {
41            Self::from_id(
42                msg_send![self.m_self(), initWithNibNamed: nib_name bundle: bundle.m_self()],
43            )
44        }
45    }
46
47    /// Initializes an instance with nib data and specified bundle for locating resources.
48    #[method]
49    pub fn init_with_nib_data_bundle(&mut self, nib_data: &NSData, bundle: &NSBundle) -> Self
50    where
51        Self: Sized + FromId,
52    {
53        unsafe {
54            Self::from_id(
55                msg_send![self.m_self(), initWithNibData: nib_data.m_self() bundle: bundle.m_self()],
56            )
57        }
58    }
59
60    /* Instantiating a Nib
61     */
62
63    /// Unarchives and instantiates the in-memory contents of the receiver's nib file, creating a distinct object tree and set of top level objects.
64    #[deprecated = "use [`instantiate_with_owner_top_level_objects`] instead."]
65    #[method]
66    pub fn instantiate_nib_with_owner_top_level_objects<T>(
67        &mut self,
68        owner: id,
69        top_lvl_objects: &NSArray<T>,
70    ) -> bool {
71        unsafe {
72            to_bool(
73                msg_send![self.m_self(), instantiateNibWithOwner: owner topLevelObjects: top_lvl_objects.m_self()],
74            )
75        }
76    }
77
78    /// Unarchives and instantiates the in-memory contents of the receiver's nib file, creating a distinct object tree and top level objects.
79    #[deprecated = "use [`instantiate_with_owner_top_level_objects`] instead."]
80    #[method]
81    pub fn instantiate_nib_with_external_name_table<K, V>(
82        &self,
83        external_name_table: &NSDictionary<K, V>,
84    ) -> bool {
85        unsafe {
86            to_bool(
87                msg_send![self.m_self(), instantiateNibWithExternalNameTable: external_name_table.m_self()],
88            )
89        }
90    }
91
92    /// Instantiates objects in the nib file with the specified owner.
93    #[method]
94    pub fn instantiate_with_owner_top_level_objects<T>(
95        &self,
96        owner: id,
97        top_lvl_objects: Option<NSArray<T>>,
98    ) {
99        unsafe {
100            let top_lvl_objects = match top_lvl_objects {
101                Some(value) => value.m_self(),
102                None => nil,
103            };
104
105            msg_send![self.m_self(), instantiateWithOwner: owner topLevelObjects: top_lvl_objects]
106        }
107    }
108}
109
110extern "C" {
111    /*Nib Loading Keys
112     */
113
114    /// The external object that is responsible for the instantiated nib.
115    #[deprecated]
116    pub static NSNibOwner: NSString;
117
118    /// An NSMutableArray object that, if present, is populated with the top-level objects of the newly instantiated nib.
119    #[deprecated]
120    pub static NSNibTopLevelObjects: NSString;
121}