Navigation.cs 7.9 KB

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