Browse Source

Implement Notepad split directions

Thomas 2 years ago
parent
commit
b4203849a5

+ 21 - 3
Terminal.Gui/Views/SplitView.cs

@@ -122,11 +122,13 @@ namespace Terminal.Gui {
 		/// </summary>
 		/// </summary>
 		/// <param name="idx"></param>
 		/// <param name="idx"></param>
 		/// <exception cref="NotImplementedException"></exception>
 		/// <exception cref="NotImplementedException"></exception>
-		internal void InsertTile (int idx)
+		public Tile InsertTile (int idx)
 		{
 		{
 			var oldTiles = Tiles.ToArray ();
 			var oldTiles = Tiles.ToArray ();
 			RebuildForTileCount (oldTiles.Length + 1);
 			RebuildForTileCount (oldTiles.Length + 1);
 
 
+			Tile toReturn = null;
+
 			for(int i=0;i<tiles.Count;i++) {
 			for(int i=0;i<tiles.Count;i++) {
 				
 				
 				if(i != idx) {
 				if(i != idx) {
@@ -139,9 +141,25 @@ namespace Terminal.Gui {
 					tiles [i] = oldTile;
 					tiles [i] = oldTile;
 					Add (tiles [i].View);
 					Add (tiles [i].View);
 				}
 				}
+				else
+				{
+					toReturn = tiles[i];
+				}
 			}
 			}
 			SetNeedsDisplay ();
 			SetNeedsDisplay ();
 			LayoutSubviews ();
 			LayoutSubviews ();
+
+			return toReturn;
+		}
+
+		///<summary>
+		/// Returns the index of the first <see cref="Tile"/> in
+		/// <see cref="Tiles"/> which contains <paramref name="view"/>.
+		///</summary>
+		public int IndexOf(View view)
+		{
+			// TODO: Could be recursive (i.e. search nested Subviews)
+			return tiles.IndexOf((t)=>t.View == view || t.View.Subviews.Contains(view));
 		}
 		}
 
 
 		/// <summary>
 		/// <summary>
@@ -302,7 +320,7 @@ namespace Terminal.Gui {
 		/// <returns><see langword="true"/> if a <see cref="View"/> was converted to a new nested
 		/// <returns><see langword="true"/> if a <see cref="View"/> was converted to a new nested
 		/// <see cref="SplitView"/>.  <see langword="false"/> if it was already a nested
 		/// <see cref="SplitView"/>.  <see langword="false"/> if it was already a nested
 		/// <see cref="SplitView"/></returns>
 		/// <see cref="SplitView"/></returns>
-		public bool TrySplitView(int idx, out SplitView result)
+		public bool TrySplitView(int idx, int panels, out SplitView result)
 		{
 		{
 			// when splitting a view into 2 sub views we will need to migrate
 			// when splitting a view into 2 sub views we will need to migrate
 			// the title too
 			// the title too
@@ -315,7 +333,7 @@ namespace Terminal.Gui {
 				return false;
 				return false;
 			}
 			}
 
 
-			var newContainer = new SplitView {
+			var newContainer = new SplitView(panels) {
 				Width = Dim.Fill (),
 				Width = Dim.Fill (),
 				Height = Dim.Fill (),
 				Height = Dim.Fill (),
 				parentSplitView = this,
 				parentSplitView = this,

+ 29 - 6
UICatalog/Scenarios/Notepad.cs

@@ -2,6 +2,7 @@
 using System.IO;
 using System.IO;
 using System.Linq;
 using System.Linq;
 using Terminal.Gui;
 using Terminal.Gui;
+using Terminal.Gui.Graphs;
 
 
 namespace UICatalog.Scenarios {
 namespace UICatalog.Scenarios {
 
 
@@ -110,26 +111,48 @@ namespace UICatalog.Scenarios {
 
 
 		private void SplitUp (TabView sender, OpenedFile tab)
 		private void SplitUp (TabView sender, OpenedFile tab)
 		{
 		{
-			
+			Split(0, Orientation.Horizontal,sender,tab);			
 		}
 		}
 		private void SplitDown (TabView sender, OpenedFile tab)
 		private void SplitDown (TabView sender, OpenedFile tab)
 		{
 		{
+			Split(1, Orientation.Horizontal,sender,tab);
 			
 			
 		}
 		}
 		private void SplitLeft (TabView sender, OpenedFile tab)
 		private void SplitLeft (TabView sender, OpenedFile tab)
 		{
 		{
-			
+			Split(0, Orientation.Vertical,sender,tab);
 		}
 		}
 		private void SplitRight (TabView sender, OpenedFile tab)
 		private void SplitRight (TabView sender, OpenedFile tab)
 		{
 		{
+			Split(1, Orientation.Vertical,sender,tab);
+		}
+
+		private void Split (int offset, Orientation orientation,TabView sender, OpenedFile tab)
+		{
+			
 			var split = (SplitView)sender.SuperView.SuperView;
 			var split = (SplitView)sender.SuperView.SuperView;
+			var tileIndex = split.IndexOf(sender);
+
+			if(tileIndex == -1)
+			{
+				return;
+			}
 
 
-			// TODO: Implement
-			split.TrySplitView (0, out var sub);
-			sub.Orientation = Terminal.Gui.Graphs.Orientation.Vertical;
+			if(orientation != split.Orientation)
+			{
+				split.TrySplitView(tileIndex,1,out split);
+				split.Orientation = orientation;
+				tileIndex = 0;
+			}
+
+			var newTile = split.InsertTile(tileIndex + offset);
 			var newTabView = CreateNewTabView ();
 			var newTabView = CreateNewTabView ();
 			tab.CloneTo (newTabView);
 			tab.CloneTo (newTabView);
-			sub.Tiles.ElementAt (1).View.Add (newTabView);
+			newTile.View.Add(newTabView);
+
+			newTabView.EnsureFocus();
+			newTabView.FocusFirst();
+			newTabView.FocusNext();
 		}
 		}
 
 
 		private TabView CreateNewTabView ()
 		private TabView CreateNewTabView ()

+ 2 - 2
UICatalog/Scenarios/SplitViewNesting.cs

@@ -187,11 +187,11 @@ namespace UICatalog.Scenarios {
 			SplitView newView;
 			SplitView newView;
 			
 			
 			if (left) {
 			if (left) {
-				to.TrySplitView(0,out newView);
+				to.TrySplitView(0,2,out newView);
 
 
 			}
 			}
 			else {
 			else {
-				to.TrySplitView (1,out newView);
+				to.TrySplitView (1,2,out newView);
 			}
 			}
 
 
 			viewsCreated++;
 			viewsCreated++;

+ 1 - 1
UnitTests/SplitViewTests.cs

@@ -595,7 +595,7 @@ namespace UnitTests {
 		private SplitView GetNestedContainer2Left1Right(bool withBorder)
 		private SplitView GetNestedContainer2Left1Right(bool withBorder)
 		{
 		{
 			var container = GetSplitView (20, 10,withBorder);
 			var container = GetSplitView (20, 10,withBorder);
-			Assert.True (container.TrySplitView (0, out var newContainer));
+			Assert.True (container.TrySplitView (0,2, out var newContainer));
 			
 			
 			newContainer.Orientation = Terminal.Gui.Graphs.Orientation.Horizontal;
 			newContainer.Orientation = Terminal.Gui.Graphs.Orientation.Horizontal;
 			newContainer.ColorScheme = new ColorScheme ();
 			newContainer.ColorScheme = new ColorScheme ();