Mouse.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  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 = GetQuitKeyAndName ()
  17. };
  18. Slider<MouseFlags> filterSlider = new ()
  19. {
  20. Title = "_Filter",
  21. X = 0,
  22. Y = 0,
  23. BorderStyle = LineStyle.Single,
  24. Type = SliderType.Multiple,
  25. Orientation = Orientation.Vertical,
  26. UseMinimumSize = true,
  27. MinimumInnerSpacing = 0
  28. };
  29. filterSlider.Options = Enum.GetValues (typeof (MouseFlags))
  30. .Cast<MouseFlags> ()
  31. .Where (value => !value.ToString ().Contains ("None") && !value.ToString ().Contains ("All"))
  32. .Select (
  33. value => new SliderOption<MouseFlags>
  34. {
  35. Legend = value.ToString (),
  36. Data = value
  37. })
  38. .ToList ();
  39. for (var i = 0; i < filterSlider.Options.Count; i++)
  40. {
  41. if (filterSlider.Options [i].Data != MouseFlags.ReportMousePosition)
  42. {
  43. filterSlider.SetOption (i);
  44. }
  45. }
  46. win.Add (filterSlider);
  47. var clearButton = new Button
  48. {
  49. Title = "_Clear Logs",
  50. X = 1,
  51. Y = Pos.Bottom (filterSlider) + 1
  52. };
  53. win.Add (clearButton);
  54. Label ml;
  55. var count = 0;
  56. ml = new () { X = Pos.Right (filterSlider), Y = 0, Text = "Mouse: " };
  57. win.Add (ml);
  58. CheckBox cbWantContinuousPresses = new ()
  59. {
  60. X = Pos.Right (filterSlider),
  61. Y = Pos.Bottom (ml),
  62. Title = "_Want Continuous Button Pressed"
  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. win.Add (cbHighlightOnPress);
  72. var demo = new MouseEventDemoView
  73. {
  74. X = Pos.Right (filterSlider),
  75. Y = Pos.Bottom (cbHighlightOnPress),
  76. Width = Dim.Fill (),
  77. Height = 15,
  78. Title = "Enter/Leave Demo",
  79. };
  80. demo.Padding.Initialized += DemoPaddingOnInitialized;
  81. void DemoPaddingOnInitialized (object o, EventArgs eventArgs)
  82. {
  83. demo.Padding.Add (
  84. new MouseEventDemoView ()
  85. {
  86. X = 0,
  87. Y = 0,
  88. Width = Dim.Fill (),
  89. Height = Dim.Func (() => demo.Padding.Thickness.Top),
  90. Title = "inPadding",
  91. Id = "inPadding"
  92. });
  93. demo.Padding.Thickness = demo.Padding.Thickness with { Top = 5 };
  94. }
  95. View sub1 = new MouseEventDemoView ()
  96. {
  97. X = 0,
  98. Y = 0,
  99. Width = Dim.Percent (20),
  100. Height = Dim.Fill (),
  101. Title = "sub1",
  102. Id = "sub1",
  103. };
  104. demo.Add (sub1);
  105. demo.Add (
  106. new MouseEventDemoView ()
  107. {
  108. X = Pos.Right (sub1) - 4,
  109. Y = Pos.Top (sub1) + 1,
  110. Width = Dim.Percent (20),
  111. Height = Dim.Fill (1),
  112. Title = "sub2",
  113. Id = "sub2",
  114. });
  115. win.Add (demo);
  116. cbHighlightOnPress.CheckedState = demo.HighlightStyle == (HighlightStyle.Pressed | HighlightStyle.PressedOutside) ? CheckState.Checked : CheckState.UnChecked;
  117. // BUGBUG: See https://github.com/gui-cs/Terminal.Gui/issues/3753
  118. cbHighlightOnPress.CheckedStateChanging += (s, e) =>
  119. {
  120. if (e.NewValue == CheckState.Checked)
  121. {
  122. demo.HighlightStyle = HighlightStyle.Pressed | HighlightStyle.PressedOutside;
  123. }
  124. else
  125. {
  126. demo.HighlightStyle = HighlightStyle.None;
  127. }
  128. foreach (View subview in demo.Subviews)
  129. {
  130. if (e.NewValue == CheckState.Checked)
  131. {
  132. subview.HighlightStyle = HighlightStyle.Pressed | HighlightStyle.PressedOutside;
  133. }
  134. else
  135. {
  136. subview.HighlightStyle = HighlightStyle.None;
  137. }
  138. }
  139. foreach (View subview in demo.Padding.Subviews)
  140. {
  141. if (e.NewValue == CheckState.Checked)
  142. {
  143. subview.HighlightStyle = HighlightStyle.Pressed | HighlightStyle.PressedOutside;
  144. }
  145. else
  146. {
  147. subview.HighlightStyle = HighlightStyle.None;
  148. }
  149. }
  150. };
  151. cbWantContinuousPresses.CheckedStateChanging += (s, e) =>
  152. {
  153. demo.WantContinuousButtonPressed = !demo.WantContinuousButtonPressed;
  154. foreach (View subview in demo.Subviews)
  155. {
  156. subview.WantContinuousButtonPressed = demo.WantContinuousButtonPressed;
  157. }
  158. foreach (View subview in demo.Padding.Subviews)
  159. {
  160. subview.WantContinuousButtonPressed = demo.WantContinuousButtonPressed;
  161. }
  162. };
  163. var label = new Label
  164. {
  165. Text = "_App Events:",
  166. X = Pos.Right (filterSlider),
  167. Y = Pos.Bottom (demo)
  168. };
  169. ObservableCollection<string> appLogList = new ();
  170. var appLog = new ListView
  171. {
  172. X = Pos.Left (label),
  173. Y = Pos.Bottom (label),
  174. Width = 50,
  175. Height = Dim.Fill (),
  176. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  177. Source = new ListWrapper<string> (appLogList)
  178. };
  179. win.Add (label, appLog);
  180. Application.MouseEvent += (sender, a) =>
  181. {
  182. int i = filterSlider.Options.FindIndex (o => o.Data == a.Flags);
  183. if (filterSlider.GetSetOptions ().Contains (i))
  184. {
  185. ml.Text = $"MouseEvent: ({a.Position}) - {a.Flags} {count}";
  186. appLogList.Add ($"({a.Position}) - {a.Flags} {count++}");
  187. appLog.MoveDown ();
  188. }
  189. };
  190. label = new ()
  191. {
  192. Text = "_Window Events:",
  193. X = Pos.Right (appLog) + 1,
  194. Y = Pos.Top (label)
  195. };
  196. ObservableCollection<string> winLogList = new ();
  197. var winLog = new ListView
  198. {
  199. X = Pos.Left (label),
  200. Y = Pos.Bottom (label),
  201. Width = Dim.Percent (50),
  202. Height = Dim.Fill (),
  203. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  204. Source = new ListWrapper<string> (winLogList)
  205. };
  206. win.Add (label, winLog);
  207. clearButton.Accepting += (s, e) =>
  208. {
  209. appLogList.Clear ();
  210. appLog.SetSource (appLogList);
  211. winLogList.Clear ();
  212. winLog.SetSource (winLogList);
  213. };
  214. win.MouseEvent += (sender, a) =>
  215. {
  216. int i = filterSlider.Options.FindIndex (o => o.Data == a.Flags);
  217. if (filterSlider.GetSetOptions ().Contains (i))
  218. {
  219. winLogList.Add ($"MouseEvent: ({a.Position}) - {a.Flags} {count++}");
  220. winLog.MoveDown ();
  221. }
  222. };
  223. win.MouseClick += (sender, a) =>
  224. {
  225. winLogList.Add ($"MouseClick: ({a.Position}) - {a.Flags} {count++}");
  226. winLog.MoveDown ();
  227. };
  228. Application.Run (win);
  229. win.Dispose ();
  230. Application.Shutdown ();
  231. }
  232. public class MouseEventDemoView : View
  233. {
  234. public MouseEventDemoView ()
  235. {
  236. CanFocus = true;
  237. Id = "mouseEventDemoView";
  238. Padding.Thickness = new Thickness (1, 1, 1, 1);
  239. Initialized += OnInitialized;
  240. void OnInitialized (object sender, EventArgs e)
  241. {
  242. TextAlignment = Alignment.Center;
  243. VerticalTextAlignment = Alignment.Center;
  244. Padding.ColorScheme = new ColorScheme (new Attribute (Color.Black));
  245. Padding.MouseEnter += PaddingOnMouseEnter;
  246. Padding.MouseLeave += PaddingOnMouseLeave;
  247. void PaddingOnMouseEnter (object o, CancelEventArgs e)
  248. {
  249. Padding.ColorScheme = Colors.ColorSchemes ["Error"];
  250. }
  251. void PaddingOnMouseLeave (object o, EventArgs e)
  252. {
  253. Padding.ColorScheme = Colors.ColorSchemes ["Dialog"];
  254. }
  255. Border.Thickness = new Thickness (1);
  256. Border.LineStyle = LineStyle.Rounded;
  257. }
  258. MouseLeave += (s, e) =>
  259. {
  260. Text = "Leave";
  261. };
  262. MouseEnter += (s, e) =>
  263. {
  264. Text = "Enter";
  265. };
  266. }
  267. }
  268. }