Struct gtk4::FileChooserNative [−][src]
pub struct FileChooserNative(_);
Expand description
FileChooserNative
is an abstraction of a dialog suitable
for use with “File Open” or “File Save as” commands.
By default, this just uses a FileChooserDialog
to implement
the actual dialog. However, on some platforms, such as Windows and
macOS, the native platform file chooser is used instead. When the
application is running in a sandboxed environment without direct
filesystem access (such as Flatpak), FileChooserNative
may call
the proper APIs (portals) to let the user choose a file and make it
available to the application.
While the API of FileChooserNative
closely mirrors FileChooserDialog
,
the main difference is that there is no access to any Window
or Widget
for the dialog. This is required, as there may not be one in the case of a
platform native dialog.
Showing, hiding and running the dialog is handled by the
NativeDialog
functions.
Note that unlike FileChooserDialog
, FileChooserNative
objects
are not toplevel widgets, and GTK does not keep them alive. It is your
responsibility to keep a reference until you are done with the
object.
Typical usage
In the simplest of cases, you can the following code to use
FileChooserNative
to select a file for opening:
⚠️ The following code is in c ⚠️
static void
on_response (GtkNativeDialog *native,
int response)
{
if (response == GTK_RESPONSE_ACCEPT)
{
GtkFileChooser *chooser = GTK_FILE_CHOOSER (native);
GFile *file = gtk_file_chooser_get_file (chooser);
open_file (file);
g_object_unref (file);
}
g_object_unref (native);
}
// ...
GtkFileChooserNative *native;
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
native = gtk_file_chooser_native_new ("Open File",
parent_window,
action,
"_Open",
"_Cancel");
g_signal_connect (native, "response", G_CALLBACK (on_response), NULL);
gtk_native_dialog_show (GTK_NATIVE_DIALOG (native));
To use a FileChooserNative
for saving, you can use this:
⚠️ The following code is in c ⚠️
static void
on_response (GtkNativeDialog *native,
int response)
{
if (response == GTK_RESPONSE_ACCEPT)
{
GtkFileChooser *chooser = GTK_FILE_CHOOSER (native);
GFile *file = gtk_file_chooser_get_file (chooser);
save_to_file (file);
g_object_unref (file);
}
g_object_unref (native);
}
// ...
GtkFileChooserNative *native;
GtkFileChooser *chooser;
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_SAVE;
native = gtk_file_chooser_native_new ("Save File",
parent_window,
action,
"_Save",
"_Cancel");
chooser = GTK_FILE_CHOOSER (native);
if (user_edited_a_new_document)
gtk_file_chooser_set_current_name (chooser, _("Untitled document"));
else
gtk_file_chooser_set_file (chooser, existing_file, NULL);
g_signal_connect (native, "response", G_CALLBACK (on_response), NULL);
gtk_native_dialog_show (GTK_NATIVE_DIALOG (native));
For more information on how to best set up a file dialog,
see the FileChooserDialog
documentation.
Response Codes
FileChooserNative
inherits from NativeDialog
,
which means it will return ResponseType::Accept
if the user accepted,
and ResponseType::Cancel
if he pressed cancel. It can also return
ResponseType::DeleteEvent
if the window was unexpectedly closed.
Differences from FileChooserDialog
There are a few things in the FileChooser
interface that
are not possible to use with FileChooserNative
, as such use would
prohibit the use of a native dialog.
No operations that change the dialog work while the dialog is visible. Set all the properties that are required before showing the dialog.
Win32 details
On windows the IFileDialog
implementation (added in Windows Vista) is
used. It supports many of the features that FileChooser
has, but
there are some things it does not handle:
- Any
FileFilter
added using a mimetype
If any of these features are used the regular FileChooserDialog
will be used in place of the native one.
Portal details
When the org.freedesktop.portal.FileChooser
portal is available on
the session bus, it is used to bring up an out-of-process file chooser.
Depending on the kind of session the application is running in, this may
or may not be a GTK file chooser.
macOS details
On macOS the NSSavePanel
and NSOpenPanel
classes are used to provide
native file chooser dialogs. Some features provided by FileChooser
are not supported:
- Shortcut folders.
Implements
NativeDialogExt
, glib::ObjectExt
, FileChooserExt
, NativeDialogExtManual
Implementations
Creates a new FileChooserNative
.
title
Title of the native
parent
Transient parent of the native
action
Open or save mode for the dialog
accept_label
text to go in the accept button, or None
for the default
cancel_label
text to go in the cancel button, or None
for the default
Returns
a new FileChooserNative
Creates a new builder-pattern struct instance to construct FileChooserNative
objects.
This method returns an instance of FileChooserNativeBuilder
which can be used to create FileChooserNative
objects.
Sets the custom label text for the accept button.
If characters in label
are preceded by an underscore, they are
underlined. If you need a literal underscore character in a label,
use “__” (two underscores). The first underlined character represents
a keyboard accelerator called a mnemonic.
Pressing Alt and that key should activate the button.
accept_label
custom label
Sets the custom label text for the cancel button.
If characters in label
are preceded by an underscore, they are
underlined. If you need a literal underscore character in a label,
use “__” (two underscores). The first underlined character represents
a keyboard accelerator called a mnemonic.
Pressing Alt and that key should activate the button.
cancel_label
custom label
Trait Implementations
type Parent = NativeDialog
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
Returns the type identifier of Self
.
Auto Trait Implementations
impl RefUnwindSafe for FileChooserNative
impl !Send for FileChooserNative
impl !Sync for FileChooserNative
impl Unpin for FileChooserNative
impl UnwindSafe for FileChooserNative
Blanket Implementations
Mutably borrows from an owned value. Read more
Upcasts an object to a superclass or interface T
. Read more
Upcasts an object to a reference of its superclass or interface T
. Read more
Tries to downcast to a subclass or interface implementor T
. Read more
Tries to downcast to a reference of its subclass or interface implementor T
. Read more
Tries to cast to an object of type T
. This handles upcasting, downcasting
and casting between interface and interface implementors. All checks are performed at
runtime, while downcast
and upcast
will do many checks at compile-time already. Read more
Tries to cast to reference to an object of type T
. This handles upcasting, downcasting
and casting between interface and interface implementors. All checks are performed at
runtime, while downcast
and upcast
will do many checks at compile-time already. Read more
Casts to T
unconditionally. Read more
Casts to &T
unconditionally. Read more
Returns true
if the object is an instance of (can be cast to) T
.
pub fn set_property<'a, N, V>(
&self,
property_name: N,
value: V
) -> Result<(), BoolError> where
N: Into<&'a str>,
V: ToValue,
pub fn set_property_from_value<'a, N>(
&self,
property_name: N,
value: &Value
) -> Result<(), BoolError> where
N: Into<&'a str>,
pub fn set_properties_from_value(
&self,
property_values: &[(&str, Value)]
) -> Result<(), BoolError>
pub fn has_property<'a, N>(&self, property_name: N, type_: Option<Type>) -> bool where
N: Into<&'a str>,
pub fn find_property<'a, N>(&self, property_name: N) -> Option<ParamSpec> where
N: Into<&'a str>,
Safety Read more
Safety Read more
Safety Read more
Safety Read more
pub fn connect<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
N: Into<&'a str>,
F: 'static + Fn(&[Value]) -> Option<Value> + Send + Sync,
Same as connect
but takes a SignalId
instead of a signal name.
pub fn connect_local<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
N: Into<&'a str>,
F: 'static + Fn(&[Value]) -> Option<Value>,
Same as connect_local
but takes a SignalId
instead of a signal name.
pub unsafe fn connect_unsafe<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
N: Into<&'a str>,
F: Fn(&[Value]) -> Option<Value>,
Same as connect_unsafe
but takes a SignalId
instead of a signal name.
Emit signal by signal id.
Same as emit
but takes Value
for the arguments.
Emit signal by its name.
Same as emit_by_name
but takes Value
for the arguments.
Emit signal with details by signal id.
Same as emit_with_details
but takes Value
for the arguments.
pub fn connect_notify<F>(&self, name: Option<&str>, f: F) -> SignalHandlerId where
F: 'static + Fn(&T, &ParamSpec) + Send + Sync,
pub fn connect_notify_local<F>(
&self,
name: Option<&str>,
f: F
) -> SignalHandlerId where
F: 'static + Fn(&T, &ParamSpec),
pub unsafe fn connect_notify_unsafe<F>(
&self,
name: Option<&str>,
f: F
) -> SignalHandlerId where
F: Fn(&T, &ParamSpec),
pub fn bind_property<'a, O, N, M>(
&'a self,
source_property: N,
target: &'a O,
target_property: M
) -> BindingBuilder<'a> where
O: ObjectType,
N: Into<&'a str>,
M: Into<&'a str>,
Returns a SendValue
clone of self
.