123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- using System.ComponentModel;
- namespace Terminal.Gui;
- // TODO: I don't love the name Shortcut, but I can't think of a better one right now. Shortcut is a bit overloaded.
- // TODO: It can mean "Application-scoped key binding" or "A key binding that is displayed in a visual way".
- // TODO: I tried `BarItem` but that's not great either as it implies it can only be used in `Bar`s.
- /// <summary>
- /// Displays a command, help text, and a key binding. Useful for displaying a command in <see cref="Bar"/> such as a
- /// menu, toolbar, or status bar.
- /// </summary>
- /// <remarks>
- /// <para>
- /// When the user clicks on the <see cref="Shortcut"/> or presses the key
- /// specified by <see cref="Key"/> the <see cref="Command.Accept"/> command is invoked, causing the
- /// <see cref="Accept"/> event to be fired
- /// </para>
- /// <para>
- /// If <see cref="KeyBindingScope"/> is <see cref="KeyBindingScope.Application"/>, the <see cref="Command"/>
- /// be invoked regardless of what View has focus, enabling an application-wide keyboard shortcut.
- /// </para>
- /// <para>
- /// Set <see cref="View.Title"/> to change the Command text displayed in the <see cref="Shortcut"/>.
- /// By default, the <see cref="Command"/> text is the <see cref="View.Title"/> of <see cref="CommandView"/>.
- /// </para>
- /// <para>
- /// Set <see cref="View.Text"/> to change the Help text displayed in the <see cref="Shortcut"/>.
- /// </para>
- /// <para>
- /// The text displayed for the <see cref="Key"/> is the string representation of the <see cref="Key"/>.
- /// If the <see cref="Key"/> is <see cref="Key.Empty"/>, the <see cref="Key"/> text is not displayed.
- /// </para>
- /// </remarks>
- public class Shortcut : View
- {
- // Hosts the Command, Help, and Key Views. Needed (IIRC - wrote a long time ago) to allow mouse clicks to be handled by the Shortcut.
- internal readonly View _container;
- /// <summary>
- /// Creates a new instance of <see cref="Shortcut"/>.
- /// </summary>
- public Shortcut ()
- {
- CanFocus = true;
- Width = Dim.Auto (DimAutoStyle.Content);
- Height = Dim.Auto (DimAutoStyle.Content);
- //Height = Dim.Auto (minimumContentDim: 1, maximumContentDim: 1);
- AddCommand (Gui.Command.HotKey, () => true);
- AddCommand (Gui.Command.Accept, OnAccept);
- KeyBindings.Add (KeyCode.Space, Gui.Command.Accept);
- KeyBindings.Add (KeyCode.Enter, Gui.Command.Accept);
- _container = new ()
- {
- Id = "_container",
- // Only the Shortcut (_container) should be able to have focus, not any subviews.
- CanFocus = true,
- Width = Dim.Auto (DimAutoStyle.Content, 1),
- Height = Dim.Auto (DimAutoStyle.Content, 1),
- BorderStyle = LineStyle.Dashed
- };
- CommandView = new ();
- HelpView = new ()
- {
- Id = "_helpView",
- // Only the Shortcut should be able to have focus, not any subviews
- CanFocus = false,
- X = Pos.Align (Alignment.End, AlignmentModes.IgnoreFirstOrLast | AlignmentModes.AddSpaceBetweenItems),
- Y = Pos.Center (),
- // Helpview is the only subview that doesn't have a min width
- Width = Dim.Auto (DimAutoStyle.Text),
- Height = Dim.Auto (DimAutoStyle.Text),
- ColorScheme = Colors.ColorSchemes ["Error"]
- };
- _container.Add (HelpView);
- // HelpView.TextAlignment = Alignment.End;
- HelpView.MouseClick += Shortcut_MouseClick;
- KeyView = new ()
- {
- Id = "_keyView",
- // Only the Shortcut should be able to have focus, not any subviews
- CanFocus = false,
- X = Pos.Align (Alignment.End, AlignmentModes.IgnoreFirstOrLast | AlignmentModes.AddSpaceBetweenItems),
- Y = Pos.Center (),
- // Bar will set the width of all KeyViews to the width of the widest KeyView.
- Width = Dim.Auto (DimAutoStyle.Text),
- Height = Dim.Auto (DimAutoStyle.Text),
- };
- _container.Add (KeyView);
- KeyView.MouseClick += Shortcut_MouseClick;
- CommandView.Margin.Thickness = new Thickness (1, 0, 1, 0);
- HelpView.Margin.Thickness = new Thickness (1, 0, 1, 0);
- KeyView.Margin.Thickness = new Thickness (1, 0, 1, 0);
- MouseClick += Shortcut_MouseClick;
- TitleChanged += Shortcut_TitleChanged;
- Initialized += OnInitialized;
- Add (_container);
- return;
- void OnInitialized (object sender, EventArgs e)
- {
- if (ColorScheme != null)
- {
- var cs = new ColorScheme (ColorScheme)
- {
- Normal = ColorScheme.HotNormal,
- HotNormal = ColorScheme.Normal
- };
- KeyView.ColorScheme = cs;
- }
- }
- }
- private void Shortcut_MouseClick (object sender, MouseEventEventArgs e)
- {
- // When the Shortcut is clicked, we want to invoke the Command and Set focus
- View view = sender as View;
- if (!e.Handled && Command.HasValue)
- {
- // If the subview (likely CommandView) didn't handle the mouse click, invoke the command.
- bool? handled = false;
- handled = InvokeCommand (Command.Value);
- if (handled.HasValue)
- {
- e.Handled = handled.Value;
- }
- }
- if (CanFocus)
- {
- SetFocus ();
- }
- e.Handled = true;
- }
- /// <inheritdoc/>
- public override ColorScheme ColorScheme
- {
- get
- {
- if (base.ColorScheme == null)
- {
- return SuperView?.ColorScheme ?? base.ColorScheme;
- }
- return base.ColorScheme;
- }
- set
- {
- base.ColorScheme = value;
- if (ColorScheme != null)
- {
- var cs = new ColorScheme (ColorScheme)
- {
- Normal = ColorScheme.HotNormal,
- HotNormal = ColorScheme.Normal
- };
- KeyView.ColorScheme = cs;
- }
- }
- }
- #region Command
- private Command? _command;
- /// <summary>
- /// Gets or sets the <see cref="Command"/> that will be invoked when the user clicks on the <see cref="Shortcut"/> or
- /// presses <see cref="Key"/>.
- /// </summary>
- public Command? Command
- {
- get => _command;
- set
- {
- if (value != null)
- {
- _command = value.Value;
- UpdateKeyBinding ();
- }
- }
- }
- private View _commandView;
- /// <summary>
- /// Gets or sets the View that displays the command text and hotkey.
- /// </summary>
- /// <remarks>
- /// <para>
- /// By default, the <see cref="View.Title"/> of the <see cref="CommandView"/> is displayed as the Shortcut's
- /// command text.
- /// </para>
- /// <para>
- /// By default, the CommandView is a <see cref="View"/> with <see cref="View.CanFocus"/> set to
- /// <see langword="false"/>.
- /// </para>
- /// <para>
- /// Setting the <see cref="CommandView"/> will add it to the <see cref="Shortcut"/> and remove any existing
- /// <see cref="CommandView"/>.
- /// </para>
- /// </remarks>
- /// <example>
- /// <para>
- /// This example illustrates how to add a <see cref="Shortcut"/> to a <see cref="StatusBar"/> that toggles the
- /// <see cref="Application.Force16Colors"/> property.
- /// </para>
- /// <code>
- /// var force16ColorsShortcut = new Shortcut
- /// {
- /// Key = Key.F6,
- /// KeyBindingScope = KeyBindingScope.HotKey,
- /// Command = Command.Accept,
- /// CommandView = new CheckBox { Text = "Force 16 Colors" }
- /// };
- /// var cb = force16ColorsShortcut.CommandView as CheckBox;
- /// cb.Checked = Application.Force16Colors;
- ///
- /// cb.Toggled += (s, e) =>
- /// {
- /// var cb = s as CheckBox;
- /// Application.Force16Colors = cb!.Checked == true;
- /// Application.Refresh();
- /// };
- /// StatusBar.Add(force16ColorsShortcut);
- /// </code>
- /// </example>
- public View CommandView
- {
- get => _commandView;
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException ();
- }
- if (_commandView is { })
- {
- _container.Remove (_commandView);
- _commandView?.Dispose ();
- }
- _commandView = value;
- _commandView.Id = "_commandView";
- // TODO: Determine if it makes sense to allow the CommandView to be focusable.
- // Right now, we don't set CanFocus to false here.
- _commandView.CanFocus = false;
- // Bar will set the width of all CommandViews to the width of the widest CommandViews.
- _commandView.Width = Dim.Auto (DimAutoStyle.Text);
- _commandView.Height = Dim.Auto (DimAutoStyle.Text);
- _commandView.X = X = Pos.Align (Alignment.End, AlignmentModes.IgnoreFirstOrLast | AlignmentModes.AddSpaceBetweenItems);
- _commandView.Y = Pos.Center ();
- _commandView.MouseClick += Shortcut_MouseClick;
- _commandView.Accept += CommandView_Accept;
- _commandView.Margin.Thickness = new (1, 0, 1, 0);
- _commandView.HotKeyChanged += (s, e) =>
- {
- if (e.NewKey != Key.Empty)
- {
- // Add it
- AddKeyBindingsForHotKey (e.OldKey, e.NewKey);
- }
- };
- _commandView.HotKeySpecifier = new ('_');
- _container.Remove (HelpView);
- _container.Remove (KeyView);
- _container.Add (_commandView, HelpView, KeyView);
- UpdateKeyBinding();
- }
- }
- private void _commandView_MouseEvent (object sender, MouseEventEventArgs e)
- {
- e.Handled = true;
- }
- private void Shortcut_TitleChanged (object sender, StateEventArgs<string> e)
- {
- // If the Title changes, update the CommandView text. This is a helper to make it easier to set the CommandView text.
- // CommandView is public and replaceable, but this is a convenience.
- _commandView.Text = Title;
- }
- private void CommandView_Accept (object sender, CancelEventArgs e)
- {
- // When the CommandView fires its Accept event, we want to act as though the
- // Shortcut was clicked.
- var args = new HandledEventArgs ();
- Accept?.Invoke (this, args);
- if (args.Handled)
- {
- e.Cancel = args.Handled;
- }
- }
- #endregion Command
- #region Help
- /// <summary>
- /// The subview that displays the help text for the command. Internal for unit testing.
- /// </summary>
- internal View HelpView { get; set; }
- /// <summary>
- /// Gets or sets the help text displayed in the middle of the Shortcut.
- /// </summary>
- public override string Text
- {
- get => base.Text;
- set
- {
- //base.Text = value;
- if (HelpView != null)
- {
- HelpView.Text = value;
- }
- }
- }
- #endregion Help
- #region Key
- private Key _key;
- /// <summary>
- /// Gets or sets the <see cref="Key"/> that will be bound to the <see cref="Command.Accept"/> command.
- /// </summary>
- public Key Key
- {
- get => _key;
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException ();
- }
- _key = value;
- if (Command != null)
- {
- UpdateKeyBinding ();
- }
- KeyView.Text = $"{Key}";
- KeyView.Visible = Key != Key.Empty;
- }
- }
- private KeyBindingScope _keyBindingScope;
- /// <summary>
- /// Gets or sets the scope for the key binding for how <see cref="Key"/> is bound to <see cref="Command"/>.
- /// </summary>
- public KeyBindingScope KeyBindingScope
- {
- get => _keyBindingScope;
- set
- {
- _keyBindingScope = value;
- if (Command != null)
- {
- UpdateKeyBinding ();
- }
- }
- }
- /// <summary>
- /// Gets the subview that displays the key. Internal for unit testing.
- /// </summary>
- internal View KeyView { get; }
- private void UpdateKeyBinding ()
- {
- if (KeyBindingScope == KeyBindingScope.Application)
- {
- // return;
- }
- if (Command != null && Key != null && Key != Key.Empty)
- {
- // CommandView holds our command/keybinding
- // Add a key binding for this command to this Shortcut
- if (CommandView.GetSupportedCommands ().Contains (Command.Value))
- {
- CommandView.KeyBindings.Remove (Key);
- CommandView.KeyBindings.Add (Key, KeyBindingScope, Command.Value);
- }
- else
- {
- // throw new InvalidOperationException ($"CommandView does not support the command {Command.Value}");
- }
- }
- }
- #endregion Key
- /// <summary>
- /// The event fired when the <see cref="Command.Accept"/> command is received. This
- /// occurs if the user clicks on the Shortcut or presses <see cref="Key"/>.
- /// </summary>
- public new event EventHandler<HandledEventArgs> Accept;
- /// <summary>
- /// Called when the <see cref="Command.Accept"/> command is received. This
- /// occurs if the user clicks on the Bar with the mouse or presses the key bound to
- /// Command.Accept (Space by default).
- /// </summary>
- protected new bool? OnAccept ()
- {
- // TODO: This is not completely thought through.
- if (Key == null || Key == Key.Empty)
- {
- return false;
- }
- var handled = false;
- var keyCopy = new Key (Key);
- switch (KeyBindingScope)
- {
- case KeyBindingScope.Application:
- // Simulate a key down to invoke the Application scoped key binding
- handled = Application.OnKeyDown (keyCopy);
- break;
- case KeyBindingScope.Focused:
- handled = InvokeCommand (Command.Value) == true;
- handled = false;
- break;
- case KeyBindingScope.HotKey:
- if (Command.HasValue)
- {
- //handled = _commandView.InvokeCommand (Gui.Command.HotKey) == true;
- //handled = false;
- }
- break;
- }
- //if (handled == false)
- {
- var args = new HandledEventArgs ();
- Accept?.Invoke (this, args);
- handled = args.Handled;
- }
- return handled;
- }
- /// <inheritdoc/>
- public override bool OnEnter (View view)
- {
- // TODO: This is a hack. Need to refine this.
- var cs = new ColorScheme (ColorScheme)
- {
- Normal = ColorScheme.Focus,
- HotNormal = ColorScheme.HotFocus
- };
- _container.ColorScheme = cs;
- cs = new (ColorScheme)
- {
- Normal = ColorScheme.HotFocus,
- HotNormal = ColorScheme.Focus
- };
- KeyView.ColorScheme = cs;
- return base.OnEnter (view);
- }
- /// <inheritdoc/>
- public override bool OnLeave (View view)
- {
- // TODO: This is a hack. Need to refine this.
- var cs = new ColorScheme (ColorScheme)
- {
- Normal = ColorScheme.Normal,
- HotNormal = ColorScheme.HotNormal
- };
- _container.ColorScheme = cs;
- cs = new (ColorScheme)
- {
- Normal = ColorScheme.HotNormal,
- HotNormal = ColorScheme.Normal
- };
- KeyView.ColorScheme = cs;
- return base.OnLeave (view);
- }
- }
|