Mouse.cs 7.7 KB

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