ASCIICustomButton.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Text;
  5. using JetBrains.Annotations;
  6. using Terminal.Gui;
  7. namespace UICatalog.Scenarios;
  8. [ScenarioMetadata ("ASCIICustomButtonTest", "ASCIICustomButton sample")]
  9. [ScenarioCategory ("Controls")]
  10. public class ASCIICustomButtonTest : Scenario
  11. {
  12. private static bool _smallerWindow;
  13. private MenuItem _miSmallerWindow;
  14. private ScrollViewTestWindow _scrollViewTestWindow;
  15. public override void Main ()
  16. {
  17. _smallerWindow = false;
  18. Application.Init ();
  19. Toplevel top = new ();
  20. var menu = new MenuBar
  21. {
  22. Menus =
  23. [
  24. new MenuBarItem (
  25. "_Window Size",
  26. new []
  27. {
  28. _miSmallerWindow =
  29. new MenuItem (
  30. "Smaller Window",
  31. "",
  32. ChangeWindowSize
  33. )
  34. {
  35. CheckType = MenuItemCheckStyle
  36. .Checked
  37. },
  38. null,
  39. new MenuItem (
  40. "Quit",
  41. "",
  42. () => Application.RequestStop (),
  43. null,
  44. null,
  45. (KeyCode)Application.QuitKey
  46. )
  47. }
  48. )
  49. ]
  50. };
  51. _scrollViewTestWindow = new ScrollViewTestWindow ();
  52. top.Add (menu, _scrollViewTestWindow);
  53. Application.Run (top);
  54. top.Dispose ();
  55. Application.Shutdown ();
  56. return;
  57. void ChangeWindowSize ()
  58. {
  59. _smallerWindow = (bool)(_miSmallerWindow.Checked = !_miSmallerWindow.Checked);
  60. top.Remove (_scrollViewTestWindow);
  61. _scrollViewTestWindow.Dispose ();
  62. _scrollViewTestWindow = new ScrollViewTestWindow ();
  63. top.Add (_scrollViewTestWindow);
  64. }
  65. }
  66. public class ASCIICustomButton : Button
  67. {
  68. private FrameView _border;
  69. private Label _fill;
  70. public string Description => $"Description of: {Id}";
  71. public void CustomInitialize ()
  72. {
  73. _border = new FrameView { Width = Width, Height = Height };
  74. var fillText = new StringBuilder ();
  75. for (var i = 0; i < Viewport.Height; i++)
  76. {
  77. if (i > 0)
  78. {
  79. fillText.AppendLine ("");
  80. }
  81. for (var j = 0; j < Viewport.Width; j++)
  82. {
  83. fillText.Append ("█");
  84. }
  85. }
  86. _fill = new Label { Visible = false, CanFocus = false, Text = fillText.ToString () };
  87. var title = new Label { X = Pos.Center (), Y = Pos.Center (), Text = Text };
  88. _border.MouseClick += This_MouseClick;
  89. _fill.MouseClick += This_MouseClick;
  90. title.MouseClick += This_MouseClick;
  91. Add (_border, _fill, title);
  92. }
  93. protected override void OnHasFocusChanged (bool newHasFocus, [CanBeNull] View previousFocusedView, [CanBeNull] View focusedVew)
  94. {
  95. if (newHasFocus)
  96. {
  97. _border.Visible = false;
  98. _fill.Visible = true;
  99. PointerEnter?.Invoke (this);
  100. }
  101. else
  102. {
  103. _border.Visible = true;
  104. _fill.Visible = false;
  105. }
  106. }
  107. public event Action<ASCIICustomButton> PointerEnter;
  108. private void This_MouseClick (object sender, MouseEventEventArgs obj) { NewMouseEvent (obj.MouseEvent); }
  109. }
  110. public class ScrollViewTestWindow : Window
  111. {
  112. private const int BUTTON_HEIGHT = 3;
  113. private const int BUTTON_WIDTH = 25;
  114. private const int BUTTONS_ON_PAGE = 7;
  115. private readonly List<Button> _buttons;
  116. private readonly ScrollView _scrollView;
  117. private ASCIICustomButton _selected;
  118. public ScrollViewTestWindow ()
  119. {
  120. Title = $"{Application.QuitKey} to Quit - Scenario: ScrollViewTestWindow";
  121. Label titleLabel = null;
  122. if (_smallerWindow)
  123. {
  124. Width = 80;
  125. Height = 25;
  126. _scrollView = new ScrollView
  127. {
  128. X = 3,
  129. Y = 1,
  130. Width = 24,
  131. Height = BUTTONS_ON_PAGE * BUTTON_HEIGHT,
  132. ShowVerticalScrollIndicator = true,
  133. ShowHorizontalScrollIndicator = false
  134. };
  135. }
  136. else
  137. {
  138. Width = Dim.Fill ();
  139. Height = Dim.Fill ();
  140. titleLabel = new Label { X = 0, Y = 0, Text = "DOCUMENTS" };
  141. _scrollView = new ScrollView
  142. {
  143. X = 0,
  144. Y = 1,
  145. Width = 27,
  146. Height = BUTTONS_ON_PAGE * BUTTON_HEIGHT,
  147. ShowVerticalScrollIndicator = true,
  148. ShowHorizontalScrollIndicator = false
  149. };
  150. }
  151. _scrollView.KeyBindings.Clear ();
  152. _buttons = new List<Button> ();
  153. Button prevButton = null;
  154. var count = 20;
  155. for (var j = 0; j < count; j++)
  156. {
  157. Pos yPos = prevButton == null ? 0 : Pos.Bottom (prevButton);
  158. var button = new ASCIICustomButton
  159. {
  160. Id = j.ToString (),
  161. Text = $"section {j}",
  162. Y = yPos,
  163. Width = BUTTON_WIDTH,
  164. Height = BUTTON_HEIGHT
  165. };
  166. button.Initialized += Button_Initialized;
  167. button.Accepted += Button_Clicked;
  168. button.PointerEnter += Button_PointerEnter;
  169. button.MouseClick += Button_MouseClick;
  170. button.KeyDown += Button_KeyPress;
  171. _scrollView.Add (button);
  172. _buttons.Add (button);
  173. prevButton = button;
  174. }
  175. var closeButton = new ASCIICustomButton
  176. {
  177. Id = "close",
  178. Text = "Close",
  179. Y = Pos.Bottom (prevButton),
  180. Width = BUTTON_WIDTH,
  181. Height = BUTTON_HEIGHT
  182. };
  183. closeButton.Initialized += Button_Initialized;
  184. closeButton.Accepted += Button_Clicked;
  185. closeButton.PointerEnter += Button_PointerEnter;
  186. closeButton.MouseClick += Button_MouseClick;
  187. closeButton.KeyDown += Button_KeyPress;
  188. _scrollView.Add (closeButton);
  189. _buttons.Add (closeButton);
  190. int pages = _buttons.Count / BUTTONS_ON_PAGE;
  191. if (_buttons.Count % BUTTONS_ON_PAGE > 0)
  192. {
  193. pages++;
  194. }
  195. // BUGBUG: set_ContentSize is supposed to be `protected`.
  196. _scrollView.SetContentSize (new (25, pages * BUTTONS_ON_PAGE * BUTTON_HEIGHT));
  197. if (_smallerWindow)
  198. {
  199. Add (_scrollView);
  200. }
  201. else
  202. {
  203. Add (titleLabel, _scrollView);
  204. }
  205. }
  206. private void Button_Initialized (object sender, EventArgs e)
  207. {
  208. var button = sender as ASCIICustomButton;
  209. button?.CustomInitialize ();
  210. }
  211. private void Button_Clicked (object sender, EventArgs e)
  212. {
  213. MessageBox.Query ("Button clicked.", $"'{_selected.Text}' clicked!", "Ok");
  214. if (_selected.Text == "Close")
  215. {
  216. Application.RequestStop ();
  217. }
  218. }
  219. private void Button_KeyPress (object sender, Key obj)
  220. {
  221. switch (obj.KeyCode)
  222. {
  223. case KeyCode.End:
  224. _scrollView.ContentOffset = new Point (
  225. _scrollView.ContentOffset.X,
  226. -(_scrollView.GetContentSize ().Height
  227. - _scrollView.Frame.Height
  228. + (_scrollView.ShowHorizontalScrollIndicator ? 1 : 0))
  229. );
  230. obj.Handled = true;
  231. return;
  232. case KeyCode.Home:
  233. _scrollView.ContentOffset = new Point (_scrollView.ContentOffset.X, 0);
  234. obj.Handled = true;
  235. return;
  236. case KeyCode.PageDown:
  237. _scrollView.ContentOffset = new Point (
  238. _scrollView.ContentOffset.X,
  239. Math.Max (
  240. _scrollView.ContentOffset.Y
  241. - _scrollView.Frame.Height,
  242. -(_scrollView.GetContentSize ().Height
  243. - _scrollView.Frame.Height
  244. + (_scrollView.ShowHorizontalScrollIndicator
  245. ? 1
  246. : 0))
  247. )
  248. );
  249. obj.Handled = true;
  250. return;
  251. case KeyCode.PageUp:
  252. _scrollView.ContentOffset = new Point (
  253. _scrollView.ContentOffset.X,
  254. Math.Min (
  255. _scrollView.ContentOffset.Y
  256. + _scrollView.Frame.Height,
  257. 0
  258. )
  259. );
  260. obj.Handled = true;
  261. return;
  262. }
  263. }
  264. private void Button_MouseClick (object sender, MouseEventEventArgs obj)
  265. {
  266. if (obj.MouseEvent.Flags == MouseFlags.WheeledDown)
  267. {
  268. _scrollView.ContentOffset = new Point (
  269. _scrollView.ContentOffset.X,
  270. _scrollView.ContentOffset.Y - BUTTON_HEIGHT
  271. );
  272. obj.Handled = true;
  273. }
  274. else if (obj.MouseEvent.Flags == MouseFlags.WheeledUp)
  275. {
  276. _scrollView.ContentOffset = new Point (
  277. _scrollView.ContentOffset.X,
  278. Math.Min (_scrollView.ContentOffset.Y + BUTTON_HEIGHT, 0)
  279. );
  280. obj.Handled = true;
  281. }
  282. }
  283. private void Button_PointerEnter (ASCIICustomButton obj)
  284. {
  285. bool? moveDown;
  286. if (obj.Frame.Y > _selected?.Frame.Y)
  287. {
  288. moveDown = true;
  289. }
  290. else if (obj.Frame.Y < _selected?.Frame.Y)
  291. {
  292. moveDown = false;
  293. }
  294. else
  295. {
  296. moveDown = null;
  297. }
  298. int offSet = _selected != null
  299. ? obj.Frame.Y - _selected.Frame.Y + -_scrollView.ContentOffset.Y % BUTTON_HEIGHT
  300. : 0;
  301. _selected = obj;
  302. if (moveDown == true && _selected.Frame.Y + _scrollView.ContentOffset.Y + BUTTON_HEIGHT >= _scrollView.Frame.Height && offSet != BUTTON_HEIGHT)
  303. {
  304. _scrollView.ContentOffset = new Point (
  305. _scrollView.ContentOffset.X,
  306. Math.Min (
  307. _scrollView.ContentOffset.Y - BUTTON_HEIGHT,
  308. -(_selected.Frame.Y
  309. - _scrollView.Frame.Height
  310. + BUTTON_HEIGHT)
  311. )
  312. );
  313. }
  314. else if (moveDown == true && _selected.Frame.Y + _scrollView.ContentOffset.Y >= _scrollView.Frame.Height)
  315. {
  316. _scrollView.ContentOffset = new Point (
  317. _scrollView.ContentOffset.X,
  318. _scrollView.ContentOffset.Y - BUTTON_HEIGHT
  319. );
  320. }
  321. else if (moveDown == true && _selected.Frame.Y + _scrollView.ContentOffset.Y < 0)
  322. {
  323. _scrollView.ContentOffset = new Point (
  324. _scrollView.ContentOffset.X,
  325. -_selected.Frame.Y
  326. );
  327. }
  328. else if (moveDown == false && _selected.Frame.Y < -_scrollView.ContentOffset.Y)
  329. {
  330. _scrollView.ContentOffset = new Point (
  331. _scrollView.ContentOffset.X,
  332. Math.Max (
  333. _scrollView.ContentOffset.Y + BUTTON_HEIGHT,
  334. _selected.Frame.Y
  335. )
  336. );
  337. }
  338. else if (moveDown == false && _selected.Frame.Y + _scrollView.ContentOffset.Y > _scrollView.Frame.Height)
  339. {
  340. _scrollView.ContentOffset = new Point (
  341. _scrollView.ContentOffset.X,
  342. -(_selected.Frame.Y - _scrollView.Frame.Height + BUTTON_HEIGHT)
  343. );
  344. }
  345. }
  346. }
  347. }