Skip to content

Commit

Permalink
Merge pull request #41 from mikeclayton/feature/preview-rounding
Browse files Browse the repository at this point in the history
Feature/preview rounding
  • Loading branch information
mikeclayton authored May 10, 2024
2 parents 3eb8e8e + 570f32f commit 4977f3f
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 39 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/01_build_test_publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ jobs:
name: FancyMouse-v0.0.0-preview
path: .build/app

- name: upload test images
if: always()
uses: actions/upload-artifact@v4
with:
name: test-images
path: ./src/FancyMouse.UnitTests

#- name: dotnet tool jb inspectcode
# working-directory: ${{ env.BUILD_PATH }}
# run: |
Expand Down
27 changes: 18 additions & 9 deletions src/FancyMouse.UnitTests/Common/Helpers/DrawingHelperTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using FancyMouse.Common.Helpers;
using FancyMouse.Common.Imaging;
Expand Down Expand Up @@ -44,12 +45,12 @@ public static IEnumerable<object[]> GetTestCases()
{
new TestCase(
previewStyle: AppSettings.DefaultSettings.PreviewStyle,
screens: new()
screens: new List<RectangleInfo>()
{
new RectangleInfo(0, 0, 500, 500),
new RectangleInfo(500, 0, 500, 500),
new RectangleInfo(500, 500, 500, 500),
new RectangleInfo(0, 500, 500, 500),
new(0, 0, 500, 500),
new(500, 0, 500, 500),
new(500, 500, 500, 500),
new(0, 500, 500, 500),
},
activatedLocation: new(x: 50, y: 50),
desktopImageFilename: "Common/Helpers/_test-4grid-desktop.png",
Expand All @@ -60,10 +61,10 @@ public static IEnumerable<object[]> GetTestCases()
{
new TestCase(
previewStyle: AppSettings.DefaultSettings.PreviewStyle,
screens: new()
screens: new List<RectangleInfo>()
{
new RectangleInfo(5120, 349, 1920, 1080),
new RectangleInfo(0, 0, 5120, 1440),
new(5120, 349, 1920, 1080),
new(0, 0, 5120, 1440),
},
activatedLocation: new(x: 50, y: 50),
desktopImageFilename: "Common/Helpers/_test-win11-desktop.png",
Expand All @@ -86,9 +87,17 @@ public void RunTestCases(TestCase data)
var imageCopyService = new StaticImageRegionCopyService(desktopImage);
using var actual = DrawingHelper.RenderPreview(previewLayout, imageCopyService);

// save the actual image so we can pick it up as a build artifact
var actualFilename = Path.GetFileNameWithoutExtension(data.ExpectedImageFilename) + "_actual" + Path.GetExtension(data.ExpectedImageFilename);
actual.Save(actualFilename, ImageFormat.Png);

// load the expected image
var expected = GetPreviewLayoutTests.LoadImageResource(data.ExpectedImageFilename);

// save the actual image so we can pick it up as a build artifact
var expectedFilename = Path.GetFileNameWithoutExtension(data.ExpectedImageFilename) + "_expected" + Path.GetExtension(data.ExpectedImageFilename);
expected.Save(expectedFilename, ImageFormat.Png);

// compare the images
var screens = System.Windows.Forms.Screen.AllScreens;
AssertImagesEqual(expected, actual);
Expand All @@ -106,7 +115,7 @@ private static Bitmap LoadImageResource(string filename)
}

var stream = assembly.GetManifestResourceStream(resourceName)
?? throw new InvalidOperationException();
?? throw new InvalidOperationException();
var image = (Bitmap)Image.FromStream(stream);
return image;
}
Expand Down
14 changes: 8 additions & 6 deletions src/FancyMouse.UnitTests/Common/Helpers/LayoutHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,19 @@ public TestCase(PreviewStyle previewStyle, List<RectangleInfo> screens, PointInf
this.ExpectedResult = expectedResult;
}

public PreviewStyle PreviewStyle { get; set; }
public PreviewStyle PreviewStyle { get; }

public List<RectangleInfo> Screens { get; set; }
public List<RectangleInfo> Screens { get; }

public PointInfo ActivatedLocation { get; set; }
public PointInfo ActivatedLocation { get; }

public PreviewLayout ExpectedResult { get; set; }
public PreviewLayout ExpectedResult { get; }
}

public static IEnumerable<object[]> GetTestCases()
{
// happy path - 50% scaling, *has* preview borders but *no* screenshot borders
// happy path - single screen with 50% scaling,
// *has* a preview borders but *no* screenshot borders
//
// +----------------+
// | |
Expand Down Expand Up @@ -180,7 +181,8 @@ public static IEnumerable<object[]> GetTestCases()
});
yield return new object[] { new TestCase(previewStyle, screens, activatedLocation, previewLayout) };

// happy path - 50% scaling, *no* preview borders but *has* screenshot borders
// happy path - single screen with 50% scaling,
// *no* preview borders but *has* screenshot borders
//
// +----------------+
// | |
Expand Down
Binary file modified src/FancyMouse.UnitTests/Common/Helpers/_test-win11-desktop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/FancyMouse.UnitTests/Common/Helpers/_test-win11-expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions src/FancyMouse/Common/Helpers/DrawingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public static Bitmap RenderPreview(
previewGraphics, previewLayout.PreviewStyle.ScreenStyle, screenshotBounds);
}

var imageUpdated = false;
var refreshRequired = false;
var placeholdersDrawn = false;
for (var i = 0; i < sourceScreens.Count; i++)
{
imageCopyService.CopyImageRegion(previewGraphics, sourceScreens[i], targetScreens[i].ContentBounds);
imageUpdated = true;
refreshRequired = true;

// show the placeholder images and show the form if it looks like it might take
// a while to capture the remaining screenshot images (but only if there are any)
Expand All @@ -71,11 +71,11 @@ public static Bitmap RenderPreview(
}

previewImageUpdatedCallback?.Invoke();
imageUpdated = false;
refreshRequired = false;
}
}

if (imageUpdated)
if (refreshRequired)
{
previewImageUpdatedCallback?.Invoke();
}
Expand All @@ -92,7 +92,7 @@ private static void DrawRaisedBorder(
Graphics graphics, BoxStyle boxStyle, BoxBounds boxBounds)
{
var borderStyle = boxStyle.BorderStyle;
if ((borderStyle.Horizontal == 0) && (borderStyle.Vertical == 0))
if ((borderStyle.Horizontal == 0) || (borderStyle.Vertical == 0))
{
return;
}
Expand Down
28 changes: 13 additions & 15 deletions src/FancyMouse/Common/Helpers/LayoutHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ public static PreviewLayout GetPreviewLayout(
}

var builder = new PreviewLayout.Builder();
builder.Screens = screens.ToList();

// calculate the bounding rectangle for the virtual screen
var virtualScreen = LayoutHelper.GetCombinedScreenBounds(screens);
builder.VirtualScreen = virtualScreen;
builder.Screens = screens;
builder.VirtualScreen = LayoutHelper.GetCombinedScreenBounds(builder.Screens);

// find the screen that contains the activated location - this is the
// one we'll show the preview form on
var activatedScreen = screens.Single(
var activatedScreen = builder.Screens.Single(
screen => screen.Contains(activatedLocation));
builder.ActivatedScreenIndex = screens.IndexOf(activatedScreen);
builder.ActivatedScreenIndex = builder.Screens.IndexOf(activatedScreen);

// work out the maximum allowed size of the preview form:
// * can't be bigger than the activated screen
Expand All @@ -43,11 +42,15 @@ public static PreviewLayout GetPreviewLayout(
.Shrink(previewStyle.CanvasStyle.BorderStyle)
.Shrink(previewStyle.CanvasStyle.PaddingStyle);

// scale the virtual screen to fit inside the content area
var screenScalingRatio = builder.VirtualScreen.Size
.ScaleToFitRatio(maxContentSize);

// work out the actual size of the "content area" by scaling the virtual screen
// to fit inside the maximum content area while maintaining its aspect ration.
// we'll also offset it to allow for any margins, borders and padding
var contentBounds = virtualScreen.Size
.ScaleToFit(maxContentSize)
var contentBounds = builder.VirtualScreen.Size
.Scale(screenScalingRatio)
.Floor()
.PlaceAt(0, 0)
.Offset(previewStyle.CanvasStyle.MarginStyle.Left, previewStyle.CanvasStyle.MarginStyle.Top)
Expand All @@ -69,18 +72,13 @@ public static PreviewLayout GetPreviewLayout(
.Clamp(activatedScreen);
builder.FormBounds = formBounds;

// get the scaling factor to draw screenshot images at by scaling the
// virtual screen to fit inside the preview content bounds
var scalingRatio = builder.VirtualScreen.Size
.ScaleToFitRatio(contentBounds.Size);

// now calculate the positions of each of the screenshot images on the preview
builder.ScreenshotBounds = screens
builder.ScreenshotBounds = builder.Screens
.Select(
screen => LayoutHelper.GetBoxBoundsFromOuterBounds(
screen
.Offset(virtualScreen.Location.ToSize().Invert())
.Scale(scalingRatio)
.Offset(builder.VirtualScreen.Location.ToSize().Invert())
.Scale(screenScalingRatio)
.Offset(builder.PreviewBounds.ContentBounds.Location.ToSize())
.Truncate(),
previewStyle.ScreenStyle))
Expand Down
2 changes: 1 addition & 1 deletion src/FancyMouse/Common/Models/Drawing/BoxBounds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public sealed class BoxBounds
*/

public BoxBounds(
internal BoxBounds(
RectangleInfo outerBounds,
RectangleInfo marginBounds,
RectangleInfo borderBounds,
Expand Down
4 changes: 4 additions & 0 deletions src/FancyMouse/Common/Models/Drawing/SizeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public SizeInfo Intersect(SizeInfo size)
public SizeInfo Invert() =>
new(-this.Width, -this.Height);

public SizeInfo Scale(decimal scalingFactor) => new(
this.Width * scalingFactor,
this.Height * scalingFactor);

public SizeInfo Shrink(BorderStyle border) =>
new(this.Width - border.Horizontal, this.Height - border.Vertical);

Expand Down
6 changes: 3 additions & 3 deletions src/FancyMouse/Common/Models/Layout/PreviewLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class Builder
{
public Builder()
{
this.Screens = new List<RectangleInfo>();
this.Screens = new();
this.ScreenshotBounds = new();
}

Expand All @@ -26,7 +26,7 @@ public RectangleInfo? VirtualScreen
set;
}

public IList<RectangleInfo> Screens
public List<RectangleInfo> Screens
{
get;
set;
Expand Down Expand Up @@ -72,7 +72,7 @@ public PreviewLayout Build()
public PreviewLayout(
PreviewStyle previewStyle,
RectangleInfo virtualScreen,
IList<RectangleInfo> screens,
List<RectangleInfo> screens,
int activatedScreenIndex,
RectangleInfo formBounds,
BoxBounds previewBounds,
Expand Down
4 changes: 4 additions & 0 deletions src/FancyMouse/UI/FancyMouseForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ private void OnPreviewImageUpdated()
{
if (!this.Visible)
{
// we seem to need to turn off topmost and then re-enable it again
// when we show the form, otherwise it doesn't always get shown topmost...
this.TopMost = false;
this.TopMost = true;
this.Show();
}

Expand Down

0 comments on commit 4977f3f

Please sign in to comment.