screencapturekit 9.0.1

Safe Rust bindings for Apple's ScreenCaptureKit framework - screen and audio capture on macOS
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
//! `SCContentSharingPicker` - UI for selecting content to share
//!
//! Available on macOS 14.0+.
//! Provides a system UI for users to select displays, windows, or applications to share.
//!
//! ## When to Use
//!
//! Use the content sharing picker when:
//! - You want users to choose what to capture via a native macOS UI
//! - You need consistent UX with other screen sharing apps
//! - You want to avoid manually listing and presenting content options
//!
//! ## APIs
//!
//! | Method | Returns | Use Case |
//! |--------|---------|----------|
//! | [`SCContentSharingPicker::show()`] | callback with [`SCPickerOutcome`] | Get filter + metadata (dimensions, picked content) |
//! | [`SCContentSharingPicker::show_filter()`] | callback with [`SCPickerFilterOutcome`] | Just get the filter |
//!
//! For async/await, use `AsyncSCContentSharingPicker` from the optional
//! `async_api` module.
//!
//! # Examples
//!
//! ## Callback API: Get filter with metadata
//! ```no_run
//! use screencapturekit::content_sharing_picker::*;
//! use screencapturekit::prelude::*;
//!
//! let config = SCContentSharingPickerConfiguration::new();
//! SCContentSharingPicker::show(&config, |outcome| {
//!     match outcome {
//!         SCPickerOutcome::Picked(result) => {
//!             let (width, height) = result.pixel_size();
//!             let filter = result.filter();
//!             println!("Selected content: {}x{}", width, height);
//!             // Create stream with the filter...
//!         }
//!         SCPickerOutcome::Cancelled => println!("Cancelled"),
//!         SCPickerOutcome::Error(e) => eprintln!("Error: {}", e),
//!     }
//! });
//! ```
//!
//! ## Async API
//! ```no_run
//! use screencapturekit::async_api::AsyncSCContentSharingPicker;
//! use screencapturekit::content_sharing_picker::*;
//!
//! async fn example() {
//!     let config = SCContentSharingPickerConfiguration::new();
//!     if let SCPickerOutcome::Picked(result) = AsyncSCContentSharingPicker::show(&config).await {
//!         let (width, height) = result.pixel_size();
//!         let filter = result.filter();
//!         println!("Selected: {}x{}", width, height);
//!     }
//! }
//! ```
//!
//! ## Configure Picker Modes
//! ```no_run
//! use screencapturekit::content_sharing_picker::*;
//!
//! let mut config = SCContentSharingPickerConfiguration::new();
//! // Only allow single display selection
//! config.set_allowed_picker_modes(&[SCContentSharingPickerMode::SingleDisplay]);
//! // Exclude specific apps from the picker
//! config.set_excluded_bundle_ids(&["com.apple.finder", "com.apple.dock"]);
//! ```

use crate::stream::content_filter::{SCContentFilter, SCShareableContentStyle};
use std::any::Any;
use std::collections::HashMap;
use std::ffi::c_void;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Mutex, PoisonError};

/// Represents the type of content selected in the picker
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SCPickedSource {
    /// A window was selected, with its title
    Window(String),
    /// A display was selected, with its ID
    Display(u32),
    /// An application was selected, with its name
    Application(String),
    /// No specific source identified
    Unknown,
}

/// Picker mode determines what content types can be selected
///
/// These modes can be combined to allow users to pick from different source types.
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum SCContentSharingPickerMode {
    /// Allow selection of a single window
    #[default]
    SingleWindow = 0,
    /// Allow selection of multiple windows
    MultipleWindows = 1,
    /// Allow selection of a single display/screen
    SingleDisplay = 2,
    /// Allow selection of a single application
    SingleApplication = 3,
    /// Allow selection of multiple applications
    MultipleApplications = 4,
}

/// Configuration for the content sharing picker
pub struct SCContentSharingPickerConfiguration {
    ptr: *const c_void,
}

impl SCContentSharingPickerConfiguration {
    /// # Panics
    ///
    /// Panics when run on macOS older than 14.0. Use [`Self::try_new`] when
    /// runtime availability is not already known.
    #[must_use]
    pub fn new() -> Self {
        Self::try_new().expect("SCContentSharingPicker requires macOS 14.0 or later")
    }

    /// Create a configuration when the picker is available on this system.
    #[must_use]
    pub fn try_new() -> Option<Self> {
        if !SCContentSharingPicker::is_available() {
            return None;
        }
        let ptr = unsafe { crate::ffi::sc_content_sharing_picker_configuration_create() };
        (!ptr.is_null()).then_some(Self { ptr })
    }

    /// Construct a configuration initialised with the system's default values
    /// (the equivalent of Apple's `SCContentSharingPicker.shared.defaultConfiguration`).
    ///
    /// Use this when you want "system defaults plus my one tweak" — call this
    /// to get the baseline, then mutate the fields you care about. Compared
    /// to [`SCContentSharingPickerConfiguration::new()`], which starts from a
    /// blank-slate `SCContentSharingPickerConfiguration()`, this preserves
    /// any system-wide picker preferences the OS applies to fresh
    /// configurations (e.g. allowed picker modes, default exclusion lists).
    ///
    /// # Panics
    ///
    /// Panics when run on macOS older than 14.0.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use screencapturekit::content_sharing_picker::*;
    ///
    /// // Start from the system defaults, then override only what you need.
    /// let mut config = SCContentSharingPickerConfiguration::default_from_system();
    /// config.set_excluded_bundle_ids(&["com.apple.dock"]);
    /// ```
    #[must_use]
    pub fn default_from_system() -> Self {
        assert!(
            SCContentSharingPicker::is_available(),
            "SCContentSharingPicker requires macOS 14.0 or later"
        );
        let ptr = unsafe { crate::ffi::sc_content_sharing_picker_create_default_configuration() };
        Self { ptr }
    }

    /// Set allowed picker modes
    pub fn set_allowed_picker_modes(&mut self, modes: &[SCContentSharingPickerMode]) {
        let mode_values: Vec<i32> = modes.iter().map(|m| *m as i32).collect();
        unsafe {
            crate::ffi::sc_content_sharing_picker_configuration_set_allowed_picker_modes(
                self.ptr,
                mode_values.as_ptr(),
                mode_values.len(),
            );
        }
    }

    /// Get the currently allowed picker modes.
    pub fn allowed_picker_modes(&self) -> Vec<SCContentSharingPickerMode> {
        let mask = unsafe {
            crate::ffi::sc_content_sharing_picker_configuration_get_allowed_picker_modes_mask(
                self.ptr,
            )
        };
        let mut modes = Vec::new();
        for (raw_value, mode) in [
            (1_u64, SCContentSharingPickerMode::SingleWindow),
            (2_u64, SCContentSharingPickerMode::MultipleWindows),
            (16_u64, SCContentSharingPickerMode::SingleDisplay),
            (4_u64, SCContentSharingPickerMode::SingleApplication),
            (8_u64, SCContentSharingPickerMode::MultipleApplications),
        ] {
            if mask & raw_value != 0 {
                modes.push(mode);
            }
        }
        modes
    }

    /// Set whether the user can change the selected content while sharing
    ///
    /// When `true`, the user can modify their selection during an active session.
    pub fn set_allows_changing_selected_content(&mut self, allows: bool) {
        unsafe {
            crate::ffi::sc_content_sharing_picker_configuration_set_allows_changing_selected_content(
                self.ptr,
                allows,
            );
        }
    }

    /// Get whether changing selected content is allowed
    pub fn allows_changing_selected_content(&self) -> bool {
        unsafe {
            crate::ffi::sc_content_sharing_picker_configuration_get_allows_changing_selected_content(
                self.ptr,
            )
        }
    }

    /// Set bundle identifiers to exclude from the picker
    ///
    /// Applications with these bundle IDs will not appear in the picker.
    pub fn set_excluded_bundle_ids(&mut self, bundle_ids: &[&str]) {
        let c_strings: Vec<std::ffi::CString> = if let Ok(ids) = bundle_ids
            .iter()
            .map(|id| std::ffi::CString::new(*id))
            .collect()
        {
            ids
        } else {
            eprintln!(
                "SCContentSharingPickerConfiguration: excluded bundle ID contains an \
                 interior NUL byte; configuration was not changed"
            );
            return;
        };
        let ptrs: Vec<*const i8> = c_strings.iter().map(|s| s.as_ptr()).collect();
        unsafe {
            crate::ffi::sc_content_sharing_picker_configuration_set_excluded_bundle_ids(
                self.ptr,
                ptrs.as_ptr(),
                ptrs.len(),
            );
        }
    }

    /// Get the list of excluded bundle identifiers
    ///
    /// A bundle ID that does not fit in the transfer buffer is skipped rather
    /// than returned truncated, so the result can be shorter than
    /// [`excluded_bundle_ids_count`](Self::excluded_bundle_ids_count).
    #[must_use]
    pub fn excluded_bundle_ids(&self) -> Vec<String> {
        let count = self.excluded_bundle_ids_count();
        let mut result = Vec::with_capacity(count);
        for i in 0..count {
            let id = unsafe {
                crate::utils::ffi_string::ffi_string_from_buffer(
                    crate::utils::ffi_string::DEFAULT_BUFFER_SIZE,
                    |buffer, len| {
                        crate::ffi::sc_content_sharing_picker_configuration_get_excluded_bundle_id_at(
                            self.ptr,
                            i,
                            buffer,
                            usize::try_from(len).unwrap_or(0),
                        )
                    },
                )
            };
            if let Some(id) = id {
                result.push(id);
            }
        }
        result
    }

    /// Number of excluded bundle identifiers configured.
    #[must_use]
    pub fn excluded_bundle_ids_count(&self) -> usize {
        unsafe {
            crate::ffi::sc_content_sharing_picker_configuration_get_excluded_bundle_ids_count(
                self.ptr,
            )
        }
    }

    /// Set window IDs to exclude from the picker
    ///
    /// Windows with these IDs will not appear in the picker.
    pub fn set_excluded_window_ids(&mut self, window_ids: &[u32]) {
        unsafe {
            crate::ffi::sc_content_sharing_picker_configuration_set_excluded_window_ids(
                self.ptr,
                window_ids.as_ptr(),
                window_ids.len(),
            );
        }
    }

    /// Get the list of excluded window IDs
    pub fn excluded_window_ids(&self) -> Vec<u32> {
        let count = unsafe {
            crate::ffi::sc_content_sharing_picker_configuration_get_excluded_window_ids_count(
                self.ptr,
            )
        };
        let mut result = Vec::with_capacity(count);
        for i in 0..count {
            let id = unsafe {
                crate::ffi::sc_content_sharing_picker_configuration_get_excluded_window_id_at(
                    self.ptr, i,
                )
            };
            result.push(id);
        }
        result
    }

    #[must_use]
    pub const fn as_ptr(&self) -> *const c_void {
        self.ptr
    }
}

impl Default for SCContentSharingPickerConfiguration {
    fn default() -> Self {
        Self::new()
    }
}

crate::utils::retained::sc_retained!(
    SCContentSharingPickerConfiguration,
    field = ptr,
    release = crate::ffi::sc_content_sharing_picker_configuration_release,
);

impl Clone for SCContentSharingPickerConfiguration {
    /// Produce an independent configuration with the same values.
    ///
    /// Deliberately **not** the retain-based clone the other wrappers in this
    /// crate use. Those wrap immutable Objective-C objects, where sharing one
    /// instance between handles is unobservable. This type is different: it
    /// wraps a mutable Swift box and exposes `&mut self` setters, so a
    /// refcount-only clone would let a `&mut` on one handle mutate state that
    /// another handle observes through a shared `&` — and, with `Send + Sync`,
    /// from another thread at the same time.
    fn clone(&self) -> Self {
        Self {
            ptr: unsafe { crate::ffi::sc_content_sharing_picker_configuration_copy(self.ptr) },
        }
    }
}

impl std::fmt::Debug for SCContentSharingPickerConfiguration {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SCContentSharingPickerConfiguration")
            .field("ptr", &self.ptr)
            .finish()
    }
}

// ============================================================================
// Simple API: Returns SCContentFilter directly
// ============================================================================

/// Result from the simple `show_filter()` API
#[derive(Debug)]
pub enum SCPickerFilterOutcome {
    /// User selected content - contains the filter to use with `SCStream`
    Filter(SCContentFilter),
    /// User cancelled the picker
    Cancelled,
    /// An error occurred
    Error(String),
}

// ============================================================================
// Main API: Returns SCPickerResult with metadata
// ============================================================================

/// Result from the main `show()` API - contains filter and content metadata
///
/// Provides access to:
/// - The `SCContentFilter` for use with `SCStream`
/// - Content dimensions and scale factor
/// - The picked windows, displays, and applications for custom filter creation
pub struct SCPickerResult {
    ptr: *const c_void,
}

impl SCPickerResult {
    /// Create from raw pointer (used by async API)
    #[cfg(feature = "async")]
    #[must_use]
    pub(crate) fn from_ptr(ptr: *const c_void) -> Self {
        Self { ptr }
    }

    /// Get the content filter for use with `SCStream::new()`
    #[must_use]
    pub fn filter(&self) -> SCContentFilter {
        let filter_ptr = unsafe { crate::ffi::sc_picker_result_get_filter(self.ptr) };
        SCContentFilter::from_picker_ptr(filter_ptr)
    }

    /// Get the content size in points (width, height)
    #[must_use]
    pub fn size(&self) -> (f64, f64) {
        let mut x = 0.0;
        let mut y = 0.0;
        let mut width = 0.0;
        let mut height = 0.0;
        unsafe {
            crate::ffi::sc_picker_result_get_content_rect(
                self.ptr,
                &mut x,
                &mut y,
                &mut width,
                &mut height,
            );
        }
        (width, height)
    }

    /// Get the content rect (x, y, width, height) in points
    #[must_use]
    pub fn rect(&self) -> (f64, f64, f64, f64) {
        let mut x = 0.0;
        let mut y = 0.0;
        let mut width = 0.0;
        let mut height = 0.0;
        unsafe {
            crate::ffi::sc_picker_result_get_content_rect(
                self.ptr,
                &mut x,
                &mut y,
                &mut width,
                &mut height,
            );
        }
        (x, y, width, height)
    }

    /// Get the point-to-pixel scale factor (typically 2.0 for Retina displays)
    #[must_use]
    pub fn scale(&self) -> f64 {
        unsafe { crate::ffi::sc_picker_result_get_scale(self.ptr) }
    }

    /// Get the pixel dimensions (size * scale)
    #[must_use]
    pub fn pixel_size(&self) -> (u32, u32) {
        let (w, h) = self.size();
        let scale = self.scale();
        #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
        let width = (w * scale) as u32;
        #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
        let height = (h * scale) as u32;
        (width, height)
    }

    /// Get the windows selected by the user
    ///
    /// Returns the picked windows that can be used to create a custom `SCContentFilter`.
    ///
    /// # Example
    /// ```no_run
    /// use screencapturekit::content_sharing_picker::*;
    /// use screencapturekit::prelude::*;
    ///
    /// let config = SCContentSharingPickerConfiguration::new();
    /// SCContentSharingPicker::show(&config, |outcome| {
    ///     if let SCPickerOutcome::Picked(result) = outcome {
    ///         let windows = result.windows();
    ///         if let Some(window) = windows.first() {
    ///             // Create custom filter with a picked window
    ///             let filter = SCContentFilter::create()
    ///                 .with_window(window)
    ///                 .build();
    ///         }
    ///     }
    /// });
    /// ```
    #[must_use]
    pub fn windows(&self) -> Vec<crate::shareable_content::SCWindow> {
        let count = unsafe { crate::ffi::sc_picker_result_get_windows_count(self.ptr) };
        (0..count)
            .filter_map(|i| {
                let ptr = unsafe { crate::ffi::sc_picker_result_get_window_at(self.ptr, i) };
                unsafe { crate::shareable_content::SCWindow::from_retained_ptr(ptr) }
            })
            .collect()
    }

    /// Get the displays selected by the user
    ///
    /// Returns the picked displays that can be used to create a custom `SCContentFilter`.
    ///
    /// # Example
    /// ```no_run
    /// use screencapturekit::content_sharing_picker::*;
    /// use screencapturekit::prelude::*;
    ///
    /// let config = SCContentSharingPickerConfiguration::new();
    /// SCContentSharingPicker::show(&config, |outcome| {
    ///     if let SCPickerOutcome::Picked(result) = outcome {
    ///         let displays = result.displays();
    ///         if let Some(display) = displays.first() {
    ///             // Create custom filter with the picked display
    ///             let filter = SCContentFilter::create()
    ///                 .with_display(display)
    ///                 .with_excluding_windows(&[])
    ///                 .build();
    ///         }
    ///     }
    /// });
    /// ```
    #[must_use]
    pub fn displays(&self) -> Vec<crate::shareable_content::SCDisplay> {
        let count = unsafe { crate::ffi::sc_picker_result_get_displays_count(self.ptr) };
        (0..count)
            .filter_map(|i| {
                let ptr = unsafe { crate::ffi::sc_picker_result_get_display_at(self.ptr, i) };
                unsafe { crate::shareable_content::SCDisplay::from_retained_ptr(ptr) }
            })
            .collect()
    }

    /// Get the applications selected by the user
    ///
    /// Returns the picked applications that can be used to create a custom `SCContentFilter`.
    #[must_use]
    pub fn applications(&self) -> Vec<crate::shareable_content::SCRunningApplication> {
        let count = unsafe { crate::ffi::sc_picker_result_get_applications_count(self.ptr) };
        (0..count)
            .filter_map(|i| {
                let ptr = unsafe { crate::ffi::sc_picker_result_get_application_at(self.ptr, i) };
                unsafe { crate::shareable_content::SCRunningApplication::from_retained_ptr(ptr) }
            })
            .collect()
    }

    /// Get the source type that was picked
    ///
    /// Returns information about what the user selected: window, display, or application.
    ///
    /// # Example
    /// ```no_run
    /// use screencapturekit::content_sharing_picker::*;
    ///
    /// fn example() {
    ///     let config = SCContentSharingPickerConfiguration::new();
    ///     SCContentSharingPicker::show(&config, |outcome| {
    ///         if let SCPickerOutcome::Picked(result) = outcome {
    ///             match result.source() {
    ///                 SCPickedSource::Window(title) => println!("[W] {}", title),
    ///                 SCPickedSource::Display(id) => println!("[D] Display {}", id),
    ///                 SCPickedSource::Application(name) => println!("[A] {}", name),
    ///                 SCPickedSource::Unknown => println!("Unknown source"),
    ///             }
    ///         }
    ///     });
    /// }
    /// ```
    #[must_use]
    #[allow(clippy::option_if_let_else)]
    pub fn source(&self) -> SCPickedSource {
        if let Some(window) = self.windows().first() {
            SCPickedSource::Window(window.title().unwrap_or_else(|| "Untitled".to_string()))
        } else if let Some(display) = self.displays().first() {
            SCPickedSource::Display(display.display_id())
        } else if let Some(app) = self.applications().first() {
            SCPickedSource::Application(app.application_name())
        } else {
            SCPickedSource::Unknown
        }
    }
}

crate::utils::retained::sc_retained!(
    SCPickerResult,
    field = ptr,
    release = crate::ffi::sc_picker_result_release,
);

impl std::fmt::Debug for SCPickerResult {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let (w, h) = self.size();
        let scale = self.scale();
        f.debug_struct("SCPickerResult")
            .field("size", &(w, h))
            .field("scale", &scale)
            .field("pixel_size", &self.pixel_size())
            .finish()
    }
}

/// Outcome from the main `show()` API
#[derive(Debug)]
pub enum SCPickerOutcome {
    /// User selected content - contains result with filter and metadata
    Picked(SCPickerResult),
    /// User cancelled the picker
    Cancelled,
    /// An error occurred
    Error(String),
}

// ============================================================================
// SCContentSharingPicker
// ============================================================================

/// System UI for selecting content to share
///
/// Available on macOS 14.0+
///
/// The picker requires user interaction and cannot block the calling thread.
/// Use one of these approaches:
///
/// - **Callback-based**: `show()` / `show_filter()` - pass a callback closure
/// - **Async/await**: `AsyncSCContentSharingPicker` from the `async_api` module
///
/// # Example (callback)
/// ```no_run
/// use screencapturekit::content_sharing_picker::*;
///
/// let config = SCContentSharingPickerConfiguration::new();
/// SCContentSharingPicker::show(&config, |outcome| {
///     if let SCPickerOutcome::Picked(result) = outcome {
///         let (width, height) = result.pixel_size();
///         let filter = result.filter();
///         // ... create stream
///     }
/// });
/// ```
///
/// # Example (async)
/// ```no_run
/// use screencapturekit::async_api::AsyncSCContentSharingPicker;
/// use screencapturekit::content_sharing_picker::*;
///
/// async fn example() {
///     let config = SCContentSharingPickerConfiguration::new();
///     if let SCPickerOutcome::Picked(result) = AsyncSCContentSharingPicker::show(&config).await {
///         let (width, height) = result.pixel_size();
///         let filter = result.filter();
///         // ... create stream
///     }
/// }
/// ```
#[derive(Debug)]
pub struct SCContentSharingPicker;

impl SCContentSharingPicker {
    fn available_or_log(operation: &str) -> bool {
        let available = Self::is_available();
        if !available {
            eprintln!("{operation} requires macOS 14.0 or later");
        }
        available
    }

    /// Whether content-sharing picker APIs are available on this system.
    #[must_use]
    pub fn is_available() -> bool {
        unsafe { crate::ffi::sc_content_sharing_picker_is_available() }
    }

    /// Show the picker UI with a callback for the result
    ///
    /// This is non-blocking - the callback is invoked when the user makes a selection
    /// or cancels the picker.
    ///
    /// # Example
    /// ```no_run
    /// use screencapturekit::content_sharing_picker::*;
    ///
    /// let config = SCContentSharingPickerConfiguration::new();
    /// SCContentSharingPicker::show(&config, |outcome| {
    ///     match outcome {
    ///         SCPickerOutcome::Picked(result) => {
    ///             let (width, height) = result.pixel_size();
    ///             let filter = result.filter();
    ///             println!("Selected {}x{}", width, height);
    ///         }
    ///         SCPickerOutcome::Cancelled => println!("Cancelled"),
    ///         SCPickerOutcome::Error(e) => eprintln!("Error: {}", e),
    ///     }
    /// });
    /// ```
    pub fn show<F>(config: &SCContentSharingPickerConfiguration, callback: F)
    where
        F: FnOnce(SCPickerOutcome) + Send + 'static,
    {
        let context = into_callback_context::<SCPickerOutcome, F>(callback);

        unsafe {
            crate::ffi::sc_content_sharing_picker_show_with_result(
                config.as_ptr(),
                picker_trampoline::<ResultDecoder>,
                context,
            );
        }
    }

    /// Show the picker UI for an existing stream (to change source while capturing)
    ///
    /// Use this when you have an active `SCStream` and want to let the user
    /// select a new content source. The callback receives the new filter
    /// which can be used with `stream.update_content_filter()`.
    ///
    /// # Example
    /// ```no_run
    /// use screencapturekit::content_sharing_picker::*;
    /// use screencapturekit::stream::SCStream;
    /// use screencapturekit::stream::configuration::SCStreamConfiguration;
    /// use screencapturekit::stream::content_filter::SCContentFilter;
    /// use screencapturekit::shareable_content::SCShareableContent;
    ///
    /// fn example() -> Option<()> {
    ///     let content = SCShareableContent::get().ok()?;
    ///     let displays = content.displays();
    ///     let display = displays.first()?;
    ///     let filter = SCContentFilter::create().with_display(display).with_excluding_windows(&[]).build();
    ///     let stream_config = SCStreamConfiguration::new();
    ///     let stream = SCStream::new(&filter, &stream_config);
    ///
    ///     // When stream is active and user wants to change source
    ///     let config = SCContentSharingPickerConfiguration::new();
    ///     SCContentSharingPicker::show_for_stream(&config, &stream, |outcome| {
    ///         if let SCPickerOutcome::Picked(result) = outcome {
    ///             // Use result.filter() with stream.update_content_filter()
    ///             let _ = result.filter();
    ///         }
    ///     });
    ///     Some(())
    /// }
    /// ```
    pub fn show_for_stream<F>(
        config: &SCContentSharingPickerConfiguration,
        stream: &crate::stream::SCStream,
        callback: F,
    ) where
        F: FnOnce(SCPickerOutcome) + Send + 'static,
    {
        let context = into_callback_context::<SCPickerOutcome, F>(callback);

        unsafe {
            crate::ffi::sc_content_sharing_picker_show_for_stream(
                config.as_ptr(),
                stream.as_ptr(),
                picker_trampoline::<ResultDecoder>,
                context,
            );
        }
    }

    /// Show the picker UI with a callback that receives just the filter
    ///
    /// This is the simple API - use when you just need the filter without metadata.
    ///
    /// # Example
    /// ```no_run
    /// use screencapturekit::content_sharing_picker::*;
    ///
    /// let config = SCContentSharingPickerConfiguration::new();
    /// SCContentSharingPicker::show_filter(&config, |outcome| {
    ///     if let SCPickerFilterOutcome::Filter(filter) = outcome {
    ///         // Use filter with SCStream
    ///     }
    /// });
    /// ```
    pub fn show_filter<F>(config: &SCContentSharingPickerConfiguration, callback: F)
    where
        F: FnOnce(SCPickerFilterOutcome) + Send + 'static,
    {
        let context = into_callback_context::<SCPickerFilterOutcome, F>(callback);

        unsafe {
            crate::ffi::sc_content_sharing_picker_show(
                config.as_ptr(),
                picker_trampoline::<FilterDecoder>,
                context,
            );
        }
    }

    /// Show the picker UI with a specific content style
    ///
    /// Presents the picker pre-filtered to a specific content type.
    ///
    /// # Arguments
    /// * `config` - The picker configuration
    /// * `style` - The content style to show (Window, Display, Application)
    /// * `callback` - Called with the picker result
    pub fn show_using_style<F>(
        config: &SCContentSharingPickerConfiguration,
        style: crate::stream::content_filter::SCShareableContentStyle,
        callback: F,
    ) where
        F: FnOnce(SCPickerOutcome) + Send + 'static,
    {
        let context = into_callback_context::<SCPickerOutcome, F>(callback);

        unsafe {
            crate::ffi::sc_content_sharing_picker_show_using_style(
                config.as_ptr(),
                style as i32,
                picker_trampoline::<ResultDecoder>,
                context,
            );
        }
    }

    /// Show the picker for an existing stream with a specific content style
    ///
    /// # Arguments
    /// * `config` - The picker configuration
    /// * `stream` - The stream to update
    /// * `style` - The content style to show (Window, Display, Application)
    /// * `callback` - Called with the picker result
    pub fn show_for_stream_using_style<F>(
        config: &SCContentSharingPickerConfiguration,
        stream: &crate::stream::SCStream,
        style: crate::stream::content_filter::SCShareableContentStyle,
        callback: F,
    ) where
        F: FnOnce(SCPickerOutcome) + Send + 'static,
    {
        let context = into_callback_context::<SCPickerOutcome, F>(callback);

        unsafe {
            crate::ffi::sc_content_sharing_picker_show_for_stream_using_style(
                config.as_ptr(),
                stream.as_ptr(),
                style as i32,
                picker_trampoline::<ResultDecoder>,
                context,
            );
        }
    }

    /// Set the maximum number of streams that can be created from the picker
    ///
    /// Pass 0 to allow unlimited streams.
    pub fn set_maximum_stream_count(count: usize) {
        if !Self::available_or_log("SCContentSharingPicker::set_maximum_stream_count") {
            return;
        }
        unsafe {
            crate::ffi::sc_content_sharing_picker_set_maximum_stream_count(count);
        }
    }

    /// Get the maximum number of streams allowed
    ///
    /// Returns 0 if unlimited streams are allowed.
    pub fn maximum_stream_count() -> usize {
        if !Self::is_available() {
            return 0;
        }
        unsafe { crate::ffi::sc_content_sharing_picker_get_maximum_stream_count() }
    }

    /// Returns whether the shared content-sharing picker is currently
    /// marked active.
    ///
    /// Apple requires `picker.isActive = true` before its UI can appear.
    /// The various `show*()` trampolines on this type set it implicitly
    /// before presenting, but this getter is useful for callers that
    /// want to:
    ///
    /// * avoid double-presenting (skip a second `show()` while the first
    ///   picker session is still up),
    /// * render UI affordances based on whether the picker is currently
    ///   visible to the user.
    #[must_use]
    pub fn is_active() -> bool {
        if !Self::is_available() {
            return false;
        }
        unsafe { crate::ffi::sc_content_sharing_picker_get_active() }
    }

    /// Mark the shared content-sharing picker active or inactive.
    ///
    /// Setting this to `false` hides the picker UI between sessions
    /// (the recommended hygiene step after a long-running app finishes
    /// using the picker — leaving it active leaves the system-level
    /// Control Center entry in a "ready to share" state).
    ///
    /// Setting to `true` is required before `present*()` can surface
    /// the picker; the `show*()` trampolines do this for you. Set it
    /// manually only if you want to opt into the picker UI without
    /// immediately presenting it.
    pub fn set_active(active: bool) {
        if !Self::available_or_log("SCContentSharingPicker::set_active") {
            return;
        }
        unsafe { crate::ffi::sc_content_sharing_picker_set_active(active) }
    }

    /// Deactivate the picker and undo any activation-policy promotion the
    /// bridge performed on behalf of a non-UI (`.prohibited`) host process.
    ///
    /// Presenting `SCContentSharingPicker` requires the process to be a
    /// regular, Dock-visible app. Pure-Rust hosts usually are not, so the
    /// bridge temporarily promotes them; the promotion is reference counted
    /// and unwound automatically when each one-shot `show*()` resolves. Call
    /// this after you are done with a *long-lived* observer session to drop
    /// the promotion immediately and clear the Control Center "ready to
    /// share" indicator.
    ///
    /// Registered observers are **not** removed — drop their
    /// [`SCPickerSubscription`] for that.
    pub fn deactivate() {
        if !Self::is_available() {
            return;
        }
        unsafe { crate::ffi::sc_content_sharing_picker_deactivate() }
    }

    // ------------------------------------------------------------------
    // Standalone configuration operations
    // ------------------------------------------------------------------

    /// Read the picker's process-wide default configuration.
    ///
    /// Equivalent to Apple's `SCContentSharingPicker.shared.defaultConfiguration`.
    /// This is the same value returned by
    /// [`SCContentSharingPickerConfiguration::default_from_system`].
    #[must_use]
    pub fn default_configuration() -> SCContentSharingPickerConfiguration {
        SCContentSharingPickerConfiguration::default_from_system()
    }

    /// Assign the picker's process-wide default configuration.
    ///
    /// Apple's `SCContentSharingPicker.defaultConfiguration` is read-write;
    /// previously this crate could only set it as a side effect of calling a
    /// `show*()` helper. Setting it explicitly is what you want when driving
    /// the picker with a persistent observer plus [`Self::present`].
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use screencapturekit::content_sharing_picker::*;
    ///
    /// let mut config = SCContentSharingPickerConfiguration::new();
    /// config.set_allows_changing_selected_content(true);
    /// SCContentSharingPicker::set_default_configuration(&config);
    /// ```
    pub fn set_default_configuration(config: &SCContentSharingPickerConfiguration) {
        unsafe {
            crate::ffi::sc_content_sharing_picker_set_default_configuration(config.as_ptr());
        }
    }

    /// Assign a picker configuration scoped to a single stream, mirroring
    /// Apple's `setConfiguration(_:for:)`.
    ///
    /// Pass `None` to clear the stream-specific configuration and fall back to
    /// the process-wide default.
    pub fn set_configuration_for_stream(
        config: Option<&SCContentSharingPickerConfiguration>,
        stream: &crate::stream::SCStream,
    ) {
        if !Self::available_or_log("SCContentSharingPicker::set_configuration_for_stream") {
            return;
        }
        let config_ptr = config.map_or(
            std::ptr::null(),
            SCContentSharingPickerConfiguration::as_ptr,
        );
        unsafe {
            crate::ffi::sc_content_sharing_picker_set_configuration_for_stream(
                config_ptr,
                stream.as_ptr(),
            );
        }
    }

    // ------------------------------------------------------------------
    // Persistent observers
    // ------------------------------------------------------------------

    /// Register a **repeating** observer that receives every picker event for
    /// as long as the returned subscription is alive.
    ///
    /// This is the API to use with
    /// [`SCContentSharingPickerConfiguration::set_allows_changing_selected_content`]:
    /// Apple re-invokes `contentSharingPicker(_:didUpdateWith:for:)` each time
    /// the user re-picks during an active share, and the one-shot
    /// [`Self::show`] family deliberately latches after the first event.
    ///
    /// The subscription unregisters on drop, so bind it to a variable that
    /// lives as long as you want events (use [`SCPickerSubscription::detach`]
    /// to keep it for the remainder of the process).
    ///
    /// Pair this with [`Self::present`] / [`Self::present_for_stream`] to
    /// surface the UI.
    ///
    /// Apple marks `SCContentSharingPicker` as `@MainActor`. Call this on the
    /// process main thread, or while an `AppKit` main run loop is active so the
    /// bridge can synchronously hop to it. Otherwise registration fails and
    /// the returned subscription is inactive.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use screencapturekit::content_sharing_picker::*;
    ///
    /// let mut config = SCContentSharingPickerConfiguration::new();
    /// config.set_allows_changing_selected_content(true);
    /// SCContentSharingPicker::set_default_configuration(&config);
    ///
    /// let subscription = SCContentSharingPicker::add_observer(|event| match event {
    ///     SCPickerEvent::Updated(result) => {
    ///         // Fires again every time the user changes their selection.
    ///         let _filter = result.filter();
    ///     }
    ///     SCPickerEvent::Cancelled => println!("cancelled"),
    ///     SCPickerEvent::Failed(err) => eprintln!("picker failed: {err}"),
    /// });
    ///
    /// SCContentSharingPicker::present();
    /// // ... keep `subscription` alive for as long as you want updates ...
    /// drop(subscription);
    /// ```
    #[must_use = "the observer is removed as soon as the subscription is dropped"]
    pub fn add_observer<F>(handler: F) -> SCPickerSubscription
    where
        F: Fn(SCPickerEvent) + Send + Sync + 'static,
    {
        if !Self::available_or_log("SCContentSharingPicker::add_observer") {
            return SCPickerSubscription { token: 0 };
        }
        let context = SCPickerObserverContext::into_raw(handler);
        let token = unsafe {
            crate::ffi::sc_content_sharing_picker_add_observer(
                observer_trampoline,
                observer_context_release,
                context,
            )
        };

        if token == 0 {
            eprintln!(
                "SCContentSharingPicker::add_observer must run on the main thread or while an \
                 AppKit main run loop is active"
            );
            observer_context_release(context);
        }

        SCPickerSubscription { token }
    }

    /// Remove every repeating observer registered through
    /// [`Self::add_observer`], regardless of which subscriptions are still
    /// alive. Returns how many were removed.
    ///
    /// Dropping the corresponding [`SCPickerSubscription`] afterwards is
    /// harmless — removal is idempotent.
    pub fn remove_all_observers() -> usize {
        if !Self::is_available() {
            return 0;
        }
        unsafe { crate::ffi::sc_content_sharing_picker_remove_all_observers() }
    }

    // ------------------------------------------------------------------
    // Standalone presentation
    // ------------------------------------------------------------------

    /// Present the picker without a content-style hint.
    ///
    /// Use with [`Self::add_observer`]; the one-shot [`Self::show`] family
    /// presents for you.
    pub fn present() {
        if !Self::available_or_log("SCContentSharingPicker::present") {
            return;
        }
        unsafe { crate::ffi::sc_content_sharing_picker_present(-1) }
    }

    /// Present the picker preselecting a content style.
    pub fn present_using_style(style: SCShareableContentStyle) {
        if !Self::available_or_log("SCContentSharingPicker::present_using_style") {
            return;
        }
        unsafe { crate::ffi::sc_content_sharing_picker_present(style as i32) }
    }

    /// Present the picker targeting an existing stream, so the user can swap
    /// the shared source mid-capture.
    pub fn present_for_stream(stream: &crate::stream::SCStream) {
        if !Self::available_or_log("SCContentSharingPicker::present_for_stream") {
            return;
        }
        unsafe { crate::ffi::sc_content_sharing_picker_present_for_stream(stream.as_ptr(), -1) }
    }

    /// Present the picker targeting an existing stream, preselecting a style.
    pub fn present_for_stream_using_style(
        stream: &crate::stream::SCStream,
        style: SCShareableContentStyle,
    ) {
        if !Self::available_or_log("SCContentSharingPicker::present_for_stream_using_style") {
            return;
        }
        unsafe {
            crate::ffi::sc_content_sharing_picker_present_for_stream(stream.as_ptr(), style as i32);
        }
    }
}

// ============================================================================
// Persistent observer: events, subscription handle, context, trampoline
// ============================================================================

/// A single event delivered to a repeating observer registered with
/// [`SCContentSharingPicker::add_observer`].
///
/// Unlike [`SCPickerOutcome`], which resolves a one-shot `show*()` call,
/// these arrive as many times as the user interacts with the picker.
#[derive(Debug)]
pub enum SCPickerEvent {
    /// The user selected (or re-selected) content. Mirrors Apple's
    /// `contentSharingPicker(_:didUpdateWith:for:)`.
    Updated(SCPickerResult),
    /// The user dismissed the picker. Mirrors
    /// `contentSharingPicker(_:didCancelFor:)`.
    Cancelled,
    /// The picker failed to start. Mirrors
    /// `contentSharingPickerStartDidFailWithError(_:)`.
    Failed(String),
}

/// Handle representing a live repeating-observer registration.
///
/// The observer is removed when this value is dropped. Call
/// [`Self::detach`] to keep the observer alive for the rest of the process.
#[derive(Debug)]
#[must_use = "the observer is removed as soon as the subscription is dropped"]
pub struct SCPickerSubscription {
    token: i64,
}

impl SCPickerSubscription {
    /// Opaque identifier for this registration. Non-zero when registration
    /// succeeded.
    #[must_use]
    pub const fn token(&self) -> i64 {
        self.token
    }

    /// Whether this subscription refers to a live registration.
    #[must_use]
    pub const fn is_active(&self) -> bool {
        self.token != 0
    }

    /// Remove the observer now instead of waiting for the drop.
    ///
    /// Returns `true` if a live observer was removed.
    pub fn unsubscribe(mut self) -> bool {
        let removed = self.remove();
        std::mem::forget(self);
        removed
    }

    /// Give up ownership without removing the observer, keeping it registered
    /// for the remainder of the process.
    ///
    /// Useful for "install once at startup" wiring where there is no natural
    /// owner for the handle. The registration can still be torn down with
    /// [`SCContentSharingPicker::remove_all_observers`].
    pub fn detach(self) {
        std::mem::forget(self);
    }

    fn remove(&mut self) -> bool {
        if self.token == 0 {
            return false;
        }
        let removed = unsafe { crate::ffi::sc_content_sharing_picker_remove_observer(self.token) };
        self.token = 0;
        removed
    }
}

impl Drop for SCPickerSubscription {
    fn drop(&mut self) {
        self.remove();
    }
}

struct SCPickerObserverContext {
    /// Cleared on unsubscribe so a callback already in flight is dropped
    /// rather than delivered after the user asked to stop listening.
    active: AtomicBool,
    handler: Box<dyn Fn(SCPickerEvent) + Send + Sync>,
}

impl SCPickerObserverContext {
    fn into_raw<F>(handler: F) -> *mut c_void
    where
        F: Fn(SCPickerEvent) + Send + Sync + 'static,
    {
        let context = std::sync::Arc::new(Self {
            active: AtomicBool::new(true),
            handler: Box::new(handler),
        });
        let mut registry = PICKER_OBSERVER_CONTEXTS
            .lock()
            .unwrap_or_else(PoisonError::into_inner);
        let contexts = registry.get_or_insert_with(HashMap::new);
        let id = loop {
            let id = NEXT_PICKER_OBSERVER_CONTEXT_ID.fetch_add(1, Ordering::Relaxed);
            if id != 0 && !contexts.contains_key(&id) {
                break id;
            }
        };
        contexts.insert(id, context);
        drop(registry);
        id as *mut c_void
    }
}

static NEXT_PICKER_OBSERVER_CONTEXT_ID: AtomicUsize = AtomicUsize::new(1);
static PICKER_OBSERVER_CONTEXTS: Mutex<
    Option<HashMap<usize, std::sync::Arc<SCPickerObserverContext>>>,
> = Mutex::new(None);

fn picker_observer_context(
    context: *mut c_void,
) -> Option<std::sync::Arc<SCPickerObserverContext>> {
    let id = context as usize;
    if id == 0 {
        return None;
    }
    PICKER_OBSERVER_CONTEXTS
        .lock()
        .unwrap_or_else(PoisonError::into_inner)
        .as_ref()?
        .get(&id)
        .cloned()
}

extern "C" fn observer_context_release(context: *mut c_void) {
    crate::utils::panic_safe::catch_user_panic("picker observer context release", || {
        let id = context as usize;
        if id == 0 {
            return;
        }
        let removed = {
            let mut contexts = PICKER_OBSERVER_CONTEXTS
                .lock()
                .unwrap_or_else(PoisonError::into_inner);
            contexts.as_mut().and_then(|contexts| contexts.remove(&id))
        };
        if let Some(context) = removed {
            context.active.store(false, Ordering::Release);
        }
    });
}

/// Trampoline for every repeating-observer event.
///
/// `event` follows the Swift bridge contract: 1 = updated (with a result
/// pointer), 0 = cancelled, anything else = start failure (with a message).
extern "C" fn observer_trampoline(
    event: i32,
    result_ptr: *const c_void,
    message: *const i8,
    context: *mut c_void,
) {
    // The whole body sits inside the barrier: the registry lookup and the
    // message decoding both allocate, and an unwind out of an `extern "C"`
    // function is undefined behaviour on this crate's MSRV.
    crate::utils::panic_safe::catch_user_panic("picker observer callback", move || {
        let Some(context) = picker_observer_context(context) else {
            if !result_ptr.is_null() {
                unsafe { crate::ffi::sc_picker_result_release(result_ptr) };
            }
            return;
        };

        if !context.active.load(Ordering::Acquire) {
            // Unsubscribed while this callback was in flight; drop the result
            // rather than delivering it. Release the retained result first.
            if !result_ptr.is_null() {
                unsafe { crate::ffi::sc_picker_result_release(result_ptr) };
            }
            return;
        }

        let decoded = match event {
            1 if !result_ptr.is_null() => {
                SCPickerEvent::Updated(SCPickerResult { ptr: result_ptr })
            }
            1 => SCPickerEvent::Failed("picker delivered an update without a result".to_string()),
            0 => SCPickerEvent::Cancelled,
            _ => {
                let text = if message.is_null() {
                    "Content sharing picker failed to start".to_string()
                } else {
                    // SAFETY: Swift passes a NUL-terminated UTF-8 buffer that is
                    // valid for the duration of this call.
                    unsafe { std::ffi::CStr::from_ptr(message) }
                        .to_string_lossy()
                        .into_owned()
                };
                SCPickerEvent::Failed(text)
            }
        };

        (context.handler)(decoded);
    });
}

// ============================================================================
// One-shot callback context + trampoline (shared by all `show*()` methods)
// ============================================================================

/// Context owned by the one-shot callback registry.
struct PickerCallbackContext<O> {
    closure: Box<dyn FnOnce(O) + Send>,
}

static NEXT_PICKER_CALLBACK_ID: AtomicUsize = AtomicUsize::new(1);
static PICKER_CALLBACKS: Mutex<Option<HashMap<usize, Box<dyn Any + Send>>>> = Mutex::new(None);

/// Store a user closure and hand Swift a token that is never dereferenced.
#[allow(clippy::significant_drop_tightening)]
fn into_callback_context<O, F>(callback: F) -> *mut c_void
where
    O: 'static,
    F: FnOnce(O) + Send + 'static,
{
    let context = Box::new(PickerCallbackContext {
        closure: Box::new(callback),
    });
    let mut callbacks = PICKER_CALLBACKS
        .lock()
        .unwrap_or_else(PoisonError::into_inner);
    let callbacks = callbacks.get_or_insert_with(HashMap::new);
    loop {
        let id = NEXT_PICKER_CALLBACK_ID.fetch_add(1, Ordering::Relaxed);
        if id != 0 && !callbacks.contains_key(&id) {
            callbacks.insert(id, context);
            return id as *mut c_void;
        }
    }
}

fn take_callback_context<O: 'static>(
    context: *mut c_void,
) -> Option<Box<PickerCallbackContext<O>>> {
    let id = context as usize;
    if id == 0 {
        return None;
    }
    let entry = PICKER_CALLBACKS
        .lock()
        .unwrap_or_else(PoisonError::into_inner)
        .as_mut()?
        .remove(&id)?;
    entry.downcast::<PickerCallbackContext<O>>().ok()
}

/// Decodes the `(code, ptr)` pair from the Swift bridge into a typed outcome.
///
/// Implemented by zero-sized marker types so a single generic trampoline can
/// serve both the result-bearing and filter-only APIs while keeping the FFI
/// signature identical.
trait PickerDecode {
    type Outcome: 'static;
    fn decode(code: i32, ptr: *const c_void) -> Self::Outcome;
    unsafe fn release(ptr: *const c_void);
}

struct ResultDecoder;
impl PickerDecode for ResultDecoder {
    type Outcome = SCPickerOutcome;
    fn decode(code: i32, ptr: *const c_void) -> SCPickerOutcome {
        match code {
            1 if !ptr.is_null() => SCPickerOutcome::Picked(SCPickerResult { ptr }),
            0 => SCPickerOutcome::Cancelled,
            _ => SCPickerOutcome::Error("Picker failed".to_string()),
        }
    }

    unsafe fn release(ptr: *const c_void) {
        if !ptr.is_null() {
            unsafe { crate::ffi::sc_picker_result_release(ptr) };
        }
    }
}

struct FilterDecoder;
impl PickerDecode for FilterDecoder {
    type Outcome = SCPickerFilterOutcome;
    fn decode(code: i32, ptr: *const c_void) -> SCPickerFilterOutcome {
        match code {
            1 if !ptr.is_null() => {
                SCPickerFilterOutcome::Filter(SCContentFilter::from_picker_ptr(ptr))
            }
            0 => SCPickerFilterOutcome::Cancelled,
            _ => SCPickerFilterOutcome::Error("Picker failed".to_string()),
        }
    }

    unsafe fn release(ptr: *const c_void) {
        if !ptr.is_null() {
            unsafe { crate::ffi::sc_content_filter_release(ptr) };
        }
    }
}

/// Single trampoline for every picker `show*()` callback.
///
/// `code` follows the Swift bridge contract (1 = picked, 0 = cancelled,
/// anything else = error). A `code` of 0 is also produced by the Swift
/// replacement path when a pending observer is superseded by a newer
/// `show*()`, so a replaced picker resolves as `Cancelled` rather than
/// leaking its context.
///
/// The registry entry is removed exactly once. Duplicate or late callbacks
/// find no entry, so they cannot dereference freed memory.
extern "C" fn picker_trampoline<D: PickerDecode>(
    code: i32,
    ptr: *const c_void,
    context: *mut c_void,
) {
    crate::utils::panic_safe::catch_user_panic("picker callback", move || {
        let Some(context) = take_callback_context::<D::Outcome>(context) else {
            unsafe { D::release(ptr) };
            return;
        };
        let outcome = D::decode(code, ptr);
        (context.closure)(outcome);
    });
}

// SAFETY: the wrapper owns its Swift box exclusively — `Clone` copies the box
// rather than retaining it, and no constructor hands out a second handle to the
// same allocation. Mutation therefore only happens through `&mut self`, which
// Rust already makes exclusive, and the `&self` methods are pure getters. The
// box itself is a Swift class, so the refcount traffic that `Drop` performs is
// atomic.
unsafe impl Send for SCContentSharingPickerConfiguration {}
unsafe impl Sync for SCContentSharingPickerConfiguration {}
// SAFETY: `SCPickerResult` holds retained Objective-C objects whose reference
// counting is atomic; it is safe to send between and share across threads.
unsafe impl Send for SCPickerResult {}
unsafe impl Sync for SCPickerResult {}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;

    #[test]
    fn duplicate_one_shot_callback_is_ignored_after_context_drop() {
        let calls = Arc::new(AtomicUsize::new(0));
        let observed = Arc::clone(&calls);
        let context = into_callback_context::<SCPickerFilterOutcome, _>(move |_| {
            observed.fetch_add(1, Ordering::SeqCst);
        });

        picker_trampoline::<FilterDecoder>(0, std::ptr::null(), context);
        picker_trampoline::<FilterDecoder>(0, std::ptr::null(), context);

        assert_eq!(calls.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn released_repeating_observer_ignores_late_callback() {
        let calls = Arc::new(AtomicUsize::new(0));
        let observed = Arc::clone(&calls);
        let context = SCPickerObserverContext::into_raw(move |_| {
            observed.fetch_add(1, Ordering::SeqCst);
        });

        observer_context_release(context);
        observer_trampoline(0, std::ptr::null(), std::ptr::null(), context);

        assert_eq!(calls.load(Ordering::SeqCst), 0);
    }
}