rust-macios 0.4.2

Apple Frameworks for Rust
Documentation
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use objc::{msg_send, runtime::Class, sel, sel_impl};

use super::{interface_impl, ns_pasteboard_item::NSPasteboardItem};

use crate::{
    foundation::{Int, NSArray, NSData, NSDictionary, NSFileWrapper, NSString, UInt},
    object,
    objective_c_runtime::{
        id,
        traits::{FromId, PNSObject},
    },
    utils::{to_bool, to_optional},
};

pub type NSPasteboardType = NSString;

extern "C" {
    /* Pasteboard Types
     */

    /// URL data for one file or resource.
    pub static NSPasteboardTypeURL: NSPasteboardType;

    /// Color data.
    pub static NSPasteboardTypeColor: NSPasteboardType;

    /// A representation of a file’s contents.
    pub static NSFileContentsPboardType: NSPasteboardType;

    /// A file URL.
    pub static NSPasteboardTypeFileURL: NSPasteboardType;

    /// Type for the find panel metadata property list.
    pub static NSFindPanelSearchOptionsPboardType: NSPasteboardType;

    /// Font and character information.
    pub static NSPasteboardTypeFont: NSPasteboardType;

    /// Type for HTML content.
    pub static NSPasteboardTypeHTML: NSPasteboardType;

    /// Multiple text selection.
    pub static NSPasteboardTypeMultipleTextSelection: NSPasteboardType;

    /// PDF data.
    pub static NSPasteboardTypePDF: NSPasteboardType;

    /// PNG image data.
    pub static NSPasteboardTypePNG: NSPasteboardType;

    /// Rich Text Format (RTF) data.
    pub static NSPasteboardTypeRTF: NSPasteboardType;

    /// RTFD formatted file contents.
    pub static NSPasteboardTypeRTFD: NSPasteboardType;

    /// Paragraph formatting information.
    pub static NSPasteboardTypeRuler: NSPasteboardType;

    /// Sound data.
    pub static NSPasteboardTypeSound: NSPasteboardType;

    /// String data.
    pub static NSPasteboardTypeString: NSPasteboardType;

    /// Tab-separated fields of text.
    pub static NSPasteboardTypeTabularText: NSPasteboardType;

    /// Type for the Find panel metadata property list.
    pub static NSPasteboardTypeTextFinderOptions: NSPasteboardType;

    /// Tag Image File Format (TIFF) data.
    pub static NSPasteboardTypeTIFF: NSPasteboardType;

}

/// Search options for the find panel.
pub type NSPasteboardTypeFindPanelSearchOptionKey = NSString;

extern "C" {
    /// A Boolean value indicating whether the search is case-insensitive.
    pub static NSFindPanelCaseInsensitiveSearch: NSPasteboardTypeFindPanelSearchOptionKey;

    /// A number object containing the match type to use in the find panel.
    pub static NSFindPanelSubstringMatch: NSPasteboardTypeFindPanelSearchOptionKey;
}

/// Search options for text in Finder.
pub type NSPasteboardTypeTextFinderOptionKey = NSString;

extern "C" {
    /// A Boolean value indicating whether the search is case insensitive.
    pub static NSTextFinderCaseInsensitiveKey: NSPasteboardTypeTextFinderOptionKey;

    /// A number object containing the match type to use.
    pub static NSTextFinderMatchingTypeKey: NSPasteboardTypeTextFinderOptionKey;
}

/// Constants that represent the standard pasteboard names.
pub type NSPasteboardName = NSString;

extern "C" {
    /* Named Pasteboards
     */

    /// The pasteboard that stores data to move as the result of a drag operation.
    pub static NSPasteboardNameDrag: NSPasteboardName;

    /// The pasteboard that holds information about the current state of the active application’s find panel.
    pub static NSPasteboardNameFind: NSPasteboardName;

    /// The pasteboard that holds font and character information and supports Copy Font and Paste Font commands that the text editor may implement.
    pub static NSPasteboardNameFont: NSPasteboardName;

    /// The pasteboard you use to perform ordinary cut, copy, and paste operations.
    pub static NSNSPasteboardNameGeneral: NSPasteboardName;

    /// The pasteboard that holds information about paragraph formats and supports the Copy Ruler and Paste Ruler commands that the text editor may implement.
    pub static NSPasteboardNameRuler: NSPasteboardName;
}

/// Options for reading pasteboard data.
pub type NSPasteboardReadingOptionKey = NSString;

extern "C" {
    /// Option for reading URLs to restrict the results to URLs with contents that conform to any of the provided UTI types.
    pub static NSPasteboardURLReadingContentsConformToTypesKey: NSPasteboardReadingOptionKey;

    pub static staticNSPasteboardURLReadingFileURLsOnlyKey: NSPasteboardReadingOptionKey;
}

/// Options that specify how to interpret data on the pasteboard when initializing pasteboard data.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u64)]
pub enum NSPasteboardReadingOptions {
    AsData = 0,
    AsString = 1 << 0,
    AsPropertyList = 1 << 1,
    AsKeyedArchive = 1 << 2,
}

/// Options for preparing the pasteboard.
#[repr(u32)]
pub enum NSPasteboardContentsOptions {
    CurrentHostOnly = 1,
}

object! {
    /// An object that transfers data to and from the pasteboard server.
    unsafe pub struct NSPasteboard;
}

#[interface_impl(NSObject)]
impl NSPasteboard {
    /* Creating and Releasing a Pasteboard
     */

    /// The shared pasteboard object to use for general content.
    #[property]
    pub fn general_pasteboard() -> NSPasteboard {
        unsafe { NSPasteboard::from_id(msg_send![Self::m_class(), generalPasteboard]) }
    }

    /// Creates a new pasteboard object that supplies the specified data in as many types as possible based on the available filter services.
    #[method]
    pub fn pasteboard_by_filtering_data_of_type(
        data: NSData,
        r#type: NSPasteboardType,
    ) -> NSPasteboard {
        unsafe {
            NSPasteboard::from_id(
                msg_send![Self::m_class(), pasteboardByFilteringData: data ofType: r#type],
            )
        }
    }

    /// Creates a new pasteboard object that supplies the specified file in as many types as possible based on the available filter services.
    #[method]
    pub fn pasteboard_by_filtering_file(filename: NSString) -> NSPasteboard {
        unsafe {
            NSPasteboard::from_id(msg_send![
                Self::m_class(),
                pasteboardByFilteringFile: filename
            ])
        }
    }

    /// Creates a new pasteboard object that supplies the specified pasteboard data in as many types as possible based on the available filter services.
    #[method]
    pub fn pasteboard_by_filtering_types_in_pasteboard(pboard: NSPasteboard) -> NSPasteboard {
        unsafe {
            NSPasteboard::from_id(msg_send![
                Self::m_class(),
                pasteboardByFilteringTypesInPasteboard: pboard
            ])
        }
    }

    /// Returns the pasteboard with the specified name.
    #[method]
    pub fn pasteboard_with_name(name: NSPasteboardName) -> NSPasteboard {
        unsafe { NSPasteboard::from_id(msg_send![Self::m_class(), pasteboardWithName: name]) }
    }

    /// Creates and returns a new pasteboard with a name that is guaranteed to be unique with respect to other pasteboards in the system.
    #[method]
    pub fn pasteboard_with_unique_name() -> NSPasteboard {
        unsafe { NSPasteboard::from_id(msg_send![Self::m_class(), pasteboardWithUniqueName]) }
    }

    /// Releases the receiver’s resources in the pasteboard server.
    #[method]
    pub fn release_globally(&self) {
        unsafe { msg_send![self.m_self(), releaseGlobally] }
    }

    /* Writing Data
     */

    /// Clears the existing contents of the pasteboard.
    #[method]
    pub fn clear_contents(&mut self) -> Int {
        unsafe { msg_send![self.m_self(), clearContents] }
    }

    /// Writes an array of objects to the receiver.
    #[method]
    pub fn write_objects(&mut self, objects: NSArray<id>) -> bool {
        unsafe { to_bool(msg_send![self.m_self(), writeObjects: objects]) }
    }

    /// Sets the data as the representation for the specified type for the first item on the receiver.
    #[method]
    pub fn set_data_for_type(&mut self, data: NSData, data_type: NSPasteboardType) -> bool {
        unsafe { to_bool(msg_send![self.m_self(), setData: data forType: data_type]) }
    }

    /// Sets the given property list as the representation for the specified type for the first item on the receiver.
    #[method]
    pub fn set_property_list_for_type(&mut self, plist: id, data_type: NSPasteboardType) -> bool {
        unsafe { to_bool(msg_send![self.m_self(), setPropertyList: plist forType: data_type]) }
    }

    /// Sets the given string as the representation for the specified type for the first item on the receiver.
    #[method]
    pub fn set_string_for_type(&mut self, string: NSString, data_type: NSPasteboardType) -> bool {
        unsafe { to_bool(msg_send![self.m_self(), setString: string forType: data_type]) }
    }

    /* Reading Data
     */

    /// Reads from the receiver objects that best match the specified array of classes.
    #[method]
    pub fn read_objects_for_classes_options(
        &self,
        class_array: NSArray<Class>,
        options: NSDictionary<NSPasteboardReadingOptionKey, id>,
    ) -> Option<NSArray<id>> {
        unsafe {
            to_optional(
                msg_send![self.m_self(), readObjectsForClasses: class_array options: options],
            )
        }
    }

    /// An array that contains all the items held by the pasteboard.
    #[property]
    pub fn pasteboard_items(&self) -> Option<NSArray<NSPasteboardItem>> {
        unsafe { to_optional(msg_send![self.m_self(), pasteboardItems]) }
    }

    /// Returns the index of the specified pasteboard item.
    #[method]
    pub fn index_of_pasteboard_item(&self, pasteboard_item: NSPasteboardItem) -> UInt {
        unsafe { msg_send![self.m_self(), indexOfPasteboardItem: pasteboard_item] }
    }

    /// Returns the data for the specified type from the first item in the receiver that contains the type.
    #[method]
    pub fn data_for_type(&self, data_type: NSPasteboardType) -> Option<NSData> {
        unsafe { to_optional(msg_send![self.m_self(), dataForType: data_type]) }
    }

    /// Returns the property list for the specified type from the first item in the receiver that contains the type.
    #[method]
    pub fn property_list_for_type(&self, data_type: NSPasteboardType) -> Option<id> {
        unsafe {
            let ptr: id = msg_send![self.m_self(), propertyListForType: data_type];

            if ptr.is_null() {
                None
            } else {
                Some(ptr)
            }
        }
    }

    /// Returns a concatenation of the strings for the specified type from all the items in the receiver that contain the type.
    #[method]
    pub fn string_for_type(&self, data_type: NSPasteboardType) -> Option<NSString> {
        unsafe { to_optional(msg_send![self.m_self(), stringForType: data_type]) }
    }

    /// Scans the specified types for a type that the receiver supports.
    #[method]
    pub fn available_type_from_array(
        &self,
        types: NSArray<NSPasteboardType>,
    ) -> Option<NSPasteboardType> {
        unsafe { to_optional(msg_send![self.m_self(), availableTypeFromArray: types]) }
    }

    /// Returns a Boolean value that indicates whether the receiver contains any items that conform to the specified UTIs.
    #[method]
    pub fn can_read_item_with_data_conforming_to_types(&self, types: NSArray<NSString>) -> bool {
        unsafe {
            to_bool(msg_send![
                self.m_self(),
                canReadItemWithDataConformingToTypes: types
            ])
        }
    }

    /// Returns a Boolean value that indicates whether the receiver contains any items that can be represented as an instance of any class in a given array.
    #[method]
    pub fn can_read_object_for_classes_options(
        &self,
        class_array: NSArray<Class>,
        options: NSDictionary<NSPasteboardReadingOptionKey, id>,
    ) -> bool {
        unsafe {
            to_bool(msg_send![self.m_self(), canReadObjectForClasses: class_array options: options])
        }
    }

    /// An array of the receiver’s supported data types.
    #[property]
    pub fn types(&self) -> Option<NSArray<NSPasteboardType>> {
        unsafe { to_optional(msg_send![self.m_self(), types]) }
    }

    /// Returns the data types that can be converted to the specified type using the available filter services.
    #[method]
    pub fn types_filterable_to(r#type: NSPasteboardType) -> NSArray<NSPasteboardType> {
        unsafe { NSArray::from_id(msg_send![Self::m_class(), typesFilterableTo: r#type]) }
    }

    /* Preparing the Pasteboard for Content
     */

    /// Prepares the pasteboard to receive new contents, removing the existing pasteboard contents.
    #[method]
    pub fn prepare_for_new_contents_with_options(
        &self,
        options: NSPasteboardContentsOptions,
    ) -> Int {
        unsafe { msg_send![self.m_self(), prepareForNewContentsWithOptions: options] }
    }

    /* Getting Information about a Pasteboard
     */

    /// The receiver’s name.
    #[property]
    pub fn name(&self) -> NSPasteboardName {
        unsafe { NSPasteboardName::from_id(msg_send![self.m_self(), name]) }
    }

    /// The receiver’s change count.
    #[property]
    pub fn change_count(&self) -> Int {
        unsafe { msg_send![self.m_self(), changeCount] }
    }

    /*  Writing Data (macOS 10.5 and Earlier)
     */

    /// Prepares the receiver for a change in its contents by declaring the new types of data it will contain and a new owner.
    #[method]
    pub fn declare_types_owner(&self, new_types: NSArray<NSPasteboardType>, new_owner: id) -> Int {
        unsafe { msg_send![self.m_self(), declareTypes: new_types owner: new_owner ] }
    }

    /// Adds promises for the specified types to the first pasteboard item.
    #[method]
    pub fn add_types_owner(&mut self, new_types: NSArray<NSPasteboardType>, new_owner: id) -> Int {
        unsafe { msg_send![self.m_self(), addTypes: new_types owner: new_owner] }
    }

    /// Writes the contents of the specified file to the pasteboard.
    #[method]
    pub fn write_file_contents(&mut self, filename: NSString) -> bool {
        unsafe { to_bool(msg_send![self.m_self(), writeFileContents: filename]) }
    }

    /// Writes the serialized contents of the specified file wrapper to the pasteboard.
    #[method]
    pub fn write_file_wrapper(&mut self, wrapper: NSFileWrapper) -> bool {
        unsafe { to_bool(msg_send![self.m_self(), writeFileWrapper: wrapper]) }
    }

    /* Reading Data (macOS 10.5 and Earlier)
     */

    /// Reads data representing a file’s contents from the receiver and writes it to the specified file.
    #[method]
    pub fn read_file_contents_type_to_file(
        &self,
        r#type: NSPasteboardType,
        filename: NSString,
    ) -> Option<NSString> {
        unsafe {
            to_optional(msg_send![
                self.m_self(),
                readFileContentsType: r#type
                toFile: filename
            ])
        }
    }

    /// Reads data representing a file’s contents from the receiver and returns it as a file wrapper.
    #[method]
    pub fn read_file_wrapper(&self) -> Option<NSFileWrapper> {
        unsafe { to_optional(msg_send![self.m_self(), readFileWrapper]) }
    }
}