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
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;
pub struct WindowController<T> {
pub objc: Id<Object>,
pub window: Window<T>
}
impl<T> WindowController<T>
where
T: WindowDelegate + 'static
{
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 }
}
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];
}
}
pub fn show(&self) {
unsafe {
let _: () = msg_send![&*self.objc, showWindow: nil];
}
}
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()
}
}