sqweek/dialog

repository·master·Indexed 20 days ago

https://github.com/sqweek/dialog

A cross-platform Go library for displaying native system dialogs, including message boxes, file pickers, and directory browsers. It supports OSX (Cocoa), Win32, and Linux (GTK3) and utilizes a builder pattern to configure and launch dialogs via methods such as Message(), File(), and Directory().

Tokens
1.7K
Snippets
10
Records
12
Agent score
20%

What's inside sqweek/dialog

  1. Platform support and requirements

    master

    The dialog library supports the following platforms:

    • OSX: Uses Cocoa (NSAlert, NSSavePanel, NSOpenPanel, NSApp). Requires macOS 10.6+ for NSApplicationActivationPolicyAccessory.
    • Win32: Uses MessageBox, GetOpenFileName, and GetSaveFileName via the github.com/TheTitanrain/w32 package.
    • Linux: Uses GTK's MessageDialog and FileChooserDialog via cgo. Requires GTK3 development packages installed on the system.
  2. How to use the dialog builder pattern

    master

    The dialog package uses a builder pattern to construct and launch dialogs. The general workflow is:

    1. Call a top-level constructor function (Message, File, or Directory) to get a builder instance.
    2. Optionally call configuration methods (like Title, Filter, or SetStartDir) to customize the dialog.
    3. Call a launcher method (like YesNo, Info, Load, Save, or Browse) to display the dialog and retrieve the result.

    If a user cancels or closes a dialog without making a selection, the launcher methods return ErrCancelled.

    if dialog.MsgDlg("%s", "Do you want to continue?").YesNo() {
        // user pressed Yes
    }
  3. Known issue: Working directory changes on Windows

    master
    On Windows, calling Load() or Save() via the file dialog will change the working directory for the entire process while the dialog is open. The working directory is reset once the dialog is closed. This is a side effect of the underlying W32API.
  4. Save a file via a save dialog

    master

    Use dialog.File() combined with .Save() to prompt the user for a destination filename. You can use .Filter(description, extension) to suggest specific file types and .Title(string) to set the dialog title.

    If the user selects an existing file, the underlying platform will spawn a confirmation dialog to ask if they want to overwrite it.

    filename, err := dialog.File().Filter("XML files", "xml").Title("Export to XML").Save()
  5. Browse for a directory

    master

    Use dialog.Directory() to open a folder selection dialog. Use .Title(string) to set the window title and .Browse() to execute the dialog. It returns the path to the selected directory and an error.

    directory, err := dialog.Directory().Title("Load images").Browse()
  6. Open a file selection dialog

    master

    Use dialog.File() to initiate a file picker. Use .Filter(description, extension) to restrict file types (e.g., .mp3). Call .Load() to open the dialog.

    If the user cancels or closes the dialog, filename will be an empty string and the error will be dialog.Cancelled.

    filename, err := dialog.File().Filter("Mp3 audio file", "mp3").Load()
  7. Show a Yes/No message dialog

    master

    Use dialog.Message to create a message box. You can chain .Title(string) to set the window title. Calling .YesNo() at the end configures the dialog to show 'Yes' and 'No' buttons. The method returns true if the dialog was displayed and the user clicked 'Yes', and false otherwise.

    ok := dialog.Message("%s", "Do you want to continue?").Title("Are you sure?").YesNo()
  8. Select directories with Directory()

    master

    Use Directory() to create a *DirectoryBuilder for selecting a single folder.

    Configuration:

    • .Title(title string): Sets the dialog window title.
    • .SetStartDir(dir string): Sets the initial directory to be used in the dialog.

    Launcher:

    • .Browse() (string, error): Spawns the directory selection dialog. Returns the selected directory path or ErrCancelled if the user cancels.
    // Example: Selecting a folder
    dir, err := dialog.Directory().
    	Title("Select Project Folder").
    	SetStartDir("/tmp").
    	Browse()
    
    if err != nil {
    	if err == dialog.ErrCancelled {
    		// user cancelled
    	}
    }
  9. Select files with File()

    master

    Use File() to create a *FileBuilder for selecting or saving files.

    Configuration:

    • .Title(title string): Sets the dialog window title.
    • .Filter(desc string, extensions ...string): Adds a file category (e.g., "Images") with specific extensions. Multiple calls are cumulative. Use the special extension "*" to allow all files when a filter is active.
    • .SetStartDir(startDir string): Sets the initial directory.
    • .SetStartFile(startFile string): Sets the initial filename.

    Launchers:

    • .Load() (string, error): Opens a file selection dialog. Returns the selected file path or ErrCancelled if the user cancels.
    • .Save() (string, error): Opens a save-as dialog. If the file exists, the user is prompted to overwrite. Returns the path, ErrCancelled if the user cancels, or ErrCancelled if they choose not to overwrite.
    // Example: Loading an image file
    path, err := dialog.File().
    	Title("Select Image").
    	Filter("Image Files", ".jpg", ".png").
    	SetStartDir("/home/user/pictures").
    	Load()
    
    if err != nil {
    	if err == dialog.ErrCancelled {
    		// user cancelled
    	}
    }
  10. Create message dialogs with Message()

    master

    Use Message(format string, args ...interface{}) to create a *MsgBuilder. This allows you to format a message string similar to fmt.Printf.

    Configuration & Launchers:

    • .Title(title string): Sets the dialog window title.
    • .YesNo() bool: Spawns a dialog with "Yes" and "No" buttons. Returns true if the user selected "Yes".
    • .Info(): Spawns an information dialog with an info icon and an "Ok" button.
    • .Error(): Spawns an error dialog with an error icon and an "Ok" button.
    // Example: Yes/No dialog
    if dialog.Message("Delete %s?", filename).Title("Confirm").YesNo() {
        // user pressed Yes
    }
    
    // Example: Info dialog
    dialog.Message("Operation successful").Info()
  11. Handle dialog cancellation with ErrCancelled

    master

    When a user cancels a dialog or closes the window without selecting an option, the launcher methods return the dialog.ErrCancelled error. You should check for this specific error to distinguish between a user action and a legitimate system error.

    var ErrCancelled = errors.New("Cancelled")