Notepad.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #nullable enable
  2. namespace UICatalog.Scenarios;
  3. [ScenarioMetadata ("Notepad", "Multi-tab text editor using the TabView control.")]
  4. [ScenarioCategory ("Controls")]
  5. [ScenarioCategory ("TabView")]
  6. [ScenarioCategory ("TextView")]
  7. public class Notepad : Scenario
  8. {
  9. private TabView? _focusedTabView;
  10. private int _numNewTabs = 1;
  11. private TabView? _tabView;
  12. public Shortcut? LenShortcut { get; private set; }
  13. public override void Main ()
  14. {
  15. Application.Init ();
  16. Window top = new ()
  17. {
  18. BorderStyle = LineStyle.None,
  19. };
  20. // MenuBar
  21. MenuBar menu = new ();
  22. menu.Add (
  23. new MenuBarItem (
  24. "_File",
  25. [
  26. new MenuItem
  27. {
  28. Title = "_New",
  29. Key = Key.N.WithCtrl.WithAlt,
  30. Action = New
  31. },
  32. new MenuItem
  33. {
  34. Title = "_Open",
  35. Action = Open
  36. },
  37. new MenuItem
  38. {
  39. Title = "_Save",
  40. Action = Save
  41. },
  42. new MenuItem
  43. {
  44. Title = "Save _As",
  45. Action = () => SaveAs ()
  46. },
  47. new MenuItem
  48. {
  49. Title = "_Close",
  50. Action = Close
  51. },
  52. new MenuItem
  53. {
  54. Title = "_Quit",
  55. Action = Quit
  56. }
  57. ]
  58. )
  59. );
  60. menu.Add (
  61. new MenuBarItem (
  62. "_About",
  63. [
  64. new MenuItem
  65. {
  66. Title = "_About",
  67. Action = () => MessageBox.Query (Application.Instance, "Notepad", "About Notepad...", "Ok")
  68. }
  69. ]
  70. )
  71. );
  72. _tabView = CreateNewTabView ();
  73. _tabView.Style.ShowBorder = true;
  74. _tabView.ApplyStyleChanges ();
  75. _tabView.X = 0;
  76. _tabView.Y = Pos.Bottom (menu);
  77. _tabView.Width = Dim.Fill ();
  78. _tabView.Height = Dim.Fill (1);
  79. LenShortcut = new (Key.Empty, "Len: ", null);
  80. // StatusBar
  81. StatusBar statusBar = new (
  82. [
  83. new (Application.QuitKey, "Quit", Quit),
  84. new (Key.F2, "Open", Open),
  85. new (Key.F1, "New", New),
  86. new (Key.F3, "Save", Save),
  87. new (Key.F6, "Close", Close),
  88. LenShortcut
  89. ]
  90. )
  91. {
  92. AlignmentModes = AlignmentModes.IgnoreFirstOrLast
  93. };
  94. top.Add (menu, _tabView, statusBar);
  95. _focusedTabView = _tabView;
  96. _tabView.SelectedTabChanged += TabView_SelectedTabChanged;
  97. _tabView.HasFocusChanging += (s, e) => _focusedTabView = _tabView;
  98. top.IsModalChanged += (s, e) =>
  99. {
  100. if (e.Value)
  101. {
  102. New ();
  103. LenShortcut.Title = $"Len:{_focusedTabView?.Text?.Length ?? 0}";
  104. }
  105. };
  106. Application.Run (top);
  107. top.Dispose ();
  108. Application.Shutdown ();
  109. }
  110. public void Save ()
  111. {
  112. if (_focusedTabView?.SelectedTab is { })
  113. {
  114. Save (_focusedTabView, _focusedTabView.SelectedTab);
  115. }
  116. }
  117. public void Save (TabView tabViewToSave, Tab tabToSave)
  118. {
  119. if (tabToSave is not OpenedFile tab)
  120. {
  121. return;
  122. }
  123. if (tab.File is null)
  124. {
  125. SaveAs ();
  126. }
  127. else
  128. {
  129. tab.Save ();
  130. }
  131. tabViewToSave.SetNeedsDraw ();
  132. }
  133. public bool SaveAs ()
  134. {
  135. if (_focusedTabView?.SelectedTab is not OpenedFile tab)
  136. {
  137. return false;
  138. }
  139. SaveDialog fd = new ();
  140. Application.Run (fd);
  141. if (string.IsNullOrWhiteSpace (fd.Path) || fd.Canceled)
  142. {
  143. fd.Dispose ();
  144. return false;
  145. }
  146. tab.File = new (fd.Path);
  147. tab.Text = fd.FileName;
  148. tab.Save ();
  149. fd.Dispose ();
  150. return true;
  151. }
  152. private void Close ()
  153. {
  154. if (_focusedTabView?.SelectedTab is { })
  155. {
  156. Close (_focusedTabView, _focusedTabView.SelectedTab);
  157. }
  158. }
  159. private void Close (TabView tv, Tab tabToClose)
  160. {
  161. if (tabToClose is not OpenedFile tab)
  162. {
  163. return;
  164. }
  165. _focusedTabView = tv;
  166. if (tab.UnsavedChanges)
  167. {
  168. int? result = MessageBox.Query (Application.Instance,
  169. "Save Changes",
  170. $"Save changes to {tab.Text.TrimEnd ('*')}",
  171. "Yes",
  172. "No",
  173. "Cancel"
  174. );
  175. if (result is null || result == 2)
  176. {
  177. // user cancelled
  178. return;
  179. }
  180. if (result == 0)
  181. {
  182. if (tab.File is null)
  183. {
  184. SaveAs ();
  185. }
  186. else
  187. {
  188. tab.Save ();
  189. }
  190. }
  191. }
  192. // close and dispose the tab
  193. tv.RemoveTab (tab);
  194. tab.View?.Dispose ();
  195. _focusedTabView = tv;
  196. // If last tab is closed, open a new one
  197. if (tv.Tabs.Count == 0)
  198. {
  199. New ();
  200. }
  201. }
  202. private TabView CreateNewTabView ()
  203. {
  204. TabView tv = new () { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  205. tv.TabClicked += TabView_TabClicked;
  206. tv.SelectedTabChanged += TabView_SelectedTabChanged;
  207. tv.HasFocusChanging += (s, e) => _focusedTabView = tv;
  208. return tv;
  209. }
  210. private void New () { Open (null!, $"new {_numNewTabs++}"); }
  211. private void Open ()
  212. {
  213. OpenDialog open = new () { Title = "Open", AllowsMultipleSelection = true };
  214. Application.Run (open);
  215. bool canceled = open.Canceled;
  216. if (!canceled)
  217. {
  218. foreach (string path in open.FilePaths)
  219. {
  220. if (string.IsNullOrEmpty (path) || !File.Exists (path))
  221. {
  222. break;
  223. }
  224. // TODO should open in focused TabView
  225. Open (new (path), Path.GetFileName (path));
  226. }
  227. }
  228. open.Dispose ();
  229. }
  230. /// <summary>Creates a new tab with initial text</summary>
  231. /// <param name="fileInfo">File that was read or null if a new blank document</param>
  232. /// <param name="tabName"></param>
  233. private void Open (FileInfo? fileInfo, string tabName)
  234. {
  235. if (_focusedTabView is null)
  236. {
  237. return;
  238. }
  239. OpenedFile tab = new (this) { DisplayText = tabName, File = fileInfo };
  240. tab.View = tab.CreateTextView (fileInfo);
  241. tab.SavedText = tab.View.Text;
  242. tab.RegisterTextViewEvents (_focusedTabView);
  243. _focusedTabView.AddTab (tab, true);
  244. }
  245. private void Quit () { Application.RequestStop (); }
  246. private void TabView_SelectedTabChanged (object? sender, TabChangedEventArgs e)
  247. {
  248. if (LenShortcut is { })
  249. {
  250. LenShortcut.Title = $"Len:{e.NewTab?.View?.Text?.Length ?? 0}";
  251. }
  252. e.NewTab?.View?.SetFocus ();
  253. }
  254. private void TabView_TabClicked (object? sender, TabMouseEventArgs e)
  255. {
  256. // we are only interested in right clicks
  257. if (!e.MouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked))
  258. {
  259. return;
  260. }
  261. View [] items;
  262. if (e.Tab is null)
  263. {
  264. items = [new MenuItem { Title = "Open", Action = Open }];
  265. }
  266. else
  267. {
  268. var tv = (TabView)sender!;
  269. items =
  270. [
  271. new MenuItem { Title = "Save", Action = () => Save (_focusedTabView!, e.Tab) },
  272. new MenuItem { Title = "Close", Action = () => Close (tv, e.Tab) }
  273. ];
  274. }
  275. PopoverMenu contextMenu = new (items);
  276. // Registering with the PopoverManager will ensure that the context menu is closed when the view is no longer focused
  277. // and the context menu is disposed when it is closed.
  278. if (sender is TabView tabView && tabView.App?.Popover is { })
  279. {
  280. tabView.App.Popover.Register (contextMenu);
  281. }
  282. contextMenu.MakeVisible (e.MouseEvent.ScreenPosition);
  283. e.MouseEvent.Handled = true;
  284. }
  285. private class OpenedFile (Notepad notepad) : Tab
  286. {
  287. private readonly Notepad _notepad = notepad;
  288. public OpenedFile CloneTo (TabView other)
  289. {
  290. OpenedFile newTab = new (_notepad) { DisplayText = Text, File = File };
  291. newTab.View = newTab.CreateTextView (newTab.File);
  292. newTab.SavedText = newTab.View.Text;
  293. newTab.RegisterTextViewEvents (other);
  294. other.AddTab (newTab, true);
  295. return newTab;
  296. }
  297. public View CreateTextView (FileInfo? file)
  298. {
  299. var initialText = string.Empty;
  300. if (file is { Exists: true })
  301. {
  302. initialText = System.IO.File.ReadAllText (file.FullName);
  303. }
  304. return new TextView
  305. {
  306. X = 0,
  307. Y = 0,
  308. Width = Dim.Fill (),
  309. Height = Dim.Fill (),
  310. Text = initialText,
  311. AllowsTab = false
  312. };
  313. }
  314. public FileInfo? File { get; set; }
  315. public void RegisterTextViewEvents (TabView parent)
  316. {
  317. if (View is not TextView textView)
  318. {
  319. return;
  320. }
  321. // when user makes changes rename tab to indicate unsaved
  322. textView.ContentsChanged += (s, k) =>
  323. {
  324. // if current text doesn't match saved text
  325. bool areDiff = UnsavedChanges;
  326. if (areDiff)
  327. {
  328. if (!DisplayText.EndsWith ('*'))
  329. {
  330. DisplayText = Text + '*';
  331. }
  332. }
  333. else
  334. {
  335. if (DisplayText.EndsWith ('*'))
  336. {
  337. DisplayText = Text.TrimEnd ('*');
  338. }
  339. }
  340. if (_notepad.LenShortcut is { })
  341. {
  342. _notepad.LenShortcut.Title = $"Len:{textView.Text.Length}";
  343. }
  344. };
  345. }
  346. /// <summary>The text of the tab the last time it was saved</summary>
  347. public string? SavedText { get; set; }
  348. public bool UnsavedChanges => View is { } && !string.Equals (SavedText, View.Text);
  349. internal void Save ()
  350. {
  351. if (View is null || File is null || string.IsNullOrWhiteSpace (File.FullName))
  352. {
  353. return;
  354. }
  355. string newText = View.Text;
  356. System.IO.File.WriteAllText (File.FullName, newText);
  357. SavedText = newText;
  358. DisplayText = DisplayText.TrimEnd ('*');
  359. }
  360. }
  361. }