@@ -8,17 +8,17 @@ the Apache 2.0 or the MIT license at the licensee's choice. The terms
8
8
and conditions of the chosen license apply to this file.
9
9
*/
10
10
11
- use crate :: common:: Error ;
12
11
#[ cfg( feature = "image-data" ) ]
13
12
use crate :: common:: ImageData ;
13
+ use crate :: common:: { private, Error } ;
14
14
use objc2:: {
15
15
msg_send_id,
16
16
rc:: { autoreleasepool, Id } ,
17
17
runtime:: ProtocolObject ,
18
18
ClassType ,
19
19
} ;
20
20
use objc2_app_kit:: { NSPasteboard , NSPasteboardTypeHTML , NSPasteboardTypeString } ;
21
- use objc2_foundation:: { NSArray , NSString } ;
21
+ use objc2_foundation:: { ns_string , NSArray , NSString } ;
22
22
use std:: {
23
23
borrow:: Cow ,
24
24
panic:: { RefUnwindSafe , UnwindSafe } ,
@@ -235,11 +235,12 @@ impl<'clipboard> Get<'clipboard> {
235
235
236
236
pub ( crate ) struct Set < ' clipboard > {
237
237
clipboard : & ' clipboard mut Clipboard ,
238
+ exclude_from_history : bool ,
238
239
}
239
240
240
241
impl < ' clipboard > Set < ' clipboard > {
241
242
pub ( crate ) fn new ( clipboard : & ' clipboard mut Clipboard ) -> Self {
242
- Self { clipboard }
243
+ Self { clipboard, exclude_from_history : false }
243
244
}
244
245
245
246
pub ( crate ) fn text ( self , data : Cow < ' _ , str > ) -> Result < ( ) , Error > {
@@ -248,6 +249,9 @@ impl<'clipboard> Set<'clipboard> {
248
249
let string_array =
249
250
NSArray :: from_vec ( vec ! [ ProtocolObject :: from_id( NSString :: from_str( & data) ) ] ) ;
250
251
let success = unsafe { self . clipboard . pasteboard . writeObjects ( & string_array) } ;
252
+
253
+ add_clipboard_exclusions ( self . clipboard , self . exclude_from_history ) ;
254
+
251
255
if success {
252
256
Ok ( ( ) )
253
257
} else {
@@ -279,6 +283,9 @@ impl<'clipboard> Set<'clipboard> {
279
283
} ;
280
284
}
281
285
}
286
+
287
+ add_clipboard_exclusions ( self . clipboard , self . exclude_from_history ) ;
288
+
282
289
if success {
283
290
Ok ( ( ) )
284
291
} else {
@@ -296,6 +303,9 @@ impl<'clipboard> Set<'clipboard> {
296
303
297
304
let image_array = NSArray :: from_vec ( vec ! [ ProtocolObject :: from_id( image) ] ) ;
298
305
let success = unsafe { self . clipboard . pasteboard . writeObjects ( & image_array) } ;
306
+
307
+ add_clipboard_exclusions ( self . clipboard , self . exclude_from_history ) ;
308
+
299
309
if success {
300
310
Ok ( ( ) )
301
311
} else {
@@ -322,3 +332,33 @@ impl<'clipboard> Clear<'clipboard> {
322
332
Ok ( ( ) )
323
333
}
324
334
}
335
+
336
+ fn add_clipboard_exclusions ( clipboard : & mut Clipboard , exclude_from_history : bool ) {
337
+ // On Mac there isn't an official standard for excluding data from clipboard, however
338
+ // there is an unofficial standard which is to set `org.nspasteboard.ConcealedType`.
339
+ //
340
+ // See http://nspasteboard.org/ for details about the community standard.
341
+ if exclude_from_history {
342
+ unsafe {
343
+ clipboard
344
+ . pasteboard
345
+ . setString_forType ( ns_string ! ( "" ) , ns_string ! ( "org.nspasteboard.ConcealedType" ) ) ;
346
+ }
347
+ }
348
+ }
349
+
350
+ /// Apple-specific extensions to the [`Set`](crate::Set) builder.
351
+ pub trait SetExtApple : private:: Sealed {
352
+ /// Excludes the data which will be set on the clipboard from being added to
353
+ /// third party clipboard history software.
354
+ ///
355
+ /// See http://nspasteboard.org/ for details about the community standard.
356
+ fn exclude_from_history ( self ) -> Self ;
357
+ }
358
+
359
+ impl SetExtApple for crate :: Set < ' _ > {
360
+ fn exclude_from_history ( mut self ) -> Self {
361
+ self . platform . exclude_from_history = true ;
362
+ self
363
+ }
364
+ }
0 commit comments