Browse Source

Merge pull request #2107 from tig/tabview_upgrade

Makes Notepad look better and fixes bugs
Tig 2 years ago
parent
commit
7f0f697741
1 changed files with 23 additions and 38 deletions
  1. 23 38
      UICatalog/Scenarios/Notepad.cs

+ 23 - 38
UICatalog/Scenarios/Notepad.cs

@@ -1,36 +1,35 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+using System.IO;
 using Terminal.Gui;
-using static UICatalog.Scenario;
 
 namespace UICatalog.Scenarios {
 
-	[ScenarioMetadata (Name: "Notepad", Description: "Multi tab text editor uising the TabView control.")]
+	[ScenarioMetadata (Name: "Notepad", Description: "Multi-tab text editor uising the TabView control.")]
 	[ScenarioCategory ("Controls"), ScenarioCategory ("TabView")]
 	public class Notepad : Scenario {
-
 		TabView tabView;
-		Label lblStatus;
 
 		private int numbeOfNewTabs = 1;
 
-		public override void Setup ()
+		// Don't create a Window, just return the top-level view
+		public override void Init (Toplevel top, ColorScheme colorScheme)
 		{
-			Win.Title = this.GetName ();
-			Win.Y = 1; // menu
-			Win.Height = Dim.Fill (1); // status bar
-			Top.LayoutSubviews ();
+			Application.Init ();
+
+			Top = top;
+			if (Top == null) {
+				Top = Application.Top;
+			}
+			Top.ColorScheme = Colors.Base;
+		}
 
+		public override void Setup ()
+		{
 			var menu = new MenuBar (new MenuBarItem [] {
 				new MenuBarItem ("_File", new MenuItem [] {
 					new MenuItem ("_New", "", () => New()),
 					new MenuItem ("_Open", "", () => Open()),
 					new MenuItem ("_Save", "", () => Save()),
-					new MenuItem ("_Save As", "", () => SaveAs()),
+					new MenuItem ("Save _As", "", () => SaveAs()),
 					new MenuItem ("_Close", "", () => Close()),
 					new MenuItem ("_Quit", "", () => Quit()),
 				})
@@ -39,16 +38,17 @@ namespace UICatalog.Scenarios {
 
 			tabView = new TabView () {
 				X = 0,
-				Y = 0,
+				Y = 1,
 				Width = Dim.Fill (),
 				Height = Dim.Fill (1),
 			};
 
-			tabView.Style.ShowBorder = false;
+			tabView.Style.ShowBorder = true;
 			tabView.ApplyStyleChanges ();
 
-			Win.Add (tabView);
+			Top.Add (tabView);
 
+			var lenStatusItem = new StatusItem (Key.CharMask, "Len: ", null);
 			var statusBar = new StatusBar (new StatusItem [] {
 				new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => Quit()),
 
@@ -58,26 +58,16 @@ namespace UICatalog.Scenarios {
 
 				new StatusItem(Key.CtrlMask | Key.S, "~^S~ Save", () => Save()),
 				new StatusItem(Key.CtrlMask | Key.W, "~^W~ Close", () => Close()),
+				lenStatusItem,
 			});
 
-			Win.Add (lblStatus = new Label ("Len:") {
-				Y = Pos.Bottom (tabView),
-				Width = Dim.Fill (),
-				TextAlignment = TextAlignment.Right
-			});
-
-			tabView.SelectedTabChanged += (s, e) => UpdateStatus (e.NewTab);
+			tabView.SelectedTabChanged += (s, e) => lenStatusItem.Title = $"Len:{(e.NewTab?.View?.Text?.Length ?? 0)}";
 
 			Top.Add (statusBar);
 
 			New ();
 		}
 
-		private void UpdateStatus (TabView.Tab newTab)
-		{
-			lblStatus.Text = $"Len:{(newTab?.View?.Text?.Length ?? 0)}";
-		}
-
 		private void New ()
 		{
 			Open ("", null, $"new {numbeOfNewTabs++}");
@@ -109,12 +99,10 @@ namespace UICatalog.Scenarios {
 			// close and dispose the tab
 			tabView.RemoveTab (tab);
 			tab.View.Dispose ();
-
 		}
 
 		private void Open ()
 		{
-
 			var open = new OpenDialog ("Open", "Open a file") { AllowsMultipleSelection = true };
 
 			Application.Run (open);
@@ -130,7 +118,6 @@ namespace UICatalog.Scenarios {
 					Open (File.ReadAllText (path), new FileInfo (path), Path.GetFileName (path));
 				}
 			}
-
 		}
 
 		/// <summary>
@@ -140,7 +127,6 @@ namespace UICatalog.Scenarios {
 		/// <param name="fileInfo">File that was read or null if a new blank document</param>
 		private void Open (string initialText, FileInfo fileInfo, string tabName)
 		{
-
 			var textView = new TextView () {
 				X = 0,
 				Y = 0,
@@ -188,7 +174,7 @@ namespace UICatalog.Scenarios {
 			}
 
 			tab.Save ();
-
+			tabView.SetNeedsDisplay ();
 		}
 
 		public bool SaveAs ()
@@ -207,14 +193,13 @@ namespace UICatalog.Scenarios {
 			}
 
 			tab.File = new FileInfo (fd.FilePath.ToString ());
+			tab.Text = fd.FileName.ToString ();
 			tab.Save ();
 
 			return true;
 		}
 
 		private class OpenedFile : TabView.Tab {
-
-
 			public FileInfo File { get; set; }
 
 			/// <summary>