Mouse.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections.Generic;
  3. using System.CommandLine;
  4. using System.Linq;
  5. using Terminal.Gui;
  6. namespace UICatalog.Scenarios;
  7. [ScenarioMetadata ("Mouse", "Demonstrates how to capture mouse events")]
  8. [ScenarioCategory ("Mouse and Keyboard")]
  9. public class Mouse : Scenario
  10. {
  11. public override void Main ()
  12. {
  13. Application.Init ();
  14. Window win = new ()
  15. {
  16. Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
  17. };
  18. var filterSlider = new Slider<MouseFlags> ()
  19. {
  20. Title = "_Filter",
  21. X = 0,
  22. Y = 0,
  23. AutoSize = true,
  24. BorderStyle = LineStyle.Single,
  25. Type = SliderType.Multiple,
  26. Orientation = Orientation.Vertical,
  27. };
  28. filterSlider.Options = Enum.GetValues (typeof (MouseFlags))
  29. .Cast<MouseFlags> ()
  30. .Where (value => !value.ToString ().Contains ("None") &&
  31. !value.ToString().Contains("All"))
  32. .Select (value => new SliderOption<MouseFlags>
  33. {
  34. Legend = value.ToString (),
  35. Data = value,
  36. })
  37. .ToList ();
  38. for (int i = 0; i < filterSlider.Options.Count; i++)
  39. {
  40. filterSlider.SetOption (i);
  41. }
  42. win.Add (filterSlider);
  43. var clearButton = new Button ()
  44. {
  45. Title = "_Clear Logs",
  46. X = 1,
  47. Y = Pos.Bottom (filterSlider) + 1,
  48. };
  49. win.Add (clearButton);
  50. Label ml;
  51. var count = 0;
  52. ml = new Label { X = Pos.Right(filterSlider), Y = 0, Text = "Mouse: " };
  53. win.Add (ml);
  54. CheckBox cbWantContinuousPresses = new ()
  55. {
  56. X = Pos.Right (filterSlider),
  57. Y = Pos.Bottom (ml),
  58. Title = "_Want Continuous Button Pressed",
  59. };
  60. cbWantContinuousPresses.Toggled += (s, e) =>
  61. {
  62. win.WantContinuousButtonPressed = !win.WantContinuousButtonPressed;
  63. };
  64. win.Add (cbWantContinuousPresses);
  65. CheckBox cbHighlightOnPress = new ()
  66. {
  67. X = Pos.Right (filterSlider),
  68. Y = Pos.Bottom (cbWantContinuousPresses),
  69. Title = "_Highlight on Press",
  70. };
  71. cbHighlightOnPress.Toggled += (s, e) =>
  72. {
  73. win.HighlightOnPress = !win.HighlightOnPress;
  74. };
  75. win.Add (cbHighlightOnPress);
  76. var demo = new MouseDemo ()
  77. {
  78. X = Pos.Right (filterSlider),
  79. Y = Pos.Bottom (cbHighlightOnPress),
  80. Width = 20,
  81. Height = 3,
  82. Text = "Enter/Leave Demo",
  83. TextAlignment = TextAlignment.Centered,
  84. VerticalTextAlignment = VerticalTextAlignment.Middle,
  85. ColorScheme = Colors.ColorSchemes ["Dialog"],
  86. };
  87. win.Add (demo);
  88. var label = new Label ()
  89. {
  90. Text = "_App Events:",
  91. X = Pos.Right (filterSlider),
  92. Y = Pos.Bottom (demo),
  93. };
  94. List<string> appLogList = new ();
  95. var appLog = new ListView
  96. {
  97. X = Pos.Left (label),
  98. Y = Pos.Bottom (label),
  99. Width = 50,
  100. Height = Dim.Fill (),
  101. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  102. Source = new ListWrapper (appLogList)
  103. };
  104. win.Add (label, appLog);
  105. Application.MouseEvent += (sender, a) =>
  106. {
  107. var i = filterSlider.Options.FindIndex (o => o.Data == a.Flags);
  108. if (filterSlider.GetSetOptions().Contains(i))
  109. {
  110. ml.Text = $"MouseEvent: ({a.X},{a.Y}) - {a.Flags} {count}";
  111. appLogList.Add ($"({a.X},{a.Y}) - {a.Flags} {count++}");
  112. appLog.MoveDown ();
  113. }
  114. };
  115. label = new Label ()
  116. {
  117. Text = "_Window Events:",
  118. X = Pos.Right (appLog)+1,
  119. Y = Pos.Top (label),
  120. };
  121. List<string> winLogList = new ();
  122. var winLog = new ListView
  123. {
  124. X = Pos.Left (label),
  125. Y = Pos.Bottom (label),
  126. Width = Dim.Percent (50),
  127. Height = Dim.Fill (),
  128. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  129. Source = new ListWrapper (winLogList)
  130. };
  131. win.Add (label, winLog);
  132. clearButton.Accept += (s, e) =>
  133. {
  134. appLogList.Clear ();
  135. appLog.SetSource (appLogList);
  136. winLogList.Clear ();
  137. winLog.SetSource(winLogList);
  138. };
  139. win.MouseEvent += (sender, a) =>
  140. {
  141. var i = filterSlider.Options.FindIndex (o => o.Data == a.MouseEvent.Flags);
  142. if (filterSlider.GetSetOptions ().Contains (i))
  143. {
  144. winLogList.Add ($"MouseEvent: ({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags} {count++}");
  145. winLog.MoveDown ();
  146. }
  147. };
  148. win.MouseClick += (sender, a) =>
  149. {
  150. winLogList.Add ($"MouseClick: ({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags} {count++}");
  151. winLog.MoveDown ();
  152. };
  153. Application.Run (win);
  154. win.Dispose ();
  155. }
  156. public class MouseDemo : View
  157. {
  158. private bool _button1PressedOnEnter = false;
  159. public MouseDemo ()
  160. {
  161. CanFocus = true;
  162. MouseEvent += (s, e) =>
  163. {
  164. if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
  165. {
  166. if (!_button1PressedOnEnter)
  167. {
  168. ColorScheme = Colors.ColorSchemes ["Toplevel"];
  169. }
  170. }
  171. if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Released))
  172. {
  173. ColorScheme = Colors.ColorSchemes ["Dialog"];
  174. _button1PressedOnEnter = false;
  175. }
  176. };
  177. MouseLeave += (s, e) =>
  178. {
  179. ColorScheme = Colors.ColorSchemes ["Dialog"];
  180. _button1PressedOnEnter = false;
  181. };
  182. MouseEnter += (s, e) =>
  183. {
  184. _button1PressedOnEnter = e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed);
  185. };
  186. }
  187. }
  188. }