ThemeViewer.cs 4.2 KB

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