Browse Source

Fixed hexview

Tig Kindel 1 year ago
parent
commit
0c80fbcba2

+ 32 - 13
Terminal.Gui/Views/HexView.cs

@@ -95,6 +95,17 @@ public partial class HexView : View {
 		KeyBindings.Add (KeyCode.CursorRight | KeyCode.CtrlMask, Command.EndOfLine);
 		KeyBindings.Add (KeyCode.CursorUp | KeyCode.CtrlMask, Command.StartOfPage);
 		KeyBindings.Add (KeyCode.CursorDown | KeyCode.CtrlMask, Command.EndOfPage);
+
+		LayoutComplete += HexView_LayoutComplete;
+	}
+
+	private void HexView_LayoutComplete (object sender, LayoutEventArgs e)
+	{
+		// Small buffers will just show the position, with the bsize field value (4 bytes)
+		bytesPerLine = bsize;
+		if (Bounds.Width - displayWidth > 17) {
+			bytesPerLine = bsize * ((Bounds.Width - displayWidth) / 18);
+		}
 	}
 
 	/// <summary>
@@ -174,19 +185,20 @@ public partial class HexView : View {
 		}
 	}
 
-	/// <inheritdoc/>
-	public override Rect Frame {
-		get => base.Frame;
-		set {
-			base.Frame = value;
-
-			// Small buffers will just show the position, with the bsize field value (4 bytes)
-			bytesPerLine = bsize;
-			if (value.Width - displayWidth > 17) {
-				bytesPerLine = bsize * ((value.Width - displayWidth) / 18);
-			}
-		}
-	}
+	//// BUGBUG: This should be Bounds. Or, even better use View.LayoutComplete event
+	///// <inheritdoc/>
+	//public override Rect Frame {
+	//	get => base.Frame;
+	//	set {
+	//		base.Frame = value;
+
+	//		// Small buffers will just show the position, with the bsize field value (4 bytes)
+	//		bytesPerLine = bsize;
+	//		if (value.Width - displayWidth > 17) {
+	//			bytesPerLine = bsize * ((value.Width - displayWidth) / 18);
+	//		}
+	//	}
+	//}
 
 	//
 	// This is used to support editing of the buffer on a peer List<>, 
@@ -214,6 +226,7 @@ public partial class HexView : View {
 		Driver.SetAttribute (current);
 		Move (0, 0);
 
+		// BUGBUG: Bounds!!!!
 		var frame = Frame;
 
 		int nblocks = bytesPerLine / bsize;
@@ -309,6 +322,7 @@ public partial class HexView : View {
 		int delta = (int)(pos - DisplayStart);
 		int line = delta / bytesPerLine;
 
+		// BUGBUG: Bounds!
 		SetNeedsDisplay (new Rect (0, line, Frame.Width, 1));
 	}
 
@@ -331,6 +345,7 @@ public partial class HexView : View {
 	bool MoveEnd ()
 	{
 		position = source.Length;
+		// BUGBUG: Bounds!
 		if (position >= DisplayStart + bytesPerLine * Frame.Height) {
 			SetDisplayStart (position);
 			SetNeedsDisplay ();
@@ -396,6 +411,7 @@ public partial class HexView : View {
 		if (position < source.Length) {
 			position++;
 		}
+		// BUGBUG: Bounds!
 		if (position >= DisplayStart + bytesPerLine * Frame.Height) {
 			SetDisplayStart (DisplayStart + bytesPerLine);
 			SetNeedsDisplay ();
@@ -424,6 +440,7 @@ public partial class HexView : View {
 
 	bool MoveDown (int bytes)
 	{
+		// BUGBUG: Bounds!
 		RedisplayLine (position);
 		if (position + bytes < source.Length) {
 			position += bytes;
@@ -513,6 +530,8 @@ public partial class HexView : View {
 	/// <inheritdoc/>
 	public override bool MouseEvent (MouseEvent me)
 	{
+		// BUGBUG: Test this with a border! Assumes Frame == Bounds!
+
 		if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) && !me.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
 								&& !me.Flags.HasFlag (MouseFlags.WheeledDown) && !me.Flags.HasFlag (MouseFlags.WheeledUp)) {
 			return false;

+ 10 - 10
Terminal.Gui/Views/TextView.cs

@@ -2156,23 +2156,23 @@ public class TextView : View {
 				_currentColumn = 0;
 				_currentRow = 0;
 				savedHeight = Height;
-				var prevLayoutStyle = LayoutStyle;
-				if (LayoutStyle == LayoutStyle.Computed) {
-					LayoutStyle = LayoutStyle.Absolute;
-				}
+				//var prevLayoutStyle = LayoutStyle;
+				//if (LayoutStyle == LayoutStyle.Computed) {
+				//	LayoutStyle = LayoutStyle.Absolute;
+				//}
 				Height = 1;
-				LayoutStyle = prevLayoutStyle;
+				//LayoutStyle = prevLayoutStyle;
 				if (!IsInitialized) {
 					_model.LoadString (Text);
 				}
 				SetNeedsDisplay ();
 			} else if (_multiline && savedHeight != null) {
-				var lyout = LayoutStyle;
-				if (LayoutStyle == LayoutStyle.Computed) {
-					LayoutStyle = LayoutStyle.Absolute;
-				}
+				//var lyout = LayoutStyle;
+				//if (LayoutStyle == LayoutStyle.Computed) {
+				//	LayoutStyle = LayoutStyle.Absolute;
+				//}
 				Height = savedHeight;
-				LayoutStyle = lyout;
+				//LayoutStyle = lyout;
 				SetNeedsDisplay ();
 			}
 		}

+ 17 - 0
UnitTests/Views/HexViewTests.cs

@@ -51,6 +51,8 @@ namespace Terminal.Gui.ViewsTests {
 				Width = 20,
 				Height = 20
 			};
+			// Needed because HexView relies on LayoutComplete to calc sizes
+			hv.LayoutSubviews ();
 
 			Assert.Empty (hv.Edits);
 			hv.AllowEdits = false;
@@ -88,6 +90,8 @@ namespace Terminal.Gui.ViewsTests {
 				Width = 20,
 				Height = 20
 			};
+			// Needed because HexView relies on LayoutComplete to calc sizes
+			hv.LayoutSubviews ();
 
 			Assert.Equal (0, hv.DisplayStart);
 
@@ -105,6 +109,9 @@ namespace Terminal.Gui.ViewsTests {
 		public void Edited_Event ()
 		{
 			var hv = new HexView (LoadStream (true)) { Width = 20, Height = 20 };
+			// Needed because HexView relies on LayoutComplete to calc sizes
+			hv.LayoutSubviews ();
+
 			KeyValuePair<long, byte> keyValuePair = default;
 			hv.Edited += (s,e) => keyValuePair = new KeyValuePair<long, byte>(e.Position,e.NewValue);
 
@@ -120,6 +127,9 @@ namespace Terminal.Gui.ViewsTests {
 		public void DiscardEdits_Method ()
 		{
 			var hv = new HexView (LoadStream (true)) { Width = 20, Height = 20 };
+			// Needed because HexView relies on LayoutComplete to calc sizes
+			hv.LayoutSubviews ();
+
 			Assert.True (hv.NewKeyDownEvent (new (KeyCode.D4)));
 			Assert.True (hv.NewKeyDownEvent (new (KeyCode.D1)));
 			Assert.Single (hv.Edits);
@@ -135,6 +145,8 @@ namespace Terminal.Gui.ViewsTests {
 		public void Position_Using_Encoding_Unicode ()
 		{
 			var hv = new HexView (LoadStream (true)) { Width = 20, Height = 20 };
+			// Needed because HexView relies on LayoutComplete to calc sizes
+			hv.LayoutSubviews ();
 			Assert.Equal (126, hv.Source.Length);
 			Assert.Equal (126, hv.Source.Position);
 			Assert.Equal (1, hv.Position);
@@ -166,6 +178,8 @@ namespace Terminal.Gui.ViewsTests {
 		public void Position_Using_Encoding_Default ()
 		{
 			var hv = new HexView (LoadStream ()) { Width = 20, Height = 20 };
+			// Needed because HexView relies on LayoutComplete to calc sizes
+			hv.LayoutSubviews ();
 			Assert.Equal (63, hv.Source.Length);
 			Assert.Equal (63, hv.Source.Position);
 			Assert.Equal (1, hv.Position);
@@ -376,6 +390,9 @@ namespace Terminal.Gui.ViewsTests {
 			original.CopyTo (copy);
 			copy.Flush ();
 			var hv = new HexView (copy) { Width = Dim.Fill (), Height = Dim.Fill () };
+			// Needed because HexView relies on LayoutComplete to calc sizes
+			hv.LayoutSubviews ();
+
 			byte [] readBuffer = new byte [hv.Source.Length];
 			hv.Source.Position = 0;
 			hv.Source.Read (readBuffer);

+ 0 - 16
UnitTests/Views/RadioGroupTests.cs

@@ -16,20 +16,12 @@ public class RadioGroupTests {
 		var rg = new RadioGroup ();
 		Assert.True (rg.CanFocus);
 		Assert.Empty (rg.RadioLabels);
-		Assert.Null (rg.X);
-		Assert.Null (rg.Y);
-		Assert.Null (rg.Width);
-		Assert.Null (rg.Height);
 		Assert.Equal (Rect.Empty, rg.Frame);
 		Assert.Equal (0, rg.SelectedItem);
 
 		rg = new RadioGroup (new string [] { "Test" });
 		Assert.True (rg.CanFocus);
 		Assert.Single (rg.RadioLabels);
-		Assert.Null (rg.X);
-		Assert.Null (rg.Y);
-		Assert.Null (rg.Width);
-		Assert.Null (rg.Height);
 		Assert.Equal (new Rect (0, 0, 0, 0), rg.Frame);
 		Assert.Equal (0, rg.SelectedItem);
 
@@ -37,10 +29,6 @@ public class RadioGroupTests {
 		Assert.True (rg.CanFocus);
 		Assert.Single (rg.RadioLabels);
 		Assert.Equal (LayoutStyle.Absolute, rg.LayoutStyle);
-		Assert.Null (rg.X);
-		Assert.Null (rg.Y);
-		Assert.Null (rg.Width);
-		Assert.Null (rg.Height);
 		Assert.Equal (new Rect (1, 2, 20, 5), rg.Frame);
 		Assert.Equal (0, rg.SelectedItem);
 
@@ -48,10 +36,6 @@ public class RadioGroupTests {
 		Assert.True (rg.CanFocus);
 		Assert.Single (rg.RadioLabels);
 		Assert.Equal (LayoutStyle.Absolute, rg.LayoutStyle);
-		Assert.Null (rg.X);
-		Assert.Null (rg.Y);
-		Assert.Null (rg.Width);
-		Assert.Null (rg.Height);
 		Assert.Equal (new Rect (1, 2, 6, 1), rg.Frame);
 		Assert.Equal (0, rg.SelectedItem);
 	}

+ 1 - 9
UnitTests/Views/ScrollViewTests.cs

@@ -16,14 +16,10 @@ namespace Terminal.Gui.ViewsTests {
 		public void Constructors_Defaults ()
 		{
 			var sv = new ScrollView ();
-			Assert.Equal (LayoutStyle.Computed, sv.LayoutStyle);
+			Assert.Equal (LayoutStyle.Absolute, sv.LayoutStyle);
 			Assert.True (sv.CanFocus);
 			Assert.Equal (new Rect (0, 0, 0, 0), sv.Frame);
 			Assert.Equal (Rect.Empty, sv.Frame);
-			Assert.Null (sv.X);
-			Assert.Null (sv.Y);
-			Assert.Null (sv.Width);
-			Assert.Null (sv.Height);
 			Assert.Equal (Point.Empty, sv.ContentOffset);
 			Assert.Equal (Size.Empty, sv.ContentSize);
 			Assert.True (sv.AutoHideScrollBars);
@@ -33,10 +29,6 @@ namespace Terminal.Gui.ViewsTests {
 			Assert.Equal (LayoutStyle.Absolute, sv.LayoutStyle);
 			Assert.True (sv.CanFocus);
 			Assert.Equal (new Rect (1, 2, 20, 10), sv.Frame);
-			Assert.Null (sv.X);
-			Assert.Null (sv.Y);
-			Assert.Null (sv.Width);
-			Assert.Null (sv.Height);
 			Assert.Equal (Point.Empty, sv.ContentOffset);
 			Assert.Equal (Size.Empty, sv.ContentSize);
 			Assert.True (sv.AutoHideScrollBars);

+ 6 - 4
UnitTests/Views/Toplevel/WindowTests.cs

@@ -15,13 +15,15 @@ public class WindowTests {
 		// Parameterless
 		var r = new Window ();
 		Assert.NotNull (r);
-		Assert.Equal (string.Empty,         r.Title);
+		Assert.Equal (string.Empty, r.Title);
+		// Toplevels have Width/Height set to Dim.Fill
 		Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
-		Assert.Equal ("Window()(0,0,0,0)",  r.ToString ());
+		// If there's no SuperView, Top, or Driver, the default Fill width is int.MaxValue
+		Assert.Equal ("Window()(0,0,2147483647,2147483647)", r.ToString ());
 		Assert.True (r.CanFocus);
 		Assert.False (r.HasFocus);
-		Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
-		Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
+		Assert.Equal (new Rect (0, 0, 2147483645, 2147483645), r.Bounds);
+		Assert.Equal (new Rect (0, 0, 2147483647, 2147483647), r.Frame);
 		Assert.Null (r.Focused);
 		Assert.NotNull (r.ColorScheme);
 		Assert.Equal (0,           r.X);