Navigation.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 ("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 += (sender, args) =>
  90. {
  91. if (progressBar.Fraction == 1.0)
  92. {
  93. progressBar.Fraction = 0;
  94. }
  95. progressBar.Fraction += 0.01f;
  96. Application.Invoke (() => { });
  97. };
  98. View overlappedView2 = CreateOverlappedView (3, 8, 10);
  99. View overlappedInOverlapped1 = CreateOverlappedView (4, 1, 4);
  100. overlappedView2.Add (overlappedInOverlapped1);
  101. View overlappedInOverlapped2 = CreateOverlappedView (5, 10, 7);
  102. overlappedView2.Add (overlappedInOverlapped2);
  103. StatusBar statusBar = new ();
  104. statusBar.Add (
  105. new Shortcut
  106. {
  107. Title = "Hide",
  108. Text = "Hotkey",
  109. Key = Key.F4,
  110. Action = () =>
  111. {
  112. // TODO: move this logic into `View.ShowHide()` or similar
  113. overlappedView2.Visible = false;
  114. overlappedView2.Enabled = overlappedView2.Visible;
  115. }
  116. });
  117. statusBar.Add (
  118. new Shortcut
  119. {
  120. Title = "Toggle Hide",
  121. Text = "App",
  122. BindKeyToApplication = true,
  123. Key = Key.F4.WithCtrl,
  124. Action = () =>
  125. {
  126. // TODO: move this logic into `View.ShowHide()` or similar
  127. overlappedView2.Visible = !overlappedView2.Visible;
  128. overlappedView2.Enabled = overlappedView2.Visible;
  129. if (overlappedView2.Visible)
  130. {
  131. overlappedView2.SetFocus ();
  132. }
  133. }
  134. });
  135. overlappedView2.Add (statusBar);
  136. ColorPicker colorPicker = new ()
  137. {
  138. Y = 12,
  139. Width = Dim.Fill (),
  140. Id = "colorPicker",
  141. Style = new ()
  142. {
  143. ShowTextFields = true,
  144. ShowColorName = true
  145. }
  146. };
  147. colorPicker.ApplyStyleChanges ();
  148. colorPicker.SelectedColor = testFrame.GetAttributeForRole (VisualRole.Normal).Background;
  149. colorPicker.ColorChanged += ColorPicker_ColorChanged;
  150. overlappedView2.Add (colorPicker);
  151. overlappedView2.Width = 50;
  152. testFrame.Add (overlappedView1);
  153. testFrame.Add (overlappedView2);
  154. DatePicker datePicker = new ()
  155. {
  156. X = 1,
  157. Y = 7,
  158. Id = "datePicker",
  159. SchemeName = "TopLevel",
  160. ShadowStyle = ShadowStyle.Transparent,
  161. BorderStyle = LineStyle.Double,
  162. CanFocus = true, // Can't drag without this? BUGBUG
  163. TabStop = TabBehavior.TabGroup,
  164. Arrangement = ViewArrangement.Movable | ViewArrangement.Overlapped
  165. };
  166. testFrame.Add (datePicker);
  167. button = new ()
  168. {
  169. X = Pos.AnchorEnd (),
  170. Y = Pos.AnchorEnd (),
  171. Title = $"TopButton _{GetNextHotKey ()}"
  172. };
  173. testFrame.Add (button);
  174. adornmentsEditor.AutoSelectSuperView = testFrame;
  175. arrangementEditor.AutoSelectSuperView = testFrame;
  176. testFrame.SetFocus ();
  177. Application.Run (app);
  178. // timer.Close ();
  179. app.Dispose ();
  180. Application.Shutdown ();
  181. return;
  182. void ColorPicker_ColorChanged (object sender, ResultEventArgs<Color> e)
  183. {
  184. testFrame.SetScheme (testFrame.GetScheme () with { Normal = new (testFrame.GetAttributeForRole (VisualRole.Normal).Foreground, e.Result) });
  185. }
  186. }
  187. private View CreateOverlappedView (int id, Pos x, Pos y)
  188. {
  189. var overlapped = new View
  190. {
  191. X = x,
  192. Y = y,
  193. Height = Dim.Auto (),
  194. Width = Dim.Auto (),
  195. Title = $"Overlapped{id} _{GetNextHotKey ()}",
  196. SchemeName = "TopLevel",
  197. Id = $"Overlapped{id}",
  198. ShadowStyle = ShadowStyle.Transparent,
  199. BorderStyle = LineStyle.Double,
  200. CanFocus = true, // Can't drag without this? BUGBUG
  201. TabStop = TabBehavior.TabGroup,
  202. Arrangement = ViewArrangement.Movable | ViewArrangement.Overlapped | ViewArrangement.Resizable
  203. };
  204. Button button = new ()
  205. {
  206. Title = $"Button{id} _{GetNextHotKey ()}"
  207. };
  208. overlapped.Add (button);
  209. button = new ()
  210. {
  211. Y = Pos.Bottom (button),
  212. Title = $"Button{id} _{GetNextHotKey ()}"
  213. };
  214. overlapped.Add (button);
  215. return overlapped;
  216. }
  217. private View CreateTiledView (int id, Pos x, Pos y)
  218. {
  219. var overlapped = new View
  220. {
  221. X = x,
  222. Y = y,
  223. Height = Dim.Auto (),
  224. Width = Dim.Auto (),
  225. Title = $"Tiled{id} _{GetNextHotKey ()}",
  226. Id = $"Tiled{id}",
  227. BorderStyle = LineStyle.Single,
  228. CanFocus = true, // Can't drag without this? BUGBUG
  229. TabStop = TabBehavior.TabStop,
  230. Arrangement = ViewArrangement.Fixed
  231. };
  232. Button button = new ()
  233. {
  234. Title = $"Tiled Button{id} _{GetNextHotKey ()}",
  235. Y = 1,
  236. };
  237. overlapped.Add (button);
  238. button = new ()
  239. {
  240. Y = Pos.Bottom (button),
  241. Title = $"Tiled Button{id} _{GetNextHotKey ()}"
  242. };
  243. overlapped.Add (button);
  244. return overlapped;
  245. }
  246. private char GetNextHotKey () { return (char)('A' + _hotkeyCount++); }
  247. }