OSDN Git Service

タブ削除の取消の処理をTabInformationsに移動
authorKimura Youichi <kim.upsilon@bucyou.net>
Fri, 16 Jun 2023 23:44:13 +0000 (08:44 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Fri, 16 Jun 2023 23:45:21 +0000 (08:45 +0900)
OpenTween.Tests/Models/TabInformationTest.cs
OpenTween/Models/TabInformations.cs
OpenTween/Tween.cs

index 40e8793..b859534 100644 (file)
@@ -168,6 +168,61 @@ namespace OpenTween.Models
         }
 
         [Fact]
+        public void CanUndoRemovedTab_Test()
+        {
+            var tab = new PublicSearchTabModel("tab");
+            this.tabinfo.AddTab(tab);
+            Assert.False(this.tabinfo.CanUndoRemovedTab);
+
+            this.tabinfo.RemoveTab(tab.TabName);
+            Assert.True(this.tabinfo.CanUndoRemovedTab);
+        }
+
+        [Fact]
+        public void UndoRemovedTab_Test()
+        {
+            var tab = new PublicSearchTabModel("tab");
+            this.tabinfo.AddTab(tab);
+            Assert.True(this.tabinfo.ContainsTab("tab"));
+            Assert.Empty(this.tabinfo.RemovedTab);
+
+            this.tabinfo.RemoveTab("tab");
+            Assert.False(this.tabinfo.ContainsTab("tab"));
+            Assert.Single(this.tabinfo.RemovedTab);
+
+            var restoredTab = this.tabinfo.UndoRemovedTab();
+            Assert.True(this.tabinfo.ContainsTab("tab"));
+            Assert.Same(tab, restoredTab);
+            Assert.Empty(this.tabinfo.RemovedTab);
+        }
+
+        [Fact]
+        public void UndoRemovedTab_EmptyError_Test()
+        {
+            Assert.Empty(this.tabinfo.RemovedTab);
+            Assert.Throws<TabException>(
+                () => this.tabinfo.UndoRemovedTab()
+            );
+        }
+
+        [Fact]
+        public void UndoRemovedTab_DuplicatedName_Test()
+        {
+            var tab = new PublicSearchTabModel("tab");
+            this.tabinfo.AddTab(tab);
+            Assert.Empty(this.tabinfo.RemovedTab);
+
+            this.tabinfo.RemoveTab("tab");
+            Assert.Single(this.tabinfo.RemovedTab);
+
+            this.tabinfo.RenameTab("Recent", "tab");
+            Assert.Throws<TabException>(
+                () => this.tabinfo.UndoRemovedTab()
+            );
+            Assert.Single(this.tabinfo.RemovedTab);
+        }
+
+        [Fact]
         public void RenameTab_PositionTest()
         {
             var replyTab = this.tabinfo.Tabs["Reply"];
index dbb125e..1941ab8 100644 (file)
@@ -176,6 +176,27 @@ namespace OpenTween.Models
             }
         }
 
+        public bool CanUndoRemovedTab
+            => this.RemovedTab.Count > 0;
+
+        public TabModel UndoRemovedTab()
+        {
+            if (!this.CanUndoRemovedTab)
+                throw new TabException("There isn't removed tab.");
+
+            var tab = this.RemovedTab.Pop();
+            if (this.ContainsTab(tab.TabName))
+            {
+                this.RemovedTab.Push(tab);
+                var message = string.Format(Properties.Resources.UndoRemovedTab_DuplicateError, tab.TabName);
+                throw new TabException(message);
+            }
+
+            this.AddTab(tab);
+
+            return tab;
+        }
+
         public void MoveTab(int newIndex, TabModel tab)
         {
             if (newIndex < 0 || newIndex >= this.tabs.Count)
index 9ce5774..1e5bb47 100644 (file)
@@ -8691,30 +8691,20 @@ namespace OpenTween
 
         private void UndoRemoveTabMenuItem_Click(object sender, EventArgs e)
         {
-            if (this.statuses.RemovedTab.Count == 0)
-            {
-                MessageBox.Show("There isn't removed tab.", "Undo", MessageBoxButtons.OK, MessageBoxIcon.Information);
-                return;
-            }
-            else
+            try
             {
-                var tb = this.statuses.RemovedTab.Pop();
-                if (this.statuses.ContainsTab(tb.TabName))
-                {
-                    var message = string.Format(Properties.Resources.UndoRemovedTab_DuplicateError, tb.TabName);
-                    MessageBox.Show(this, message, ApplicationSettings.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
-                    this.statuses.RemovedTab.Push(tb);
-                    return;
-                }
-
-                this.statuses.AddTab(tb);
-                this.AddNewTab(tb, startup: false);
+                var restoredTab = this.statuses.UndoRemovedTab();
+                this.AddNewTab(restoredTab, startup: false);
 
                 var tabIndex = this.statuses.Tabs.Count - 1;
                 this.ListTab.SelectedIndex = tabIndex;
 
                 this.SaveConfigsTabs();
             }
+            catch (TabException ex)
+            {
+                MessageBox.Show(this, ex.Message, ApplicationSettings.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
+            }
         }
 
         private async Task DoMoveToRTHome()
@@ -8963,14 +8953,7 @@ namespace OpenTween
 
         private void MenuItemEdit_DropDownOpening(object sender, EventArgs e)
         {
-            if (this.statuses.RemovedTab.Count == 0)
-            {
-                this.UndoRemoveTabMenuItem.Enabled = false;
-            }
-            else
-            {
-                this.UndoRemoveTabMenuItem.Enabled = true;
-            }
+            this.UndoRemoveTabMenuItem.Enabled = this.statuses.CanUndoRemovedTab;
 
             if (this.CurrentTab.TabType == MyCommon.TabUsageType.PublicSearch)
                 this.PublicSearchQueryMenuItem.Enabled = true;