strata-ui-platform 0.1.5

Native Hardware Abstraction Layer for Android, iOS, macOS, Windows, Linux, and Web
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
//! Cross-platform native OS dialogs (file pickers, folder selectors, and message boxes).
//!
//! Provides native system dialogs across Linux (GTK / XDG Desktop Portal),
//! Windows (`comdlg32` / Win32 `GetOpenFileNameW` / `MessageBoxW`), and
//! macOS (AppKit `NSOpenPanel`, `NSSavePanel`, `NSAlert`).
//!
//! Includes a thread-safe mock override system (`set_dialog_mock_response`)
//! allowing automated CI and headless tests to verify dialog logic deterministically.

use std::path::PathBuf;
#[cfg(any(test, feature = "test-internals"))]
use std::sync::Mutex;

/// File type filter used by file picker dialogs.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DialogFilter {
    pub name: String,
    pub extensions: Vec<String>,
}

impl DialogFilter {
    /// Creates a new filter with a human-readable display name and allowed file extensions.
    pub fn new(name: impl Into<String>, extensions: &[&str]) -> Self {
        Self {
            name: name.into(),
            extensions: extensions
                .iter()
                .map(|ext| ext.trim_start_matches('.').to_string())
                .collect(),
        }
    }
}

/// Configuration options for opening files.
#[derive(Debug, Clone, Default)]
pub struct OpenFileOptions {
    pub title: Option<String>,
    pub starting_directory: Option<PathBuf>,
    pub filters: Vec<DialogFilter>,
    pub multiple: bool,
}

impl OpenFileOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    pub fn with_directory(mut self, directory: impl Into<PathBuf>) -> Self {
        self.starting_directory = Some(directory.into());
        self
    }

    pub fn with_filter(mut self, name: impl Into<String>, extensions: &[&str]) -> Self {
        self.filters.push(DialogFilter::new(name, extensions));
        self
    }

    pub fn with_multiple(mut self, multiple: bool) -> Self {
        self.multiple = multiple;
        self
    }
}

/// Configuration options for saving a file.
#[derive(Debug, Clone, Default)]
pub struct SaveFileOptions {
    pub title: Option<String>,
    pub starting_directory: Option<PathBuf>,
    pub default_name: Option<String>,
    pub filters: Vec<DialogFilter>,
}

impl SaveFileOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    pub fn with_directory(mut self, directory: impl Into<PathBuf>) -> Self {
        self.starting_directory = Some(directory.into());
        self
    }

    pub fn with_default_name(mut self, default_name: impl Into<String>) -> Self {
        self.default_name = Some(default_name.into());
        self
    }

    pub fn with_filter(mut self, name: impl Into<String>, extensions: &[&str]) -> Self {
        self.filters.push(DialogFilter::new(name, extensions));
        self
    }
}

/// Configuration options for selecting a folder.
#[derive(Debug, Clone, Default)]
pub struct PickFolderOptions {
    pub title: Option<String>,
    pub starting_directory: Option<PathBuf>,
}

impl PickFolderOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    pub fn with_directory(mut self, directory: impl Into<PathBuf>) -> Self {
        self.starting_directory = Some(directory.into());
        self
    }
}

/// Semantic type of message box or alert.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MessageBoxType {
    #[default]
    Info,
    Warning,
    Error,
    Question,
}

/// Set of buttons to present in a message box.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MessageBoxButtons {
    #[default]
    Ok,
    OkCancel,
    YesNo,
    YesNoCancel,
}

/// Result returned when a user interacts with a message box.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MessageBoxResult {
    Ok,
    Cancel,
    Yes,
    No,
}

/// Mock response for automated test suites.
#[derive(Debug, Clone)]
pub enum DialogMockResponse {
    OpenFile(Option<PathBuf>),
    OpenFiles(Vec<PathBuf>),
    SaveFile(Option<PathBuf>),
    PickFolder(Option<PathBuf>),
    MessageBox(MessageBoxResult),
}

#[cfg(any(test, feature = "test-internals"))]
static DIALOG_MOCK: Mutex<Option<DialogMockResponse>> = Mutex::new(None);

/// Sets an explicit mock response for testing.
///
/// NOTE: This hook is strictly feature-gated to `test-internals` and unit tests to prevent
/// unauthorized backdoor overrides in production release builds.
#[cfg(any(test, feature = "test-internals"))]
pub fn set_dialog_mock_response(response: Option<DialogMockResponse>) {
    let mut lock = DIALOG_MOCK.lock().unwrap();
    *lock = response;
}

#[cfg(any(test, feature = "test-internals"))]
fn check_mock_response() -> Option<DialogMockResponse> {
    DIALOG_MOCK.lock().unwrap().clone()
}

#[cfg(not(any(test, feature = "test-internals")))]
#[inline(always)]
fn check_mock_response() -> Option<DialogMockResponse> {
    None
}

/// Prompts user to select a single file to open.
pub fn open_file(options: OpenFileOptions) -> Option<PathBuf> {
    if let Some(DialogMockResponse::OpenFile(path)) = check_mock_response() {
        return path;
    }

    #[cfg(target_os = "linux")]
    {
        linux_backend::open_file(&options)
    }
    #[cfg(windows)]
    {
        windows_backend::open_file(&options)
    }
    #[cfg(target_os = "macos")]
    {
        macos_backend::open_file(&options)
    }
    #[cfg(not(any(target_os = "linux", windows, target_os = "macos")))]
    {
        let _ = options;
        None
    }
}

/// Prompts user to select multiple files to open.
pub fn open_multiple_files(options: OpenFileOptions) -> Vec<PathBuf> {
    if let Some(DialogMockResponse::OpenFiles(paths)) = check_mock_response() {
        return paths;
    }

    #[cfg(target_os = "linux")]
    {
        linux_backend::open_multiple_files(&options)
    }
    #[cfg(windows)]
    {
        windows_backend::open_multiple_files(&options)
    }
    #[cfg(target_os = "macos")]
    {
        macos_backend::open_multiple_files(&options)
    }
    #[cfg(not(any(target_os = "linux", windows, target_os = "macos")))]
    {
        let _ = options;
        Vec::new()
    }
}

/// Prompts user to choose a destination path for saving a file.
pub fn save_file(options: SaveFileOptions) -> Option<PathBuf> {
    if let Some(DialogMockResponse::SaveFile(path)) = check_mock_response() {
        return path;
    }

    #[cfg(target_os = "linux")]
    {
        linux_backend::save_file(&options)
    }
    #[cfg(windows)]
    {
        windows_backend::save_file(&options)
    }
    #[cfg(target_os = "macos")]
    {
        macos_backend::save_file(&options)
    }
    #[cfg(not(any(target_os = "linux", windows, target_os = "macos")))]
    {
        let _ = options;
        None
    }
}

/// Prompts user to choose a directory / folder.
pub fn pick_folder(options: PickFolderOptions) -> Option<PathBuf> {
    if let Some(DialogMockResponse::PickFolder(path)) = check_mock_response() {
        return path;
    }

    #[cfg(target_os = "linux")]
    {
        linux_backend::pick_folder(&options)
    }
    #[cfg(windows)]
    {
        windows_backend::pick_folder(&options)
    }
    #[cfg(target_os = "macos")]
    {
        macos_backend::pick_folder(&options)
    }
    #[cfg(not(any(target_os = "linux", windows, target_os = "macos")))]
    {
        let _ = options;
        None
    }
}

/// Shows a native operating system message alert / confirmation box.
pub fn show_message_box(
    title: &str,
    message: &str,
    box_type: MessageBoxType,
    buttons: MessageBoxButtons,
) -> MessageBoxResult {
    if let Some(DialogMockResponse::MessageBox(result)) = check_mock_response() {
        return result;
    }

    #[cfg(target_os = "linux")]
    {
        linux_backend::show_message_box(title, message, box_type, buttons)
    }
    #[cfg(windows)]
    {
        windows_backend::show_message_box(title, message, box_type, buttons)
    }
    #[cfg(target_os = "macos")]
    {
        macos_backend::show_message_box(title, message, box_type, buttons)
    }
    #[cfg(not(any(target_os = "linux", windows, target_os = "macos")))]
    {
        let _ = (title, message, box_type, buttons);
        MessageBoxResult::Ok
    }
}

/// Non-blocking single-file open dialog safe to call inside UI event handlers without nesting `dialog.run()`.
pub fn open_file_async<F>(options: OpenFileOptions, callback: F)
where
    F: FnOnce(Option<PathBuf>) + 'static,
{
    if let Some(DialogMockResponse::OpenFile(path)) = check_mock_response() {
        callback(path);
        return;
    }

    #[cfg(all(target_os = "linux", feature = "gtk-compat"))]
    {
        linux_backend::open_file_async(&options, callback);
    }
    #[cfg(not(all(target_os = "linux", feature = "gtk-compat")))]
    {
        callback(open_file(options));
    }
}

/// Non-blocking multi-file open dialog safe to call inside UI event handlers without nesting `dialog.run()`.
pub fn open_multiple_files_async<F>(options: OpenFileOptions, callback: F)
where
    F: FnOnce(Vec<PathBuf>) + 'static,
{
    if let Some(DialogMockResponse::OpenFiles(paths)) = check_mock_response() {
        callback(paths);
        return;
    }

    #[cfg(all(target_os = "linux", feature = "gtk-compat"))]
    {
        linux_backend::open_multiple_files_async(&options, callback);
    }
    #[cfg(not(all(target_os = "linux", feature = "gtk-compat")))]
    {
        callback(open_multiple_files(options));
    }
}

/// Non-blocking file save dialog safe to call inside UI event handlers without nesting `dialog.run()`.
pub fn save_file_async<F>(options: SaveFileOptions, callback: F)
where
    F: FnOnce(Option<PathBuf>) + 'static,
{
    if let Some(DialogMockResponse::SaveFile(path)) = check_mock_response() {
        callback(path);
        return;
    }

    #[cfg(all(target_os = "linux", feature = "gtk-compat"))]
    {
        linux_backend::save_file_async(&options, callback);
    }
    #[cfg(not(all(target_os = "linux", feature = "gtk-compat")))]
    {
        callback(save_file(options));
    }
}

/// Non-blocking folder selection dialog safe to call inside UI event handlers without nesting `dialog.run()`.
pub fn pick_folder_async<F>(options: PickFolderOptions, callback: F)
where
    F: FnOnce(Option<PathBuf>) + 'static,
{
    if let Some(DialogMockResponse::PickFolder(path)) = check_mock_response() {
        callback(path);
        return;
    }

    #[cfg(all(target_os = "linux", feature = "gtk-compat"))]
    {
        linux_backend::pick_folder_async(&options, callback);
    }
    #[cfg(not(all(target_os = "linux", feature = "gtk-compat")))]
    {
        callback(pick_folder(options));
    }
}

#[cfg(all(target_os = "linux", feature = "gtk-compat"))]
mod linux_backend {
    use super::*;
    use gtk::prelude::*;
    use std::cell::RefCell;
    use std::path::PathBuf;
    use std::rc::Rc;

    fn active_parent_window() -> Option<gtk::Window> {
        let windows: Vec<gtk::Window> = gtk::Window::list_toplevels()
            .into_iter()
            .filter_map(|w| w.downcast::<gtk::Window>().ok())
            .collect();
        windows
            .iter()
            .find(|w| w.is_visible() && w.is_active())
            .cloned()
            .or_else(|| windows.into_iter().find(|w| w.is_visible()))
    }

    fn create_file_chooser(
        title: &str,
        action: gtk::FileChooserAction,
        accept_label: &str,
        filters: &[DialogFilter],
        starting_directory: Option<&PathBuf>,
    ) -> gtk::FileChooserNative {
        let parent = active_parent_window();
        let dialog = gtk::FileChooserNative::new(
            Some(title),
            parent.as_ref(),
            action,
            Some(accept_label),
            Some("Cancel"),
        );
        apply_filters(&dialog, filters);
        if let Some(dir) = starting_directory {
            dialog.set_current_folder(dir);
        }
        dialog
    }

    fn build_open_dialog(options: &OpenFileOptions, multiple: bool) -> gtk::FileChooserNative {
        let default_title = if multiple { "Open Files" } else { "Open File" };
        let dialog = create_file_chooser(
            options.title.as_deref().unwrap_or(default_title),
            gtk::FileChooserAction::Open,
            "Open",
            &options.filters,
            options.starting_directory.as_ref(),
        );
        if multiple {
            dialog.set_select_multiple(true);
        }
        dialog
    }

    fn build_save_dialog(options: &SaveFileOptions) -> gtk::FileChooserNative {
        let dialog = create_file_chooser(
            options.title.as_deref().unwrap_or("Save File"),
            gtk::FileChooserAction::Save,
            "Save",
            &options.filters,
            options.starting_directory.as_ref(),
        );
        dialog.set_do_overwrite_confirmation(true);
        if let Some(name) = &options.default_name {
            dialog.set_current_name(name);
        }
        dialog
    }

    fn build_folder_dialog(options: &PickFolderOptions) -> gtk::FileChooserNative {
        create_file_chooser(
            options.title.as_deref().unwrap_or("Select Folder"),
            gtk::FileChooserAction::SelectFolder,
            "Select",
            &[],
            options.starting_directory.as_ref(),
        )
    }

    fn run_native_dialog_async<T: 'static>(
        dialog: gtk::FileChooserNative,
        on_accept: impl FnOnce(&gtk::FileChooserNative) -> T + 'static,
        empty: impl FnOnce() -> T + 'static,
        callback: impl FnOnce(T) + 'static,
    ) {
        if !gtk::is_initialized() {
            callback(empty());
            return;
        }
        // Keep a strong handle to the native dialog until the user picks or cancels,
        // and use non-blocking show() + connect_response so we never enter a nested
        // gtk::dialog::run() loop while AppRuntime event borrows are active.
        let keep_dlg = Rc::new(RefCell::new(Some(dialog.clone())));
        let accept_cell = Rc::new(RefCell::new(Some(on_accept)));
        let empty_cell = Rc::new(RefCell::new(Some(empty)));
        let cb_cell = Rc::new(RefCell::new(Some(callback)));

        let keep_dlg_cb = keep_dlg.clone();
        dialog.connect_response(move |dlg, response| {
            let value = if response == gtk::ResponseType::Accept {
                accept_cell.borrow_mut().take().map(|f| f(dlg))
            } else {
                empty_cell.borrow_mut().take().map(|f| f())
            };
            dlg.hide();
            dlg.destroy();
            let _ = keep_dlg_cb.borrow_mut().take();
            if let (Some(v), Some(cb)) = (value, cb_cell.borrow_mut().take()) {
                cb(v);
                crate::gtk_window::post_platform_event(crate::events::PlatformEvent::AnimationTick);
            }
        });
        dialog.show();
    }

    pub fn open_file(options: &OpenFileOptions) -> Option<PathBuf> {
        if !gtk::is_initialized() {
            return None;
        }
        let dialog = build_open_dialog(options, false);
        let response = dialog.run();
        if response == gtk::ResponseType::Accept {
            dialog.filename()
        } else {
            None
        }
    }

    pub fn open_multiple_files(options: &OpenFileOptions) -> Vec<PathBuf> {
        if !gtk::is_initialized() {
            return Vec::new();
        }
        let dialog = build_open_dialog(options, true);
        let response = dialog.run();
        if response == gtk::ResponseType::Accept {
            dialog.filenames()
        } else {
            Vec::new()
        }
    }

    pub fn save_file(options: &SaveFileOptions) -> Option<PathBuf> {
        if !gtk::is_initialized() {
            return None;
        }
        let dialog = build_save_dialog(options);
        let response = dialog.run();
        if response == gtk::ResponseType::Accept {
            dialog.filename()
        } else {
            None
        }
    }

    pub fn pick_folder(options: &PickFolderOptions) -> Option<PathBuf> {
        if !gtk::is_initialized() {
            return None;
        }
        let dialog = build_folder_dialog(options);
        let response = dialog.run();
        if response == gtk::ResponseType::Accept {
            dialog.filename()
        } else {
            None
        }
    }

    pub fn open_file_async<F: FnOnce(Option<PathBuf>) + 'static>(
        options: &OpenFileOptions,
        callback: F,
    ) {
        if !gtk::is_initialized() {
            callback(None);
            return;
        }
        run_native_dialog_async(
            build_open_dialog(options, false),
            |dlg| dlg.filename(),
            || None,
            callback,
        );
    }

    pub fn open_multiple_files_async<F: FnOnce(Vec<PathBuf>) + 'static>(
        options: &OpenFileOptions,
        callback: F,
    ) {
        if !gtk::is_initialized() {
            callback(Vec::new());
            return;
        }
        run_native_dialog_async(
            build_open_dialog(options, true),
            |dlg| dlg.filenames(),
            Vec::new,
            callback,
        );
    }

    pub fn save_file_async<F: FnOnce(Option<PathBuf>) + 'static>(
        options: &SaveFileOptions,
        callback: F,
    ) {
        if !gtk::is_initialized() {
            callback(None);
            return;
        }
        run_native_dialog_async(
            build_save_dialog(options),
            |dlg| dlg.filename(),
            || None,
            callback,
        );
    }

    pub fn pick_folder_async<F: FnOnce(Option<PathBuf>) + 'static>(
        options: &PickFolderOptions,
        callback: F,
    ) {
        if !gtk::is_initialized() {
            callback(None);
            return;
        }
        run_native_dialog_async(
            build_folder_dialog(options),
            |dlg| dlg.filename(),
            || None,
            callback,
        );
    }

    pub fn show_message_box(
        title: &str,
        message: &str,
        box_type: MessageBoxType,
        buttons: MessageBoxButtons,
    ) -> MessageBoxResult {
        if !gtk::is_initialized() {
            return MessageBoxResult::Ok;
        }
        let message_type = match box_type {
            MessageBoxType::Info => gtk::MessageType::Info,
            MessageBoxType::Warning => gtk::MessageType::Warning,
            MessageBoxType::Error => gtk::MessageType::Error,
            MessageBoxType::Question => gtk::MessageType::Question,
        };
        let buttons_type = match buttons {
            MessageBoxButtons::Ok => gtk::ButtonsType::Ok,
            MessageBoxButtons::OkCancel => gtk::ButtonsType::OkCancel,
            MessageBoxButtons::YesNo => gtk::ButtonsType::YesNo,
            MessageBoxButtons::YesNoCancel => gtk::ButtonsType::None,
        };
        let dialog = gtk::MessageDialog::new(
            active_parent_window().as_ref(),
            gtk::DialogFlags::MODAL,
            message_type,
            buttons_type,
            message,
        );
        dialog.set_title(title);
        if buttons == MessageBoxButtons::YesNoCancel {
            dialog.add_button("Yes", gtk::ResponseType::Yes);
            dialog.add_button("No", gtk::ResponseType::No);
            dialog.add_button("Cancel", gtk::ResponseType::Cancel);
        }
        let response = dialog.run();
        dialog.close();
        match response {
            gtk::ResponseType::Ok | gtk::ResponseType::Accept => MessageBoxResult::Ok,
            gtk::ResponseType::Yes => MessageBoxResult::Yes,
            gtk::ResponseType::No => MessageBoxResult::No,
            _ => MessageBoxResult::Cancel,
        }
    }

    fn apply_filters(dialog: &gtk::FileChooserNative, filters: &[DialogFilter]) {
        for f in filters {
            let filter = gtk::FileFilter::new();
            filter.set_name(Some(&f.name));
            for ext in &f.extensions {
                filter.add_pattern(&format!("*.{}", ext));
                filter.add_pattern(&format!("*.{}", ext.to_uppercase()));
            }
            dialog.add_filter(filter);
        }
    }
}

#[cfg(all(target_os = "linux", not(feature = "gtk-compat")))]
mod linux_backend {
    use super::*;
    use std::path::PathBuf;

    pub fn open_file(_options: &OpenFileOptions) -> Option<PathBuf> {
        None
    }

    pub fn open_multiple_files(_options: &OpenFileOptions) -> Vec<PathBuf> {
        Vec::new()
    }

    pub fn save_file(_options: &SaveFileOptions) -> Option<PathBuf> {
        None
    }

    pub fn pick_folder(_options: &PickFolderOptions) -> Option<PathBuf> {
        None
    }

    pub fn show_message_box(
        _title: &str,
        _message: &str,
        _box_type: MessageBoxType,
        _buttons: MessageBoxButtons,
    ) -> MessageBoxResult {
        MessageBoxResult::Ok
    }
}

#[cfg(windows)]
mod windows_backend {
    use super::*;
    use std::path::PathBuf;
    use windows_sys::Win32::Foundation::HWND;
    use windows_sys::Win32::UI::Controls::Dialogs::{
        GetOpenFileNameW, GetSaveFileNameW, OFN_ALLOWMULTISELECT, OFN_EXPLORER, OFN_FILEMUSTEXIST,
        OFN_OVERWRITEPROMPT, OFN_PATHMUSTEXIST, OPENFILENAMEW,
    };
    use windows_sys::Win32::UI::WindowsAndMessaging::{
        MessageBoxW, IDCANCEL, IDNO, IDOK, IDYES, MB_ICONERROR, MB_ICONINFORMATION,
        MB_ICONQUESTION, MB_ICONWARNING, MB_OK, MB_OKCANCEL, MB_YESNO, MB_YESNOCANCEL,
    };

    pub fn open_file(options: &OpenFileOptions) -> Option<PathBuf> {
        let mut paths = run_open_dialog(options, false);
        paths.pop()
    }

    pub fn open_multiple_files(options: &OpenFileOptions) -> Vec<PathBuf> {
        run_open_dialog(options, true)
    }

    fn run_open_dialog(options: &OpenFileOptions, multiple: bool) -> Vec<PathBuf> {
        unsafe {
            let mut buf = vec![0u16; 65536];
            let filter = build_filter_string(&options.filters);
            let title = options.title.as_ref().map(|t| {
                t.encode_utf16()
                    .chain(std::iter::once(0))
                    .collect::<Vec<u16>>()
            });
            let dir = options
                .starting_directory
                .as_ref()
                .and_then(|d| d.to_str())
                .map(|s| {
                    s.encode_utf16()
                        .chain(std::iter::once(0))
                        .collect::<Vec<u16>>()
                });

            let mut flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
            if multiple {
                flags |= OFN_ALLOWMULTISELECT;
            }

            let mut ofn: OPENFILENAMEW = std::mem::zeroed();
            ofn.lStructSize = std::mem::size_of::<OPENFILENAMEW>() as u32;
            ofn.hwndOwner = 0 as HWND;
            ofn.lpstrFilter = filter.as_ptr();
            ofn.lpstrFile = buf.as_mut_ptr();
            ofn.nMaxFile = buf.len() as u32;
            ofn.lpstrInitialDir = dir.as_ref().map(|d| d.as_ptr()).unwrap_or(std::ptr::null());
            ofn.lpstrTitle = title
                .as_ref()
                .map(|t| t.as_ptr())
                .unwrap_or(std::ptr::null());
            ofn.Flags = flags;

            if GetOpenFileNameW(&mut ofn) == 0 {
                return Vec::new();
            }

            parse_open_file_buffer(&buf)
        }
    }

    pub fn save_file(options: &SaveFileOptions) -> Option<PathBuf> {
        unsafe {
            let mut buf = vec![0u16; 4096];
            if let Some(default_name) = &options.default_name {
                let name_wide: Vec<u16> = default_name.encode_utf16().collect();
                let len = name_wide.len().min(buf.len().saturating_sub(1));
                buf[..len].copy_from_slice(&name_wide[..len]);
            }

            let filter = build_filter_string(&options.filters);
            let title = options.title.as_ref().map(|t| {
                t.encode_utf16()
                    .chain(std::iter::once(0))
                    .collect::<Vec<u16>>()
            });
            let dir = options
                .starting_directory
                .as_ref()
                .and_then(|d| d.to_str())
                .map(|s| {
                    s.encode_utf16()
                        .chain(std::iter::once(0))
                        .collect::<Vec<u16>>()
                });

            let mut ofn: OPENFILENAMEW = std::mem::zeroed();
            ofn.lStructSize = std::mem::size_of::<OPENFILENAMEW>() as u32;
            ofn.hwndOwner = 0 as HWND;
            ofn.lpstrFilter = filter.as_ptr();
            ofn.lpstrFile = buf.as_mut_ptr();
            ofn.nMaxFile = buf.len() as u32;
            ofn.lpstrInitialDir = dir.as_ref().map(|d| d.as_ptr()).unwrap_or(std::ptr::null());
            ofn.lpstrTitle = title
                .as_ref()
                .map(|t| t.as_ptr())
                .unwrap_or(std::ptr::null());
            ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;

            if GetSaveFileNameW(&mut ofn) == 0 {
                return None;
            }

            let end = buf.iter().position(|&c| c == 0).unwrap_or(buf.len());
            let path_str = String::from_utf16_lossy(&buf[..end]);
            if path_str.is_empty() {
                None
            } else {
                Some(PathBuf::from(path_str))
            }
        }
    }

    pub fn pick_folder(options: &PickFolderOptions) -> Option<PathBuf> {
        // Fallback folder picker via Windows standard directory selection
        let mut open_opt = OpenFileOptions::new();
        open_opt.title = options.title.clone();
        open_opt.starting_directory = options.starting_directory.clone();
        open_file(&open_opt).and_then(|p| {
            if p.is_dir() {
                Some(p)
            } else {
                p.parent().map(|d| d.to_path_buf())
            }
        })
    }

    pub fn show_message_box(
        title: &str,
        message: &str,
        box_type: MessageBoxType,
        buttons: MessageBoxButtons,
    ) -> MessageBoxResult {
        unsafe {
            let wide_msg: Vec<u16> = message.encode_utf16().chain(std::iter::once(0)).collect();
            let wide_title: Vec<u16> = title.encode_utf16().chain(std::iter::once(0)).collect();

            let mut flags = match box_type {
                MessageBoxType::Info => MB_ICONINFORMATION,
                MessageBoxType::Warning => MB_ICONWARNING,
                MessageBoxType::Error => MB_ICONERROR,
                MessageBoxType::Question => MB_ICONQUESTION,
            };

            flags |= match buttons {
                MessageBoxButtons::Ok => MB_OK,
                MessageBoxButtons::OkCancel => MB_OKCANCEL,
                MessageBoxButtons::YesNo => MB_YESNO,
                MessageBoxButtons::YesNoCancel => MB_YESNOCANCEL,
            };

            let res = MessageBoxW(0 as HWND, wide_msg.as_ptr(), wide_title.as_ptr(), flags);
            match res {
                IDOK => MessageBoxResult::Ok,
                IDCANCEL => MessageBoxResult::Cancel,
                IDYES => MessageBoxResult::Yes,
                IDNO => MessageBoxResult::No,
                _ => MessageBoxResult::Ok,
            }
        }
    }

    fn build_filter_string(filters: &[DialogFilter]) -> Vec<u16> {
        let mut out = Vec::new();
        for f in filters {
            out.extend(f.name.encode_utf16());
            out.push(0);
            let exts: Vec<String> = f.extensions.iter().map(|e| format!("*.{}", e)).collect();
            let ext_str = exts.join(";");
            out.extend(ext_str.encode_utf16());
            out.push(0);
        }
        if filters.is_empty() {
            out.extend("All Files (*.*)".encode_utf16());
            out.push(0);
            out.extend("*.*".encode_utf16());
            out.push(0);
        }
        out.push(0);
        out
    }

    fn parse_open_file_buffer(buf: &[u16]) -> Vec<PathBuf> {
        let mut chunks = Vec::new();
        let mut start = 0;
        for i in 0..buf.len() {
            if buf[i] == 0 {
                if start == i {
                    break;
                }
                chunks.push(String::from_utf16_lossy(&buf[start..i]));
                start = i + 1;
            }
        }
        if chunks.is_empty() {
            return Vec::new();
        }
        if chunks.len() == 1 {
            return vec![PathBuf::from(&chunks[0])];
        }
        let base_dir = PathBuf::from(&chunks[0]);
        chunks[1..]
            .iter()
            .map(|filename| base_dir.join(filename))
            .collect()
    }
}

#[cfg(target_os = "macos")]
mod macos_backend {
    use super::*;
    use std::ffi::{c_char, c_void, CString};
    use std::path::PathBuf;

    #[link(name = "objc")]
    extern "C" {
        fn objc_getClass(name: *const c_char) -> *mut c_void;
        fn sel_registerName(name: *const c_char) -> *mut c_void;
        fn objc_msgSend();
    }

    #[link(name = "AppKit", kind = "framework")]
    extern "C" {
        static NSApp: *mut c_void;
    }

    unsafe fn is_appkit_main_thread_ready() -> bool {
        if NSApp.is_null() {
            return false;
        }
        let thread_cls = objc_getClass(b"NSThread\0".as_ptr() as *const c_char);
        if thread_cls.is_null() {
            return false;
        }
        let is_main_sel = sel_registerName(b"isMainThread\0".as_ptr() as *const c_char);
        let msg_bool: unsafe extern "C" fn(*mut c_void, *mut c_void) -> i8 =
            std::mem::transmute(objc_msgSend as *const ());
        msg_bool(thread_cls, is_main_sel) != 0
    }

    pub fn open_file(options: &OpenFileOptions) -> Option<PathBuf> {
        let mut files = run_open_panel(options, false);
        files.pop()
    }

    pub fn open_multiple_files(options: &OpenFileOptions) -> Vec<PathBuf> {
        run_open_panel(options, true)
    }

    fn run_open_panel(options: &OpenFileOptions, multiple: bool) -> Vec<PathBuf> {
        unsafe {
            if !is_appkit_main_thread_ready() {
                return Vec::new();
            }
            let panel_cls = objc_getClass(b"NSOpenPanel\0".as_ptr() as *const c_char);
            if panel_cls.is_null() {
                return Vec::new();
            }
            let open_sel = sel_registerName(b"openPanel\0".as_ptr() as *const c_char);
            let msg_open: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void =
                std::mem::transmute(objc_msgSend as *const ());
            let panel = msg_open(panel_cls, open_sel);
            if panel.is_null() {
                return Vec::new();
            }

            let set_files_sel = sel_registerName(b"setCanChooseFiles:\0".as_ptr() as *const c_char);
            let msg_set_bool: unsafe extern "C" fn(*mut c_void, *mut c_void, bool) =
                std::mem::transmute(objc_msgSend as *const ());
            msg_set_bool(panel, set_files_sel, true);

            let set_dirs_sel =
                sel_registerName(b"setCanChooseDirectories:\0".as_ptr() as *const c_char);
            msg_set_bool(panel, set_dirs_sel, false);

            let set_multi_sel =
                sel_registerName(b"setAllowsMultipleSelection:\0".as_ptr() as *const c_char);
            msg_set_bool(panel, set_multi_sel, multiple);

            if let Some(title) = &options.title {
                let set_title_sel = sel_registerName(b"setTitle:\0".as_ptr() as *const c_char);
                let ns_title = ns_string(title);
                let msg_set_title: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void) =
                    std::mem::transmute(objc_msgSend as *const ());
                msg_set_title(panel, set_title_sel, ns_title);
            }

            let run_modal_sel = sel_registerName(b"runModal\0".as_ptr() as *const c_char);
            let msg_run: unsafe extern "C" fn(*mut c_void, *mut c_void) -> isize =
                std::mem::transmute(objc_msgSend as *const ());
            let response = msg_run(panel, run_modal_sel);

            // NSModalResponseOK is 1
            if response != 1 {
                return Vec::new();
            }

            let urls_sel = sel_registerName(b"URLs\0".as_ptr() as *const c_char);
            let msg_urls: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void =
                std::mem::transmute(objc_msgSend as *const ());
            let urls_array = msg_urls(panel, urls_sel);
            parse_ns_array_urls(urls_array)
        }
    }

    pub fn save_file(options: &SaveFileOptions) -> Option<PathBuf> {
        unsafe {
            if !is_appkit_main_thread_ready() {
                return None;
            }
            let panel_cls = objc_getClass(b"NSSavePanel\0".as_ptr() as *const c_char);
            if panel_cls.is_null() {
                return None;
            }
            let save_sel = sel_registerName(b"savePanel\0".as_ptr() as *const c_char);
            let msg_save: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void =
                std::mem::transmute(objc_msgSend as *const ());
            let panel = msg_save(panel_cls, save_sel);
            if panel.is_null() {
                return None;
            }

            if let Some(title) = &options.title {
                let set_title_sel = sel_registerName(b"setTitle:\0".as_ptr() as *const c_char);
                let ns_title = ns_string(title);
                let msg_set_str: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void) =
                    std::mem::transmute(objc_msgSend as *const ());
                msg_set_str(panel, set_title_sel, ns_title);
            }

            if let Some(name) = &options.default_name {
                let set_name_sel =
                    sel_registerName(b"setNameFieldStringValue:\0".as_ptr() as *const c_char);
                let ns_name = ns_string(name);
                let msg_set_str: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void) =
                    std::mem::transmute(objc_msgSend as *const ());
                msg_set_str(panel, set_name_sel, ns_name);
            }

            let run_modal_sel = sel_registerName(b"runModal\0".as_ptr() as *const c_char);
            let msg_run: unsafe extern "C" fn(*mut c_void, *mut c_void) -> isize =
                std::mem::transmute(objc_msgSend as *const ());
            let response = msg_run(panel, run_modal_sel);

            if response != 1 {
                return None;
            }

            let url_sel = sel_registerName(b"URL\0".as_ptr() as *const c_char);
            let msg_url: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void =
                std::mem::transmute(objc_msgSend as *const ());
            let url = msg_url(panel, url_sel);
            path_from_ns_url(url)
        }
    }

    pub fn pick_folder(options: &PickFolderOptions) -> Option<PathBuf> {
        unsafe {
            if !is_appkit_main_thread_ready() {
                return None;
            }
            let panel_cls = objc_getClass(b"NSOpenPanel\0".as_ptr() as *const c_char);
            if panel_cls.is_null() {
                return None;
            }
            let open_sel = sel_registerName(b"openPanel\0".as_ptr() as *const c_char);
            let msg_open: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void =
                std::mem::transmute(objc_msgSend as *const ());
            let panel = msg_open(panel_cls, open_sel);
            if panel.is_null() {
                return None;
            }

            let set_files_sel = sel_registerName(b"setCanChooseFiles:\0".as_ptr() as *const c_char);
            let msg_set_bool: unsafe extern "C" fn(*mut c_void, *mut c_void, bool) =
                std::mem::transmute(objc_msgSend as *const ());
            msg_set_bool(panel, set_files_sel, false);

            let set_dirs_sel =
                sel_registerName(b"setCanChooseDirectories:\0".as_ptr() as *const c_char);
            msg_set_bool(panel, set_dirs_sel, true);

            let set_multi_sel =
                sel_registerName(b"setAllowsMultipleSelection:\0".as_ptr() as *const c_char);
            msg_set_bool(panel, set_multi_sel, false);

            if let Some(title) = &options.title {
                let set_title_sel = sel_registerName(b"setTitle:\0".as_ptr() as *const c_char);
                let ns_title = ns_string(title);
                let msg_set_title: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void) =
                    std::mem::transmute(objc_msgSend as *const ());
                msg_set_title(panel, set_title_sel, ns_title);
            }

            let run_modal_sel = sel_registerName(b"runModal\0".as_ptr() as *const c_char);
            let msg_run: unsafe extern "C" fn(*mut c_void, *mut c_void) -> isize =
                std::mem::transmute(objc_msgSend as *const ());
            let response = msg_run(panel, run_modal_sel);

            if response != 1 {
                return None;
            }

            let url_sel = sel_registerName(b"URL\0".as_ptr() as *const c_char);
            let msg_url: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void =
                std::mem::transmute(objc_msgSend as *const ());
            let url = msg_url(panel, url_sel);
            path_from_ns_url(url)
        }
    }

    pub fn show_message_box(
        title: &str,
        message: &str,
        _box_type: MessageBoxType,
        buttons: MessageBoxButtons,
    ) -> MessageBoxResult {
        unsafe {
            if !is_appkit_main_thread_ready() {
                return MessageBoxResult::Ok;
            }
            let alert_cls = objc_getClass(b"NSAlert\0".as_ptr() as *const c_char);
            if alert_cls.is_null() {
                return MessageBoxResult::Ok;
            }
            let alloc_sel = sel_registerName(b"alloc\0".as_ptr() as *const c_char);
            let init_sel = sel_registerName(b"init\0".as_ptr() as *const c_char);
            let msg_alloc: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void =
                std::mem::transmute(objc_msgSend as *const ());
            let alert_inst = msg_alloc(alert_cls, alloc_sel);
            let alert = msg_alloc(alert_inst, init_sel);

            let set_msg_sel = sel_registerName(b"setMessageText:\0".as_ptr() as *const c_char);
            let ns_title = ns_string(title);
            let msg_set_str: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void) =
                std::mem::transmute(objc_msgSend as *const ());
            msg_set_str(alert, set_msg_sel, ns_title);

            let set_info_sel = sel_registerName(b"setInformativeText:\0".as_ptr() as *const c_char);
            let ns_msg = ns_string(message);
            msg_set_str(alert, set_info_sel, ns_msg);

            let add_btn_sel = sel_registerName(b"addButtonWithTitle:\0".as_ptr() as *const c_char);
            match buttons {
                MessageBoxButtons::Ok => {
                    msg_set_str(alert, add_btn_sel, ns_string("OK"));
                }
                MessageBoxButtons::OkCancel => {
                    msg_set_str(alert, add_btn_sel, ns_string("OK"));
                    msg_set_str(alert, add_btn_sel, ns_string("Cancel"));
                }
                MessageBoxButtons::YesNo => {
                    msg_set_str(alert, add_btn_sel, ns_string("Yes"));
                    msg_set_str(alert, add_btn_sel, ns_string("No"));
                }
                MessageBoxButtons::YesNoCancel => {
                    msg_set_str(alert, add_btn_sel, ns_string("Yes"));
                    msg_set_str(alert, add_btn_sel, ns_string("No"));
                    msg_set_str(alert, add_btn_sel, ns_string("Cancel"));
                }
            }

            let run_modal_sel = sel_registerName(b"runModal\0".as_ptr() as *const c_char);
            let msg_run: unsafe extern "C" fn(*mut c_void, *mut c_void) -> isize =
                std::mem::transmute(objc_msgSend as *const ());
            let response = msg_run(alert, run_modal_sel);

            // First button added returns 1000, second 1001, third 1002
            match buttons {
                MessageBoxButtons::Ok => MessageBoxResult::Ok,
                MessageBoxButtons::OkCancel => {
                    if response == 1000 {
                        MessageBoxResult::Ok
                    } else {
                        MessageBoxResult::Cancel
                    }
                }
                MessageBoxButtons::YesNo => {
                    if response == 1000 {
                        MessageBoxResult::Yes
                    } else {
                        MessageBoxResult::No
                    }
                }
                MessageBoxButtons::YesNoCancel => {
                    if response == 1000 {
                        MessageBoxResult::Yes
                    } else if response == 1001 {
                        MessageBoxResult::No
                    } else {
                        MessageBoxResult::Cancel
                    }
                }
            }
        }
    }

    unsafe fn ns_string(s: &str) -> *mut c_void {
        let cls = objc_getClass(b"NSString\0".as_ptr() as *const c_char);
        let sel = sel_registerName(b"stringWithUTF8String:\0".as_ptr() as *const c_char);
        let cstr = CString::new(s).unwrap_or_default();
        let msg: unsafe extern "C" fn(*mut c_void, *mut c_void, *const c_char) -> *mut c_void =
            std::mem::transmute(objc_msgSend as *const ());
        msg(cls, sel, cstr.as_ptr())
    }

    unsafe fn path_from_ns_url(url: *mut c_void) -> Option<PathBuf> {
        if url.is_null() {
            return None;
        }
        let path_sel = sel_registerName(b"path\0".as_ptr() as *const c_char);
        let msg_path: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void =
            std::mem::transmute(objc_msgSend as *const ());
        let path_str = msg_path(url, path_sel);
        if path_str.is_null() {
            return None;
        }
        let utf8_sel = sel_registerName(b"UTF8String\0".as_ptr() as *const c_char);
        let msg_utf8: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *const c_char =
            std::mem::transmute(objc_msgSend as *const ());
        let c_str = msg_utf8(path_str, utf8_sel);
        if c_str.is_null() {
            return None;
        }
        let slice = std::ffi::CStr::from_ptr(c_str).to_str().ok()?;
        Some(PathBuf::from(slice))
    }

    unsafe fn parse_ns_array_urls(array: *mut c_void) -> Vec<PathBuf> {
        if array.is_null() {
            return Vec::new();
        }
        let count_sel = sel_registerName(b"count\0".as_ptr() as *const c_char);
        let msg_count: unsafe extern "C" fn(*mut c_void, *mut c_void) -> usize =
            std::mem::transmute(objc_msgSend as *const ());
        let count = msg_count(array, count_sel);
        let obj_at_sel = sel_registerName(b"objectAtIndex:\0".as_ptr() as *const c_char);
        let msg_obj: unsafe extern "C" fn(*mut c_void, *mut c_void, usize) -> *mut c_void =
            std::mem::transmute(objc_msgSend as *const ());

        let mut out = Vec::with_capacity(count);
        for i in 0..count {
            let url = msg_obj(array, obj_at_sel, i);
            if let Some(path) = path_from_ns_url(url) {
                out.push(path);
            }
        }
        out
    }
}