rust_macios/appkit/
ns_view_controller.rs

1use bitflags::bitflags;
2use objc::{class, msg_send, sel, sel_impl};
3
4use crate::{
5    foundation::{NSBundle, UInt},
6    object,
7    objective_c_runtime::{id, traits::FromId},
8};
9
10use super::{interface_impl, INSResponder, INSView, NSNibName, NSView};
11
12bitflags! {
13    pub struct NSViewControllerTransitionOptions: UInt {
14        const NONE = 0x0;
15        const CROSSFADE = 0x1;
16        const SLIDE_UP = 0x10;
17        const SLIDE_DOWN = 0x20;
18        const SLIDE_LEFT = 0x40;
19        const SLIDE_RIGHT = 0x80;
20        const SLIDE_FORWARD = 0x140;
21        const SLIDE_BACKWARD = 0x180;
22        const ALLOW_USER_INTERACTION = 0x1000;
23    }
24}
25
26object! {
27    /// A controller that manages a view, typically loaded from a nib file.
28    unsafe pub struct NSViewController;
29}
30
31impl NSViewController {
32    /// Creates a new view controller.
33    pub fn new() -> Self {
34        unsafe { Self::from_id(msg_send![class!(NSViewController), new]) }
35    }
36}
37
38impl Default for NSViewController {
39    fn default() -> Self {
40        Self::new()
41    }
42}
43
44impl INSResponder for NSViewController {}
45
46#[interface_impl(NSResponder)]
47impl NSViewController {
48    /* Creating A View Controller
49     */
50
51    /// Returns a view controller object initialized to the nib file in the specified bundle.
52    #[method]
53    pub fn init_with_nib_name_bundle(nib_name: NSNibName, bundle: NSBundle) -> Self
54    where
55        Self: Sized + FromId,
56    {
57        unsafe {
58            let obj: id = msg_send![Self::m_class(), alloc];
59            Self::from_id(msg_send![obj, initWithNibName: nib_name bundle: bundle])
60        }
61    }
62
63    /// Instantiates a view from a nib file and sets the value of the view property.
64    #[method]
65    pub fn load_view(&self) {
66        unsafe { msg_send![self.m_self(), loadView] }
67    }
68
69    /* Represented Object
70     */
71
72    /// Returns the represented object of the view controller.
73    #[property]
74    pub fn represented_object(&self) -> id {
75        unsafe { msg_send![self.m_self(), representedObject] }
76    }
77
78    /* Nib Properties
79     */
80
81    /// The nib bundle to be loaded to instantiate the receiver’s primary view.
82    #[property]
83    pub fn nib_bundle(&self) -> NSBundle {
84        unsafe { NSBundle::from_id(msg_send![self.m_self(), nibBundle]) }
85    }
86
87    /// The name of the nib file to be loaded to instantiate the receiver’s primary view.
88    #[property]
89    pub fn nib_name(&self) -> NSNibName {
90        unsafe { NSNibName::from_id(msg_send![self.m_self(), nibName]) }
91    }
92
93    /* View Properties
94     */
95
96    /// The view controller’s primary view.
97    #[property]
98    pub fn view(&self) -> NSView {
99        unsafe { NSView::from_id(msg_send![self.m_self(), view]) }
100    }
101
102    /// Sets the view controller’s primary view.
103    ///
104    /// # Arguments
105    ///
106    /// * `view` - The view to be set as the primary view.
107    #[property]
108    pub fn set_view<V>(&self, view: V)
109    where
110        V: INSView,
111    {
112        unsafe { msg_send![self.m_self(), setView: view] }
113    }
114
115    /// The localized title of the receiver’s primary view.
116    #[property]
117    pub fn title(&self) -> id {
118        unsafe { msg_send![self.m_self(), title] }
119    }
120
121    /// Sets the localized title of the receiver’s primary view.
122    ///
123    /// # Arguments
124    ///
125    /// * `title` - The title to be set as the primary view.
126    #[property]
127    pub fn set_title(&self, title: id) {
128        unsafe { msg_send![self.m_self(), setTitle: title] }
129    }
130
131    /* Responding to View Events
132     */
133
134    /// Called after the view controller’s view has been loaded into memory.
135    #[property]
136    pub fn view_did_load(&self) {
137        unsafe { msg_send![self.m_self(), viewDidLoad] }
138    }
139}