diff --git a/Terminal.Gui/App/IModalRunnable.cs b/Terminal.Gui/App/IModalRunnable.cs index 29bf97ce2..01fec71d9 100644 --- a/Terminal.Gui/App/IModalRunnable.cs +++ b/Terminal.Gui/App/IModalRunnable.cs @@ -30,7 +30,7 @@ namespace Terminal.Gui.App; public interface IModalRunnable : IRunnable { /// - /// Gets the result data from the modal operation, or null if not accepted. + /// Gets or sets the result data from the modal operation. /// /// /// @@ -38,15 +38,16 @@ public interface IModalRunnable : IRunnable /// file selected). The result should be extracted from the modal's state before views are disposed. /// /// - /// null indicates the modal was stopped without accepting (ESC key, cancel button, close without action). - /// Non-null contains the type-safe result data. + /// For value types (like ), implementations should use a nullable type (e.g., int?) + /// where null indicates the modal was stopped without accepting. + /// For reference types, null similarly indicates cancellation. /// /// - /// For example: - /// - : Returns button index (int) or custom result - /// - : Returns button index (int) - /// - : Returns selected file path (string) + /// Examples: + /// - : Implement with IModalRunnable<int?>, returns button index or null + /// - : Implement with IModalRunnable<int?>, returns button index or null + /// - : Implement with IModalRunnable<string?>, returns file path or null /// /// - TResult? Result { get; set; } + TResult Result { get; set; } } diff --git a/Terminal.Gui/Views/Dialog.cs b/Terminal.Gui/Views/Dialog.cs index 4037d7312..6ebe0a93a 100644 --- a/Terminal.Gui/Views/Dialog.cs +++ b/Terminal.Gui/Views/Dialog.cs @@ -7,13 +7,19 @@ namespace Terminal.Gui.Views; /// scheme. /// /// +/// /// To run the modally, create the , and pass it to /// . This will execute the dialog until /// it terminates via the (`Esc` by default), /// or when one of the views or buttons added to the dialog calls /// . +/// +/// +/// Dialog implements with int? as the result type. +/// The property contains the index of the button that was clicked, or null if canceled. +/// /// -public class Dialog : Window +public class Dialog : Window, IModalRunnable { /// /// Initializes a new instance of the class with no s. @@ -60,6 +66,24 @@ public class Dialog : Window _buttons.Add (button); Add (button); + + // Subscribe to the button's Accept command to set Result + button.Accepting += Button_Accepting; + } + + private void Button_Accepting (object? sender, CommandEventArgs e) + { + // Set Result to the index of the button that was clicked + if (sender is Button button) + { + int index = _buttons.IndexOf (button); + if (index >= 0) + { + Result = index; + // For backward compatibility, set Canceled = false + Canceled = false; + } + } } // TODO: Update button.X = Pos.Justify when alignment changes @@ -85,7 +109,14 @@ public class Dialog : Window } /// Gets a value indicating whether the was canceled. - /// The default value is . + /// + /// The default value is . + /// + /// Obsolete: Use instead. When is null, the dialog was canceled. + /// This property is maintained for backward compatibility. + /// + /// + [Obsolete ("Use Result property instead. Result == null indicates the dialog was canceled.")] public bool Canceled { get { return _canceled; } @@ -101,6 +132,29 @@ public class Dialog : Window } } + /// + /// Gets or sets the result of the modal dialog operation. + /// + /// + /// + /// Contains the zero-based index of the button that was clicked to close the dialog, + /// or null if the dialog was canceled (e.g., ESC key pressed). + /// + /// + /// The button index corresponds to the order buttons were added via or + /// the initializer. + /// + /// + /// For backward compatibility with the property: + /// - == null means the dialog was canceled ( == true) + /// - != null means a button was clicked ( == false) + /// + /// + /// This property implements where TResult is int?. + /// + /// + public int? Result { get; set; } + /// /// Defines the default border styling for . Can be configured via /// .