Program.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // Example demonstrating how to make ANY View into a popover without implementing IPopover
  2. using Terminal.Gui;
  3. using Terminal.Gui.App;
  4. using Terminal.Gui.Configuration;
  5. using Terminal.Gui.Drawing;
  6. using Terminal.Gui.ViewBase;
  7. using Terminal.Gui.Views;
  8. using Attribute = Terminal.Gui.Drawing.Attribute;
  9. IApplication app = Application.Create ();
  10. app.Init ();
  11. // Create a main window with some buttons to trigger popovers
  12. Window mainWindow = new ()
  13. {
  14. Title = "PopoverWrapper Example - Press buttons to show popovers",
  15. X = 0,
  16. Y = 0,
  17. Width = Dim.Fill (),
  18. Height = Dim.Fill ()
  19. };
  20. Label label = new ()
  21. {
  22. Text = "Click buttons below or press their hotkeys to show different popovers.\nPress Esc to close a popover.",
  23. X = Pos.Center (),
  24. Y = 1,
  25. Width = Dim.Fill (),
  26. Height = 2,
  27. TextAlignment = Alignment.Center
  28. };
  29. mainWindow.Add (label);
  30. // Example 1: Simple view as popover
  31. Button button1 = new ()
  32. {
  33. Title = "_1: Simple View Popover",
  34. X = Pos.Center (),
  35. Y = Pos.Top (label) + 3
  36. };
  37. button1.Accepting += (s, e) =>
  38. {
  39. IApplication? application = (s as View)?.App;
  40. if (application is null)
  41. {
  42. return;
  43. }
  44. View simpleView = new ()
  45. {
  46. Title = "Simple Popover",
  47. Width = Dim.Auto (),
  48. Height = Dim.Auto (),
  49. SchemeName = SchemeManager.SchemesToSchemeName (Schemes.Menu)
  50. };
  51. simpleView.Add (
  52. new Label
  53. {
  54. Text = "This is a simple View wrapped as a popover!\n\nPress Esc or click outside to dismiss.",
  55. X = Pos.Center (),
  56. Y = Pos.Center (),
  57. TextAlignment = Alignment.Center
  58. });
  59. PopoverWrapper<View> popover = simpleView.AsPopover ();
  60. popover.X = Pos.Center ();
  61. popover.Y = Pos.Center ();
  62. application.Popover?.Register (popover);
  63. application.Popover?.Show (popover);
  64. e.Handled = true;
  65. };
  66. mainWindow.Add (button1);
  67. // Example 2: ListView as popover
  68. Button button2 = new ()
  69. {
  70. Title = "_2: ListView Popover",
  71. X = Pos.Center (),
  72. Y = Pos.Bottom (button1) + 1
  73. };
  74. ListView listView = new ()
  75. {
  76. Title = "Select an Item",
  77. X = Pos.Center (),
  78. Y = Pos.Center (),
  79. Width = Dim.Percent (30),
  80. Height = Dim.Percent (40),
  81. BorderStyle = LineStyle.Single,
  82. Source = new ListWrapper<string> (["Apple", "Banana", "Cherry", "Date", "Elderberry", "Fig", "Grape"]),
  83. SelectedItem = 0
  84. };
  85. PopoverWrapper<ListView> listViewPopover = listView.AsPopover ();
  86. listView.SelectedItemChanged += (sender, args) =>
  87. {
  88. listViewPopover.Visible = false;
  89. if (listView.SelectedItem is { } selectedItem && selectedItem >= 0)
  90. {
  91. MessageBox.Query (app, "Selected", $"You selected: {listView.Source.ToList () [selectedItem]}", "OK");
  92. }
  93. };
  94. button2.Accepting += (s, e) =>
  95. {
  96. IApplication? application = (s as View)?.App;
  97. if (application is null)
  98. {
  99. return;
  100. }
  101. listViewPopover.X = Pos.Center ();
  102. listViewPopover.Y = Pos.Center ();
  103. application.Popover?.Register (listViewPopover);
  104. application.Popover?.Show (listViewPopover);
  105. e.Handled = true;
  106. };
  107. mainWindow.Add (button2);
  108. // Example 3: Form as popover
  109. Button button3 = new ()
  110. {
  111. Title = "_3: Form Popover",
  112. X = Pos.Center (),
  113. Y = Pos.Bottom (button2) + 1
  114. };
  115. button3.Accepting += (s, e) =>
  116. {
  117. IApplication? application = (s as View)?.App;
  118. if (application is null)
  119. {
  120. return;
  121. }
  122. View formView = CreateFormView (application);
  123. PopoverWrapper<View> popover = formView.AsPopover ();
  124. popover.X = Pos.Center ();
  125. popover.Y = Pos.Center ();
  126. application.Popover?.Register (popover);
  127. application.Popover?.Show (popover);
  128. e.Handled = true;
  129. };
  130. mainWindow.Add (button3);
  131. // Example 4: ColorPicker as popover
  132. Button button4 = new ()
  133. {
  134. Title = "_4: ColorPicker Popover",
  135. X = Pos.Center (),
  136. Y = Pos.Bottom (button3) + 1
  137. };
  138. button4.Accepting += (s, e) =>
  139. {
  140. IApplication? application = (s as View)?.App;
  141. if (application is null)
  142. {
  143. return;
  144. }
  145. ColorPicker colorPicker = new ()
  146. {
  147. Title = "Pick a Border Color",
  148. BorderStyle = LineStyle.Single
  149. };
  150. colorPicker.Selecting += (sender, args) =>
  151. {
  152. ColorPicker? picker = sender as ColorPicker;
  153. if (picker is { })
  154. {
  155. Scheme old = application.TopRunnableView.Border.GetScheme ();
  156. application.TopRunnableView.Border.SetScheme (old with { Normal = new Attribute (picker.SelectedColor, Color.Black) });
  157. }
  158. args.Handled = true;
  159. };
  160. PopoverWrapper<ColorPicker> popover = colorPicker.AsPopover ();
  161. popover.X = Pos.Center ();
  162. popover.Y = Pos.Center ();
  163. application.Popover?.Register (popover);
  164. application.Popover?.Show (popover);
  165. e.Handled = true;
  166. };
  167. mainWindow.Add (button4);
  168. // Example 5: Custom position and size
  169. Button button5 = new ()
  170. {
  171. Title = "_5: Positioned Popover",
  172. X = Pos.Center (),
  173. Y = Pos.Bottom (button4) + 1
  174. };
  175. button5.Accepting += (s, e) =>
  176. {
  177. IApplication? application = (s as View)?.App;
  178. if (application is null)
  179. {
  180. return;
  181. }
  182. View customView = new ()
  183. {
  184. Title = "Custom Position",
  185. X = Pos.Percent (10),
  186. Y = Pos.Percent (10),
  187. Width = Dim.Percent (50),
  188. Height = Dim.Percent (60),
  189. BorderStyle = LineStyle.Double
  190. };
  191. customView.Add (
  192. new Label
  193. {
  194. Text = "This popover has a custom position and size.\n\nYou can set X, Y, Width, and Height\nusing Pos and Dim to position it anywhere.",
  195. X = 2,
  196. Y = 2
  197. });
  198. Button closeButton = new ()
  199. {
  200. Title = "Close",
  201. X = Pos.Center (),
  202. Y = Pos.AnchorEnd (1)
  203. };
  204. closeButton.Accepting += (sender, args) =>
  205. {
  206. if (customView.SuperView is PopoverWrapper<View> wrapper)
  207. {
  208. wrapper.Visible = false;
  209. }
  210. args.Handled = true;
  211. };
  212. customView.Add (closeButton);
  213. PopoverWrapper<View> popover = customView.AsPopover ();
  214. application.Popover?.Register (popover);
  215. popover.X = Pos.Center ();
  216. popover.Y = Pos.Center ();
  217. application.Popover?.Show (popover);
  218. e.Handled = true;
  219. };
  220. mainWindow.Add (button5);
  221. // Quit button
  222. Button quitButton = new ()
  223. {
  224. Title = "_Quit",
  225. X = Pos.Center (),
  226. Y = Pos.AnchorEnd (1)
  227. };
  228. quitButton.Accepting += (s, e) =>
  229. {
  230. app.RequestStop ();
  231. e.Handled = true;
  232. };
  233. mainWindow.Add (quitButton);
  234. app.Run (mainWindow);
  235. mainWindow.Dispose ();
  236. app.Dispose ();
  237. // Helper method to create a form view
  238. View CreateFormView (IApplication application)
  239. {
  240. View form = new ()
  241. {
  242. Title = "User Registration Form",
  243. X = Pos.Center (),
  244. Y = Pos.Center (),
  245. Width = Dim.Percent (60),
  246. Height = Dim.Percent (50),
  247. BorderStyle = LineStyle.Single
  248. };
  249. Label nameLabel = new ()
  250. {
  251. Text = "Name:",
  252. X = 2,
  253. Y = 1
  254. };
  255. TextField nameField = new ()
  256. {
  257. X = Pos.Right (nameLabel) + 2,
  258. Y = Pos.Top (nameLabel),
  259. Width = Dim.Fill (2)
  260. };
  261. Label emailLabel = new ()
  262. {
  263. Text = "Email:",
  264. X = Pos.Left (nameLabel),
  265. Y = Pos.Bottom (nameLabel) + 1
  266. };
  267. TextField emailField = new ()
  268. {
  269. X = Pos.Right (emailLabel) + 2,
  270. Y = Pos.Top (emailLabel),
  271. Width = Dim.Fill (2)
  272. };
  273. Label ageLabel = new ()
  274. {
  275. Text = "Age:",
  276. X = Pos.Left (nameLabel),
  277. Y = Pos.Bottom (emailLabel) + 1
  278. };
  279. TextField ageField = new ()
  280. {
  281. X = Pos.Right (ageLabel) + 2,
  282. Y = Pos.Top (ageLabel),
  283. Width = Dim.Percent (20)
  284. };
  285. CheckBox agreeCheckbox = new ()
  286. {
  287. Title = "I agree to the terms and conditions",
  288. X = Pos.Left (nameLabel),
  289. Y = Pos.Bottom (ageLabel) + 1
  290. };
  291. Button submitButton = new ()
  292. {
  293. Title = "Submit",
  294. X = Pos.Center () - 8,
  295. Y = Pos.AnchorEnd (2),
  296. IsDefault = true
  297. };
  298. submitButton.Accepting += (s, e) =>
  299. {
  300. if (string.IsNullOrWhiteSpace (nameField.Text))
  301. {
  302. MessageBox.ErrorQuery (application, "Error", "Name is required!", "OK");
  303. e.Handled = true;
  304. return;
  305. }
  306. if (agreeCheckbox.CheckedState != CheckState.Checked)
  307. {
  308. MessageBox.ErrorQuery (application, "Error", "You must agree to the terms!", "OK");
  309. e.Handled = true;
  310. return;
  311. }
  312. MessageBox.Query (
  313. application,
  314. "Success",
  315. $"Registration submitted!\n\nName: {nameField.Text}\nEmail: {emailField.Text}\nAge: {ageField.Text}",
  316. "OK");
  317. if (form.SuperView is PopoverWrapper<View> wrapper)
  318. {
  319. wrapper.Visible = false;
  320. }
  321. e.Handled = true;
  322. };
  323. Button cancelButton = new ()
  324. {
  325. Title = "Cancel",
  326. X = Pos.Center () + 4,
  327. Y = Pos.Top (submitButton)
  328. };
  329. cancelButton.Accepting += (s, e) =>
  330. {
  331. if (form.SuperView is PopoverWrapper<View> wrapper)
  332. {
  333. wrapper.Visible = false;
  334. }
  335. e.Handled = true;
  336. };
  337. form.Add (nameLabel, nameField);
  338. form.Add (emailLabel, emailField);
  339. form.Add (ageLabel, ageField);
  340. form.Add (agreeCheckbox);
  341. form.Add (submitButton, cancelButton);
  342. return form;
  343. }