ThemeViewer.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #nullable enable
  2. using Terminal.Gui;
  3. namespace UICatalog.Scenarios;
  4. public class ThemeViewer : FrameView
  5. {
  6. public ThemeViewer ()
  7. {
  8. BorderStyle = LineStyle.Rounded;
  9. Border!.Thickness = new (0, 1, 0, 0);
  10. Margin!.Thickness = new (0, 0, 1, 0);
  11. TabStop = TabBehavior.TabStop;
  12. CanFocus = true;
  13. Height = Dim.Fill ();
  14. Width = Dim.Auto ();
  15. Title = $"{ThemeManager.Theme}";
  16. VerticalScrollBar.AutoShow = true;
  17. HorizontalScrollBar.AutoShow = true;
  18. SubViewsLaidOut += (sender, _) =>
  19. {
  20. if (sender is View sendingView)
  21. {
  22. sendingView.SetContentSize (new Size (sendingView.GetContentSize ().Width, sendingView.GetHeightRequiredForSubViews ()));
  23. }
  24. };
  25. AddCommand (Command.Up, () => ScrollVertical (-1));
  26. AddCommand (Command.Down, () => ScrollVertical (1));
  27. AddCommand (Command.PageUp, () => ScrollVertical (-SubViews.OfType<SchemeViewer> ().First ().Frame.Height));
  28. AddCommand (Command.PageDown, () => ScrollVertical (SubViews.OfType<SchemeViewer> ().First ().Frame.Height));
  29. AddCommand (
  30. Command.Start,
  31. () =>
  32. {
  33. Viewport = Viewport with { Y = 0 };
  34. return true;
  35. });
  36. AddCommand (
  37. Command.End,
  38. () =>
  39. {
  40. Viewport = Viewport with { Y = GetContentSize ().Height };
  41. return true;
  42. });
  43. AddCommand (Command.ScrollDown, () => ScrollVertical (1));
  44. AddCommand (Command.ScrollUp, () => ScrollVertical (-1));
  45. AddCommand (Command.ScrollRight, () => ScrollHorizontal (1));
  46. AddCommand (Command.ScrollLeft, () => ScrollHorizontal (-1));
  47. KeyBindings.Add (Key.CursorUp, Command.Up);
  48. KeyBindings.Add (Key.CursorDown, Command.Down);
  49. KeyBindings.Add (Key.CursorLeft, Command.Left);
  50. KeyBindings.Add (Key.CursorRight, Command.Right);
  51. KeyBindings.Add (Key.PageUp, Command.PageUp);
  52. KeyBindings.Add (Key.PageDown, Command.PageDown);
  53. KeyBindings.Add (Key.Home, Command.Start);
  54. KeyBindings.Add (Key.End, Command.End);
  55. KeyBindings.Add (PopoverMenu.DefaultKey, Command.Context);
  56. MouseBindings.Add (MouseFlags.Button1DoubleClicked, Command.Accept);
  57. MouseBindings.ReplaceCommands (MouseFlags.Button3Clicked, Command.Context);
  58. MouseBindings.ReplaceCommands (MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl, Command.Context);
  59. MouseBindings.Add (MouseFlags.WheeledDown, Command.ScrollDown);
  60. MouseBindings.Add (MouseFlags.WheeledUp, Command.ScrollUp);
  61. MouseBindings.Add (MouseFlags.WheeledLeft, Command.ScrollLeft);
  62. MouseBindings.Add (MouseFlags.WheeledRight, Command.ScrollRight);
  63. SchemeViewer? prevSchemeViewer = null;
  64. foreach (KeyValuePair<string, Scheme?> kvp in SchemeManager.GetSchemesForCurrentTheme ())
  65. {
  66. var schemeViewer = new SchemeViewer
  67. {
  68. Id = $"schemeViewer for {kvp.Key}",
  69. SchemeName = kvp.Key
  70. };
  71. if (prevSchemeViewer is { })
  72. {
  73. schemeViewer.Y = Pos.Bottom (prevSchemeViewer);
  74. }
  75. prevSchemeViewer = schemeViewer;
  76. base.Add (schemeViewer);
  77. }
  78. ThemeManager.ThemeChanged += OnThemeManagerOnThemeChanged;
  79. }
  80. /// <inheritdoc/>
  81. protected override void OnFocusedChanged (View? previousFocused, View? focused)
  82. {
  83. base.OnFocusedChanged (previousFocused, focused);
  84. if (focused is { })
  85. {
  86. SchemeName = focused.Title;
  87. }
  88. }
  89. private void OnThemeManagerOnThemeChanged (object? _, StringPropertyEventArgs args) { Title = args.NewString!; }
  90. protected override void Dispose (bool disposing)
  91. {
  92. if (disposing)
  93. {
  94. ThemeManager.ThemeChanged -= OnThemeManagerOnThemeChanged;
  95. }
  96. base.Dispose (disposing);
  97. }
  98. }