Navigation.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using System.Text;
  2. using System.Timers;
  3. using Terminal.Gui;
  4. namespace UICatalog.Scenarios;
  5. [ScenarioMetadata ("Navigation", "Navigation Tester")]
  6. [ScenarioCategory ("Mouse and Keyboard")]
  7. [ScenarioCategory ("Layout")]
  8. [ScenarioCategory ("Navigation")]
  9. public class Navigation : Scenario
  10. {
  11. private int _hotkeyCount;
  12. public override void Main ()
  13. {
  14. Application.Init ();
  15. Window app = new ()
  16. {
  17. Title = GetQuitKeyAndName (),
  18. TabStop = TabBehavior.TabGroup
  19. };
  20. var adornmentsEditor = new AdornmentsEditor
  21. {
  22. X = 0,
  23. Y = 0,
  24. AutoSelectViewToEdit = true,
  25. ShowViewIdentifier = true,
  26. TabStop = TabBehavior.NoStop
  27. };
  28. app.Add (adornmentsEditor);
  29. var arrangementEditor = new ArrangementEditor()
  30. {
  31. X = Pos.Right (adornmentsEditor),
  32. Y = 0,
  33. //Height = Dim.Fill(),
  34. AutoSelectViewToEdit = true,
  35. TabStop = TabBehavior.NoStop
  36. };
  37. app.Add (arrangementEditor);
  38. FrameView testFrame = new ()
  39. {
  40. Title = "_1 Test Frame",
  41. X = Pos.Right (arrangementEditor),
  42. Y = 1,
  43. Width = Dim.Fill (),
  44. Height = Dim.Fill ()
  45. };
  46. app.Add (testFrame);
  47. Button button = new ()
  48. {
  49. X = 0,
  50. Y = 0,
  51. Title = $"TopButton _{GetNextHotKey ()}"
  52. };
  53. button.Accepting += (sender, args) => MessageBox.Query ("hi", button.Title, "_Ok");
  54. testFrame.Add (button);
  55. View tiledView1 = CreateTiledView (0, 2, 2);
  56. View tiledView2 = CreateTiledView (1, Pos.Right (tiledView1), Pos.Top (tiledView1));
  57. testFrame.Add (tiledView1);
  58. testFrame.Add (tiledView2);
  59. View tiledView3 = CreateTiledView (1, Pos.Right (tiledView2), Pos.Top (tiledView2));
  60. tiledView3.TabStop = TabBehavior.TabGroup;
  61. tiledView3.BorderStyle = LineStyle.Double;
  62. testFrame.Add (tiledView3);
  63. View overlappedView1 = CreateOverlappedView (2, 10, Pos.Center ());
  64. View tiledSubView = CreateTiledView (4, 0, 2);
  65. overlappedView1.Add (tiledSubView);
  66. ProgressBar progressBar = new ()
  67. {
  68. X = Pos.AnchorEnd (),
  69. Y = Pos.AnchorEnd (),
  70. Width = Dim.Fill (),
  71. Id = "progressBar",
  72. BorderStyle = LineStyle.Rounded
  73. };
  74. overlappedView1.Add (progressBar);
  75. //Timer timer = new (1)
  76. //{
  77. // AutoReset = true
  78. //};
  79. //timer.Elapsed += (o, args) =>
  80. // {
  81. // if (progressBar.Fraction == 1.0)
  82. // {
  83. // progressBar.Fraction = 0;
  84. // }
  85. // progressBar.Fraction += 0.01f;
  86. // Application.Invoke (() => progressBar.SetNeedsDraw ());
  87. // ;
  88. // };
  89. //timer.Start ();
  90. Application.Iteration += (sender, args) =>
  91. {
  92. if (progressBar.Fraction == 1.0)
  93. {
  94. progressBar.Fraction = 0;
  95. }
  96. progressBar.Fraction += 0.01f;
  97. Application.Invoke (() => { });
  98. };
  99. View overlappedView2 = CreateOverlappedView (3, 8, 10);
  100. View overlappedInOverlapped1 = CreateOverlappedView (4, 1, 4);
  101. overlappedView2.Add (overlappedInOverlapped1);
  102. View overlappedInOverlapped2 = CreateOverlappedView (5, 10, 7);
  103. overlappedView2.Add (overlappedInOverlapped2);
  104. StatusBar statusBar = new ();
  105. statusBar.Add (
  106. new Shortcut
  107. {
  108. Title = "Hide",
  109. Text = "Hotkey",
  110. Key = Key.F4,
  111. Action = () =>
  112. {
  113. // TODO: move this logic into `View.ShowHide()` or similar
  114. overlappedView2.Visible = false;
  115. overlappedView2.Enabled = overlappedView2.Visible;
  116. }
  117. });
  118. statusBar.Add (
  119. new Shortcut
  120. {
  121. Title = "Toggle Hide",
  122. Text = "App",
  123. BindKeyToApplication = true,
  124. Key = Key.F4.WithCtrl,
  125. Action = () =>
  126. {
  127. // TODO: move this logic into `View.ShowHide()` or similar
  128. overlappedView2.Visible = !overlappedView2.Visible;
  129. overlappedView2.Enabled = overlappedView2.Visible;
  130. if (overlappedView2.Visible)
  131. {
  132. overlappedView2.SetFocus ();
  133. }
  134. }
  135. });
  136. overlappedView2.Add (statusBar);
  137. ColorPicker colorPicker = new ()
  138. {
  139. Y = 12,
  140. Width = Dim.Fill (),
  141. Id = "colorPicker",
  142. Style = new ()
  143. {
  144. ShowTextFields = true,
  145. ShowColorName = true
  146. }
  147. };
  148. colorPicker.ApplyStyleChanges ();
  149. colorPicker.SelectedColor = testFrame.ColorScheme.Normal.Background;
  150. colorPicker.ColorChanged += ColorPicker_ColorChanged;
  151. overlappedView2.Add (colorPicker);
  152. overlappedView2.Width = 50;
  153. testFrame.Add (overlappedView1);
  154. testFrame.Add (overlappedView2);
  155. DatePicker datePicker = new ()
  156. {
  157. X = 1,
  158. Y = 7,
  159. Id = "datePicker",
  160. ColorScheme = Colors.ColorSchemes ["Toplevel"],
  161. ShadowStyle = ShadowStyle.Transparent,
  162. BorderStyle = LineStyle.Double,
  163. CanFocus = true, // Can't drag without this? BUGBUG
  164. TabStop = TabBehavior.TabGroup,
  165. Arrangement = ViewArrangement.Movable | ViewArrangement.Overlapped
  166. };
  167. testFrame.Add (datePicker);
  168. button = new ()
  169. {
  170. X = Pos.AnchorEnd (),
  171. Y = Pos.AnchorEnd (),
  172. Title = $"TopButton _{GetNextHotKey ()}"
  173. };
  174. testFrame.Add (button);
  175. adornmentsEditor.AutoSelectSuperView = testFrame;
  176. arrangementEditor.AutoSelectSuperView = testFrame;
  177. testFrame.SetFocus ();
  178. Application.Run (app);
  179. // timer.Close ();
  180. app.Dispose ();
  181. Application.Shutdown ();
  182. return;
  183. void ColorPicker_ColorChanged (object sender, ColorEventArgs e)
  184. {
  185. testFrame.ColorScheme = testFrame.ColorScheme with { Normal = new (testFrame.ColorScheme.Normal.Foreground, e.CurrentValue) };
  186. }
  187. }
  188. private View CreateOverlappedView (int id, Pos x, Pos y)
  189. {
  190. var overlapped = new View
  191. {
  192. X = x,
  193. Y = y,
  194. Height = Dim.Auto (),
  195. Width = Dim.Auto (),
  196. Title = $"Overlapped{id} _{GetNextHotKey ()}",
  197. ColorScheme = Colors.ColorSchemes ["Toplevel"],
  198. Id = $"Overlapped{id}",
  199. ShadowStyle = ShadowStyle.Transparent,
  200. BorderStyle = LineStyle.Double,
  201. CanFocus = true, // Can't drag without this? BUGBUG
  202. TabStop = TabBehavior.TabGroup,
  203. Arrangement = ViewArrangement.Movable | ViewArrangement.Overlapped | ViewArrangement.Resizable
  204. };
  205. Button button = new ()
  206. {
  207. Title = $"Button{id} _{GetNextHotKey ()}"
  208. };
  209. overlapped.Add (button);
  210. button = new ()
  211. {
  212. Y = Pos.Bottom (button),
  213. Title = $"Button{id} _{GetNextHotKey ()}"
  214. };
  215. overlapped.Add (button);
  216. return overlapped;
  217. }
  218. private View CreateTiledView (int id, Pos x, Pos y)
  219. {
  220. var overlapped = new View
  221. {
  222. X = x,
  223. Y = y,
  224. Height = Dim.Auto (),
  225. Width = Dim.Auto (),
  226. Title = $"Tiled{id} _{GetNextHotKey ()}",
  227. Id = $"Tiled{id}",
  228. BorderStyle = LineStyle.Single,
  229. CanFocus = true, // Can't drag without this? BUGBUG
  230. TabStop = TabBehavior.TabStop,
  231. Arrangement = ViewArrangement.Fixed
  232. };
  233. Button button = new ()
  234. {
  235. Title = $"Tiled Button{id} _{GetNextHotKey ()}",
  236. Y = 1,
  237. };
  238. overlapped.Add (button);
  239. button = new ()
  240. {
  241. Y = Pos.Bottom (button),
  242. Title = $"Tiled Button{id} _{GetNextHotKey ()}"
  243. };
  244. overlapped.Add (button);
  245. return overlapped;
  246. }
  247. private char GetNextHotKey () { return (char)('A' + _hotkeyCount++); }
  248. }