From 1d9a2e90b52d32f07b6dd44c135d831760dce5e6 Mon Sep 17 00:00:00 2001 From: Benjamin Popp Date: Sun, 16 Sep 2018 10:38:56 -0500 Subject: [PATCH] updating from review comments. DataFormats.None doesn't use IsUnused, so IsUnused should be removed a speculative code. Update ViewPort to not allow any more than one line-completions worth of empty bytes at the start. --- Gen3Hex/ViewModel/DataFormats.cs | 12 +++--------- Gen3Hex/ViewModel/ViewPort.cs | 10 +++++----- HexTests/ViewPortTests.cs | 26 ++++++++++++++++++++++++-- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/Gen3Hex/ViewModel/DataFormats.cs b/Gen3Hex/ViewModel/DataFormats.cs index 2cee1d49..89bfa902 100644 --- a/Gen3Hex/ViewModel/DataFormats.cs +++ b/Gen3Hex/ViewModel/DataFormats.cs @@ -30,16 +30,10 @@ namespace HavenSoft.ViewModel.DataFormats { /// Basically everything is 'None' unless we have special information about it. /// public class None : IDataFormat { - public bool IsUnused { get; } - public None(byte value) => IsUnused = value == 0x00 || value == 0xFF; + public static None Instance { get; } = new None(); + private None() { } public void Visit(IDataFormatVisitor visitor, byte data) => visitor.Visit(this, data); - public bool Equals(IDataFormat format) { - if (format is None that) { - return this.IsUnused == that.IsUnused; - } - - return false; - } + public bool Equals(IDataFormat format) => format is None; } } \ No newline at end of file diff --git a/Gen3Hex/ViewModel/ViewPort.cs b/Gen3Hex/ViewModel/ViewPort.cs index 6d620cc1..00142292 100644 --- a/Gen3Hex/ViewModel/ViewPort.cs +++ b/Gen3Hex/ViewModel/ViewPort.cs @@ -36,7 +36,7 @@ namespace HavenSoft.Gen3Hex.ViewModel { public int Width { get => width; set { - if (Update(ref width, value)) { + if (Update(ref width, value) && width > 0 && height > 0) { UpdateScrollRange(); } } @@ -51,7 +51,7 @@ namespace HavenSoft.Gen3Hex.ViewModel { public int Height { get => height; set { - if (Update(ref height, value)) { + if (Update(ref height, value) && width > 0 && height > 0) { UpdateScrollRange(); } } @@ -111,7 +111,7 @@ namespace HavenSoft.Gen3Hex.ViewModel { if (index < 0 || index >= data.Length) return new HexElement { Format = Undefined.Instance }; return new HexElement { - Format = new None(data[index]), + Format = None.Instance, Value = data[index], }; } @@ -130,12 +130,12 @@ namespace HavenSoft.Gen3Hex.ViewModel { private void UpdateScrollRange() { var lineCount = (int)Math.Ceiling((double)data.Length / width); - MinimumScroll = 1 - height; + MinimumScroll = 0; MaximumScroll = lineCount - 1; var newCurrentScroll = (int)Math.Ceiling((double)dataIndex / width); // screen size changes while scrolled above the data can make the data scroll completely out of view - while (newCurrentScroll <= -Height) { + while (newCurrentScroll < 0) { newCurrentScroll++; dataIndex += Width; } diff --git a/HexTests/ViewPortTests.cs b/HexTests/ViewPortTests.cs index a97dcff8..d93d74ea 100644 --- a/HexTests/ViewPortTests.cs +++ b/HexTests/ViewPortTests.cs @@ -32,6 +32,14 @@ namespace HavenSoft.HexTests { Assert.Equal(0, viewPort.MaximumScroll); } + [Fact] + public void ViewPortScrollStartsAtTopRow() { + var loadedFile = new LoadedFile("test", new byte[25]); + var viewPort = new ViewPort(loadedFile) { Width = 5, Height = 5 }; + + Assert.Equal(0, viewPort.ScrollValue); + } + /// /// The scroll bar is in terms of lines. /// The viewport should not be able to scroll such that all the data is out of view. @@ -41,7 +49,7 @@ namespace HavenSoft.HexTests { var loadedFile = new LoadedFile("test", new byte[25]); var viewPort = new ViewPort(loadedFile) { Width = 5, Height = 5 }; - Assert.Equal(-4, viewPort.MinimumScroll); + Assert.Equal(0, viewPort.MinimumScroll); Assert.Equal(4, viewPort.MaximumScroll); } @@ -52,7 +60,7 @@ namespace HavenSoft.HexTests { viewPort.ScrollValue = -10; - Assert.Equal(-4, viewPort.MinimumScroll); + Assert.Equal(0, viewPort.MinimumScroll); } [Fact] @@ -79,6 +87,20 @@ namespace HavenSoft.HexTests { Assert.Equal(6, viewPort.MaximumScroll); } + [Fact] + public void ResizingCannotLeaveTotallyBlankLineAtTop() { + var loadedFile = new LoadedFile("test", new byte[25]); + var viewPort = new ViewPort(loadedFile) { Width = 5, Height = 5 }; + + viewPort.ScrollValue++; // scroll down one line + viewPort.Width--; // decrease the width so that there is data 2 lines above + viewPort.ScrollValue = 0; // scroll up to top + viewPort.Width--; // decrease the width to make the top line totally blank + + // expected: viewPort should auto-scroll here to make the top line full of data again + Assert.Equal(0, viewPort.ScrollValue); + } + [Fact] public void RequestingOutOfRangeDataReturnsUnavailable() { var loadedFile = new LoadedFile("test", new byte[25]);