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.

This commit is contained in:
Benjamin Popp
2018-09-16 10:38:56 -05:00
parent 90f70f3779
commit 826dbdbdb9
3 changed files with 32 additions and 16 deletions

View File

@@ -30,16 +30,10 @@ namespace HavenSoft.ViewModel.DataFormats {
/// Basically everything is 'None' unless we have special information about it.
/// </summary>
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;
}
}

View File

@@ -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;
}

View File

@@ -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);
}
/// <summary>
/// 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]);