Program.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. namespace StandaloneExample {
  2. using System.Linq;
  3. using Terminal.Gui;
  4. using System;
  5. using NStack;
  6. using System.Text;
  7. using Rune = System.Rune;
  8. using System.Runtime.InteropServices;
  9. using System.Diagnostics;
  10. static class Demo {
  11. class Box10x : View {
  12. public Box10x (int x, int y) : base (new Rect (x, y, 10, 10))
  13. {
  14. }
  15. public override void Redraw (Rect region)
  16. {
  17. Driver.SetAttribute (ColorScheme.Focus);
  18. for (int y = 0; y < 10; y++) {
  19. Move (0, y);
  20. for (int x = 0; x < 10; x++) {
  21. Driver.AddRune ((Rune)('0' + ((x + y) % 10)));
  22. }
  23. }
  24. }
  25. }
  26. class Filler : View {
  27. public Filler (Rect rect) : base (rect)
  28. {
  29. }
  30. public override void Redraw (Rect region)
  31. {
  32. Driver.SetAttribute (ColorScheme.Focus);
  33. var f = Frame;
  34. for (int y = 0; y < f.Width; y++) {
  35. Move (0, y);
  36. for (int x = 0; x < f.Height; x++) {
  37. var r = (x % 3) switch {
  38. 0 => '.',
  39. 1 => 'o',
  40. _ => 'O',
  41. };
  42. Driver.AddRune (r);
  43. }
  44. }
  45. }
  46. }
  47. static void ShowTextAlignments ()
  48. {
  49. var container = new Window ("Show Text Alignments - Press Esc to return") {
  50. X = 0,
  51. Y = 0,
  52. Width = Dim.Fill (),
  53. Height = Dim.Fill ()
  54. };
  55. container.KeyUp += (e) => {
  56. if (e.KeyEvent.Key == Key.Esc)
  57. container.Running = false;
  58. };
  59. const int i = 0;
  60. const string txt = "Hello world, how are you doing today?";
  61. container.Add (
  62. new Label ($"{i + 1}-{txt}") { TextAlignment = TextAlignment.Left, Y = 3, Width = Dim.Fill () },
  63. new Label ($"{i + 2}-{txt}") { TextAlignment = TextAlignment.Right, Y = 5, Width = Dim.Fill () },
  64. new Label ($"{i + 3}-{txt}") { TextAlignment = TextAlignment.Centered, Y = 7, Width = Dim.Fill () },
  65. new Label ($"{i + 4}-{txt}") { TextAlignment = TextAlignment.Justified, Y = 9, Width = Dim.Fill () }
  66. );
  67. Application.Run (container);
  68. }
  69. static void ShowEntries (View container)
  70. {
  71. scrollView = new ScrollView (new Rect (50, 10, 20, 8)) {
  72. ContentSize = new Size (100, 100),
  73. ContentOffset = new Point (-1, -1),
  74. ShowVerticalScrollIndicator = true,
  75. ShowHorizontalScrollIndicator = true
  76. };
  77. AddScrollViewChild ();
  78. // This is just to debug the visuals of the scrollview when small
  79. var scrollView2 = new ScrollView (new Rect (72, 10, 3, 3)) {
  80. ContentSize = new Size (100, 100),
  81. ShowVerticalScrollIndicator = true,
  82. ShowHorizontalScrollIndicator = true
  83. };
  84. scrollView2.Add (new Box10x (0, 0));
  85. var progress = new ProgressBar (new Rect (68, 1, 10, 1));
  86. bool timer (MainLoop _)
  87. {
  88. progress.Pulse ();
  89. return true;
  90. }
  91. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (300), timer);
  92. // A little convoluted, this is because I am using this to test the
  93. // layout based on referencing elements of another view:
  94. var login = new Label ("Login: ") { X = 3, Y = 6 };
  95. var password = new Label ("Password: ") {
  96. X = Pos.Left (login),
  97. Y = Pos.Bottom (login) + 1
  98. };
  99. var loginText = new TextField ("") {
  100. X = Pos.Right (password),
  101. Y = Pos.Top (login),
  102. Width = 40
  103. };
  104. var passText = new TextField ("") {
  105. Secret = true,
  106. X = Pos.Left (loginText),
  107. Y = Pos.Top (password),
  108. Width = Dim.Width (loginText)
  109. };
  110. // Add some content
  111. container.Add (
  112. login,
  113. loginText,
  114. password,
  115. passText,
  116. new FrameView (new Rect (3, 10, 25, 6), "Options", new View [] {
  117. new CheckBox (1, 0, "Remember me"),
  118. new RadioGroup (1, 2, new ustring [] { "_Personal", "_Company" }) }
  119. ),
  120. new ListView (new Rect (60, 6, 16, 4), new string [] {
  121. "First row",
  122. "<>",
  123. "This is a very long row that should overflow what is shown",
  124. "4th",
  125. "There is an empty slot on the second row",
  126. "Whoa",
  127. "This is so cool"
  128. }),
  129. scrollView,
  130. scrollView2,
  131. new Button ("Ok") { X = 3, Y = 19 },
  132. new Button ("Cancel") { X = 10, Y = 19 },
  133. progress,
  134. new Label ("Press F9 (on Unix ESC+9 is an alias) to activate the menubar") { X = 3, Y = 22 }
  135. );
  136. }
  137. private static void AddScrollViewChild ()
  138. {
  139. if (isBox10x) {
  140. scrollView.Add (new Box10x (0, 0));
  141. } else {
  142. scrollView.Add (new Filler (new Rect (0, 0, 40, 40)));
  143. }
  144. scrollView.ContentOffset = Point.Empty;
  145. }
  146. static void NewFile ()
  147. {
  148. var okButton = new Button ("Ok", is_default: true);
  149. okButton.Clicked += () => Application.RequestStop ();
  150. var cancelButton = new Button ("Cancel");
  151. cancelButton.Clicked += () => Application.RequestStop ();
  152. var d = new Dialog (
  153. "New File", 50, 20,
  154. okButton,
  155. cancelButton);
  156. var ml2 = new Label (1, 1, "Mouse Debug Line");
  157. d.Add (ml2);
  158. Application.Run (d);
  159. }
  160. static bool Quit ()
  161. {
  162. var n = MessageBox.Query (50, 7, "Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
  163. return n == 0;
  164. }
  165. static void Close ()
  166. {
  167. MessageBox.ErrorQuery (50, 7, "Error", "There is nothing to close", "Ok");
  168. }
  169. private static void ScrollViewCheck ()
  170. {
  171. isBox10x = miScrollViewCheck.Children [0].Checked = !miScrollViewCheck.Children [0].Checked;
  172. miScrollViewCheck.Children [1].Checked = !miScrollViewCheck.Children [1].Checked;
  173. scrollView.RemoveAll ();
  174. AddScrollViewChild ();
  175. }
  176. public static Label ml;
  177. private static MenuBarItem miScrollViewCheck;
  178. private static bool isBox10x = true;
  179. private static Window win;
  180. private static ScrollView scrollView;
  181. static void Main (string [] args)
  182. {
  183. if (args.Length > 0 && args.Contains ("-usc")) {
  184. Application.UseSystemConsole = true;
  185. }
  186. Console.OutputEncoding = Encoding.Default;
  187. Application.Init ();
  188. var top = Application.Top;
  189. win = new Window ("Hello") {
  190. X = 0,
  191. Y = 1,
  192. Width = Dim.Fill (),
  193. Height = Dim.Fill () - 1
  194. };
  195. StringBuilder aboutMessage = new StringBuilder ();
  196. aboutMessage.AppendLine (@"A comprehensive sample library for");
  197. aboutMessage.AppendLine (@"");
  198. aboutMessage.AppendLine (@" _______ _ _ _____ _ ");
  199. aboutMessage.AppendLine (@" |__ __| (_) | | / ____| (_) ");
  200. aboutMessage.AppendLine (@" | | ___ _ __ _ __ ___ _ _ __ __ _| || | __ _ _ _ ");
  201. aboutMessage.AppendLine (@" | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | || | |_ | | | | | ");
  202. aboutMessage.AppendLine (@" | | __/ | | | | | | | | | | | (_| | || |__| | |_| | | ");
  203. aboutMessage.AppendLine (@" |_|\___|_| |_| |_| |_|_|_| |_|\__,_|_(_)_____|\__,_|_| ");
  204. aboutMessage.AppendLine (@"");
  205. aboutMessage.AppendLine (@"https://github.com/gui-cs/Terminal.Gui");
  206. var menu = new MenuBar (new MenuBarItem [] {
  207. new MenuBarItem ("_File", new MenuItem [] {
  208. new MenuItem ("_New", "Creates new file", NewFile),
  209. new MenuItem ("_Open", "", null),
  210. new MenuItem ("_Close", "", () => Close ()),
  211. new MenuItem ("_Quit", "", () => { if (Quit ()) top.Running = false; })
  212. }),
  213. new MenuBarItem ("_Edit", new MenuItem [] {
  214. new MenuItem ("_Copy", "", null),
  215. new MenuItem ("C_ut", "", null),
  216. new MenuItem ("_Paste", "", null)
  217. }),
  218. new MenuBarItem ("A_ssorted", new MenuItem [] {
  219. new MenuItem ("_Show text alignments", "", () => ShowTextAlignments (), null, null, Key.AltMask | Key.CtrlMask | Key.G)
  220. }),
  221. miScrollViewCheck = new MenuBarItem ("ScrollView", new MenuItem [] {
  222. new MenuItem ("Box10x", "", () => ScrollViewCheck()) {CheckType = MenuItemCheckStyle.Radio, Checked = true },
  223. new MenuItem ("Filler", "", () => ScrollViewCheck()) {CheckType = MenuItemCheckStyle.Radio }
  224. }),
  225. new MenuBarItem ("_Help", new MenuItem [] {
  226. new MenuItem ("_gui.cs API Overview", "", () => OpenUrl ("https://gui-cs.github.io/Terminal.Gui/articles/overview.html"), null, null, Key.F1),
  227. new MenuItem ("gui.cs _README", "", () => OpenUrl ("https://github.com/gui-cs/Terminal.Gui"), null, null, Key.F2),
  228. new MenuItem ("_About...", "About UI Catalog",
  229. () => MessageBox.Query ("About UI Catalog", aboutMessage.ToString(), "_Ok"), null, null, Key.CtrlMask | Key.A)
  230. })
  231. });
  232. ShowEntries (win);
  233. int count = 0;
  234. ml = new Label (new Rect (3, 17, 47, 1), "Mouse: ");
  235. Application.RootMouseEvent += (me) => ml.Text = $"Mouse: ({me.X},{me.Y}) - {me.Flags} {count++}";
  236. win.Add (ml);
  237. var statusBar = new StatusBar (new StatusItem [] {
  238. new StatusItem(Key.F1, "~F1~ Help", () => MessageBox.Query (50, 7, "Help", "Helping", "Ok")),
  239. new StatusItem(Key.F2, "~F2~ Load", () => MessageBox.Query (50, 7, "Load", "Loading", "Ok")),
  240. new StatusItem(Key.F3, "~F3~ Save", () => MessageBox.Query (50, 7, "Save", "Saving", "Ok")),
  241. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => { if (Quit ()) top.Running = false; }),
  242. new StatusItem(Key.Null, Application.Driver.GetType().Name, null)
  243. });
  244. top.Add (win, menu, statusBar);
  245. Application.Run ();
  246. Application.Shutdown ();
  247. }
  248. private static void OpenUrl (string url)
  249. {
  250. try {
  251. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  252. url = url.Replace ("&", "^&");
  253. Process.Start (new ProcessStartInfo ("cmd", $"/c start {url}") { CreateNoWindow = true });
  254. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
  255. using (var process = new Process {
  256. StartInfo = new ProcessStartInfo {
  257. FileName = "xdg-open",
  258. Arguments = url,
  259. RedirectStandardError = true,
  260. RedirectStandardOutput = true,
  261. CreateNoWindow = true,
  262. UseShellExecute = false
  263. }
  264. }) {
  265. process.Start ();
  266. }
  267. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  268. Process.Start ("open", url);
  269. }
  270. } catch {
  271. throw;
  272. }
  273. }
  274. }
  275. }