filedialog

package
v0.0.0-...-1055dbb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: BSD-2-Clause Imports: 23 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package filedialog shows the operating system's own file dialog — open one or several files, choose a directory, pick a path to save to — on macOS, on Windows, and in a GNOME or KDE session, and does nothing elsewhere.

dlg := filedialog.New(open, cancel).
    Mode(filedialog.OpenMulti).
    Extensions(".jpg", ".png")

filedialog.Present(screen, dlg).Presented(picking)

It is the native counterpart of filepicker, the picker made of gift views, and is called the same way: the same modes, the same callbacks, presented like a ui.Sheet. An application chooses one, or both:

picker := filepicker.New(os.DirFS(root), open, cancel).Extensions(".jpg")
dlg := filedialog.New(open, cancel).Extensions(".jpg").Root(root)
filedialog.Present(screen, dlg).Fallback(picker).Presented(picking)

shows the system's dialog where there is one, and the in-app picker where there is not. With Dialog.Root, both report the same fs.FS paths, so one onPick serves both.

Where it works

  • macOS: NSOpenPanel and NSSavePanel, as a sheet on the application's window.
  • Windows: the common item dialog (IFileOpenDialog, IFileSaveDialog), owned by the application's window.
  • GNOME and KDE: the FileChooser of xdg-desktop-portal, over the session bus. The dialog is modal to the application's window when its X11 window can be found, under XWayland too, and a window of its own otherwise.

When nothing happens

Everywhere else — another Linux desktop, no session bus, a gift kiosk, the BSDs, Android, iOS, the web — and where the system's dialog cannot be reached, nothing is shown. onPick is never called, onCancel is, as the answer "no file", so that an application that closes its presented state from onCancel is not left with a button that does nothing; with a PresentView.Fallback, the fallback is shown instead and onCancel is not called. The reason is logged once per process, at Error, to the first dialog that has a Dialog.Logger, and Available reports false. With a nil onCancel, nothing is reported at all.

A dialog that cannot be shown only right now answers just that request the same way, logged at Warn, and leaves Available true: on macOS before the application runs, which is when NSApplication exists, and a Directory dialog on a portal whose FileChooser is older than version 3.

Threading

Both callbacks run on the UI goroutine. Showing a dialog never blocks it: Dialog.Open returns at once and the answer arrives through the post function it was given; Present uses the Post of its component's gift.Token. One native dialog is open at a time per process; another Dialog.Open meanwhile answers with onCancel and ErrBusy. A dialog closed from code, by Presented(false) or the cancel function of Open, reports nothing. On macOS and Windows the system confirms such a close asynchronously, and a dialog opened again before it did gets ErrBusy.

Paths

Without Dialog.Root the paths are absolute operating system paths. With it they are fs.FS paths relative to the root, as package filepicker reports them for os.DirFS(root); a pick outside the root is a cancel, logged at Warn. A sandboxed application (Flatpak, Snap) gets paths under /run/user/<uid>/doc from the portal, which are outside any root.

What it does not do

No custom filter function, no hidden files switch, no root the person cannot leave, no more than one filter group and no "All files" entry: the common ground of the three systems. Whether the system appends an extension to a typed Save name differs: Windows appends the first of Dialog.Extensions; macOS appends the system's preferred extension of its file type, so ".jpg" may become ".jpeg"; the portal appends none. The real dialog cannot be driven by package gifttest; the portable parts are tested with a fake system.

Index

Constants

View Source
const (
	// Open picks one existing file.
	Open = filepicker.Open
	// OpenMulti picks one or more existing files.
	OpenMulti = filepicker.OpenMulti
	// Directory picks one directory.
	Directory = filepicker.Directory
	// Save picks the path of a file to save to. The system asks before an
	// existing file is chosen; nothing is written.
	Save = filepicker.Save
)

The modes, the same constants as those of package filepicker.

Variables

View Source
var ErrBusy = errors.New("gift/filedialog: a native file dialog is already open")

ErrBusy is returned by Dialog.Open while another native file dialog of this process is open.

View Source
var ErrUnsupported = errors.New("gift/filedialog: no native file dialog here")

ErrUnsupported is wrapped by every error of a system that has no native file dialog, or whose dialog could not be reached.

Functions

func Available

func Available() bool

Available reports whether this process is one where a native dialog is attempted: macOS, Windows, or a GNOME or KDE session with a session bus, outside a gift kiosk. It does not block and does not ask the system, so a true can still end in a dialog that cannot be shown; after such a lasting failure it reports false for the rest of the process.

Types

type Dialog

type Dialog struct {
	// contains filtered or unexported fields
}

Dialog describes one native file dialog. Create one with New, configure it with its methods, which return a changed copy, and show it with Present or Dialog.Open.

func New

func New(onPick func(paths []string), onCancel func()) Dialog

New returns a dialog that calls onPick with the picked paths and onCancel when the person backs out, or when there is no native dialog to show. The default mode is Open.

onPick receives one path in every mode but OpenMulti, and never an empty slice. Both run on the UI goroutine. With a nil onCancel, a cancelled or unavailable dialog reports nothing at all.

func (Dialog) Extensions

func (d Dialog) Extensions(exts ...string) Dialog

Extensions limits the files that can be picked to these extensions, ignoring case: ".jpg" and "JPG" are the same. They form one filter; there is no "All files" entry. Directory mode ignores them.

func (Dialog) FilterName

func (d Dialog) FilterName(s string) Dialog

FilterName sets the label of the filter made from Dialog.Extensions, where the system shows one. The default is "Supported files".

func (Dialog) Logger

func (d Dialog) Logger(l *slog.Logger) Dialog

Logger sets where the dialog reports what went wrong. The default is nowhere; gift never logs to slog.Default.

func (Dialog) Mode

func (d Dialog) Mode(m Mode) Dialog

Mode sets what the dialog picks; see Mode.

func (Dialog) Open

func (d Dialog) Open(post func(func())) (cancel func(), err error)

Open shows the dialog now and returns at once; the answer arrives later, through post, on the UI goroutine. post is how a function reaches the UI executor: gift.App.Post, or the Post method of a gift.Token.

Where there is no native dialog, nothing is shown and onCancel is posted. The error wraps ErrUnsupported when that is already known; otherwise it is nil and the answer comes asynchronously, like any other. With another dialog of this process open, onCancel is posted and the error is ErrBusy. With a nil post nothing is shown, nothing is reported, and the error wraps ErrUnsupported.

The returned function closes the dialog if it is still open and drops any answer still to come. It is never nil and may be called more than once, and before the system has shown the dialog.

func (Dialog) Root

func (d Dialog) Root(dir string) Dialog

Root makes the dialog report fs.FS paths relative to dir, "/"-separated, the same paths filepicker.New reports for os.DirFS(dir). A pick outside dir is reported as a cancel, logged at Warn: the person cannot be kept inside it. Without a root, the paths are absolute operating system paths.

func (Dialog) Start

func (d Dialog) Start(dir string) Dialog

Start sets the directory the dialog opens in, an operating system path. A path that is not an existing directory is ignored. The default is the root set with Dialog.Root, else the system's choice.

func (Dialog) SuggestedName

func (d Dialog) SuggestedName(s string) Dialog

SuggestedName fills the name field of Save mode.

func (Dialog) Title

func (d Dialog) Title(s string) Dialog

Title sets the title of the dialog. The default names the mode: "Open", "Choose a folder", "Save". macOS shows it as the message above the panel.

type Mode

type Mode = filepicker.Mode

Mode says what a dialog picks. It is the type of the in-app picker, so one value configures both Dialog.Mode and filepicker.PickerView.Mode.

type PresentView

type PresentView struct {
	// contains filtered or unexported fields
}

PresentView shows a native file dialog over its content while it is presented, in the shape of ui.Sheet. Create one with Present.

dlg := filedialog.New(open, cancel).Mode(filedialog.OpenMulti).Extensions(".jpg", ".png")
filedialog.Present(screen, dlg).Presented(picking)

The dialog opens when Presented turns true, and closes, without a callback, when it turns false or the view is unmounted. The application sets Presented back to false itself, from onPick and from onCancel, as it does for a sheet. A dialog that stays presented after it answered is not opened again until Presented has been false.

With PresentView.Fallback, a system without a native dialog shows the given view in a ui.Sheet instead, normally a filepicker.PickerView with the same callbacks; onCancel is then not called for the missing dialog. With a fallback, PresentView is a screen like a ui.Sheet and fills the area it is given; without one it takes the size of its content.

func Present

func Present(content gift.View, dialog Dialog) PresentView

Present returns content with dialog shown over it while presented. content must not be nil.

func (PresentView) Build

func (p PresentView) Build(bc *gift.BuildContext) gift.Element

Build implements gift.View. The state — whether the dialog is open, whether the fallback is shown — lives in a component of its own.

func (PresentView) Fallback

func (p PresentView) Fallback(v gift.View) PresentView

Fallback sets the view shown in a ui.Sheet where there is no native dialog. Its scrim and escape key call the dialog's onCancel.

func (PresentView) FallbackHeight

func (p PresentView) FallbackHeight(f float32) PresentView

FallbackHeight sets the height of the fallback sheet as a fraction of the height it is presented in, in (0, 1]; see ui.SheetView.Height.

func (PresentView) Flex

func (p PresentView) Flex(f float32) PresentView

Flex makes the view take a share of the remaining main axis space of its parent stack.

func (PresentView) Key

func (p PresentView) Key(s string) PresentView

Key sets the reconciliation key of this view among its siblings.

func (PresentView) Presented

func (p PresentView) Presented(b bool) PresentView

Presented says whether the dialog is open right now. The default is "presented", as for ui.SheetView.Presented.

func (PresentView) ViewType

func (p PresentView) ViewType() gift.TypeID

ViewType implements gift.View.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL