浏览代码

Refactor and simplify resolvers

Thomas 2 年之前
父节点
当前提交
a443ec7986
共有 2 个文件被更改,包括 69 次插入100 次删除
  1. 58 88
      Terminal.Gui/Core/Graphs/LineCanvas.cs
  2. 11 12
      UnitTests/LineCanvasTests.cs

+ 58 - 88
Terminal.Gui/Core/Graphs/LineCanvas.cs

@@ -102,142 +102,112 @@ namespace Terminal.Gui.Graphs {
 
 
 		private abstract class IntersectionRuneResolver
 		private abstract class IntersectionRuneResolver
 		{
 		{
-			public Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects)
-			{
-				var useDouble = intersects.Any (i => i.Line.Style == BorderStyle.Double && i.Line.Length != 0);
-				var useRounded = intersects.Any (i => i.Line.Style == BorderStyle.Rounded && i.Line.Length != 0);
+			readonly Rune round;
+			readonly Rune doubleH;
+			readonly Rune doubleV;
+			readonly Rune doubleBoth;
+			readonly Rune normal;
 
 
-				return GetRuneForIntersects (driver, intersects, useDouble, useRounded);
+			public IntersectionRuneResolver(Rune round, Rune doubleH, Rune doubleV, Rune doubleBoth, Rune normal)
+			{
+				this.round = round;
+				this.doubleH = doubleH;
+				this.doubleV = doubleV;
+				this.doubleBoth = doubleBoth;
+				this.normal = normal;
 			}
 			}
 
 
-			protected abstract Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded);
-		}
-
-		private abstract class CornerIntersectionRuneResolver : IntersectionRuneResolver {
-			protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
+			public Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects)
 			{
 			{
-				
+				var useRounded = intersects.Any (i => i.Line.Style == BorderStyle.Rounded && i.Line.Length != 0);
+
 				bool doubleHorizontal = intersects.Any(l=>l.Line.Orientation == Orientation.Horizontal && l.Line.Style == BorderStyle.Double);
 				bool doubleHorizontal = intersects.Any(l=>l.Line.Orientation == Orientation.Horizontal && l.Line.Style == BorderStyle.Double);
 				bool doubleVertical = intersects.Any(l=>l.Line.Orientation == Orientation.Vertical && l.Line.Style == BorderStyle.Double);
 				bool doubleVertical = intersects.Any(l=>l.Line.Orientation == Orientation.Vertical && l.Line.Style == BorderStyle.Double);
 
 
-				return GetRuneForIntersects(driver,useRounded,doubleHorizontal,doubleVertical);
-			}
 
 
-			protected abstract Rune? GetRuneForIntersects(ConsoleDriver driver, bool useRounded, bool doubleHorizontal, bool doubleVertical);
-		}
-
-		private class ULIntersectionRuneResolver : CornerIntersectionRuneResolver 
-		{
-			protected override Rune? GetRuneForIntersects (ConsoleDriver driver, bool useRounded, bool doubleHorizontal, bool doubleVertical)
-			{
 				if(doubleHorizontal)
 				if(doubleHorizontal)
 				{
 				{
-						return doubleVertical ? driver.ULDCorner : '╒';
+						return doubleVertical ? doubleBoth : doubleH;
 				}
 				}
 				
 				
 				if(doubleVertical)
 				if(doubleVertical)
 				{
 				{
-					return '╓';
+					return doubleV;
 				}
 				}
 
 
-				return useRounded ? driver.ULRCorner : driver.ULCorner;
+				return useRounded ? round : normal;
 			}
 			}
 		}
 		}
-		private class URIntersectionRuneResolver : CornerIntersectionRuneResolver 
+
+		private class ULIntersectionRuneResolver : IntersectionRuneResolver 
 		{
 		{
-			protected override Rune? GetRuneForIntersects (ConsoleDriver driver, bool useRounded, bool doubleHorizontal, bool doubleVertical)
+			public ULIntersectionRuneResolver() :
+				base('╭','╒','╓','╔','┌')
 			{
 			{
-				if(doubleHorizontal)
-				{
-						return doubleVertical ? driver.URDCorner : '╕';
-				}
-				
-				if(doubleVertical)
-				{
-					return '╖';
-				}
 				
 				
-				return useRounded ? driver.URRCorner : driver.URCorner;
 			}
 			}
 		}
 		}
-		private class LLIntersectionRuneResolver : CornerIntersectionRuneResolver 
+		private class URIntersectionRuneResolver : IntersectionRuneResolver 
 		{
 		{
-			protected override Rune? GetRuneForIntersects (ConsoleDriver driver, bool useRounded, bool doubleHorizontal, bool doubleVertical)
+
+			public URIntersectionRuneResolver() :
+				base('╮','╕','╖','╗','┐')
 			{
 			{
-				if(doubleHorizontal)
-				{
-						return doubleVertical ? driver.LLDCorner : '╘';
-				}
 				
 				
-				if(doubleVertical)
-				{
-					return '╙';
-				}
-
-				return useRounded ? driver.LLRCorner : driver.LLCorner;
 			}
 			}
 		}
 		}
-		private class LRIntersectionRuneResolver : CornerIntersectionRuneResolver 
+		private class LLIntersectionRuneResolver : IntersectionRuneResolver 
 		{
 		{
-			protected override Rune? GetRuneForIntersects (ConsoleDriver driver, bool useRounded, bool doubleHorizontal, bool doubleVertical)
+
+			public LLIntersectionRuneResolver() :
+				base('╰','╘','╙','╚','└')
 			{
 			{
-				if(doubleHorizontal)
-				{
-						return doubleVertical ? driver.LRDCorner : '╛';
-				}
 				
 				
-				if(doubleVertical)
-				{
-					return '╜';
-				}
-
-				return useRounded ? driver.LRRCorner : driver.LRCorner;
 			}
 			}
 		}
 		}
-		private class TopTeeIntersectionRuneResolver : IntersectionRuneResolver 
+		private class LRIntersectionRuneResolver : IntersectionRuneResolver 
 		{
 		{
-			protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
+			public LRIntersectionRuneResolver() :
+				base('╯','╛','╜','╝','┘')
 			{
 			{
-				// TODO: Handle all relevant permutations of double lines into single lines
-				// to make F type borders instead.
-				return useDouble ? '╦' : driver.TopTee;
+				
 			}
 			}
 		}
 		}
+
+		private class TopTeeIntersectionRuneResolver : IntersectionRuneResolver 
+		{
+			public TopTeeIntersectionRuneResolver():
+				base('┬','╤','╥','╦','┬'){
+					
+				}
+		}
 		private class LeftTeeIntersectionRuneResolver : IntersectionRuneResolver 
 		private class LeftTeeIntersectionRuneResolver : IntersectionRuneResolver 
 		{
 		{
-			protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
-			{
-				// TODO: Handle all relevant permutations of double lines into single lines
-				// to make F type borders instead.
-				return useDouble ? '╠' : driver.LeftTee;
-			}
+			public LeftTeeIntersectionRuneResolver():
+				base('├','╞','╟','╠','├'){
+					
+				}
 		}
 		}
 		private class RightTeeIntersectionRuneResolver : IntersectionRuneResolver 
 		private class RightTeeIntersectionRuneResolver : IntersectionRuneResolver 
 		{
 		{
-			protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
-			{
-				// TODO: Handle all relevant permutations of double lines into single lines
-				// to make F type borders instead.
-				return useDouble ? '╣' : driver.RightTee;
-			}
+			public RightTeeIntersectionRuneResolver():
+				base('┤','╡','╢','╣','┤'){
+					
+				}
 		}
 		}
 		private class BottomTeeIntersectionRuneResolver : IntersectionRuneResolver 
 		private class BottomTeeIntersectionRuneResolver : IntersectionRuneResolver 
 		{
 		{
-			protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
-			{
-				// TODO: Handle all relevant permutations of double lines into single lines
-				// to make F type borders instead.
-				return useDouble ? '╩' : driver.BottomTee;
-			}
+			public BottomTeeIntersectionRuneResolver():
+				base('┴','╧','╨','╩','┴'){
+					
+				}
 		}
 		}
 		private class CrosshairIntersectionRuneResolver : IntersectionRuneResolver 
 		private class CrosshairIntersectionRuneResolver : IntersectionRuneResolver 
 		{
 		{
-			protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
-			{
-				// TODO: Handle all relevant permutations of double lines into single lines
-				// to make F type borders instead.
-				return useDouble ? '╬' : '┼';
-			}
+			public CrosshairIntersectionRuneResolver():
+				base('┼','╪','╫','╬','┼'){
+					
+				}
 		}
 		}
 
 
 		private Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects)
 		private Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects)

+ 11 - 12
UnitTests/LineCanvasTests.cs

@@ -233,19 +233,18 @@ namespace Terminal.Gui.Core {
 			canvas.AddLine (new Point (0, 4), -4, Orientation.Vertical, thinStyle);
 			canvas.AddLine (new Point (0, 4), -4, Orientation.Vertical, thinStyle);
 
 
 
 
-			canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical, BorderStyle.Double);
+			canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical,thinStyle);
 			canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, BorderStyle.Double);
 			canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, BorderStyle.Double);
 
 
 			v.Redraw (v.Bounds);
 			v.Redraw (v.Bounds);
 
 
-// TODO: Fix those Ts!
 			string looksLike =
 			string looksLike =
 @"    
 @"    
-╒═══════╕
-│    
-╠════╬═══╣
-│    
-╘═══════╛
+╒═══════╕
+│    
+╞════╪═══╡
+│    
+╘═══════╛
 ";
 ";
 			TestHelpers.AssertDriverContentsAre (looksLike, output);
 			TestHelpers.AssertDriverContentsAre (looksLike, output);
 		}
 		}
@@ -265,18 +264,18 @@ namespace Terminal.Gui.Core {
 
 
 
 
 			canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical, BorderStyle.Double);
 			canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical, BorderStyle.Double);
-			canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, BorderStyle.Double);
+			canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, thinStyle);
 
 
 			v.Redraw (v.Bounds);
 			v.Redraw (v.Bounds);
 
 
-// TODO: Fix those Ts!
 			string looksLike =
 			string looksLike =
 @"    
 @"    
-╓───────╖
+╓───────╖
 ║    ║   ║
 ║    ║   ║
-╠════╬═══╣
+╟────╫───╢
 ║    ║   ║
 ║    ║   ║
-╙────╩───╜
+╙────╨───╜
+
 ";
 ";
 			TestHelpers.AssertDriverContentsAre (looksLike, output);
 			TestHelpers.AssertDriverContentsAre (looksLike, output);
 		}
 		}