EventLog.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. }
  30. public ExpanderButton? ExpandButton { get; }
  31. private readonly ObservableCollection<string> _eventSource = [];
  32. private View? _viewToLog;
  33. public View? ViewToLog
  34. {
  35. get => _viewToLog;
  36. set
  37. {
  38. if (_viewToLog == value)
  39. {
  40. return;
  41. }
  42. _viewToLog = value;
  43. if (_viewToLog is { })
  44. {
  45. _viewToLog.Initialized += (s, args) =>
  46. {
  47. View? sender = s as View;
  48. _eventSource.Add ($"Initialized: {GetIdentifyingString (sender)}");
  49. MoveEnd ();
  50. };
  51. _viewToLog.MouseClick += (s, args) =>
  52. {
  53. View? sender = s as View;
  54. _eventSource.Add ($"MouseClick: {args}");
  55. MoveEnd ();
  56. };
  57. _viewToLog.HandlingHotKey += (s, args) =>
  58. {
  59. View? sender = s as View;
  60. _eventSource.Add ($"HandlingHotKey: {args.Context.Command} {args.Context.Data}");
  61. MoveEnd ();
  62. };
  63. _viewToLog.Selecting += (s, args) =>
  64. {
  65. View? sender = s as View;
  66. _eventSource.Add ($"Selecting: {args.Context.Command} {args.Context.Data}");
  67. MoveEnd ();
  68. };
  69. _viewToLog.Accepting += (s, args) =>
  70. {
  71. View? sender = s as View;
  72. _eventSource.Add ($"Accepting: {args.Context.Command} {args.Context.Data}");
  73. MoveEnd ();
  74. };
  75. }
  76. }
  77. }
  78. private void EventLog_Initialized (object? _, EventArgs e)
  79. {
  80. Border?.Add (ExpandButton!);
  81. Source = new ListWrapper<string> (_eventSource);
  82. }
  83. private string GetIdentifyingString (View? view)
  84. {
  85. if (view is null)
  86. {
  87. return "null";
  88. }
  89. if (!string.IsNullOrEmpty (view.Title))
  90. {
  91. return view.Title;
  92. }
  93. if (!string.IsNullOrEmpty (view.Text))
  94. {
  95. return view.Text;
  96. }
  97. return view.GetType ().Name;
  98. }
  99. }