Files
Terminal.Gui/Terminal.Gui/FileServices/FileDialogState.cs
Thomas Nind 130fc5713d Fixes #2582 - Refactors FileDialog for cleaner data model (#2583)
* WIP: Add ITableDataSource

* WIP: Refactor TableView

* WIP: Port CSVEditor

* WIP: Port TableEditor

* WIP: Port MultiColouredTable scenario

* Fix bug of adding duplicate column styles

* Update tests to use DataTableSource

* Tidy up

* Add EnumerableTableDataSource<T>

* Add test for EnumerableTableDataSource

* Add test for EnumerableTableDataSource

* Add code example to xmldoc

* Add ProcessTable scenario

* Rename ITableDataSource to ITableSource and update docs

* Rename EnumerableTableDataSource to EnumerableTableSource

* Fixed Frame != Bounds; changed UICat Scenarios list to use tableview!

* Fix scroll resetting in ProcessTable scenario

* Fix unit tests by setting Frame to same as Bounds

* Document why we have to measure our data for use with TableView

* WIP: Simplify FileDialogs use of TableView

* WIP start migrating sorter

* WIP new filedialog table source mostly working

* WIP remove sorter class

* Refactor GetOrderByValue to be adjacent to GetColumnValue

* Fix collection navigator back so it ignores icon

* Fix unit tests

* Tidy up

* Fix UseColors

* Add test for UseColors

---------

Co-authored-by: Tig Kindel <tig@users.noreply.github.com>
2023-04-29 12:04:20 +02:00

84 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
namespace Terminal.Gui {
internal class FileDialogState {
public FileSystemInfoStats Selected { get; set; }
protected readonly FileDialog Parent;
/// <summary>
/// Gets what was entered in the path text box of the dialog
/// when the state was active.
/// </summary>
public string Path { get; }
public FileDialogState (IDirectoryInfo dir, FileDialog parent)
{
this.Directory = dir;
Parent = parent;
Path = parent.Path;
this.RefreshChildren ();
}
public IDirectoryInfo Directory { get; }
public FileSystemInfoStats [] Children { get; internal set; }
internal virtual void RefreshChildren ()
{
var dir = this.Directory;
Children = GetChildren (dir).ToArray ();
}
protected virtual IEnumerable<FileSystemInfoStats> GetChildren (IDirectoryInfo dir)
{
try {
List<FileSystemInfoStats> children;
// if directories only
if (Parent.OpenMode == OpenMode.Directory) {
children = dir.GetDirectories ().Select (e => new FileSystemInfoStats (e, Parent.Style.Culture)).ToList ();
} else {
children = dir.GetFileSystemInfos ().Select (e => new FileSystemInfoStats (e, Parent.Style.Culture)).ToList ();
}
// if only allowing specific file types
if (Parent.AllowedTypes.Any () && Parent.OpenMode == OpenMode.File) {
children = children.Where (
c => c.IsDir () ||
(c.FileSystemInfo is IFileInfo f && Parent.IsCompatibleWithAllowedExtensions (f)))
.ToList ();
}
// if theres a UI filter in place too
if (Parent.CurrentFilter != null) {
children = children.Where (MatchesApiFilter).ToList ();
}
// allow navigating up as '..'
if (dir.Parent != null) {
children.Add (new FileSystemInfoStats (dir.Parent, Parent.Style.Culture) { IsParent = true });
}
return children;
} catch (Exception) {
// Access permissions Exceptions, Dir not exists etc
return Enumerable.Empty<FileSystemInfoStats> ();
}
}
protected bool MatchesApiFilter (FileSystemInfoStats arg)
{
return arg.IsDir () ||
(arg.FileSystemInfo is IFileInfo f && Parent.CurrentFilter.IsAllowed (f.FullName));
}
}
}