瀏覽代碼

Moved `Canceled` from `FileDialog` to `Dialog` (because that's just a good idea and it provides a good case for having a property test for Disposal on get/set).

Tweaked `Run_Dispose_Dialog` to illustrate how broken it is to have `Run/End` dispose `Top`.
Tig 1 年之前
父節點
當前提交
0a6193a7a3
共有 3 個文件被更改,包括 44 次插入4 次删除
  1. 36 0
      Terminal.Gui/Views/Dialog.cs
  2. 7 4
      Terminal.Gui/Views/FileDialog.cs
  3. 1 0
      UnitTests/Dialogs/DialogTests.cs

+ 36 - 0
Terminal.Gui/Views/Dialog.cs

@@ -68,9 +68,45 @@ public class Dialog : Window
         Modal = true;
         ButtonAlignment = DefaultButtonAlignment;
 
+        AddCommand (Command.QuitToplevel, () =>
+                                          {
+                                              Canceled = true;
+                                              RequestStop ();
+                                              return true;
+                                          });
         KeyBindings.Add (Key.Esc, Command.QuitToplevel);
     }
 
+
+    private bool _canceled;
+
+    /// <summary>Gets a value indicating whether the <see cref="Dialog"/> was canceled.</summary>
+    /// <remarks>The default value is <see langword="true"/>.</remarks>
+    public bool Canceled
+    {
+        get
+        {
+#if DEBUG_IDISPOSABLE
+            if (WasDisposed)
+            {
+                throw new ObjectDisposedException (GetType ().FullName);
+            }
+#endif
+            return _canceled;
+        }
+        set
+        {
+#if DEBUG_IDISPOSABLE
+            if (WasDisposed)
+            {
+                throw new ObjectDisposedException (GetType ().FullName);
+            }
+#endif
+            _canceled = value;
+            return;
+        }
+    }
+
     /// <summary>Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the bottom of the dialog.</summary>
     public ButtonAlignments ButtonAlignment { get; set; }
 

+ 7 - 4
Terminal.Gui/Views/FileDialog.cs

@@ -60,6 +60,9 @@ public class FileDialog : Dialog
     /// <remarks>This overload is mainly useful for testing.</remarks>
     internal FileDialog (IFileSystem fileSystem)
     {
+        // Assume canceled
+        Canceled = true;
+
         _fileSystem = fileSystem;
         Style = new FileDialogStyle (fileSystem);
 
@@ -83,7 +86,10 @@ public class FileDialog : Dialog
                                   NavigateIf (k, KeyCode.CursorUp, _tableView);
                                   NavigateIf (k, KeyCode.CursorRight, _btnOk);
                               };
-        _btnCancel.Accept += (s, e) => { Application.RequestStop (); };
+        _btnCancel.Accept += (s, e) => {
+                                 Canceled = true;
+                                 Application.RequestStop ();
+                             };
 
         _btnUp = new Button { X = 0, Y = 1, NoPadding = true };
         _btnUp.Text = GetUpButtonText ();
@@ -314,9 +320,6 @@ public class FileDialog : Dialog
         set => _tableView.MultiSelect = value;
     }
 
-    /// <summary>Gets a value indicating whether the <see cref="FileDialog"/> was closed without confirming a selection.</summary>
-    public bool Canceled { get; private set; } = true;
-
     /// <summary>The UI selected <see cref="IAllowedType"/> from combo box. May be null.</summary>
     public IAllowedType CurrentFilter { get; private set; }
 

+ 1 - 0
UnitTests/Dialogs/DialogTests.cs

@@ -1353,6 +1353,7 @@ public class DialogTests
         // This is instance and the user is responsible to set to null or not
         // because in the Application it's all working as expected.
         // Fortunately, this instance is not null so that we can obtain the value of its properties
+        Assert.True (dlg.Canceled);
         Assert.NotNull (dlg);
 
         Shutdown();