Navigation.cs 9.1 KB

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