Mouse.cs 7.8 KB

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