github.com/rribou/wails/v2@v2.6.3/pkg/runtime/dialog.go (about)

     1  package runtime
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"github.com/rribou/wails/v2/internal/frontend"
     7  	"github.com/rribou/wails/v2/internal/fs"
     8  )
     9  
    10  // FileFilter defines a filter for dialog boxes
    11  type FileFilter = frontend.FileFilter
    12  
    13  // OpenDialogOptions contains the options for the OpenDialogOptions runtime method
    14  type OpenDialogOptions = frontend.OpenDialogOptions
    15  
    16  // SaveDialogOptions contains the options for the SaveDialog runtime method
    17  type SaveDialogOptions = frontend.SaveDialogOptions
    18  
    19  type DialogType = frontend.DialogType
    20  
    21  const (
    22  	InfoDialog     = frontend.InfoDialog
    23  	WarningDialog  = frontend.WarningDialog
    24  	ErrorDialog    = frontend.ErrorDialog
    25  	QuestionDialog = frontend.QuestionDialog
    26  )
    27  
    28  // MessageDialogOptions contains the options for the Message dialogs, EG Info, Warning, etc runtime methods
    29  type MessageDialogOptions = frontend.MessageDialogOptions
    30  
    31  // OpenDirectoryDialog prompts the user to select a directory
    32  func OpenDirectoryDialog(ctx context.Context, dialogOptions OpenDialogOptions) (string, error) {
    33  	appFrontend := getFrontend(ctx)
    34  	if dialogOptions.DefaultDirectory != "" {
    35  		if !fs.DirExists(dialogOptions.DefaultDirectory) {
    36  			return "", fmt.Errorf("default directory '%s' does not exist", dialogOptions.DefaultDirectory)
    37  		}
    38  	}
    39  	return appFrontend.OpenDirectoryDialog(dialogOptions)
    40  }
    41  
    42  // OpenFileDialog prompts the user to select a file
    43  func OpenFileDialog(ctx context.Context, dialogOptions OpenDialogOptions) (string, error) {
    44  	appFrontend := getFrontend(ctx)
    45  	if dialogOptions.DefaultDirectory != "" {
    46  		if !fs.DirExists(dialogOptions.DefaultDirectory) {
    47  			return "", fmt.Errorf("default directory '%s' does not exist", dialogOptions.DefaultDirectory)
    48  		}
    49  	}
    50  	return appFrontend.OpenFileDialog(dialogOptions)
    51  }
    52  
    53  // OpenMultipleFilesDialog prompts the user to select a file
    54  func OpenMultipleFilesDialog(ctx context.Context, dialogOptions OpenDialogOptions) ([]string, error) {
    55  	appFrontend := getFrontend(ctx)
    56  	if dialogOptions.DefaultDirectory != "" {
    57  		if !fs.DirExists(dialogOptions.DefaultDirectory) {
    58  			return nil, fmt.Errorf("default directory '%s' does not exist", dialogOptions.DefaultDirectory)
    59  		}
    60  	}
    61  	return appFrontend.OpenMultipleFilesDialog(dialogOptions)
    62  }
    63  
    64  // SaveFileDialog prompts the user to select a file
    65  func SaveFileDialog(ctx context.Context, dialogOptions SaveDialogOptions) (string, error) {
    66  	appFrontend := getFrontend(ctx)
    67  	if dialogOptions.DefaultDirectory != "" {
    68  		if !fs.DirExists(dialogOptions.DefaultDirectory) {
    69  			return "", fmt.Errorf("default directory '%s' does not exist", dialogOptions.DefaultDirectory)
    70  		}
    71  	}
    72  	return appFrontend.SaveFileDialog(dialogOptions)
    73  }
    74  
    75  // MessageDialog show a message dialog to the user
    76  func MessageDialog(ctx context.Context, dialogOptions MessageDialogOptions) (string, error) {
    77  	appFrontend := getFrontend(ctx)
    78  	return appFrontend.MessageDialog(dialogOptions)
    79  }