Files
Terminal.Gui/Terminal.Gui/FileServices/FileDialogHistory.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

98 lines
2.3 KiB
C#

using System.IO.Abstractions;
namespace Terminal.Gui;
internal class FileDialogHistory
{
private readonly Stack<FileDialogState> back = new ();
private readonly FileDialog dlg;
private readonly Stack<FileDialogState> forward = new ();
public FileDialogHistory (FileDialog dlg) { this.dlg = dlg; }
public bool Back ()
{
IDirectoryInfo goTo = null;
FileSystemInfoStats restoreSelection = null;
string restorePath = null;
if (CanBack ())
{
FileDialogState backTo = back.Pop ();
goTo = backTo.Directory;
restoreSelection = backTo.Selected;
restorePath = backTo.Path;
}
else if (CanUp ())
{
goTo = dlg.State?.Directory.Parent;
}
// nowhere to go
if (goTo is null)
{
return false;
}
forward.Push (dlg.State);
dlg.PushState (goTo, false, true, false, restorePath);
if (restoreSelection is { })
{
dlg.RestoreSelection (restoreSelection.FileSystemInfo);
}
return true;
}
internal bool CanBack () { return back.Count > 0; }
internal bool CanForward () { return forward.Count > 0; }
internal bool CanUp () { return dlg.State?.Directory.Parent != null; }
internal void ClearForward () { forward.Clear (); }
internal bool Forward ()
{
if (forward.Count > 0)
{
dlg.PushState (forward.Pop ().Directory, true, true, false);
return true;
}
return false;
}
internal void Push (FileDialogState state, bool clearForward)
{
if (state is null)
{
return;
}
// if changing to a new directory push onto the Back history
if (back.Count == 0 || back.Peek ().Directory.FullName != state.Directory.FullName)
{
back.Push (state);
if (clearForward)
{
ClearForward ();
}
}
}
internal bool Up ()
{
IDirectoryInfo parent = dlg.State?.Directory.Parent;
if (parent is { })
{
back.Push (new FileDialogState (parent, dlg));
dlg.PushState (parent, false);
return true;
}
return false;
}
}