Files
Terminal.Gui/Terminal.Gui/FileServices/FileDialogState.cs
dodexahedron 34bef2c839 Fixes #3242 - Replaces simple null checks (#3248)
* Replace all 342 `== null` with `is null`

* Replace 354 `!= null` with `is { }`

* Wrap these in conditionals since they break tests against Release configuration

The members they depend on do not exist in Release configuration

* Split these up and dispose properly

This test needs to be revisited for several reasons at some point.

* Fix release configuration tests

* Declare interface these already support

* Annotate constructor properly and use throw helper

* Move class to its own file

* Rename these files so they nest in the solution explorer

* Make this a record type and remove now-redundant/illegal members

* Reference passing to avoid some struct copies

* Simplify this

* Carry reference passing through as appropriate

* Turn this into a record struct

* Remove unused internal constructor and its test

It was only used by that test.

* Simplify this constructor

* This should be a property

* Simplify constructor

* Simplify GetHashCode

* Mark this ignored just in case

* Missed a couple of opportunities for reference passing

* record struct already does this by value

* Remove unused class

* Simplify the type initializer and Reset method

* Implement INotifyCollectionChanged and IDictionary by delegating to ColorSchemes

* Fix for reflection-based configuration

* Make CI  build happy by disambiguiating this attribute
2024-02-16 16:46:25 -07:00

91 lines
2.8 KiB
C#

using System.IO.Abstractions;
namespace Terminal.Gui;
internal class FileDialogState
{
protected readonly FileDialog Parent;
public FileDialogState (IDirectoryInfo dir, FileDialog parent)
{
Directory = dir;
Parent = parent;
Path = parent.Path;
RefreshChildren ();
}
public FileSystemInfoStats [] Children { get; internal set; }
public IDirectoryInfo Directory { get; }
/// <summary>Gets what was entered in the path text box of the dialog when the state was active.</summary>
public string Path { get; }
public FileSystemInfoStats Selected { get; set; }
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 is { })
{
children = children.Where (MatchesApiFilter).ToList ();
}
// allow navigating up as '..'
if (dir.Parent is { })
{
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));
}
internal virtual void RefreshChildren ()
{
IDirectoryInfo dir = Directory;
Children = GetChildren (dir).ToArray ();
}
}