Shortcut.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. #nullable enable
  2. using System.ComponentModel;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// Displays a command, help text, and a key binding. When the key specified by <see cref="Key"/> is pressed, the
  6. /// command will be invoked. Useful for
  7. /// displaying a command in <see cref="Bar"/> such as a
  8. /// menu, toolbar, or status bar.
  9. /// </summary>
  10. /// <remarks>
  11. /// <para>
  12. /// The following user actions will invoke the <see cref="Command.Accept"/>, causing the
  13. /// <see cref="View.Accepting"/> event to be fired:
  14. /// - Clicking on the <see cref="Shortcut"/>.
  15. /// - Pressing the key specified by <see cref="Key"/>.
  16. /// - Pressing the HotKey specified by <see cref="CommandView"/>.
  17. /// </para>
  18. /// <para>
  19. /// If <see cref="BindKeyToApplication"/> is <see langword="true"/>, <see cref="Key"/> will invoke
  20. /// <see cref="Command.Accept"/>
  21. /// regardless of what View has focus, enabling an application-wide keyboard shortcut.
  22. /// </para>
  23. /// <para>
  24. /// By default, a Shortcut displays the command text on the left side, the help text in the middle, and the key
  25. /// binding on the
  26. /// right side. Set <see cref="AlignmentModes"/> to <see cref="AlignmentModes.EndToStart"/> to reverse the order.
  27. /// </para>
  28. /// <para>
  29. /// The command text can be set by setting the <see cref="CommandView"/>'s Text property or by setting
  30. /// <see cref="View.Title"/>.
  31. /// </para>
  32. /// <para>
  33. /// The help text can be set by setting the <see cref="HelpText"/> property or by setting <see cref="View.Text"/>.
  34. /// </para>
  35. /// <para>
  36. /// The key text is set by setting the <see cref="Key"/> property.
  37. /// If the <see cref="Key"/> is <see cref="Key.Empty"/>, the <see cref="Key"/> text is not displayed.
  38. /// </para>
  39. /// </remarks>
  40. public class Shortcut : View, IOrientation, IDesignable
  41. {
  42. /// <summary>
  43. /// Creates a new instance of <see cref="Shortcut"/>.
  44. /// </summary>
  45. public Shortcut () : this (Key.Empty, null, null, null) { }
  46. /// <summary>
  47. /// Creates a new instance of <see cref="Shortcut"/>.
  48. /// </summary>
  49. /// <remarks>
  50. /// <para>
  51. /// This is a helper API that mimics the V1 API for creating StatusItems.
  52. /// </para>
  53. /// </remarks>
  54. /// <param name="key"></param>
  55. /// <param name="commandText">The text to display for the command.</param>
  56. /// <param name="action"></param>
  57. /// <param name="helpText">The help text to display.</param>
  58. public Shortcut (Key key, string? commandText, Action? action, string? helpText = null)
  59. {
  60. Id = $"shortcut:{commandText}";
  61. HighlightStyle = HighlightStyle.None;
  62. CanFocus = true;
  63. if (Border is { })
  64. {
  65. Border.Settings &= ~BorderSettings.Title;
  66. }
  67. Width = GetWidthDimAuto ();
  68. Height = Dim.Auto (DimAutoStyle.Content, 1);
  69. _orientationHelper = new (this);
  70. _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e);
  71. _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e);
  72. AddCommands ();
  73. TitleChanged += Shortcut_TitleChanged; // This needs to be set before CommandView is set
  74. CommandView = new ()
  75. {
  76. Id = "CommandView",
  77. Width = Dim.Auto (),
  78. Height = Dim.Fill ()
  79. };
  80. Title = commandText ?? string.Empty;
  81. HelpView.Id = "_helpView";
  82. HelpView.CanFocus = false;
  83. HelpView.Text = helpText ?? string.Empty;
  84. KeyView.Id = "_keyView";
  85. KeyView.CanFocus = false;
  86. key ??= Key.Empty;
  87. Key = key;
  88. Action = action;
  89. ShowHide ();
  90. }
  91. // Helper to set Width consistently
  92. internal Dim GetWidthDimAuto ()
  93. {
  94. return Dim.Auto (
  95. DimAutoStyle.Content,
  96. minimumContentDim: Dim.Func (() => _minimumNaturalWidth ?? 0),
  97. maximumContentDim: Dim.Func (() => _minimumNaturalWidth ?? 0))!;
  98. }
  99. private AlignmentModes _alignmentModes = AlignmentModes.StartToEnd | AlignmentModes.IgnoreFirstOrLast;
  100. // This is used to calculate the minimum width of the Shortcut when Width is NOT Dim.Auto
  101. // It is calculated by setting Width to DimAuto temporarily and forcing layout.
  102. // Once Frame.Width gets below this value, LayoutStarted makes HelpView an KeyView smaller.
  103. private int? _minimumNaturalWidth;
  104. /// <inheritdoc/>
  105. protected override bool OnHighlight (CancelEventArgs<HighlightStyle> args)
  106. {
  107. if (args.NewValue.HasFlag (HighlightStyle.Hover))
  108. {
  109. SetFocus ();
  110. return true;
  111. }
  112. return false;
  113. }
  114. /// <summary>
  115. /// Gets or sets the <see cref="AlignmentModes"/> for this <see cref="Shortcut"/>.
  116. /// </summary>
  117. /// <remarks>
  118. /// <para>
  119. /// The default is <see cref="AlignmentModes.StartToEnd"/>. This means that the CommandView will be on the left,
  120. /// HelpView in the middle, and KeyView on the right.
  121. /// </para>
  122. /// </remarks>
  123. public AlignmentModes AlignmentModes
  124. {
  125. get => _alignmentModes;
  126. set
  127. {
  128. _alignmentModes = value;
  129. SetCommandViewDefaultLayout ();
  130. SetHelpViewDefaultLayout ();
  131. SetKeyViewDefaultLayout ();
  132. }
  133. }
  134. // When one of the subviews is "empty" we don't want to show it. So we
  135. // Use Add/Remove. We need to be careful to add them in the right order
  136. // so Pos.Align works correctly.
  137. internal void ShowHide ()
  138. {
  139. RemoveAll ();
  140. if (CommandView.Visible)
  141. {
  142. Add (CommandView);
  143. SetCommandViewDefaultLayout ();
  144. }
  145. if (HelpView.Visible && !string.IsNullOrEmpty (HelpView.Text))
  146. {
  147. Add (HelpView);
  148. SetHelpViewDefaultLayout ();
  149. }
  150. if (KeyView.Visible && (Key != Key.Empty || KeyView.Text != string.Empty))
  151. {
  152. Add (KeyView);
  153. SetKeyViewDefaultLayout ();
  154. }
  155. // BUGBUG: Causes ever other layout to lose focus colors
  156. //SetColors ();
  157. }
  158. // Force Width to DimAuto to calculate natural width and then set it back
  159. private void ForceCalculateNaturalWidth ()
  160. {
  161. // Get the natural size of each subview
  162. CommandView.SetRelativeLayout (Application.Screen.Size);
  163. HelpView.SetRelativeLayout (Application.Screen.Size);
  164. KeyView.SetRelativeLayout (Application.Screen.Size);
  165. _minimumNaturalWidth = PosAlign.CalculateMinDimension (0, SubViews, Dimension.Width);
  166. // Reset our relative layout
  167. SetRelativeLayout (SuperView?.GetContentSize () ?? Application.Screen.Size);
  168. }
  169. // TODO: Enable setting of the margin thickness
  170. private Thickness GetMarginThickness ()
  171. {
  172. return new (1, 0, 1, 0);
  173. }
  174. // When layout starts, we need to adjust the layout of the HelpView and KeyView
  175. /// <inheritdoc />
  176. protected override void OnSubViewLayout (LayoutEventArgs e)
  177. {
  178. base.OnSubViewLayout (e);
  179. ShowHide ();
  180. ForceCalculateNaturalWidth ();
  181. if (Width is DimAuto widthAuto || HelpView!.Margin is null)
  182. {
  183. return;
  184. }
  185. // Frame.Width is smaller than the natural width. Reduce width of HelpView.
  186. _maxHelpWidth = int.Max (0, GetContentSize ().Width - CommandView.Frame.Width - KeyView.Frame.Width);
  187. if (_maxHelpWidth < 3)
  188. {
  189. Thickness t = GetMarginThickness ();
  190. switch (_maxHelpWidth)
  191. {
  192. case 0:
  193. case 1:
  194. // Scrunch it by removing both margins
  195. HelpView.Margin.Thickness = new (t.Right - 1, t.Top, t.Left - 1, t.Bottom);
  196. break;
  197. case 2:
  198. // Scrunch just the right margin
  199. HelpView.Margin.Thickness = new (t.Right, t.Top, t.Left - 1, t.Bottom);
  200. break;
  201. }
  202. }
  203. else
  204. {
  205. // Reset to default
  206. HelpView.Margin.Thickness = GetMarginThickness ();
  207. }
  208. }
  209. #region Accept/Select/HotKey Command Handling
  210. private void AddCommands ()
  211. {
  212. // Accept (Enter key) -
  213. AddCommand (Command.Accept, DispatchCommand);
  214. // Hotkey -
  215. AddCommand (Command.HotKey, DispatchCommand);
  216. // Select (Space key or click) -
  217. AddCommand (Command.Select, DispatchCommand);
  218. }
  219. /// <summary>
  220. /// Called when a Command has been invoked on this Shortcut.
  221. /// </summary>
  222. /// <param name="commandContext"></param>
  223. /// <returns></returns>
  224. internal virtual bool? DispatchCommand (ICommandContext? commandContext)
  225. {
  226. Logging.Trace($"{commandContext?.Source?.Title}");
  227. CommandContext<KeyBinding>? keyCommandContext = commandContext as CommandContext<KeyBinding>? ?? default (CommandContext<KeyBinding>);
  228. if (keyCommandContext?.Binding.Data != this)
  229. {
  230. // Invoke Select on the CommandView to cause it to change state if it wants to
  231. // If this causes CommandView to raise Accept, we eat it
  232. keyCommandContext = keyCommandContext!.Value with { Binding = keyCommandContext.Value.Binding with { Data = this } };
  233. Logging.Trace ($"Invoking Select on CommandView.");
  234. CommandView.InvokeCommand (Command.Select, keyCommandContext);
  235. }
  236. // BUGBUG: Why does this use keyCommandContext and not commandContext?
  237. Logging.Trace ($"RaiseSelecting ...");
  238. if (RaiseSelecting (keyCommandContext) is true)
  239. {
  240. return true;
  241. }
  242. // The default HotKey handler sets Focus
  243. Logging.Trace ($"SetFocus...");
  244. SetFocus ();
  245. var cancel = false;
  246. if (commandContext is { })
  247. {
  248. commandContext.Source = this;
  249. }
  250. Logging.Trace ($"RaiseAccepting...");
  251. cancel = RaiseAccepting (commandContext) is true;
  252. if (cancel)
  253. {
  254. return true;
  255. }
  256. if (commandContext?.Command != Command.Accept)
  257. {
  258. // return false;
  259. }
  260. if (Action is { })
  261. {
  262. Logging.Trace ($"Invoke Action...");
  263. Action.Invoke ();
  264. // Assume if there's a subscriber to Action, it's handled.
  265. cancel = true;
  266. }
  267. return cancel;
  268. }
  269. /// <summary>
  270. /// Gets or sets the action to be invoked when the shortcut key is pressed or the shortcut is clicked on with the
  271. /// mouse.
  272. /// </summary>
  273. /// <remarks>
  274. /// Note, the <see cref="View.Accepting"/> event is fired first, and if cancelled, the event will not be invoked.
  275. /// </remarks>
  276. public Action? Action { get; set; }
  277. #endregion Accept/Select/HotKey Command Handling
  278. #region IOrientation members
  279. private readonly OrientationHelper _orientationHelper;
  280. /// <summary>
  281. /// Gets or sets the <see cref="Orientation"/> for this <see cref="Bar"/>. The default is
  282. /// <see cref="Orientation.Horizontal"/>.
  283. /// </summary>
  284. /// <remarks>
  285. /// </remarks>
  286. public Orientation Orientation
  287. {
  288. get => _orientationHelper.Orientation;
  289. set => _orientationHelper.Orientation = value;
  290. }
  291. /// <inheritdoc/>
  292. public event EventHandler<CancelEventArgs<Orientation>>? OrientationChanging;
  293. /// <inheritdoc/>
  294. public event EventHandler<EventArgs<Orientation>>? OrientationChanged;
  295. /// <summary>Called when <see cref="Orientation"/> has changed.</summary>
  296. /// <param name="newOrientation"></param>
  297. public void OnOrientationChanged (Orientation newOrientation)
  298. {
  299. // TODO: Determine what, if anything, is opinionated about the orientation.
  300. SetNeedsLayout ();
  301. }
  302. #endregion
  303. #region Command
  304. private View _commandView = new ();
  305. /// <summary>
  306. /// Gets or sets the View that displays the command text and hotkey.
  307. /// </summary>
  308. /// <exception cref="ArgumentNullException"></exception>
  309. /// <remarks>
  310. /// <para>
  311. /// By default, the <see cref="View.Title"/> of the <see cref="CommandView"/> is displayed as the Shortcut's
  312. /// command text.
  313. /// </para>
  314. /// <para>
  315. /// By default, the CommandView is a <see cref="View"/> with <see cref="View.CanFocus"/> set to
  316. /// <see langword="false"/>.
  317. /// </para>
  318. /// <para>
  319. /// Setting the <see cref="CommandView"/> will add it to the <see cref="Shortcut"/> and remove any existing
  320. /// <see cref="CommandView"/>.
  321. /// </para>
  322. /// </remarks>
  323. /// <example>
  324. /// <para>
  325. /// This example illustrates how to add a <see cref="Shortcut"/> to a <see cref="StatusBar"/> that toggles the
  326. /// <see cref="Application.Force16Colors"/> property.
  327. /// </para>
  328. /// <code>
  329. /// var force16ColorsShortcut = new Shortcut
  330. /// {
  331. /// Key = Key.F6,
  332. /// KeyBindingScope = KeyBindingScope.HotKey,
  333. /// CommandView = new CheckBox { Text = "Force 16 Colors" }
  334. /// };
  335. /// var cb = force16ColorsShortcut.CommandView as CheckBox;
  336. /// cb.Checked = Application.Force16Colors;
  337. ///
  338. /// cb.Toggled += (s, e) =>
  339. /// {
  340. /// var cb = s as CheckBox;
  341. /// Application.Force16Colors = cb!.Checked == true;
  342. /// Application.Refresh();
  343. /// };
  344. /// StatusBar.Add(force16ColorsShortcut);
  345. /// </code>
  346. /// </example>
  347. public View CommandView
  348. {
  349. get => _commandView;
  350. set
  351. {
  352. ArgumentNullException.ThrowIfNull (value);
  353. if (value == null)
  354. {
  355. throw new ArgumentNullException ();
  356. }
  357. // Clean up old
  358. _commandView.Selecting -= CommandViewOnSelecting;
  359. _commandView.Accepting -= CommandViewOnAccepted;
  360. Remove (_commandView);
  361. _commandView?.Dispose ();
  362. // Set new
  363. _commandView = value;
  364. _commandView.Id = "_commandView";
  365. // The default behavior is for CommandView to not get focus. I
  366. // If you want it to get focus, you need to set it.
  367. _commandView.CanFocus = false;
  368. _commandView.HotKeyChanged += (s, e) =>
  369. {
  370. if (e.NewKey != Key.Empty)
  371. {
  372. // Add it
  373. AddKeyBindingsForHotKey (e.OldKey, e.NewKey);
  374. }
  375. };
  376. _commandView.HotKeySpecifier = new ('_');
  377. Title = _commandView.Text;
  378. _commandView.Selecting += CommandViewOnSelecting;
  379. _commandView.Accepting += CommandViewOnAccepted;
  380. //ShowHide ();
  381. UpdateKeyBindings (Key.Empty);
  382. return;
  383. void CommandViewOnAccepted (object? sender, CommandEventArgs e)
  384. {
  385. // Always eat CommandView.Accept
  386. e.Cancel = true;
  387. }
  388. void CommandViewOnSelecting (object? sender, CommandEventArgs e)
  389. {
  390. if ((e.Context is CommandContext<KeyBinding> keyCommandContext && keyCommandContext.Binding.Data != this) ||
  391. e.Context is CommandContext<MouseBinding>)
  392. {
  393. // Forward command to ourselves
  394. InvokeCommand<KeyBinding> (Command.Select, new ([Command.Select], null, this));
  395. }
  396. e.Cancel = true;
  397. }
  398. }
  399. }
  400. private void SetCommandViewDefaultLayout ()
  401. {
  402. if (CommandView.Margin is { })
  403. {
  404. CommandView.Margin.Thickness = GetMarginThickness ();
  405. }
  406. CommandView.X = Pos.Align (Alignment.End, AlignmentModes);
  407. CommandView.VerticalTextAlignment = Alignment.Center;
  408. CommandView.TextAlignment = Alignment.Start;
  409. CommandView.TextFormatter.WordWrap = false;
  410. CommandView.HighlightStyle = HighlightStyle.None;
  411. }
  412. private void Shortcut_TitleChanged (object? sender, EventArgs<string> e)
  413. {
  414. // If the Title changes, update the CommandView text.
  415. // This is a helper to make it easier to set the CommandView text.
  416. // CommandView is public and replaceable, but this is a convenience.
  417. _commandView.Text = Title;
  418. //_commandView.Title = Title;
  419. }
  420. #endregion Command
  421. #region Help
  422. // The maximum width of the HelpView. Calculated in OnLayoutStarted and used in HelpView.Width (Dim.Auto/Func).
  423. private int _maxHelpWidth = 0;
  424. /// <summary>
  425. /// The subview that displays the help text for the command. Internal for unit testing.
  426. /// </summary>
  427. public View HelpView { get; } = new ();
  428. private void SetHelpViewDefaultLayout ()
  429. {
  430. if (HelpView.Margin is { })
  431. {
  432. HelpView.Margin.Thickness = GetMarginThickness ();
  433. }
  434. HelpView.X = Pos.Align (Alignment.End, AlignmentModes);
  435. _maxHelpWidth = HelpView.Text.GetColumns ();
  436. HelpView.Width = Dim.Auto (DimAutoStyle.Text, maximumContentDim: Dim.Func ((() => _maxHelpWidth)));
  437. HelpView.Height = Dim.Fill ();
  438. HelpView.Visible = true;
  439. HelpView.VerticalTextAlignment = Alignment.Center;
  440. HelpView.TextAlignment = Alignment.Start;
  441. HelpView.TextFormatter.WordWrap = false;
  442. HelpView.HighlightStyle = HighlightStyle.None;
  443. }
  444. /// <summary>
  445. /// Gets or sets the help text displayed in the middle of the Shortcut. Identical in function to <see cref="HelpText"/>
  446. /// .
  447. /// </summary>
  448. public override string Text
  449. {
  450. get => HelpView.Text;
  451. set
  452. {
  453. HelpView.Text = value;
  454. ShowHide ();
  455. }
  456. }
  457. /// <summary>
  458. /// Gets or sets the help text displayed in the middle of the Shortcut.
  459. /// </summary>
  460. public string HelpText
  461. {
  462. get => HelpView.Text;
  463. set
  464. {
  465. HelpView.Text = value;
  466. ShowHide ();
  467. }
  468. }
  469. #endregion Help
  470. #region Key
  471. private Key _key = Key.Empty;
  472. /// <summary>
  473. /// Gets or sets the <see cref="Key"/> that will be bound to the <see cref="Command.Accept"/> command.
  474. /// </summary>
  475. public Key Key
  476. {
  477. get => _key;
  478. set
  479. {
  480. ArgumentNullException.ThrowIfNull (value);
  481. Key oldKey = _key;
  482. _key = value;
  483. UpdateKeyBindings (oldKey);
  484. KeyView.Text = Key == Key.Empty ? string.Empty : $"{Key}";
  485. ShowHide ();
  486. }
  487. }
  488. private bool _bindKeyToApplication = false;
  489. /// <summary>
  490. /// Gets or sets whether <see cref="Key"/> is bound to <see cref="Command"/> via <see cref="View.HotKeyBindings"/> or <see cref="Application.KeyBindings"/>.
  491. /// </summary>
  492. public bool BindKeyToApplication
  493. {
  494. get => _bindKeyToApplication;
  495. set
  496. {
  497. if (value == _bindKeyToApplication)
  498. {
  499. return;
  500. }
  501. if (_bindKeyToApplication)
  502. {
  503. Application.KeyBindings.Remove (Key);
  504. }
  505. else
  506. {
  507. HotKeyBindings.Remove (Key);
  508. }
  509. _bindKeyToApplication = value;
  510. UpdateKeyBindings (Key.Empty);
  511. }
  512. }
  513. /// <summary>
  514. /// Gets the subview that displays the key. Internal for unit testing.
  515. /// </summary>
  516. public View KeyView { get; } = new ();
  517. private int _minimumKeyTextSize;
  518. /// <summary>
  519. /// Gets or sets the minimum size of the key text. Useful for aligning the key text with other <see cref="Shortcut"/>s.
  520. /// </summary>
  521. public int MinimumKeyTextSize
  522. {
  523. get => _minimumKeyTextSize;
  524. set
  525. {
  526. if (value == _minimumKeyTextSize)
  527. {
  528. //return;
  529. }
  530. _minimumKeyTextSize = value;
  531. SetKeyViewDefaultLayout ();
  532. }
  533. }
  534. private void SetKeyViewDefaultLayout ()
  535. {
  536. if (KeyView.Margin is { })
  537. {
  538. KeyView.Margin.Thickness = GetMarginThickness ();
  539. }
  540. KeyView.X = Pos.Align (Alignment.End, AlignmentModes);
  541. KeyView.Width = Dim.Auto (DimAutoStyle.Text, minimumContentDim: Dim.Func (() => MinimumKeyTextSize));
  542. KeyView.Height = Dim.Fill ();
  543. KeyView.Visible = true;
  544. // Right align the text in the keyview
  545. KeyView.TextAlignment = Alignment.End;
  546. KeyView.VerticalTextAlignment = Alignment.Center;
  547. KeyView.KeyBindings.Clear ();
  548. HelpView.HighlightStyle = HighlightStyle.None;
  549. }
  550. private void UpdateKeyBindings (Key oldKey)
  551. {
  552. if (!Key.IsValid)
  553. {
  554. return;
  555. }
  556. if (BindKeyToApplication)
  557. {
  558. if (oldKey != Key.Empty)
  559. {
  560. Application.KeyBindings.Remove (oldKey);
  561. }
  562. Application.KeyBindings.Remove (Key);
  563. Application.KeyBindings.Add (Key, this, Command.HotKey);
  564. }
  565. else
  566. {
  567. if (oldKey != Key.Empty)
  568. {
  569. HotKeyBindings.Remove (oldKey);
  570. }
  571. HotKeyBindings.Remove (Key);
  572. HotKeyBindings.Add (Key, Command.HotKey);
  573. }
  574. }
  575. #endregion Key
  576. #region Focus
  577. /// <inheritdoc/>
  578. public override ColorScheme? ColorScheme
  579. {
  580. get => base.ColorScheme;
  581. set
  582. {
  583. base.ColorScheme = _nonFocusColorScheme = value;
  584. SetColors ();
  585. }
  586. }
  587. private bool _forceFocusColors;
  588. /// <summary>
  589. /// TODO: IS this needed?
  590. /// </summary>
  591. public bool ForceFocusColors
  592. {
  593. get => _forceFocusColors;
  594. set
  595. {
  596. _forceFocusColors = value;
  597. SetColors (value);
  598. //SetNeedsDraw();
  599. }
  600. }
  601. private ColorScheme? _nonFocusColorScheme;
  602. /// <summary>
  603. /// </summary>
  604. internal void SetColors (bool highlight = false)
  605. {
  606. if (HasFocus || highlight || ForceFocusColors)
  607. {
  608. if (_nonFocusColorScheme is null)
  609. {
  610. _nonFocusColorScheme = base.ColorScheme;
  611. }
  612. base.ColorScheme ??= new (Attribute.Default);
  613. // When we have focus, we invert the colors
  614. base.ColorScheme = new (base.ColorScheme)
  615. {
  616. Normal = GetFocusColor (),
  617. HotNormal = GetHotFocusColor (),
  618. HotFocus = GetHotNormalColor (),
  619. Focus = GetNormalColor (),
  620. };
  621. }
  622. else
  623. {
  624. if (_nonFocusColorScheme is { })
  625. {
  626. base.ColorScheme = _nonFocusColorScheme;
  627. //_nonFocusColorScheme = null;
  628. }
  629. else
  630. {
  631. base.ColorScheme = SuperView?.ColorScheme ?? base.ColorScheme;
  632. }
  633. }
  634. // Set KeyView's colors to show "hot"
  635. if (IsInitialized && base.ColorScheme is { })
  636. {
  637. var cs = new ColorScheme (base.ColorScheme)
  638. {
  639. Normal = GetHotNormalColor (),
  640. HotNormal = GetNormalColor ()
  641. };
  642. KeyView.ColorScheme = cs;
  643. }
  644. if (CommandView.Margin is { })
  645. {
  646. CommandView.Margin.ColorScheme = base.ColorScheme;
  647. }
  648. if (HelpView.Margin is { })
  649. {
  650. HelpView.Margin.ColorScheme = base.ColorScheme;
  651. }
  652. if (KeyView.Margin is { })
  653. {
  654. KeyView.Margin.ColorScheme = base.ColorScheme;
  655. }
  656. }
  657. /// <inheritdoc/>
  658. protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? view)
  659. {
  660. SetColors ();
  661. }
  662. #endregion Focus
  663. /// <inheritdoc/>
  664. public bool EnableForDesign ()
  665. {
  666. Title = "_Shortcut";
  667. HelpText = "Shortcut help";
  668. Key = Key.F1;
  669. return true;
  670. }
  671. /// <inheritdoc/>
  672. protected override void Dispose (bool disposing)
  673. {
  674. if (disposing)
  675. {
  676. TitleChanged -= Shortcut_TitleChanged;
  677. if (CommandView.SuperView is null)
  678. {
  679. CommandView.Dispose ();
  680. }
  681. if (HelpView.SuperView is null)
  682. {
  683. HelpView.Dispose ();
  684. }
  685. if (KeyView.SuperView is null)
  686. {
  687. KeyView.Dispose ();
  688. }
  689. }
  690. base.Dispose (disposing);
  691. }
  692. }