|
@@ -1,10 +1,8 @@
|
|
|
-using System.ComponentModel;
|
|
|
-using System.Threading.Channels;
|
|
|
-
|
|
|
-namespace Terminal.Gui;
|
|
|
+namespace Terminal.Gui;
|
|
|
|
|
|
/// <summary>
|
|
|
-/// Displays a command, help text, and a key binding. When the key specified by <see cref="Key"/> is pressed, the command will be invoked. Useful for
|
|
|
+/// Displays a command, help text, and a key binding. When the key specified by <see cref="Key"/> is pressed, the
|
|
|
+/// command will be invoked. Useful for
|
|
|
/// displaying a command in <see cref="Bar"/> such as a
|
|
|
/// menu, toolbar, or status bar.
|
|
|
/// </summary>
|
|
@@ -12,12 +10,13 @@ namespace Terminal.Gui;
|
|
|
/// <para>
|
|
|
/// The following user actions will invoke the <see cref="Command.Accept"/>, causing the
|
|
|
/// <see cref="View.Accept"/> event to be fired:
|
|
|
-/// - Clicking on the <see cref="Shortcut"/>.
|
|
|
-/// - Pressing the key specified by <see cref="Key"/>.
|
|
|
-/// - Pressing the HotKey specified by <see cref="CommandView"/>.
|
|
|
+/// - Clicking on the <see cref="Shortcut"/>.
|
|
|
+/// - Pressing the key specified by <see cref="Key"/>.
|
|
|
+/// - Pressing the HotKey specified by <see cref="CommandView"/>.
|
|
|
/// </para>
|
|
|
/// <para>
|
|
|
-/// If <see cref="KeyBindingScope"/> is <see cref="KeyBindingScope.Application"/>, <see cref="Key"/> will invoked <see cref="Command.Accept"/>
|
|
|
+/// If <see cref="KeyBindingScope"/> is <see cref="KeyBindingScope.Application"/>, <see cref="Key"/> will invoked
|
|
|
+/// <see cref="Command.Accept"/>
|
|
|
/// command regardless of what View has focus, enabling an application-wide keyboard shortcut.
|
|
|
/// </para>
|
|
|
/// <para>
|
|
@@ -37,7 +36,7 @@ namespace Terminal.Gui;
|
|
|
/// If the <see cref="Key"/> is <see cref="Key.Empty"/>, the <see cref="Key"/> text is not displayed.
|
|
|
/// </para>
|
|
|
/// </remarks>
|
|
|
-public class Shortcut : View
|
|
|
+public class Shortcut : View, IOrientation, IDesignable
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// Creates a new instance of <see cref="Shortcut"/>.
|
|
@@ -60,6 +59,10 @@ public class Shortcut : View
|
|
|
Width = GetWidthDimAuto ();
|
|
|
Height = Dim.Auto (DimAutoStyle.Content, 1);
|
|
|
|
|
|
+ _orientationHelper = new (this);
|
|
|
+ _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e);
|
|
|
+ _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e);
|
|
|
+
|
|
|
AddCommand (Command.HotKey, ctx => OnAccept (ctx));
|
|
|
AddCommand (Command.Accept, ctx => OnAccept (ctx));
|
|
|
AddCommand (Command.Select, ctx => OnSelect (ctx));
|
|
@@ -132,33 +135,30 @@ public class Shortcut : View
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Creates a new instance of <see cref="Shortcut"/>.
|
|
|
/// </summary>
|
|
|
public Shortcut () : this (Key.Empty, string.Empty, null) { }
|
|
|
|
|
|
- private Orientation _orientation = Orientation.Horizontal;
|
|
|
+ private readonly OrientationHelper _orientationHelper;
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Gets or sets the <see cref="Orientation"/> for this <see cref="Shortcut"/>. The default is
|
|
|
- /// <see cref="Orientation.Horizontal"/>, which is ideal for status bar, menu bar, and tool bar items If set to
|
|
|
- /// <see cref="Orientation.Vertical"/>,
|
|
|
- /// the Shortcut will be configured for vertical layout, which is ideal for menu items.
|
|
|
- /// </summary>
|
|
|
- public Orientation Orientation
|
|
|
+ private AlignmentModes _alignmentModes = AlignmentModes.StartToEnd | AlignmentModes.IgnoreFirstOrLast;
|
|
|
+
|
|
|
+ // This is used to calculate the minimum width of the Shortcut when the width is NOT Dim.Auto
|
|
|
+ private int? _minimumDimAutoWidth;
|
|
|
+
|
|
|
+ private Color? _savedForeColor;
|
|
|
+
|
|
|
+ /// <inheritdoc/>
|
|
|
+ public bool EnableForDesign ()
|
|
|
{
|
|
|
- get => _orientation;
|
|
|
- set
|
|
|
- {
|
|
|
- _orientation = value;
|
|
|
+ Title = "_Shortcut";
|
|
|
+ HelpText = "Shortcut help";
|
|
|
+ Key = Key.F1;
|
|
|
|
|
|
- // TODO: Determine what, if anything, is opinionated about the orientation.
|
|
|
- }
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
- private AlignmentModes _alignmentModes = AlignmentModes.StartToEnd | AlignmentModes.IgnoreFirstOrLast;
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Gets or sets the <see cref="AlignmentModes"/> for this <see cref="Shortcut"/>.
|
|
|
/// </summary>
|
|
@@ -180,6 +180,30 @@ public class Shortcut : View
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <inheritdoc/>
|
|
|
+ protected override void Dispose (bool disposing)
|
|
|
+ {
|
|
|
+ if (disposing)
|
|
|
+ {
|
|
|
+ if (CommandView?.IsAdded == false)
|
|
|
+ {
|
|
|
+ CommandView.Dispose ();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (HelpView?.IsAdded == false)
|
|
|
+ {
|
|
|
+ HelpView.Dispose ();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (KeyView?.IsAdded == false)
|
|
|
+ {
|
|
|
+ KeyView.Dispose ();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ base.Dispose (disposing);
|
|
|
+ }
|
|
|
+
|
|
|
// When one of the subviews is "empty" we don't want to show it. So we
|
|
|
// Use Add/Remove. We need to be careful to add them in the right order
|
|
|
// so Pos.Align works correctly.
|
|
@@ -203,8 +227,15 @@ public class Shortcut : View
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // This is used to calculate the minimum width of the Shortcut when the width is NOT Dim.Auto
|
|
|
- private int? _minimumDimAutoWidth;
|
|
|
+ private Thickness GetMarginThickness ()
|
|
|
+ {
|
|
|
+ if (Orientation == Orientation.Vertical)
|
|
|
+ {
|
|
|
+ return new (1, 0, 1, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ return new (1, 0, 1, 0);
|
|
|
+ }
|
|
|
|
|
|
// When layout starts, we need to adjust the layout of the HelpView and KeyView
|
|
|
private void OnLayoutStarted (object sender, LayoutEventArgs e)
|
|
@@ -283,18 +314,16 @@ public class Shortcut : View
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private Thickness GetMarginThickness ()
|
|
|
+ private bool? OnSelect (CommandContext ctx)
|
|
|
{
|
|
|
- if (Orientation == Orientation.Vertical)
|
|
|
+ if (CommandView.GetSupportedCommands ().Contains (Command.Select))
|
|
|
{
|
|
|
- return new (1, 0, 1, 0);
|
|
|
+ return CommandView.InvokeCommand (Command.Select, ctx.Key, ctx.KeyBinding);
|
|
|
}
|
|
|
|
|
|
- return new (1, 0, 1, 0);
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
- private Color? _savedForeColor;
|
|
|
-
|
|
|
private void Shortcut_Highlight (object sender, CancelEventArgs<HighlightStyle> e)
|
|
|
{
|
|
|
if (e.CurrentValue.HasFlag (HighlightStyle.Pressed))
|
|
@@ -344,9 +373,45 @@ public class Shortcut : View
|
|
|
private void Subview_MouseClick (object sender, MouseEventEventArgs e)
|
|
|
{
|
|
|
// TODO: Remove. This does nothing.
|
|
|
- return;
|
|
|
}
|
|
|
|
|
|
+ #region IOrientation members
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Gets or sets the <see cref="Orientation"/> for this <see cref="Bar"/>. The default is
|
|
|
+ /// <see cref="Orientation.Horizontal"/>.
|
|
|
+ /// </summary>
|
|
|
+ /// <remarks>
|
|
|
+ /// <para>
|
|
|
+ /// Horizontal orientation arranges the command, help, and key parts of each <see cref="Shortcut"/>s from right to
|
|
|
+ /// left
|
|
|
+ /// Vertical orientation arranges the command, help, and key parts of each <see cref="Shortcut"/>s from left to
|
|
|
+ /// right.
|
|
|
+ /// </para>
|
|
|
+ /// </remarks>
|
|
|
+
|
|
|
+ public Orientation Orientation
|
|
|
+ {
|
|
|
+ get => _orientationHelper.Orientation;
|
|
|
+ set => _orientationHelper.Orientation = value;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <inheritdoc/>
|
|
|
+ public event EventHandler<CancelEventArgs<Orientation>> OrientationChanging;
|
|
|
+
|
|
|
+ /// <inheritdoc/>
|
|
|
+ public event EventHandler<EventArgs<Orientation>> OrientationChanged;
|
|
|
+
|
|
|
+ /// <summary>Called when <see cref="Orientation"/> has changed.</summary>
|
|
|
+ /// <param name="newOrientation"></param>
|
|
|
+ public void OnOrientationChanged (Orientation newOrientation)
|
|
|
+ {
|
|
|
+ // TODO: Determine what, if anything, is opinionated about the orientation.
|
|
|
+ SetNeedsLayout ();
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
#region Command
|
|
|
|
|
|
private View _commandView = new ();
|
|
@@ -433,9 +498,7 @@ public class Shortcut : View
|
|
|
SetHelpViewDefaultLayout ();
|
|
|
SetKeyViewDefaultLayout ();
|
|
|
ShowHide ();
|
|
|
- UpdateKeyBinding ();
|
|
|
-
|
|
|
- return;
|
|
|
+ UpdateKeyBinding (Key.Empty);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -475,38 +538,38 @@ public class Shortcut : View
|
|
|
HelpView.VerticalTextAlignment = Alignment.Center;
|
|
|
}
|
|
|
|
|
|
-/// <summary>
|
|
|
-/// Gets or sets the help text displayed in the middle of the Shortcut. Identical in function to <see cref="HelpText"/>
|
|
|
-/// .
|
|
|
-/// </summary>
|
|
|
-public override string Text
|
|
|
-{
|
|
|
- get => HelpView?.Text;
|
|
|
- set
|
|
|
+ /// <summary>
|
|
|
+ /// Gets or sets the help text displayed in the middle of the Shortcut. Identical in function to <see cref="HelpText"/>
|
|
|
+ /// .
|
|
|
+ /// </summary>
|
|
|
+ public override string Text
|
|
|
{
|
|
|
- if (HelpView is {})
|
|
|
+ get => HelpView?.Text;
|
|
|
+ set
|
|
|
{
|
|
|
- HelpView.Text = value;
|
|
|
- ShowHide ();
|
|
|
+ if (HelpView is { })
|
|
|
+ {
|
|
|
+ HelpView.Text = value;
|
|
|
+ ShowHide ();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-/// <summary>
|
|
|
-/// Gets or sets the help text displayed in the middle of the Shortcut.
|
|
|
-/// </summary>
|
|
|
-public string HelpText
|
|
|
-{
|
|
|
- get => HelpView?.Text;
|
|
|
- set
|
|
|
+ /// <summary>
|
|
|
+ /// Gets or sets the help text displayed in the middle of the Shortcut.
|
|
|
+ /// </summary>
|
|
|
+ public string HelpText
|
|
|
{
|
|
|
- if (HelpView is {})
|
|
|
+ get => HelpView?.Text;
|
|
|
+ set
|
|
|
{
|
|
|
- HelpView.Text = value;
|
|
|
- ShowHide ();
|
|
|
+ if (HelpView is { })
|
|
|
+ {
|
|
|
+ HelpView.Text = value;
|
|
|
+ ShowHide ();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
#endregion Help
|
|
|
|
|
@@ -527,9 +590,10 @@ public string HelpText
|
|
|
throw new ArgumentNullException ();
|
|
|
}
|
|
|
|
|
|
+ Key oldKey = _key;
|
|
|
_key = value;
|
|
|
|
|
|
- UpdateKeyBinding ();
|
|
|
+ UpdateKeyBinding (oldKey);
|
|
|
|
|
|
KeyView.Text = Key == Key.Empty ? string.Empty : $"{Key}";
|
|
|
ShowHide ();
|
|
@@ -546,9 +610,24 @@ public string HelpText
|
|
|
get => _keyBindingScope;
|
|
|
set
|
|
|
{
|
|
|
+ if (value == _keyBindingScope)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (_keyBindingScope == KeyBindingScope.Application)
|
|
|
+ {
|
|
|
+ Application.KeyBindings.Remove (Key);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (_keyBindingScope is KeyBindingScope.HotKey or KeyBindingScope.Focused)
|
|
|
+ {
|
|
|
+ KeyBindings.Remove (Key);
|
|
|
+ }
|
|
|
+
|
|
|
_keyBindingScope = value;
|
|
|
|
|
|
- UpdateKeyBinding ();
|
|
|
+ UpdateKeyBinding (Key.Empty);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -561,7 +640,7 @@ public string HelpText
|
|
|
private int _minimumKeyTextSize;
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Gets or sets the minimum size of the key text. Useful for aligning the key text with other <see cref="Shortcut"/>s.
|
|
|
+ /// Gets or sets the minimum size of the key text. Useful for aligning the key text with other <see cref="Shortcut"/>s.
|
|
|
/// </summary>
|
|
|
public int MinimumKeyTextSize
|
|
|
{
|
|
@@ -600,9 +679,9 @@ public string HelpText
|
|
|
KeyView.KeyBindings.Clear ();
|
|
|
}
|
|
|
|
|
|
- private void UpdateKeyBinding ()
|
|
|
+ private void UpdateKeyBinding (Key oldKey)
|
|
|
{
|
|
|
- if (Key != null)
|
|
|
+ if (Key != null && Key.IsValid)
|
|
|
{
|
|
|
// Disable the command view key bindings
|
|
|
CommandView.KeyBindings.Remove (Key);
|
|
@@ -610,11 +689,21 @@ public string HelpText
|
|
|
|
|
|
if (KeyBindingScope.FastHasFlags (KeyBindingScope.Application))
|
|
|
{
|
|
|
+ if (oldKey != Key.Empty)
|
|
|
+ {
|
|
|
+ Application.KeyBindings.Remove (oldKey);
|
|
|
+ }
|
|
|
+
|
|
|
Application.KeyBindings.Remove (Key);
|
|
|
Application.KeyBindings.Add (Key, this, Command.Accept);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ if (oldKey != Key.Empty)
|
|
|
+ {
|
|
|
+ KeyBindings.Remove (oldKey);
|
|
|
+ }
|
|
|
+
|
|
|
KeyBindings.Remove (Key);
|
|
|
KeyBindings.Add (Key, KeyBindingScope | KeyBindingScope.HotKey, Command.Accept);
|
|
|
}
|
|
@@ -675,6 +764,7 @@ public string HelpText
|
|
|
if (Action is { })
|
|
|
{
|
|
|
Action.Invoke ();
|
|
|
+
|
|
|
// Assume if there's a subscriber to Action, it's handled.
|
|
|
cancel = true;
|
|
|
}
|
|
@@ -694,17 +784,6 @@ public string HelpText
|
|
|
|
|
|
#endregion Accept Handling
|
|
|
|
|
|
- private bool? OnSelect (CommandContext ctx)
|
|
|
- {
|
|
|
- if (CommandView.GetSupportedCommands ().Contains (Command.Select))
|
|
|
- {
|
|
|
- return CommandView.InvokeCommand (Command.Select, ctx.Key, ctx.KeyBinding);
|
|
|
- }
|
|
|
- return false;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
#region Focus
|
|
|
|
|
|
/// <inheritdoc/>
|
|
@@ -753,7 +832,8 @@ public string HelpText
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- View _lastFocusedView;
|
|
|
+ private View _lastFocusedView;
|
|
|
+
|
|
|
/// <inheritdoc/>
|
|
|
public override bool OnEnter (View view)
|
|
|
{
|
|
@@ -773,28 +853,4 @@ public string HelpText
|
|
|
}
|
|
|
|
|
|
#endregion Focus
|
|
|
-
|
|
|
- /// <inheritdoc/>
|
|
|
- protected override void Dispose (bool disposing)
|
|
|
- {
|
|
|
- if (disposing)
|
|
|
- {
|
|
|
- if (CommandView?.IsAdded == false)
|
|
|
- {
|
|
|
- CommandView.Dispose ();
|
|
|
- }
|
|
|
-
|
|
|
- if (HelpView?.IsAdded == false)
|
|
|
- {
|
|
|
- HelpView.Dispose ();
|
|
|
- }
|
|
|
-
|
|
|
- if (KeyView?.IsAdded == false)
|
|
|
- {
|
|
|
- KeyView.Dispose ();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- base.Dispose (disposing);
|
|
|
- }
|
|
|
}
|