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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT
use crate::{FileFilter, Window};
use glib::{
prelude::*,
signal::{connect_raw, SignalHandlerId},
translate::*,
};
use std::{boxed::Box as Box_, fmt, mem::transmute, pin::Pin, ptr};
glib::wrapper! {
/// A [`FileDialog`][crate::FileDialog] object collects the arguments that
/// are needed to present a file chooser dialog to the
/// user, such as a title for the dialog and whether it
/// should be modal.
///
/// The dialog is shown with [`open()`][Self::open()],
/// [`save()`][Self::save()], etc. These APIs follow the
/// GIO async pattern, and the result can be obtained by calling
/// the corresponding finish function, for example
/// `Gtk::FileDialog::open_finish()`.
///
/// ## Properties
///
///
/// #### `accept-label`
/// Label for the file chooser's accept button.
///
/// Readable | Writeable
///
///
/// #### `default-filter`
/// The default filter, that is, the filter that is initially
/// active in the file chooser dialog.
///
/// If the default filter is [`None`], the first filter of [`filters`][struct@crate::FileDialog#filters]
/// is used as the default filter. If that property contains no filter, the dialog will
/// be unfiltered.
///
/// If [`filters`][struct@crate::FileDialog#filters] is not [`None`], the default filter should be part
/// of the list. If it is not, the dialog may choose to not make it available.
///
/// Readable | Writeable
///
///
/// #### `filters`
/// The list of filters.
///
/// See [`default-filter`][struct@crate::FileDialog#default-filter] about how those two properties interact.
///
/// Readable | Writeable
///
///
/// #### `initial-file`
/// The initial file, that is, the file that is initially selected
/// in the file chooser dialog
///
/// This is a utility property that sets both [`initial-folder`][struct@crate::FileDialog#initial-folder] and
/// [`initial-name`][struct@crate::FileDialog#initial-name].
///
/// Readable | Writeable
///
///
/// #### `initial-folder`
/// The initial folder, that is, the directory that is initially
/// opened in the file chooser dialog
///
/// Readable | Writeable
///
///
/// #### `initial-name`
/// The initial name, that is, the filename that is initially
/// selected in the file chooser dialog.
///
/// Readable | Writeable
///
///
/// #### `modal`
/// Whether the file chooser dialog is modal.
///
/// Readable | Writeable
///
///
/// #### `title`
/// A title that may be shown on the file chooser dialog.
///
/// Readable | Writeable
///
/// # Implements
///
/// [`trait@glib::ObjectExt`]
#[doc(alias = "GtkFileDialog")]
pub struct FileDialog(Object<ffi::GtkFileDialog, ffi::GtkFileDialogClass>);
match fn {
type_ => || ffi::gtk_file_dialog_get_type(),
}
}
impl FileDialog {
/// Creates a new [`FileDialog`][crate::FileDialog] object.
///
/// # Returns
///
/// the new [`FileDialog`][crate::FileDialog]
#[doc(alias = "gtk_file_dialog_new")]
pub fn new() -> FileDialog {
assert_initialized_main_thread!();
unsafe { from_glib_full(ffi::gtk_file_dialog_new()) }
}
// rustdoc-stripper-ignore-next
/// Creates a new builder-pattern struct instance to construct [`FileDialog`] objects.
///
/// This method returns an instance of [`FileDialogBuilder`](crate::builders::FileDialogBuilder) which can be used to create [`FileDialog`] objects.
pub fn builder() -> FileDialogBuilder {
FileDialogBuilder::new()
}
///
/// # Returns
///
/// the label shown on the file chooser's accept button.
#[doc(alias = "gtk_file_dialog_get_accept_label")]
#[doc(alias = "get_accept_label")]
pub fn accept_label(&self) -> Option<glib::GString> {
unsafe { from_glib_none(ffi::gtk_file_dialog_get_accept_label(self.to_glib_none().0)) }
}
/// Gets the filter that will be selected by default
/// in the file chooser dialog.
///
/// # Returns
///
/// the current filter
#[doc(alias = "gtk_file_dialog_get_default_filter")]
#[doc(alias = "get_default_filter")]
pub fn default_filter(&self) -> Option<FileFilter> {
unsafe {
from_glib_none(ffi::gtk_file_dialog_get_default_filter(
self.to_glib_none().0,
))
}
}
/// Gets the filters that will be offered to the user
/// in the file chooser dialog.
///
/// # Returns
///
/// the filters, as
/// a `GListModel` of `GtkFileFilters`
#[doc(alias = "gtk_file_dialog_get_filters")]
#[doc(alias = "get_filters")]
pub fn filters(&self) -> Option<gio::ListModel> {
unsafe { from_glib_none(ffi::gtk_file_dialog_get_filters(self.to_glib_none().0)) }
}
/// Gets the file that will be initially selected in
/// the file chooser dialog.
///
/// # Returns
///
/// the file
#[doc(alias = "gtk_file_dialog_get_initial_file")]
#[doc(alias = "get_initial_file")]
pub fn initial_file(&self) -> Option<gio::File> {
unsafe { from_glib_none(ffi::gtk_file_dialog_get_initial_file(self.to_glib_none().0)) }
}
/// Gets the folder that will be set as the
/// initial folder in the file chooser dialog.
///
/// # Returns
///
/// the folder
#[doc(alias = "gtk_file_dialog_get_initial_folder")]
#[doc(alias = "get_initial_folder")]
pub fn initial_folder(&self) -> Option<gio::File> {
unsafe {
from_glib_none(ffi::gtk_file_dialog_get_initial_folder(
self.to_glib_none().0,
))
}
}
/// Gets the name for the file that should be initially set.
///
/// # Returns
///
/// the name
#[doc(alias = "gtk_file_dialog_get_initial_name")]
#[doc(alias = "get_initial_name")]
pub fn initial_name(&self) -> Option<glib::GString> {
unsafe { from_glib_none(ffi::gtk_file_dialog_get_initial_name(self.to_glib_none().0)) }
}
/// Returns whether the file chooser dialog
/// blocks interaction with the parent window
/// while it is presented.
///
/// # Returns
///
/// `TRUE` if the file chooser dialog is modal
#[doc(alias = "gtk_file_dialog_get_modal")]
#[doc(alias = "get_modal")]
pub fn is_modal(&self) -> bool {
unsafe { from_glib(ffi::gtk_file_dialog_get_modal(self.to_glib_none().0)) }
}
/// Returns the title that will be shown on the
/// file chooser dialog.
///
/// # Returns
///
/// the title
#[doc(alias = "gtk_file_dialog_get_title")]
#[doc(alias = "get_title")]
pub fn title(&self) -> glib::GString {
unsafe { from_glib_none(ffi::gtk_file_dialog_get_title(self.to_glib_none().0)) }
}
/// This function initiates a file selection operation by
/// presenting a file chooser dialog to the user.
///
/// The @callback will be called when the dialog is dismissed.
/// It should call `Gtk::FileDialog::open_finish()`
/// to obtain the result.
/// ## `parent`
/// the parent [`Window`][crate::Window]
/// ## `cancellable`
/// a `GCancellable` to cancel the operation
/// ## `callback`
/// a callback to call when the operation is complete
#[doc(alias = "gtk_file_dialog_open")]
pub fn open<P: FnOnce(Result<gio::File, glib::Error>) + 'static>(
&self,
parent: Option<&impl IsA<Window>>,
cancellable: Option<&impl IsA<gio::Cancellable>>,
callback: P,
) {
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn open_trampoline<
P: FnOnce(Result<gio::File, glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut gio::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = ptr::null_mut();
let ret = ffi::gtk_file_dialog_open_finish(_source_object as *mut _, res, &mut error);
let result = if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
let callback = open_trampoline::<P>;
unsafe {
ffi::gtk_file_dialog_open(
self.to_glib_none().0,
parent.map(|p| p.as_ref()).to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
pub fn open_future(
&self,
parent: Option<&(impl IsA<Window> + Clone + 'static)>,
) -> Pin<Box_<dyn std::future::Future<Output = Result<gio::File, glib::Error>> + 'static>> {
let parent = parent.map(ToOwned::to_owned);
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
obj.open(
parent.as_ref().map(::std::borrow::Borrow::borrow),
Some(cancellable),
move |res| {
send.resolve(res);
},
);
}))
}
/// This function initiates a multi-file selection operation by
/// presenting a file chooser dialog to the user.
///
/// The file chooser will initially be opened in the directory
/// [`initial-folder`][struct@crate::FileDialog#initial-folder].
///
/// The @callback will be called when the dialog is dismissed.
/// It should call `Gtk::FileDialog::open_multiple_finish()`
/// to obtain the result.
/// ## `parent`
/// the parent [`Window`][crate::Window]
/// ## `cancellable`
/// a `GCancellable` to cancel the operation
/// ## `callback`
/// a callback to call when the operation is complete
#[doc(alias = "gtk_file_dialog_open_multiple")]
pub fn open_multiple<P: FnOnce(Result<gio::ListModel, glib::Error>) + 'static>(
&self,
parent: Option<&impl IsA<Window>>,
cancellable: Option<&impl IsA<gio::Cancellable>>,
callback: P,
) {
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn open_multiple_trampoline<
P: FnOnce(Result<gio::ListModel, glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut gio::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = ptr::null_mut();
let ret = ffi::gtk_file_dialog_open_multiple_finish(
_source_object as *mut _,
res,
&mut error,
);
let result = if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
let callback = open_multiple_trampoline::<P>;
unsafe {
ffi::gtk_file_dialog_open_multiple(
self.to_glib_none().0,
parent.map(|p| p.as_ref()).to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
pub fn open_multiple_future(
&self,
parent: Option<&(impl IsA<Window> + Clone + 'static)>,
) -> Pin<Box_<dyn std::future::Future<Output = Result<gio::ListModel, glib::Error>> + 'static>>
{
let parent = parent.map(ToOwned::to_owned);
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
obj.open_multiple(
parent.as_ref().map(::std::borrow::Borrow::borrow),
Some(cancellable),
move |res| {
send.resolve(res);
},
);
}))
}
/// This function initiates a file save operation by
/// presenting a file chooser dialog to the user.
///
/// The @callback will be called when the dialog is dismissed.
/// It should call `Gtk::FileDialog::save_finish()`
/// to obtain the result.
/// ## `parent`
/// the parent [`Window`][crate::Window]
/// ## `cancellable`
/// a `GCancellable` to cancel the operation
/// ## `callback`
/// a callback to call when the operation is complete
#[doc(alias = "gtk_file_dialog_save")]
pub fn save<P: FnOnce(Result<gio::File, glib::Error>) + 'static>(
&self,
parent: Option<&impl IsA<Window>>,
cancellable: Option<&impl IsA<gio::Cancellable>>,
callback: P,
) {
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn save_trampoline<
P: FnOnce(Result<gio::File, glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut gio::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = ptr::null_mut();
let ret = ffi::gtk_file_dialog_save_finish(_source_object as *mut _, res, &mut error);
let result = if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
let callback = save_trampoline::<P>;
unsafe {
ffi::gtk_file_dialog_save(
self.to_glib_none().0,
parent.map(|p| p.as_ref()).to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
pub fn save_future(
&self,
parent: Option<&(impl IsA<Window> + Clone + 'static)>,
) -> Pin<Box_<dyn std::future::Future<Output = Result<gio::File, glib::Error>> + 'static>> {
let parent = parent.map(ToOwned::to_owned);
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
obj.save(
parent.as_ref().map(::std::borrow::Borrow::borrow),
Some(cancellable),
move |res| {
send.resolve(res);
},
);
}))
}
/// This function initiates a directory selection operation by
/// presenting a file chooser dialog to the user.
///
/// If you pass @initial_folder, the file chooser will initially be
/// opened in the parent directory of that folder, otherwise, it
/// will be in the directory [`initial-folder`][struct@crate::FileDialog#initial-folder].
///
/// The @callback will be called when the dialog is dismissed.
/// It should call `Gtk::FileDialog::select_folder_finish()`
/// to obtain the result.
/// ## `parent`
/// the parent [`Window`][crate::Window]
/// ## `cancellable`
/// a `GCancellable` to cancel the operation
/// ## `callback`
/// a callback to call when the operation is complete
#[doc(alias = "gtk_file_dialog_select_folder")]
pub fn select_folder<P: FnOnce(Result<gio::File, glib::Error>) + 'static>(
&self,
parent: Option<&impl IsA<Window>>,
cancellable: Option<&impl IsA<gio::Cancellable>>,
callback: P,
) {
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn select_folder_trampoline<
P: FnOnce(Result<gio::File, glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut gio::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = ptr::null_mut();
let ret = ffi::gtk_file_dialog_select_folder_finish(
_source_object as *mut _,
res,
&mut error,
);
let result = if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
let callback = select_folder_trampoline::<P>;
unsafe {
ffi::gtk_file_dialog_select_folder(
self.to_glib_none().0,
parent.map(|p| p.as_ref()).to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
pub fn select_folder_future(
&self,
parent: Option<&(impl IsA<Window> + Clone + 'static)>,
) -> Pin<Box_<dyn std::future::Future<Output = Result<gio::File, glib::Error>> + 'static>> {
let parent = parent.map(ToOwned::to_owned);
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
obj.select_folder(
parent.as_ref().map(::std::borrow::Borrow::borrow),
Some(cancellable),
move |res| {
send.resolve(res);
},
);
}))
}
/// This function initiates a multi-directory selection operation by
/// presenting a file chooser dialog to the user.
///
/// The file chooser will initially be opened in the directory
/// [`initial-folder`][struct@crate::FileDialog#initial-folder].
///
/// The @callback will be called when the dialog is dismissed.
/// It should call `Gtk::FileDialog::select_multiple_folders_finish()`
/// to obtain the result.
/// ## `parent`
/// the parent [`Window`][crate::Window]
/// ## `cancellable`
/// a `GCancellable` to cancel the operation
/// ## `callback`
/// a callback to call when the operation is complete
#[doc(alias = "gtk_file_dialog_select_multiple_folders")]
pub fn select_multiple_folders<
P: FnOnce(Result<Option<gio::ListModel>, glib::Error>) + 'static,
>(
&self,
parent: Option<&impl IsA<Window>>,
cancellable: Option<&impl IsA<gio::Cancellable>>,
callback: P,
) {
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn select_multiple_folders_trampoline<
P: FnOnce(Result<Option<gio::ListModel>, glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut gio::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = ptr::null_mut();
let ret = ffi::gtk_file_dialog_select_multiple_folders_finish(
_source_object as *mut _,
res,
&mut error,
);
let result = if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
let callback = select_multiple_folders_trampoline::<P>;
unsafe {
ffi::gtk_file_dialog_select_multiple_folders(
self.to_glib_none().0,
parent.map(|p| p.as_ref()).to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
pub fn select_multiple_folders_future(
&self,
parent: Option<&(impl IsA<Window> + Clone + 'static)>,
) -> Pin<
Box_<
dyn std::future::Future<Output = Result<Option<gio::ListModel>, glib::Error>> + 'static,
>,
> {
let parent = parent.map(ToOwned::to_owned);
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
obj.select_multiple_folders(
parent.as_ref().map(::std::borrow::Borrow::borrow),
Some(cancellable),
move |res| {
send.resolve(res);
},
);
}))
}
/// Sets the label shown on the file chooser's accept button.
///
/// Leaving the accept label unset or setting it as `NULL` will fall back to
/// a default label, depending on what API is used to launch the file dialog.
/// ## `accept_label`
/// the new accept label
#[doc(alias = "gtk_file_dialog_set_accept_label")]
pub fn set_accept_label(&self, accept_label: Option<&str>) {
unsafe {
ffi::gtk_file_dialog_set_accept_label(
self.to_glib_none().0,
accept_label.to_glib_none().0,
);
}
}
/// Sets the filter that will be selected by default
/// in the file chooser dialog.
///
/// If set to [`None`], the first item in [`filters`][struct@crate::FileDialog#filters]
/// will be used as the default filter. If that list is empty, the dialog
/// will be unfiltered.
/// ## `filter`
/// a [`FileFilter`][crate::FileFilter]
#[doc(alias = "gtk_file_dialog_set_default_filter")]
pub fn set_default_filter(&self, filter: Option<&FileFilter>) {
unsafe {
ffi::gtk_file_dialog_set_default_filter(self.to_glib_none().0, filter.to_glib_none().0);
}
}
/// Sets the filters that will be offered to the user
/// in the file chooser dialog.
/// ## `filters`
/// a `GListModel` of `GtkFileFilters`
#[doc(alias = "gtk_file_dialog_set_filters")]
pub fn set_filters(&self, filters: Option<&impl IsA<gio::ListModel>>) {
unsafe {
ffi::gtk_file_dialog_set_filters(
self.to_glib_none().0,
filters.map(|p| p.as_ref()).to_glib_none().0,
);
}
}
/// Sets the file that will be initially selected in
/// the file chooser dialog.
///
/// This function is a shortcut for calling both
/// gtk_file_dialog_set_initial_folder() and
/// gtk_file_dialog_set_initial_name() with the directory and
/// name of @file respectively.
/// ## `file`
/// a `GFile`
#[doc(alias = "gtk_file_dialog_set_initial_file")]
pub fn set_initial_file(&self, file: Option<&impl IsA<gio::File>>) {
unsafe {
ffi::gtk_file_dialog_set_initial_file(
self.to_glib_none().0,
file.map(|p| p.as_ref()).to_glib_none().0,
);
}
}
/// Sets the folder that will be set as the
/// initial folder in the file chooser dialog.
/// ## `folder`
/// a `GFile`
#[doc(alias = "gtk_file_dialog_set_initial_folder")]
pub fn set_initial_folder(&self, folder: Option<&impl IsA<gio::File>>) {
unsafe {
ffi::gtk_file_dialog_set_initial_folder(
self.to_glib_none().0,
folder.map(|p| p.as_ref()).to_glib_none().0,
);
}
}
/// Sets the name for the file that should be initially set.
/// For saving dialogs, this will usually be pre-entered into the name field.
///
/// If a file with this name already exists in the directory set via
/// [`initial-folder`][struct@crate::FileDialog#initial-folder], the dialog should preselect it.
/// ## `name`
/// a UTF8 string
#[doc(alias = "gtk_file_dialog_set_initial_name")]
pub fn set_initial_name(&self, name: Option<&str>) {
unsafe {
ffi::gtk_file_dialog_set_initial_name(self.to_glib_none().0, name.to_glib_none().0);
}
}
/// Sets whether the file chooser dialog
/// blocks interaction with the parent window
/// while it is presented.
/// ## `modal`
/// the new value
#[doc(alias = "gtk_file_dialog_set_modal")]
pub fn set_modal(&self, modal: bool) {
unsafe {
ffi::gtk_file_dialog_set_modal(self.to_glib_none().0, modal.into_glib());
}
}
/// Sets the title that will be shown on the
/// file chooser dialog.
/// ## `title`
/// the new title
#[doc(alias = "gtk_file_dialog_set_title")]
pub fn set_title(&self, title: &str) {
unsafe {
ffi::gtk_file_dialog_set_title(self.to_glib_none().0, title.to_glib_none().0);
}
}
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
#[doc(alias = "accept-label")]
pub fn connect_accept_label_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_accept_label_trampoline<F: Fn(&FileDialog) + 'static>(
this: *mut ffi::GtkFileDialog,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(&from_glib_borrow(this))
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
b"notify::accept-label\0".as_ptr() as *const _,
Some(transmute::<_, unsafe extern "C" fn()>(
notify_accept_label_trampoline::<F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
#[doc(alias = "default-filter")]
pub fn connect_default_filter_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_default_filter_trampoline<F: Fn(&FileDialog) + 'static>(
this: *mut ffi::GtkFileDialog,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(&from_glib_borrow(this))
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
b"notify::default-filter\0".as_ptr() as *const _,
Some(transmute::<_, unsafe extern "C" fn()>(
notify_default_filter_trampoline::<F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
#[doc(alias = "filters")]
pub fn connect_filters_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_filters_trampoline<F: Fn(&FileDialog) + 'static>(
this: *mut ffi::GtkFileDialog,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(&from_glib_borrow(this))
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
b"notify::filters\0".as_ptr() as *const _,
Some(transmute::<_, unsafe extern "C" fn()>(
notify_filters_trampoline::<F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
#[doc(alias = "initial-file")]
pub fn connect_initial_file_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_initial_file_trampoline<F: Fn(&FileDialog) + 'static>(
this: *mut ffi::GtkFileDialog,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(&from_glib_borrow(this))
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
b"notify::initial-file\0".as_ptr() as *const _,
Some(transmute::<_, unsafe extern "C" fn()>(
notify_initial_file_trampoline::<F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
#[doc(alias = "initial-folder")]
pub fn connect_initial_folder_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_initial_folder_trampoline<F: Fn(&FileDialog) + 'static>(
this: *mut ffi::GtkFileDialog,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(&from_glib_borrow(this))
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
b"notify::initial-folder\0".as_ptr() as *const _,
Some(transmute::<_, unsafe extern "C" fn()>(
notify_initial_folder_trampoline::<F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
#[doc(alias = "initial-name")]
pub fn connect_initial_name_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_initial_name_trampoline<F: Fn(&FileDialog) + 'static>(
this: *mut ffi::GtkFileDialog,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(&from_glib_borrow(this))
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
b"notify::initial-name\0".as_ptr() as *const _,
Some(transmute::<_, unsafe extern "C" fn()>(
notify_initial_name_trampoline::<F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
#[doc(alias = "modal")]
pub fn connect_modal_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_modal_trampoline<F: Fn(&FileDialog) + 'static>(
this: *mut ffi::GtkFileDialog,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(&from_glib_borrow(this))
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
b"notify::modal\0".as_ptr() as *const _,
Some(transmute::<_, unsafe extern "C" fn()>(
notify_modal_trampoline::<F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
#[doc(alias = "title")]
pub fn connect_title_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_title_trampoline<F: Fn(&FileDialog) + 'static>(
this: *mut ffi::GtkFileDialog,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(&from_glib_borrow(this))
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
b"notify::title\0".as_ptr() as *const _,
Some(transmute::<_, unsafe extern "C" fn()>(
notify_title_trampoline::<F> as *const (),
)),
Box_::into_raw(f),
)
}
}
}
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
impl Default for FileDialog {
fn default() -> Self {
Self::new()
}
}
// rustdoc-stripper-ignore-next
/// A [builder-pattern] type to construct [`FileDialog`] objects.
///
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
#[must_use = "The builder must be built to be used"]
pub struct FileDialogBuilder {
builder: glib::object::ObjectBuilder<'static, FileDialog>,
}
impl FileDialogBuilder {
fn new() -> Self {
Self {
builder: glib::object::Object::builder(),
}
}
/// Label for the file chooser's accept button.
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
pub fn accept_label(self, accept_label: impl Into<glib::GString>) -> Self {
Self {
builder: self.builder.property("accept-label", accept_label.into()),
}
}
/// The default filter, that is, the filter that is initially
/// active in the file chooser dialog.
///
/// If the default filter is [`None`], the first filter of [`filters`][struct@crate::FileDialog#filters]
/// is used as the default filter. If that property contains no filter, the dialog will
/// be unfiltered.
///
/// If [`filters`][struct@crate::FileDialog#filters] is not [`None`], the default filter should be part
/// of the list. If it is not, the dialog may choose to not make it available.
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
pub fn default_filter(self, default_filter: &FileFilter) -> Self {
Self {
builder: self
.builder
.property("default-filter", default_filter.clone()),
}
}
/// The list of filters.
///
/// See [`default-filter`][struct@crate::FileDialog#default-filter] about how those two properties interact.
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
pub fn filters(self, filters: &impl IsA<gio::ListModel>) -> Self {
Self {
builder: self.builder.property("filters", filters.clone().upcast()),
}
}
/// The initial file, that is, the file that is initially selected
/// in the file chooser dialog
///
/// This is a utility property that sets both [`initial-folder`][struct@crate::FileDialog#initial-folder] and
/// [`initial-name`][struct@crate::FileDialog#initial-name].
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
pub fn initial_file(self, initial_file: &impl IsA<gio::File>) -> Self {
Self {
builder: self
.builder
.property("initial-file", initial_file.clone().upcast()),
}
}
/// The initial folder, that is, the directory that is initially
/// opened in the file chooser dialog
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
pub fn initial_folder(self, initial_folder: &impl IsA<gio::File>) -> Self {
Self {
builder: self
.builder
.property("initial-folder", initial_folder.clone().upcast()),
}
}
/// The initial name, that is, the filename that is initially
/// selected in the file chooser dialog.
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
pub fn initial_name(self, initial_name: impl Into<glib::GString>) -> Self {
Self {
builder: self.builder.property("initial-name", initial_name.into()),
}
}
/// Whether the file chooser dialog is modal.
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
pub fn modal(self, modal: bool) -> Self {
Self {
builder: self.builder.property("modal", modal),
}
}
/// A title that may be shown on the file chooser dialog.
#[cfg(feature = "v4_10")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
pub fn title(self, title: impl Into<glib::GString>) -> Self {
Self {
builder: self.builder.property("title", title.into()),
}
}
// rustdoc-stripper-ignore-next
/// Build the [`FileDialog`].
#[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
pub fn build(self) -> FileDialog {
self.builder.build()
}
}
impl fmt::Display for FileDialog {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("FileDialog")
}
}