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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use block2::RcBlock;
use core_graphics::{display::CGDirectDisplayID, window::CGWindowID};
use libc::pid_t;
use objc2::{extern_class, msg_send, msg_send_id, mutability::InteriorMutable, rc::Id, ClassType};
use objc2_foundation::{CGRect, NSArray, NSError, NSInteger, NSObject, NSObjectProtocol, NSString};

extern_class!(
    #[derive(Debug, PartialEq, Eq, Hash)]
    pub struct SCRunningApplication;

    unsafe impl ClassType for SCRunningApplication {
        type Super = NSObject;
        type Mutability = InteriorMutable;
    }
);

unsafe impl NSObjectProtocol for SCRunningApplication {}

impl SCRunningApplication {
    pub fn new() -> Id<Self> {
        unsafe { msg_send_id![SCRunningApplication::class(), new] }
    }

    pub fn bundle_identifier(&self) -> Id<NSString> {
        unsafe { msg_send_id![self, bundleIdentifier] }
    }

    pub fn application_name(&self) -> Id<NSString> {
        unsafe { msg_send_id![self, applicationName] }
    }

    pub fn process_id(&self) -> pid_t {
        unsafe { msg_send![self, processID] }
    }
}

extern_class!(
    #[derive(Debug, PartialEq, Eq, Hash)]
    pub struct SCWindow;

    unsafe impl ClassType for SCWindow {
        type Super = NSObject;
        type Mutability = InteriorMutable;
    }
);

unsafe impl NSObjectProtocol for SCWindow {}

impl SCWindow {
    pub fn new() -> Id<Self> {
        unsafe { msg_send_id![SCWindow::class(), new] }
    }

    pub fn window_id(&self) -> CGWindowID {
        unsafe { msg_send![self, windowID] }
    }

    pub fn frame(&self) -> CGRect {
        unsafe { msg_send![self, frame] }
    }

    pub fn title(&self) -> Option<Id<NSString>> {
        unsafe { msg_send_id![self, title] }
    }

    pub fn window_layer(&self) -> NSInteger {
        unsafe { msg_send![self, windowLayer] }
    }

    pub fn owning_application(&self) -> Option<Id<SCRunningApplication>> {
        unsafe { msg_send_id![self, owningApplication] }
    }

    pub fn on_screen(&self) -> bool {
        unsafe { msg_send![self, isOnScreen] }
    }

    pub fn active(&self) -> bool {
        unsafe { msg_send![self, isActive] }
    }
}

extern_class!(
    #[derive(Debug, PartialEq, Eq, Hash)]
    pub struct SCDisplay;

    unsafe impl ClassType for SCDisplay {
        type Super = NSObject;
        type Mutability = InteriorMutable;
    }
);

unsafe impl NSObjectProtocol for SCDisplay {}

impl SCDisplay {
    pub fn new() -> Id<Self> {
        unsafe { msg_send_id![SCDisplay::class(), new] }
    }

    pub fn display_id(&self) -> CGDirectDisplayID {
        unsafe { msg_send![self, displayID] }
    }

    pub fn width(&self) -> NSInteger {
        unsafe { msg_send![self, width] }
    }

    pub fn height(&self) -> NSInteger {
        unsafe { msg_send![self, height] }
    }

    pub fn frame(&self) -> CGRect {
        unsafe { msg_send![self, frame] }
    }
}

extern_class!(
    #[derive(Debug, PartialEq, Eq, Hash)]
    pub struct SCShareableContent;

    unsafe impl ClassType for SCShareableContent {
        type Super = NSObject;
        type Mutability = InteriorMutable;
    }
);

unsafe impl NSObjectProtocol for SCShareableContent {}

type CompletionHandler = RcBlock<dyn Fn(*mut SCShareableContent, *mut NSError)>;

impl SCShareableContent {
    pub fn new() -> Id<Self> {
        unsafe { msg_send_id![SCShareableContent::class(), new] }
    }

    fn new_completion_handler<F>(closure: F) -> CompletionHandler
    where
        F: Fn(Option<Id<SCShareableContent>>, Option<Id<NSError>>) + 'static,
    {
        RcBlock::new(move |sc: *mut Self, error: *mut NSError| {
            closure(
                if sc.is_null() {
                    None
                } else {
                    unsafe { Id::retain(sc) }
                },
                if error.is_null() {
                    None
                } else {
                    unsafe { Id::retain(error) }
                },
            );
        })
    }

    pub fn get_shareable_content_with_completion_closure<F>(closure: F)
    where
        F: Fn(Option<Id<SCShareableContent>>, Option<Id<NSError>>) + 'static,
    {
        let handler = Self::new_completion_handler(closure);
        unsafe { msg_send![class!(SCShareableContent), getShareableContentWithCompletionHandler: &*handler] }
    }

    pub fn get_shareable_content_excluding_desktop_windows<F>(exclude_desktop_windows: bool, on_screen_windows_only: bool, closure: F)
    where
        F: Fn(Option<Id<SCShareableContent>>, Option<Id<NSError>>) + 'static,
    {
        let handler = Self::new_completion_handler(closure);
        unsafe {
            msg_send![class!(SCShareableContent), getShareableContentExcludingDesktopWindows: exclude_desktop_windows onScreenWindowsOnly: on_screen_windows_only completionHandler: &*handler]
        }
    }

    pub fn get_shareable_content_excluding_desktop_windows_below_window<F>(exclude_desktop_windows: bool, window: &SCWindow, closure: F)
    where
        F: Fn(Option<Id<SCShareableContent>>, Option<Id<NSError>>) + 'static,
    {
        let handler = Self::new_completion_handler(closure);
        unsafe {
            msg_send![class!(SCShareableContent), getShareableContentExcludingDesktopWindows: exclude_desktop_windows onScreenWindowsOnlyBelowWindow: window completionHandler: &*handler]
        }
    }

    pub fn get_shareable_content_excluding_desktop_windows_above_window<F>(&self, exclude_desktop_windows: bool, window: &SCWindow, closure: F)
    where
        F: Fn(Option<Id<SCShareableContent>>, Option<Id<NSError>>) + 'static,
    {
        let handler = Self::new_completion_handler(closure);
        unsafe {
            msg_send![class!(SCShareableContent), getShareableContentExcludingDesktopWindows: exclude_desktop_windows onScreenWindowsOnlyAboveWindow: window completionHandler: &*handler]
        }
    }

    pub fn windows(&self) -> Id<NSArray<SCWindow>> {
        unsafe { msg_send_id![self, windows] }
    }

    pub fn displays(&self) -> Id<NSArray<SCDisplay>> {
        unsafe { msg_send_id![self, displays] }
    }

    pub fn applications(&self) -> Id<NSArray<SCRunningApplication>> {
        unsafe { msg_send_id![self, applications] }
    }
}