From e975f35d14a372062ed174002ca486abc7148c43 Mon Sep 17 00:00:00 2001
From: cryham <cryham@gmail.com>
Date: Sun, 10 Mar 2019 12:01:05 +0100
Subject: [PATCH 2/2] Undo Close Tab

---
 src/fmain.lfm             | 10 ++++++++++
 src/fmain.pas             |  2 ++
 src/ufileviewnotebook.pas | 23 ++++++++++++++++++++++-
 src/uglobs.pas            |  1 +
 src/umaincommands.pas     |  6 ++++++
 5 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/src/fmain.lfm b/src/fmain.lfm
index c3cc3113..ace620c9 100644
--- a/src/fmain.lfm
+++ b/src/fmain.lfm
@@ -1219,6 +1219,9 @@ object frmMain: TfrmMain
       object miLine15: TMenuItem
         Caption = '-'
       end
+      object mnuUndoCloseTab: TMenuItem
+        Action = actUndoCloseTab
+      end
       object mnuCloseTab: TMenuItem
         Action = actCloseTab
       end
@@ -1682,6 +1685,13 @@ object frmMain: TfrmMain
       Caption = '&Benchmark'
       OnExecute = actExecute
     end
+    object actUndoCloseTab: TAction
+      Tag = 21
+      Category = 'Tabs'
+      Caption = '&Undo Close Tab'
+      HelpType = htKeyword
+      OnExecute = actExecute
+    end
     object actNewTab: TAction
       Tag = 21
       Category = 'Tabs'
diff --git a/src/fmain.pas b/src/fmain.pas
index 130f66dc..f12ca463 100644
--- a/src/fmain.pas
+++ b/src/fmain.pas
@@ -120,6 +120,7 @@ type
     actFocusTreeView: TAction;
     actToggleFullscreenConsole: TAction;
     actSrcOpenDrives: TAction;
+    actUndoCloseTab: TAction;
     actRightReverseOrder: TAction;
     actLeftReverseOrder: TAction;
     actRightFlatView: TAction;
@@ -334,6 +335,7 @@ type
     mnuTabOptionPathResets: TMenuItem;
     mnuCloseAllTabs: TMenuItem;
     mnuCloseTab: TMenuItem;
+    mnuUndoCloseTab: TMenuItem;
     miLine15: TMenuItem;
     mnuOpenDirInNewTab: TMenuItem;
     mnuNewTab: TMenuItem;
diff --git a/src/ufileviewnotebook.pas b/src/ufileviewnotebook.pas
index b470486e..e639fa2c 100644
--- a/src/ufileviewnotebook.pas
+++ b/src/ufileviewnotebook.pas
@@ -150,6 +150,7 @@ type
     FNotebookSide: TFilePanelSelect;
     FOnCloseTabClicked: TNotifyEvent;
     FPageControl: TFileViewPageControl;
+    FClosedPages: array of TFileViewPage;
 
     function GetActivePage: TFileViewPage;
     function GetActiveView: TFileView;
@@ -186,6 +187,7 @@ type
     function NewEmptyPage: TFileViewPage;
     function NewPage(CloneFromPage: TFileViewPage): TFileViewPage;
     function NewPage(CloneFromView: TFileView): TFileViewPage;
+    procedure UndoClosePage();
     procedure DeletePage(Index: Integer);
     procedure RemovePage(Index: Integer); reintroduce;
     procedure RemovePage(var APage: TFileViewPage);
@@ -877,8 +879,27 @@ var
   APage: TFileViewPage;
 begin
   APage:= GetPage(Index);
+  SetLength(FClosedPages, Length(FClosedPages) + 1);  // push
+  FClosedPages[High(FClosedPages)]:= APage;
+
   FPageControl.Pages[Index].Free;
-  APage.Free;
+  //APage.Free;
+end;
+
+procedure TFileViewNotebook.UndoClosePage();
+var
+  ANewPage: TFileViewPage;
+  Last: TFileViewPage;
+begin
+  if Length(FClosedPages) > 0 then
+  begin
+    ANewPage := NewEmptyPage();
+    Last := FClosedPages[High(FClosedPages)];
+    ANewPage.AssignPage(Last);
+    ANewPage.MakeActive;
+    SetLength(FClosedPages, Length(FClosedPages) - 1);  // pop
+    Last.Free;
+  end;
 end;
 
 procedure TFileViewNotebook.RemovePage(Index: Integer);
diff --git a/src/uglobs.pas b/src/uglobs.pas
index 39ae4fa8..55c4f21d 100644
--- a/src/uglobs.pas
+++ b/src/uglobs.pas
@@ -974,6 +974,7 @@ begin
       AddIfNotExists(['Ctrl+T'],[],'cm_NewTab');
       AddIfNotExists(['Ctrl+U'],[],'cm_Exchange');
       AddIfNotExists(['Ctrl+W'],[],'cm_CloseTab');
+      AddIfNotExists(['Ctrl+Shift+W'],[],'cm_UndoCloseTab');
       AddIfNotExists(['Ctrl+F1'],[],'cm_BriefView');
       AddIfNotExists(['Ctrl+F2'],[],'cm_ColumnsView');
       AddIfNotExists(['Ctrl+F3'],[],'cm_SortByName');
diff --git a/src/umaincommands.pas b/src/umaincommands.pas
index dd56c60b..09fa9827 100644
--- a/src/umaincommands.pas
+++ b/src/umaincommands.pas
@@ -187,6 +187,7 @@ type
    procedure cm_QuickSearch(const Params: array of string);
    procedure cm_QuickFilter(const Params: array of string);
    procedure cm_SrcOpenDrives(const {%H-}Params: array of string);
+   procedure cm_UndoCloseTab(const {%H-}Params: array of string);
    procedure cm_LeftOpenDrives(const {%H-}Params: array of string);
    procedure cm_RightOpenDrives(const {%H-}Params: array of string);
    procedure cm_OpenBar(const {%H-}Params: array of string);
@@ -1365,6 +1366,11 @@ begin
   frmMain.ShowDrivesList(frmMain.SelectedPanel);
 end;
 
+procedure TMainCommands.cm_UndoCloseTab(const Params: array of string);
+begin
+  frmMain.ActiveNotebook.UndoClosePage();
+end;
+
 procedure TMainCommands.cm_LeftOpenDrives(const Params: array of string);
 begin
   frmMain.ShowDrivesList(fpLeft);
-- 
2.21.0.windows.1

