Skip to main content

FileDialog

Struct FileDialog 

Source
pub struct FileDialog { /* private fields */ }
Expand description

A modern, single-pane Open / Save file dialog.

Built on the general-purpose Modal, FileDialog presents a file browser in its own top-level window: the current path along the top, one combined list of folders (shown first) and files below it, a File name field and a File types filter dropdown along the bottom, and OK / Cancel to their right. That’s the arrangement modern KDE / Windows pickers use, rather than the Win 3.1 two-column “Directories” / “Drives” layout — a single flat pane with no separate drive selector.

Each section label carries a keyboard accelerator that moves focus to its control: Alt+L (“Location”) to the list, Alt+N to the File name field, Alt+T to the File types filter.

The application owns it as an overlay — typically Rc<RefCell<FileDialog>> added with Column::add_overlay, exactly like Dialog — and opens it with show_open or show_save, passing a callback that receives the chosen Path when the user confirms. Cancelling (the Cancel button, Escape, or the window’s close button) simply closes the dialog without calling back.

use std::cell::RefCell;
use std::rc::Rc;
use saudade::{FileDialog, FileFilter};

let dialog = Rc::new(RefCell::new(
    FileDialog::new().with_filters(vec![
        FileFilter::new("Text Files (*.txt)", ["*.txt"]),
        FileFilter::all_files(),
    ]),
));

// From a menu / button handler:
dialog.borrow_mut().show_open(|cx, path| {
    // load `path` …
    cx.request_paint();
});

Interaction:

  • single-click a file to put its name in the File name field;
  • double-click a file (or select it and press Enter / OK) to open it;
  • double-click a folder — or .. — to descend / ascend (with a folder selected in the list, Enter navigates into it too);
  • type a name and press Enter / OK to accept it; type a wildcard pattern (e.g. *.rs) to re-filter the list; type a directory name to descend.

Implementations§

Source§

impl FileDialog

Source

pub fn new() -> Self

Source

pub fn with_directory(self, dir: impl Into<PathBuf>) -> Self

Set the directory the dialog opens at.

Source

pub fn set_directory(&mut self, dir: impl Into<PathBuf>)

Set the directory the next show_* opens at, after construction — handy for a shared dialog that should reopen near the current document.

Source

pub fn with_filters(self, filters: Vec<FileFilter>) -> Self

Replace the file-type filters. The first becomes the initial selection; an empty list falls back to “All Files”.

Source

pub fn show_open<F>(&mut self, on_open: F)
where F: FnMut(&mut EventCtx, &Path) + 'static,

Open the dialog to pick a file. on_open runs with the chosen path when the user confirms (Open / double-click / Enter); Cancel or Escape close it without calling back. The focused File name field starts empty — the user picks a file from the list or types a name.

Source

pub fn show_save<F>(&mut self, suggested_name: impl Into<String>, on_save: F)
where F: FnMut(&mut EventCtx, &Path) + 'static,

Open the dialog to choose a save destination. suggested_name pre-fills the focused File name field (e.g. the current document’s name), fully selected with the cursor at the end so it’s ready to keep, edit, or replace by typing; on_save runs with the chosen path on confirm. The path need not exist yet.

Source

pub fn dismiss(&mut self)

Close the dialog programmatically (no callback runs).

Source

pub fn is_open(&self) -> bool

Trait Implementations§

Source§

impl Default for FileDialog

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Widget for FileDialog

Source§

fn bounds(&self) -> Rect

Logical bounds relative to the window root, in saudade pixels.
Source§

fn layout(&mut self, bounds: Rect)

Position the widget inside the rectangle the parent has allocated. Read more
Source§

fn paint(&mut self, painter: &mut Painter<'_>, theme: &Theme)

Render the widget in the normal pass.
Source§

fn paint_overlay(&mut self, painter: &mut Painter<'_>, theme: &Theme)

Render anything that needs to float on top of every sibling — open menu popups, tooltips, drag previews. Runs after every widget’s regular paint is finished. Default: no-op.
Source§

fn event(&mut self, event: &Event, ctx: &mut EventCtx)

Handle a typed input event. Default: ignore.
Source§

fn captures_pointer(&self) -> bool

Internal hook for capture-on-press dispatch. Default: never captured. Implementations like Button override this so pointer events keep flowing to them while a press is in progress, even if the cursor leaves the widget’s bounds.
Source§

fn accepts_accelerators(&self) -> bool

true if this widget should receive every keyboard event regardless of focus. Used by menu bars so that Alt+letter accelerators reach them even while a sibling (e.g., a text editor) holds focus.
Source§

fn popup_request(&self) -> Option<PopupRequest>

Ask the runtime to host a popup window for this widget. Returning Some makes the runtime open (or move) a borderless top-level window at the indicated logical-coord rect so the popup can extend past the main window’s edges. Container widgets propagate this from their children. Default: no popup. Read more
Source§

fn collect_popups(&self, out: &mut Vec<PopupRequest>)

Collect every popup this widget and its descendants currently want the runtime to host, outermost first. Read more
Source§

fn wants_ticks(&self) -> bool

true if this widget needs periodic Event::Tick events to drive an animation. The runtime polls this after every dispatch and, while any widget in the tree wants ticks, fires Tick at roughly 60 Hz. Container widgets propagate from children. Default: no animation.
Source§

fn on_cancel(&mut self, _ctx: &mut EventCtx)

Called when the widget is cancelled — closed by Escape or a window’s close button (which the runtime maps to Escape), with no child consuming that Escape first — rather than dismissed by its own request. A hosting Modal calls it on its content just before tearing it down; the runtime calls it on the root widget when the main window is closed, so a dialog used directly as the window root gets the same hook. It does not fire when the content asked to be dismissed itself (e.g. an OK / Cancel button calling EventCtx::request_dismiss), since that path already chose what to commit or revert. A dialog that previews edits live overrides this to roll them back. Default: no-op.
Source§

fn focusable(&self) -> bool

true if this widget accepts keyboard focus. The parent container remembers the last focusable widget the user clicked, and routes keyboard events only there.
Source§

fn set_focused(&mut self, _focused: bool)

Inform the widget that it has gained or lost keyboard focus. Default: ignore. Editing widgets override this to show/hide their cursor or to commit pending input.
Source§

fn focus_first(&mut self) -> bool

Try to give keyboard focus to this widget or one of its descendants. Returns true if a focusable target was located and now holds focus. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more