EventLog.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #nullable enable
  2. using System;
  3. using System.Collections.ObjectModel;
  4. using System.Diagnostics.Tracing;
  5. using System.Text;
  6. using Terminal.Gui;
  7. namespace UICatalog.Scenarios;
  8. /// <summary>
  9. /// An event log that automatically shows the events that are raised.
  10. /// </summary>
  11. /// <remarks>
  12. /// </remarks>
  13. /// </example>
  14. public class EventLog : ListView
  15. {
  16. public EventLog ()
  17. {
  18. Title = "Event Log";
  19. CanFocus = false;
  20. X = Pos.AnchorEnd ();
  21. Y = 0;
  22. Width = Dim.Func (() => Math.Min (SuperView!.Viewport.Width / 3, MaxLength + GetAdornmentsThickness ().Horizontal));
  23. Height = Dim.Fill ();
  24. ExpandButton = new ()
  25. {
  26. Orientation = Orientation.Horizontal
  27. };
  28. Initialized += EventLog_Initialized;
  29. BorderStyle = LineStyle.Dotted;
  30. }
  31. public ExpanderButton? ExpandButton { get; }
  32. private readonly ObservableCollection<string> _eventSource = [];
  33. private View? _viewToLog;
  34. public View? ViewToLog
  35. {
  36. get => _viewToLog;
  37. set
  38. {
  39. if (_viewToLog == value)
  40. {
  41. return;
  42. }
  43. _viewToLog = value;
  44. if (_viewToLog is { })
  45. {
  46. _viewToLog.Initialized += (s, args) =>
  47. {
  48. View? sender = s as View;
  49. _eventSource.Add ($"Initialized: {GetIdentifyingString (sender)}");
  50. MoveEnd ();
  51. };
  52. _viewToLog.MouseClick += (s, args) =>
  53. {
  54. View? sender = s as View;
  55. _eventSource.Add ($"MouseClick: {args}");
  56. MoveEnd ();
  57. };
  58. _viewToLog.HandlingHotKey += (s, args) =>
  59. {
  60. View? sender = s as View;
  61. _eventSource.Add ($"HandlingHotKey: {args.Context.Command} {args.Context.Data}");
  62. MoveEnd ();
  63. };
  64. _viewToLog.Selecting += (s, args) =>
  65. {
  66. View? sender = s as View;
  67. _eventSource.Add ($"Selecting: {args.Context.Command} {args.Context.Data}");
  68. MoveEnd ();
  69. };
  70. _viewToLog.Accepting += (s, args) =>
  71. {
  72. View? sender = s as View;
  73. _eventSource.Add ($"Accepting: {args.Context.Command} {args.Context.Data}");
  74. MoveEnd ();
  75. };
  76. }
  77. }
  78. }
  79. private void EventLog_Initialized (object? _, EventArgs e)
  80. {
  81. Border.Add (ExpandButton!);
  82. Source = new ListWrapper<string> (_eventSource);
  83. }
  84. private string GetIdentifyingString (View? view)
  85. {
  86. if (view is null)
  87. {
  88. return "null";
  89. }
  90. if (!string.IsNullOrEmpty (view.Title))
  91. {
  92. return view.Title;
  93. }
  94. if (!string.IsNullOrEmpty (view.Text))
  95. {
  96. return view.Text;
  97. }
  98. return view.GetType ().Name;
  99. }
  100. }