rust_macios/appkit/
ns_pasteboard.rs

1use objc::{msg_send, runtime::Class, sel, sel_impl};
2
3use super::{interface_impl, ns_pasteboard_item::NSPasteboardItem};
4
5use crate::{
6    foundation::{Int, NSArray, NSData, NSDictionary, NSFileWrapper, NSString, UInt},
7    object,
8    objective_c_runtime::{
9        id,
10        traits::{FromId, PNSObject},
11    },
12    utils::{to_bool, to_optional},
13};
14
15pub type NSPasteboardType = NSString;
16
17extern "C" {
18    /* Pasteboard Types
19     */
20
21    /// URL data for one file or resource.
22    pub static NSPasteboardTypeURL: NSPasteboardType;
23
24    /// Color data.
25    pub static NSPasteboardTypeColor: NSPasteboardType;
26
27    /// A representation of a file’s contents.
28    pub static NSFileContentsPboardType: NSPasteboardType;
29
30    /// A file URL.
31    pub static NSPasteboardTypeFileURL: NSPasteboardType;
32
33    /// Type for the find panel metadata property list.
34    pub static NSFindPanelSearchOptionsPboardType: NSPasteboardType;
35
36    /// Font and character information.
37    pub static NSPasteboardTypeFont: NSPasteboardType;
38
39    /// Type for HTML content.
40    pub static NSPasteboardTypeHTML: NSPasteboardType;
41
42    /// Multiple text selection.
43    pub static NSPasteboardTypeMultipleTextSelection: NSPasteboardType;
44
45    /// PDF data.
46    pub static NSPasteboardTypePDF: NSPasteboardType;
47
48    /// PNG image data.
49    pub static NSPasteboardTypePNG: NSPasteboardType;
50
51    /// Rich Text Format (RTF) data.
52    pub static NSPasteboardTypeRTF: NSPasteboardType;
53
54    /// RTFD formatted file contents.
55    pub static NSPasteboardTypeRTFD: NSPasteboardType;
56
57    /// Paragraph formatting information.
58    pub static NSPasteboardTypeRuler: NSPasteboardType;
59
60    /// Sound data.
61    pub static NSPasteboardTypeSound: NSPasteboardType;
62
63    /// String data.
64    pub static NSPasteboardTypeString: NSPasteboardType;
65
66    /// Tab-separated fields of text.
67    pub static NSPasteboardTypeTabularText: NSPasteboardType;
68
69    /// Type for the Find panel metadata property list.
70    pub static NSPasteboardTypeTextFinderOptions: NSPasteboardType;
71
72    /// Tag Image File Format (TIFF) data.
73    pub static NSPasteboardTypeTIFF: NSPasteboardType;
74
75}
76
77/// Search options for the find panel.
78pub type NSPasteboardTypeFindPanelSearchOptionKey = NSString;
79
80extern "C" {
81    /// A Boolean value indicating whether the search is case-insensitive.
82    pub static NSFindPanelCaseInsensitiveSearch: NSPasteboardTypeFindPanelSearchOptionKey;
83
84    /// A number object containing the match type to use in the find panel.
85    pub static NSFindPanelSubstringMatch: NSPasteboardTypeFindPanelSearchOptionKey;
86}
87
88/// Search options for text in Finder.
89pub type NSPasteboardTypeTextFinderOptionKey = NSString;
90
91extern "C" {
92    /// A Boolean value indicating whether the search is case insensitive.
93    pub static NSTextFinderCaseInsensitiveKey: NSPasteboardTypeTextFinderOptionKey;
94
95    /// A number object containing the match type to use.
96    pub static NSTextFinderMatchingTypeKey: NSPasteboardTypeTextFinderOptionKey;
97}
98
99/// Constants that represent the standard pasteboard names.
100pub type NSPasteboardName = NSString;
101
102extern "C" {
103    /* Named Pasteboards
104     */
105
106    /// The pasteboard that stores data to move as the result of a drag operation.
107    pub static NSPasteboardNameDrag: NSPasteboardName;
108
109    /// The pasteboard that holds information about the current state of the active application’s find panel.
110    pub static NSPasteboardNameFind: NSPasteboardName;
111
112    /// The pasteboard that holds font and character information and supports Copy Font and Paste Font commands that the text editor may implement.
113    pub static NSPasteboardNameFont: NSPasteboardName;
114
115    /// The pasteboard you use to perform ordinary cut, copy, and paste operations.
116    pub static NSNSPasteboardNameGeneral: NSPasteboardName;
117
118    /// The pasteboard that holds information about paragraph formats and supports the Copy Ruler and Paste Ruler commands that the text editor may implement.
119    pub static NSPasteboardNameRuler: NSPasteboardName;
120}
121
122/// Options for reading pasteboard data.
123pub type NSPasteboardReadingOptionKey = NSString;
124
125extern "C" {
126    /// Option for reading URLs to restrict the results to URLs with contents that conform to any of the provided UTI types.
127    pub static NSPasteboardURLReadingContentsConformToTypesKey: NSPasteboardReadingOptionKey;
128
129    pub static staticNSPasteboardURLReadingFileURLsOnlyKey: NSPasteboardReadingOptionKey;
130}
131
132/// Options that specify how to interpret data on the pasteboard when initializing pasteboard data.
133#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
134#[repr(u64)]
135pub enum NSPasteboardReadingOptions {
136    AsData = 0,
137    AsString = 1 << 0,
138    AsPropertyList = 1 << 1,
139    AsKeyedArchive = 1 << 2,
140}
141
142/// Options for preparing the pasteboard.
143#[repr(u32)]
144pub enum NSPasteboardContentsOptions {
145    CurrentHostOnly = 1,
146}
147
148object! {
149    /// An object that transfers data to and from the pasteboard server.
150    unsafe pub struct NSPasteboard;
151}
152
153#[interface_impl(NSObject)]
154impl NSPasteboard {
155    /* Creating and Releasing a Pasteboard
156     */
157
158    /// The shared pasteboard object to use for general content.
159    #[property]
160    pub fn general_pasteboard() -> NSPasteboard {
161        unsafe { NSPasteboard::from_id(msg_send![Self::m_class(), generalPasteboard]) }
162    }
163
164    /// Creates a new pasteboard object that supplies the specified data in as many types as possible based on the available filter services.
165    #[method]
166    pub fn pasteboard_by_filtering_data_of_type(
167        data: NSData,
168        r#type: NSPasteboardType,
169    ) -> NSPasteboard {
170        unsafe {
171            NSPasteboard::from_id(
172                msg_send![Self::m_class(), pasteboardByFilteringData: data ofType: r#type],
173            )
174        }
175    }
176
177    /// Creates a new pasteboard object that supplies the specified file in as many types as possible based on the available filter services.
178    #[method]
179    pub fn pasteboard_by_filtering_file(filename: NSString) -> NSPasteboard {
180        unsafe {
181            NSPasteboard::from_id(msg_send![
182                Self::m_class(),
183                pasteboardByFilteringFile: filename
184            ])
185        }
186    }
187
188    /// Creates a new pasteboard object that supplies the specified pasteboard data in as many types as possible based on the available filter services.
189    #[method]
190    pub fn pasteboard_by_filtering_types_in_pasteboard(pboard: NSPasteboard) -> NSPasteboard {
191        unsafe {
192            NSPasteboard::from_id(msg_send![
193                Self::m_class(),
194                pasteboardByFilteringTypesInPasteboard: pboard
195            ])
196        }
197    }
198
199    /// Returns the pasteboard with the specified name.
200    #[method]
201    pub fn pasteboard_with_name(name: NSPasteboardName) -> NSPasteboard {
202        unsafe { NSPasteboard::from_id(msg_send![Self::m_class(), pasteboardWithName: name]) }
203    }
204
205    /// Creates and returns a new pasteboard with a name that is guaranteed to be unique with respect to other pasteboards in the system.
206    #[method]
207    pub fn pasteboard_with_unique_name() -> NSPasteboard {
208        unsafe { NSPasteboard::from_id(msg_send![Self::m_class(), pasteboardWithUniqueName]) }
209    }
210
211    /// Releases the receiver’s resources in the pasteboard server.
212    #[method]
213    pub fn release_globally(&self) {
214        unsafe { msg_send![self.m_self(), releaseGlobally] }
215    }
216
217    /* Writing Data
218     */
219
220    /// Clears the existing contents of the pasteboard.
221    #[method]
222    pub fn clear_contents(&mut self) -> Int {
223        unsafe { msg_send![self.m_self(), clearContents] }
224    }
225
226    /// Writes an array of objects to the receiver.
227    #[method]
228    pub fn write_objects(&mut self, objects: NSArray<id>) -> bool {
229        unsafe { to_bool(msg_send![self.m_self(), writeObjects: objects]) }
230    }
231
232    /// Sets the data as the representation for the specified type for the first item on the receiver.
233    #[method]
234    pub fn set_data_for_type(&mut self, data: NSData, data_type: NSPasteboardType) -> bool {
235        unsafe { to_bool(msg_send![self.m_self(), setData: data forType: data_type]) }
236    }
237
238    /// Sets the given property list as the representation for the specified type for the first item on the receiver.
239    #[method]
240    pub fn set_property_list_for_type(&mut self, plist: id, data_type: NSPasteboardType) -> bool {
241        unsafe { to_bool(msg_send![self.m_self(), setPropertyList: plist forType: data_type]) }
242    }
243
244    /// Sets the given string as the representation for the specified type for the first item on the receiver.
245    #[method]
246    pub fn set_string_for_type(&mut self, string: NSString, data_type: NSPasteboardType) -> bool {
247        unsafe { to_bool(msg_send![self.m_self(), setString: string forType: data_type]) }
248    }
249
250    /* Reading Data
251     */
252
253    /// Reads from the receiver objects that best match the specified array of classes.
254    #[method]
255    pub fn read_objects_for_classes_options(
256        &self,
257        class_array: NSArray<Class>,
258        options: NSDictionary<NSPasteboardReadingOptionKey, id>,
259    ) -> Option<NSArray<id>> {
260        unsafe {
261            to_optional(
262                msg_send![self.m_self(), readObjectsForClasses: class_array options: options],
263            )
264        }
265    }
266
267    /// An array that contains all the items held by the pasteboard.
268    #[property]
269    pub fn pasteboard_items(&self) -> Option<NSArray<NSPasteboardItem>> {
270        unsafe { to_optional(msg_send![self.m_self(), pasteboardItems]) }
271    }
272
273    /// Returns the index of the specified pasteboard item.
274    #[method]
275    pub fn index_of_pasteboard_item(&self, pasteboard_item: NSPasteboardItem) -> UInt {
276        unsafe { msg_send![self.m_self(), indexOfPasteboardItem: pasteboard_item] }
277    }
278
279    /// Returns the data for the specified type from the first item in the receiver that contains the type.
280    #[method]
281    pub fn data_for_type(&self, data_type: NSPasteboardType) -> Option<NSData> {
282        unsafe { to_optional(msg_send![self.m_self(), dataForType: data_type]) }
283    }
284
285    /// Returns the property list for the specified type from the first item in the receiver that contains the type.
286    #[method]
287    pub fn property_list_for_type(&self, data_type: NSPasteboardType) -> Option<id> {
288        unsafe {
289            let ptr: id = msg_send![self.m_self(), propertyListForType: data_type];
290
291            if ptr.is_null() {
292                None
293            } else {
294                Some(ptr)
295            }
296        }
297    }
298
299    /// Returns a concatenation of the strings for the specified type from all the items in the receiver that contain the type.
300    #[method]
301    pub fn string_for_type(&self, data_type: NSPasteboardType) -> Option<NSString> {
302        unsafe { to_optional(msg_send![self.m_self(), stringForType: data_type]) }
303    }
304
305    /// Scans the specified types for a type that the receiver supports.
306    #[method]
307    pub fn available_type_from_array(
308        &self,
309        types: NSArray<NSPasteboardType>,
310    ) -> Option<NSPasteboardType> {
311        unsafe { to_optional(msg_send![self.m_self(), availableTypeFromArray: types]) }
312    }
313
314    /// Returns a Boolean value that indicates whether the receiver contains any items that conform to the specified UTIs.
315    #[method]
316    pub fn can_read_item_with_data_conforming_to_types(&self, types: NSArray<NSString>) -> bool {
317        unsafe {
318            to_bool(msg_send![
319                self.m_self(),
320                canReadItemWithDataConformingToTypes: types
321            ])
322        }
323    }
324
325    /// 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.
326    #[method]
327    pub fn can_read_object_for_classes_options(
328        &self,
329        class_array: NSArray<Class>,
330        options: NSDictionary<NSPasteboardReadingOptionKey, id>,
331    ) -> bool {
332        unsafe {
333            to_bool(msg_send![self.m_self(), canReadObjectForClasses: class_array options: options])
334        }
335    }
336
337    /// An array of the receiver’s supported data types.
338    #[property]
339    pub fn types(&self) -> Option<NSArray<NSPasteboardType>> {
340        unsafe { to_optional(msg_send![self.m_self(), types]) }
341    }
342
343    /// Returns the data types that can be converted to the specified type using the available filter services.
344    #[method]
345    pub fn types_filterable_to(r#type: NSPasteboardType) -> NSArray<NSPasteboardType> {
346        unsafe { NSArray::from_id(msg_send![Self::m_class(), typesFilterableTo: r#type]) }
347    }
348
349    /* Preparing the Pasteboard for Content
350     */
351
352    /// Prepares the pasteboard to receive new contents, removing the existing pasteboard contents.
353    #[method]
354    pub fn prepare_for_new_contents_with_options(
355        &self,
356        options: NSPasteboardContentsOptions,
357    ) -> Int {
358        unsafe { msg_send![self.m_self(), prepareForNewContentsWithOptions: options] }
359    }
360
361    /* Getting Information about a Pasteboard
362     */
363
364    /// The receiver’s name.
365    #[property]
366    pub fn name(&self) -> NSPasteboardName {
367        unsafe { NSPasteboardName::from_id(msg_send![self.m_self(), name]) }
368    }
369
370    /// The receiver’s change count.
371    #[property]
372    pub fn change_count(&self) -> Int {
373        unsafe { msg_send![self.m_self(), changeCount] }
374    }
375
376    /*  Writing Data (macOS 10.5 and Earlier)
377     */
378
379    /// Prepares the receiver for a change in its contents by declaring the new types of data it will contain and a new owner.
380    #[method]
381    pub fn declare_types_owner(&self, new_types: NSArray<NSPasteboardType>, new_owner: id) -> Int {
382        unsafe { msg_send![self.m_self(), declareTypes: new_types owner: new_owner ] }
383    }
384
385    /// Adds promises for the specified types to the first pasteboard item.
386    #[method]
387    pub fn add_types_owner(&mut self, new_types: NSArray<NSPasteboardType>, new_owner: id) -> Int {
388        unsafe { msg_send![self.m_self(), addTypes: new_types owner: new_owner] }
389    }
390
391    /// Writes the contents of the specified file to the pasteboard.
392    #[method]
393    pub fn write_file_contents(&mut self, filename: NSString) -> bool {
394        unsafe { to_bool(msg_send![self.m_self(), writeFileContents: filename]) }
395    }
396
397    /// Writes the serialized contents of the specified file wrapper to the pasteboard.
398    #[method]
399    pub fn write_file_wrapper(&mut self, wrapper: NSFileWrapper) -> bool {
400        unsafe { to_bool(msg_send![self.m_self(), writeFileWrapper: wrapper]) }
401    }
402
403    /* Reading Data (macOS 10.5 and Earlier)
404     */
405
406    /// Reads data representing a file’s contents from the receiver and writes it to the specified file.
407    #[method]
408    pub fn read_file_contents_type_to_file(
409        &self,
410        r#type: NSPasteboardType,
411        filename: NSString,
412    ) -> Option<NSString> {
413        unsafe {
414            to_optional(msg_send![
415                self.m_self(),
416                readFileContentsType: r#type
417                toFile: filename
418            ])
419        }
420    }
421
422    /// Reads data representing a file’s contents from the receiver and returns it as a file wrapper.
423    #[method]
424    pub fn read_file_wrapper(&self) -> Option<NSFileWrapper> {
425        unsafe { to_optional(msg_send![self.m_self(), readFileWrapper]) }
426    }
427}