1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//! A `WindowController` is useful for handling certain document patterns on macOS.
//!
//! (iOS has no equivalent, as `UIWindowController` is private there).
//!
//! In particular, this is useful for certain situations regarding document handling
//! (which this framework does not yet cover, but may eventually). Note that this control can only
//! be created by providing a `WindowDelegate`.
//!
//! >If your application only uses a single `Window`, you may not even need this - just set the
//! autosave name on your `Window` to get the benefit of cached window location across restarts.
//!
//! # How to use
//!
//! ```rust,no_run
//! use cacao::appkit::AppDelegate;
//! use cacao::appkit::window::{WindowController, WindowDelegate};
//!
//! #[derive(Default)]
//! struct MyWindow;
//!
//! impl WindowDelegate for MyWindow {
//!     const NAME: &'static str = "RootView";
//!     // Your implementation here...
//! }
//!
//! struct MyApp {
//!     pub window: WindowController<MyWindow>
//! }
//! ```

use std::fmt;

use objc::runtime::Object;
use objc::{msg_send, sel, sel_impl};
use objc_id::Id;

use crate::appkit::window::{Window, WindowConfig, WindowDelegate, WINDOW_DELEGATE_PTR};
use crate::foundation::{id, nil};
use crate::utils::Controller;

mod class;
use class::register_window_controller_class;

/// A `WindowController` wraps your `WindowDelegate` into an underlying `Window`, and
/// provides some extra lifecycle methods.
pub struct WindowController<T> {
    /// A handler to the underlying `NSWindowController`.
    pub objc: Id<Object>,

    /// The underlying `Window` that this controller wraps.
    pub window: Window<T>
}

impl<T> WindowController<T>
where
    T: WindowDelegate + 'static
{
    /// Allocates and configures an `NSWindowController` in the Objective-C/Cocoa runtime that maps over
    /// to your supplied delegate.
    pub fn with(config: WindowConfig, delegate: T) -> Self {
        let window = Window::with(config, delegate);

        let objc = unsafe {
            let window_controller_class = register_window_controller_class::<T>();
            let controller_alloc: id = msg_send![window_controller_class, alloc];
            let controller: id = msg_send![controller_alloc, initWithWindow:&*window.objc];

            if let Some(delegate) = &window.delegate {
                let ptr: *const T = &**delegate;
                (&mut *controller).set_ivar(WINDOW_DELEGATE_PTR, ptr as usize);
            }

            Id::from_ptr(controller)
        };

        WindowController { objc, window }
    }

    /// Given a view, sets it as the content view controller for this window.
    pub fn set_content_view_controller<C: Controller + 'static>(&self, controller: &C) {
        let backing_node = controller.get_backing_node();

        unsafe {
            let _: () = msg_send![&*self.objc, setContentViewController:&*backing_node];
        }
    }

    /// Shows the window, running a configuration pass if necessary.
    pub fn show(&self) {
        unsafe {
            let _: () = msg_send![&*self.objc, showWindow: nil];
        }
    }

    /// Closes the window.
    pub fn close(&self) {
        unsafe {
            let _: () = msg_send![&*self.objc, close];
        }
    }
}

impl<T> fmt::Debug for WindowController<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("WindowController").field("objc", &self.objc).finish()
    }
}