Fixed Dialog & Messagebox

This commit is contained in:
Tig
2024-10-08 00:49:51 -04:00
parent 4410b54d61
commit b2049d8991
12 changed files with 41 additions and 37 deletions

View File

@@ -68,9 +68,9 @@ public partial class View // Command APIs
/// If <see langword="true"/> the event was canceled. If <see langword="false"/> the event was raised but not canceled.
/// If <see langword="null"/> no event was raised.
/// </returns>
protected bool? RaiseAccepting ()
protected bool? RaiseAccepting (CommandContext ctx)
{
CommandEventArgs args = new ();
CommandEventArgs args = new () { Context = ctx };
// Best practice is to invoke the virtual method first.
// This allows derived classes to handle the event and potentially cancel it.

View File

@@ -79,7 +79,7 @@ public class Button : View, IDesignable
return true;
}
bool? handled = RaiseAccepting ();
bool? handled = RaiseAccepting (ctx);
if (handled == true)
{
@@ -132,6 +132,7 @@ public class Button : View, IDesignable
return;
}
// TODO: With https://github.com/gui-cs/Terminal.Gui/issues/3778 we won't have to pass data:
e.Handled = InvokeCommand (Command.HotKey, new (Command.HotKey, null, data: this)) == true;
}

View File

@@ -84,7 +84,7 @@ public class ComboBox : View, IDesignable
{
return null;
}
return ActivateSelected ();
return ActivateSelected (ctx);
});
AddCommand (Command.Toggle, () => ExpandCollapse ());
AddCommand (Command.Expand, () => Expand ());
@@ -392,7 +392,7 @@ public class ComboBox : View, IDesignable
}
}
private bool ActivateSelected ()
private bool ActivateSelected (CommandContext ctx)
{
if (HasItems ())
{
@@ -401,7 +401,7 @@ public class ComboBox : View, IDesignable
return false;
}
return RaiseAccepting () == true;
return RaiseAccepting (ctx) == true;
}
return false;

View File

@@ -138,9 +138,9 @@ public class ListView : View, IDesignable
AddCommand (Command.ScrollRight, () => ScrollHorizontal (1));
// Accept (Enter key) - Raise Accept event - DO NOT advance state
AddCommand (Command.Accept, () =>
AddCommand (Command.Accept, (ctx) =>
{
if (RaiseAccepting () == true)
if (RaiseAccepting (ctx) == true)
{
return true;
}

View File

@@ -119,14 +119,14 @@ public class MenuBar : View, IDesignable
AddCommand (
Command.Accept,
() =>
(ctx) =>
{
if (Menus.Length > 0)
{
ProcessMenu (_selected, Menus [_selected]);
}
return RaiseAccepting ();
return RaiseAccepting (ctx);
}
);
AddCommand (Command.Toggle, ctx =>

View File

@@ -338,6 +338,7 @@ public static class MessageBox
// Create button array for Dialog
var count = 0;
List<Button> buttonList = new ();
Clicked = -1;
if (buttons is { })
{
@@ -350,12 +351,28 @@ public static class MessageBox
{
var b = new Button
{
Text = s,
Text = $"_{s}",
Data = count,
};
if (count == defaultButton)
{
b.IsDefault = true;
b.Accepting += (s, e) =>
{
// TODO: With https://github.com/gui-cs/Terminal.Gui/issues/3778 we can simplify this
if (e.Context.Data is Button button)
{
Clicked = (int)button.Data!;
}
else if (e.Context.KeyBinding?.BoundView is Button btn)
{
Clicked = (int)btn.Data!;
}
e.Cancel = true;
Application.RequestStop ();
};
}
buttonList.Add (b);
@@ -373,7 +390,7 @@ public static class MessageBox
};
d.Width = Dim.Auto (DimAutoStyle.Auto,
minimumContentDim: Dim.Func (() => (int)((Application.Screen.Width - d.GetAdornmentsThickness ().Horizontal) * (DefaultMinimumWidth / 100f) )),
minimumContentDim: Dim.Func (() => (int)((Application.Screen.Width - d.GetAdornmentsThickness ().Horizontal) * (DefaultMinimumWidth / 100f))),
maximumContentDim: Dim.Func (() => (int)((Application.Screen.Width - d.GetAdornmentsThickness ().Horizontal) * 0.9f)));
d.Height = Dim.Auto (DimAutoStyle.Auto,
@@ -400,21 +417,6 @@ public static class MessageBox
d.TextFormatter.WordWrap = wrapMessage;
d.TextFormatter.MultiLine = !wrapMessage;
// Setup actions
Clicked = -1;
for (var n = 0; n < buttonList.Count; n++)
{
int buttonId = n;
Button b = buttonList [n];
b.Accepting += (s, e) =>
{
Clicked = buttonId;
Application.RequestStop ();
};
}
// Run the modal; do not shut down the mainloop driver when done
Application.Run (d);
d.Dispose ();

View File

@@ -50,7 +50,7 @@ public class RadioGroup : View, IDesignable, IOrientation
});
// Accept (Enter key) - Raise Accept event - DO NOT advance state
AddCommand (Command.Accept, () => RaiseAccepting ());
AddCommand (Command.Accept, RaiseAccepting);
// Hotkey - ctx may indicate a radio item hotkey was pressed. Beahvior depends on HasFocus
// If HasFocus and it's this.HotKey invoke Select command - DO NOT raise Accept

View File

@@ -344,7 +344,7 @@ public class Shortcut : View, IOrientation, IDesignable
var cancel = false;
cancel = RaiseAccepting () is true;
cancel = RaiseAccepting (ctx) is true;
if (cancel)
{

View File

@@ -1423,7 +1423,7 @@ public class Slider<T> : View, IOrientation
AddCommand (Command.RightExtend, () => ExtendPlus ());
AddCommand (Command.LeftExtend, () => ExtendMinus ());
AddCommand (Command.Select, () => Select ());
AddCommand (Command.Accept, () => Accept ());
AddCommand (Command.Accept, (ctx) => Accept (ctx));
SetKeyBindings ();
}
@@ -1785,11 +1785,11 @@ public class Slider<T> : View, IOrientation
return SetFocusedOption ();
}
internal bool Accept ()
internal bool Accept (CommandContext ctx)
{
SetFocusedOption ();
return RaiseAccepting () == true;
return RaiseAccepting (ctx) == true;
}
internal bool MovePlus ()

View File

@@ -2012,7 +2012,7 @@ public class TextView : View
// Things this view knows how to do
// Note - NewLine is only bound to Enter if Multiline is true
AddCommand (Command.NewLine, () => ProcessEnterKey ());
AddCommand (Command.NewLine, (ctx) => ProcessEnterKey (ctx));
AddCommand (
Command.PageDown,
@@ -6043,7 +6043,7 @@ public class TextView : View
Paste ();
}
private bool ProcessEnterKey ()
private bool ProcessEnterKey (CommandContext ctx)
{
ResetColumnTrack ();
@@ -6056,7 +6056,7 @@ public class TextView : View
{
// By Default pressing ENTER should be ignored (OnAccept will return false or null). Only cancel if the
// event was fired and set Cancel = true.
return RaiseAccepting () is null or false;
return RaiseAccepting (ctx) is null or false;
}
SetWrapModel ();

View File

@@ -443,10 +443,10 @@ public class TreeView<T> : View, ITreeView where T : class
/// <para>This method also ensures that the selected object is visible.</para>
/// </summary>
/// <returns><see langword="true"/> if <see cref="ObjectActivated"/> was fired.</returns>
public bool? ActivateSelectedObjectIfAny ()
public bool? ActivateSelectedObjectIfAny (CommandContext ctx)
{
// By default, Command.Accept calls OnAccept, so we need to call it here to ensure that the event is fired.
if (RaiseAccepting () == true)
if (RaiseAccepting (ctx) == true)
{
return true;
}

View File

@@ -258,6 +258,7 @@ public class Dialogs : Scenario
button.Accepting += (s, e) =>
{
clicked = buttonId;
e.Cancel = true;
Application.RequestStop ();
};
buttons.Add (button);