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