Shortcut.cs 25 KB

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