Skip to content

Commit

Permalink
Add ability to only show folders in File Browser dialogs (#2238)
Browse files Browse the repository at this point in the history
  • Loading branch information
corivera committed Sep 13, 2023
1 parent 9ca2fbc commit b6ee9a6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ public class FileBrowserOpenParams
public string ExpandPath;

/// <summary>
/// File extension filter (e.g. *.bak)
/// File extension filter (e.g. *.bak). Ignored if <see cref="ShowFoldersOnly"/> is set to <c>true</c>.
/// </summary>
public string[] FileFilters;

/// <summary>
/// True if this is a request to change file filter
/// </summary>
public bool ChangeFilter;

/// <summary>
/// Whether to only show folders in the file browser.
/// </summary>
public bool? ShowFoldersOnly;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,23 @@ internal class FileBrowserOperation : FileBrowserBase, IDisposable
private bool fileTreeCreated;
private CancellationTokenSource cancelSource;
private CancellationToken cancelToken;
private bool showFoldersOnly;

#region Constructors

/// <summary>
/// Initializes a new instance of the <see cref="FileBrowser"/> class.
/// </summary>
/// <param name="connection">The connection object</param>
/// <param name="fileFilters">The file extension filters</param>
public FileBrowserOperation(ServerConnection connection, string expandPath, string[] fileFilters = null)
/// <param name="connection">The connection object.</param>
/// <param name="expandPath">The initial folder to open in the file dialog.</param>
/// <param name="fileFilters">The file extension filters. Ignored if <see cref="showFoldersOnly"/> is set to <c>true</c>.</param>
/// <param name="showFoldersOnly">Whether to only show folders in the file browser.</param>
public FileBrowserOperation(ServerConnection connection, string expandPath, string[] fileFilters = null, bool? showFoldersOnly = null)
{
this.cancelSource = new CancellationTokenSource();
this.cancelToken = cancelSource.Token;
this.connection = connection;
this.showFoldersOnly = showFoldersOnly ?? false;
this.Initialize(expandPath, fileFilters);
}

Expand Down Expand Up @@ -236,9 +240,10 @@ public List<FileTreeNode> GetChildren(string filePath)
}
treeNode.IsFile = isFile;

// if the node is a directory, or if we are browsing for files and the file name is allowed,
// add the node to the tree
if (!isFile || (this.FilterFile(treeNode.Name, this.fileFilters)))
// If the node is a directory, or if we are browsing for files and the file name is allowed,
// add the node to the tree. Files will be skipped instead if the dialog is only showing folders,
// regardless of any provided file filters.
if (!isFile || (this.FilterFile(treeNode.Name, this.fileFilters) && !this.showFoldersOnly))
{
children.Add(treeNode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ internal async Task RunFileBrowserOpenTask(FileBrowserOpenParams fileBrowserPara
{
if (!fileBrowserParams.ChangeFilter)
{
browser = new FileBrowserOperation(bindingContext.ServerConnection, fileBrowserParams.ExpandPath, fileBrowserParams.FileFilters);
browser = new FileBrowserOperation(bindingContext.ServerConnection, fileBrowserParams.ExpandPath, fileBrowserParams.FileFilters, fileBrowserParams.ShowFoldersOnly);
}
else
{
Expand Down

0 comments on commit b6ee9a6

Please sign in to comment.