github.com/AlpineAIO/wails/v2@v2.0.0-beta.32.0.20240505041856-1047a8fa5fef/internal/go-common-file-dialog/cfd/DialogConfig.go (about)

     1  // Cross-platform.
     2  
     3  package cfd
     4  
     5  type FileFilter struct {
     6  	// The display name of the filter (That is shown to the user)
     7  	DisplayName string
     8  	// The filter pattern. Eg. "*.txt;*.png" to select all txt and png files, "*.*" to select any files, etc.
     9  	Pattern string
    10  }
    11  
    12  type DialogConfig struct {
    13  	// The title of the dialog
    14  	Title string
    15  	// The role of the dialog. This is used to derive the dialog's GUID, which the
    16  	// OS will use to differentiate it from dialogs that are intended for other purposes.
    17  	// This means that, for example, a dialog with role "Import" will have a different
    18  	// previous location that it will open to than a dialog with role "Open". Can be any string.
    19  	Role string
    20  	// The default folder - the folder that is used the first time the user opens it
    21  	// (after the first time their last used location is used).
    22  	DefaultFolder string
    23  	// The initial folder - the folder that the dialog always opens to if not empty.
    24  	// If this is not empty, it will override the "default folder" behaviour and
    25  	// the dialog will always open to this folder.
    26  	Folder string
    27  	// The file filters that restrict which types of files the dialog is able to choose.
    28  	// Ignored by Select Folder Dialog.
    29  	FileFilters []FileFilter
    30  	// Sets the initially selected file filter. This is an index of FileFilters.
    31  	// Ignored by Select Folder Dialog.
    32  	SelectedFileFilterIndex uint
    33  	// The initial name of the file (I.E. the text in the file name text box) when the user opens the dialog.
    34  	// For the Select Folder Dialog, this sets the initial folder name.
    35  	FileName string
    36  	// The default extension applied when a user does not provide one as part of the file name.
    37  	// If the user selects a different file filter, the default extension will be automatically updated to match the new file filter.
    38  	// For Open / Open Multiple File Dialog, this only has an effect when the user specifies a file name with no extension and a file with the default extension exists.
    39  	// For Save File Dialog, this extension will be used whenever a user does not specify an extension.
    40  	// Ignored by Select Folder Dialog.
    41  	DefaultExtension string
    42  	// ParentWindowHandle is the handle (HWND) to the parent window of the dialog.
    43  	// If left as 0 / nil, the dialog will have no parent window.
    44  	ParentWindowHandle uintptr
    45  }
    46  
    47  var defaultFilters = []FileFilter{
    48  	{
    49  		DisplayName: "All Files (*.*)",
    50  		Pattern:     "*.*",
    51  	},
    52  }
    53  
    54  func (config *DialogConfig) apply(dialog Dialog) (err error) {
    55  	if config.Title != "" {
    56  		err = dialog.SetTitle(config.Title)
    57  		if err != nil {
    58  			return
    59  		}
    60  	}
    61  
    62  	if config.Role != "" {
    63  		err = dialog.SetRole(config.Role)
    64  		if err != nil {
    65  			return
    66  		}
    67  	}
    68  
    69  	if config.Folder != "" {
    70  		err = dialog.SetFolder(config.Folder)
    71  		if err != nil {
    72  			return
    73  		}
    74  	}
    75  
    76  	if config.DefaultFolder != "" {
    77  		err = dialog.SetDefaultFolder(config.DefaultFolder)
    78  		if err != nil {
    79  			return
    80  		}
    81  	}
    82  
    83  	if config.FileName != "" {
    84  		err = dialog.SetFileName(config.FileName)
    85  		if err != nil {
    86  			return
    87  		}
    88  	}
    89  
    90  	dialog.SetParentWindowHandle(config.ParentWindowHandle)
    91  
    92  	if dialog, ok := dialog.(FileDialog); ok {
    93  		var fileFilters []FileFilter
    94  		if config.FileFilters != nil && len(config.FileFilters) > 0 {
    95  			fileFilters = config.FileFilters
    96  		} else {
    97  			fileFilters = defaultFilters
    98  		}
    99  		err = dialog.SetFileFilters(fileFilters)
   100  		if err != nil {
   101  			return
   102  		}
   103  
   104  		if config.SelectedFileFilterIndex != 0 {
   105  			err = dialog.SetSelectedFileFilterIndex(config.SelectedFileFilterIndex)
   106  			if err != nil {
   107  				return
   108  			}
   109  		}
   110  
   111  		if config.DefaultExtension != "" {
   112  			err = dialog.SetDefaultExtension(config.DefaultExtension)
   113  			if err != nil {
   114  				return
   115  			}
   116  		}
   117  	}
   118  
   119  	return
   120  }