Mouse.cs 12 KB

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