Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#42 - separate code shared with PowerToys Mouse Jump #43

Merged
merged 4 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@
</PropertyGroup>

<ItemGroup>
<None Remove="Common\Helpers\_test-4grid-desktop.png" />
<None Remove="Common\Helpers\_test-4grid-expected.png" />
<None Remove="Common\Helpers\_test-win11-desktop.png" />
<None Remove="Common\Helpers\_test-win11-expected.png" />
<None Remove="Helpers\_test-4grid-desktop.png" />
<None Remove="Helpers\_test-4grid-expected.png" />
<None Remove="Helpers\_test-win11-desktop.png" />
<None Remove="Helpers\_test-win11-expected.png" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />
<EmbeddedResource Include="Helpers\_test-4grid-desktop.png" />
<EmbeddedResource Include="Helpers\_test-4grid-expected.png" />
<EmbeddedResource Include="Helpers\_test-win11-desktop.png" />
<EmbeddedResource Include="Helpers\_test-win11-expected.png" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FancyMouse\FancyMouse.csproj" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.4.3" />
<PackageReference Include="MSTest.TestFramework" Version="3.4.3" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Common\Helpers\_test-4grid-desktop.png" />
<EmbeddedResource Include="Common\Helpers\_test-4grid-expected.png" />
<EmbeddedResource Include="Common\Helpers\_test-win11-desktop.png" />
<EmbeddedResource Include="Common\Helpers\_test-win11-expected.png" />
<ProjectReference Include="..\FancyMouse.Common\FancyMouse.Common.csproj" />
<ProjectReference Include="..\FancyMouse.Settings\FancyMouse.Settings.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
using FancyMouse.Common.Imaging;
using FancyMouse.Common.Models.Drawing;
using FancyMouse.Common.Models.Styles;
using FancyMouse.Internal.Models.Settings;
using FancyMouse.Settings;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FancyMouse.UnitTests.Common.Helpers;
namespace FancyMouse.Common.UnitTests.Helpers;

[TestClass]
public static class DrawingHelperTests
Expand Down Expand Up @@ -53,8 +53,8 @@ public static IEnumerable<object[]> GetTestCases()
new(0, 500, 500, 500),
},
activatedLocation: new(x: 50, y: 50),
desktopImageFilename: "Common/Helpers/_test-4grid-desktop.png",
expectedImageFilename: "Common/Helpers/_test-4grid-expected.png"),
desktopImageFilename: "Helpers/_test-4grid-desktop.png",
expectedImageFilename: "Helpers/_test-4grid-expected.png"),
};
/* win 11 */
yield return new object[]
Expand All @@ -67,8 +67,8 @@ public static IEnumerable<object[]> GetTestCases()
new(0, 0, 5120, 1440),
},
activatedLocation: new(x: 50, y: 50),
desktopImageFilename: "Common/Helpers/_test-win11-desktop.png",
expectedImageFilename: "Common/Helpers/_test-win11-expected.png"),
desktopImageFilename: "Helpers/_test-win11-desktop.png",
expectedImageFilename: "Helpers/_test-win11-expected.png"),
};
}

Expand Down Expand Up @@ -99,7 +99,6 @@ public void RunTestCases(TestCase data)
expected.Save(expectedFilename, ImageFormat.Png);

// compare the images
var screens = System.Windows.Forms.Screen.AllScreens;
AssertImagesEqual(expected, actual);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using FancyMouse.Common.Models.Styles;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FancyMouse.UnitTests.Common.Helpers;
namespace FancyMouse.Common.UnitTests.Helpers;

[TestClass]
public static class LayoutHelperTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using FancyMouse.Common.Models.Drawing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FancyMouse.UnitTests.Common.Helpers;
namespace FancyMouse.Common.UnitTests.Helpers;

[TestClass]
public static class MouseHelperTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using FancyMouse.Common.Models.Drawing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FancyMouse.UnitTests.Common.Models.Drawing;
namespace FancyMouse.Common.UnitTests.Models.Drawing;

[TestClass]
public static class RectangleInfoTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using FancyMouse.Common.Models.Drawing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FancyMouse.UnitTests.Common.Models.Drawing;
namespace FancyMouse.Common.UnitTests.Models.Drawing;

[TestClass]
public static class SizeInfoTests
Expand Down
25 changes: 25 additions & 0 deletions src/FancyMouse.Common/FancyMouse.Common.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="8.0.7" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Update="StyleCop.Analyzers" Version="1.2.0-beta.507">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using FancyMouse.Common.Imaging;
Expand All @@ -8,7 +9,7 @@

namespace FancyMouse.Common.Helpers;

internal static class DrawingHelper
public static class DrawingHelper
{
public static Bitmap RenderPreview(
PreviewLayout previewLayout,
Expand Down Expand Up @@ -97,8 +98,13 @@ private static void DrawRaisedBorder(
return;
}

if (borderStyle.Color is null)
{
return;
}

// draw the main box border
using var borderBrush = new SolidBrush(borderStyle.Color);
using var borderBrush = new SolidBrush(borderStyle.Color.Value);
var borderRegion = new Region(boxBounds.BorderBounds.ToRectangle());
borderRegion.Exclude(boxBounds.PaddingBounds.ToRectangle());
graphics.FillRegion(borderBrush, borderRegion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace FancyMouse.Common.Helpers;

internal static class LayoutHelper
public static class LayoutHelper
{
public static PreviewLayout GetPreviewLayout(
PreviewStyle previewStyle, List<RectangleInfo> screens, PointInfo activatedLocation)
Expand Down Expand Up @@ -87,7 +87,7 @@ public static PreviewLayout GetPreviewLayout(
return builder.Build();
}

internal static RectangleInfo GetCombinedScreenBounds(List<RectangleInfo> screens)
private static RectangleInfo GetCombinedScreenBounds(List<RectangleInfo> screens)
{
return screens.Skip(1).Aggregate(
seed: screens.First(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace FancyMouse.Common.Helpers;

internal static class MouseHelper
public static class MouseHelper
{
/// <summary>
/// Calculates where to move the cursor to by projecting a point from
Expand All @@ -18,7 +18,7 @@ internal static class MouseHelper
/// or even negative if the primary monitor is not the at the top-left of the
/// entire desktop rectangle, so results may contain negative coordinates.
/// </remarks>
internal static PointInfo GetJumpLocation(PointInfo previewLocation, SizeInfo previewSize, RectangleInfo desktopBounds)
public static PointInfo GetJumpLocation(PointInfo previewLocation, SizeInfo previewSize, RectangleInfo desktopBounds)
{
return previewLocation
.Scale(previewSize.ScaleToFitRatio(desktopBounds.Size))
Expand All @@ -28,7 +28,7 @@ internal static PointInfo GetJumpLocation(PointInfo previewLocation, SizeInfo pr
/// <summary>
/// Get the current position of the cursor.
/// </summary>
internal static PointInfo GetCursorPosition()
public static PointInfo GetCursorPosition()
{
var lpPoint = new LPPOINT(new POINT(0, 0));
var result = User32.GetCursorPos(lpPoint);
Expand All @@ -51,7 +51,7 @@ internal static PointInfo GetCursorPosition()
/// <remarks>
/// See https://github.com/mikeclayton/FancyMouse/pull/3
/// </remarks>
internal static void SetCursorPosition(PointInfo location)
public static void SetCursorPosition(PointInfo location)
{
// set the new cursor position *twice* - the cursor sometimes end up in
// the wrong place if we try to cross the dead space between non-aligned
Expand Down Expand Up @@ -97,7 +97,7 @@ internal static void SetCursorPosition(PointInfo location)
/// See https://github.com/microsoft/PowerToys/issues/24523
/// https://github.com/microsoft/PowerToys/pull/24527
/// </remarks>
internal static void SimulateMouseMovementEvent(PointInfo location)
private static void SimulateMouseMovementEvent(PointInfo location)
{
var inputs = new User32.INPUT[]
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace FancyMouse.Common.Helpers;

internal static class ScreenHelper
public static class ScreenHelper
{
/// <summary>
/// Duplicates functionality available in System.Windows.Forms.SystemInformation
/// to reduce the dependency on WinForms
/// </summary>
public static RectangleInfo GetVirtualScreen()
private static RectangleInfo GetVirtualScreen()
{
return new(
User32.GetSystemMetrics(SYSTEM_METRICS_INDEX.SM_XVIRTUALSCREEN),
Expand All @@ -21,7 +21,7 @@ public static RectangleInfo GetVirtualScreen()
User32.GetSystemMetrics(SYSTEM_METRICS_INDEX.SM_CYVIRTUALSCREEN));
}

internal static IEnumerable<ScreenInfo> GetAllScreens()
public static IEnumerable<ScreenInfo> GetAllScreens()
{
// enumerate the monitors attached to the system
var hMonitors = new List<HMONITOR>();
Expand Down Expand Up @@ -71,7 +71,7 @@ internal static IEnumerable<ScreenInfo> GetAllScreens()
}
}

internal static ScreenInfo GetScreenFromPoint(
public static ScreenInfo GetScreenFromPoint(
List<ScreenInfo> screens,
PointInfo pt)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Drawing;
using FancyMouse.Common.Models.Drawing;
using FancyMouse.Common.NativeMethods;
using static FancyMouse.Common.NativeMethods.Core;
Expand All @@ -9,7 +10,7 @@ namespace FancyMouse.Common.Imaging;
/// Implements an IImageRegionCopyService that uses the current desktop window as the copy source.
/// This is used during the main application runtime to generate preview images of the desktop.
/// </summary>
internal sealed class DesktopImageRegionCopyService : IImageRegionCopyService
public sealed class DesktopImageRegionCopyService : IImageRegionCopyService
{
/// <summary>
/// Copies the source region from the current desktop window
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using FancyMouse.Common.Models.Drawing;
using System.Drawing;
using FancyMouse.Common.Models.Drawing;

namespace FancyMouse.Common.Imaging;

internal interface IImageRegionCopyService
public interface IImageRegionCopyService
{
/// <summary>
/// Copies the source region from the provider's source image (e.g. the interactive desktop,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using FancyMouse.Common.Models.Drawing;
using System.Drawing;
using FancyMouse.Common.Models.Drawing;

namespace FancyMouse.Common.Imaging;

/// <summary>
/// Implements an IImageRegionCopyService that uses the specified image as the copy source.
/// This is used for testing the DrawingHelper rather than as part of the main application.
/// </summary>
internal sealed class StaticImageRegionCopyService : IImageRegionCopyService
public sealed class StaticImageRegionCopyService : IImageRegionCopyService
{
public StaticImageRegionCopyService(Image sourceImage)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace FancyMouse.Common.Models.Drawing;
using System.Drawing;

namespace FancyMouse.Common.Models.Drawing;

/// <summary>
/// Immutable version of a System.Drawing.Point object with some extra utility methods.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using System.Drawing;
using System.Text.Json.Serialization;
using FancyMouse.Common.Models.Styles;
using BorderStyle = FancyMouse.Common.Models.Styles.BorderStyle;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ namespace FancyMouse.Common.Models.Drawing;
/// Immutable version of a System.Windows.Forms.Screen object so we don't need to
/// take a dependency on WinForms just for screen info.
/// </summary>
internal sealed class ScreenInfo
public sealed class ScreenInfo
{
internal ScreenInfo(HMONITOR handle, bool primary, RectangleInfo displayArea, RectangleInfo workingArea)
public ScreenInfo(int handle, bool primary, RectangleInfo displayArea, RectangleInfo workingArea)
{
// this.Handle is a HMONITOR that has been cast to an int because we don't want
// to expose the HMONITOR type outside the current assembly.
this.Handle = handle;
this.Primary = primary;
this.DisplayArea = displayArea ?? throw new ArgumentNullException(nameof(displayArea));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FancyMouse.Common.Models.Styles;
using System.Drawing;
using FancyMouse.Common.Models.Styles;
using BorderStyle = FancyMouse.Common.Models.Styles.BorderStyle;

namespace FancyMouse.Common.Models.Drawing;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace FancyMouse.Common.Models.Styles;
using System.Drawing;

namespace FancyMouse.Common.Models.Styles;

/// <summary>
/// Represents the background fill style for a drawing object.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
namespace FancyMouse.Common.Models.Styles;
using System.Drawing;

namespace FancyMouse.Common.Models.Styles;

/// <summary>
/// Represents the border style for a drawing object.
/// </summary>
public sealed class BorderStyle
{
public static readonly BorderStyle Empty = new(Color.Transparent, 0, 0);
public static readonly BorderStyle Empty = new(null, 0, 0);

public BorderStyle(Color color, decimal all, decimal depth)
public BorderStyle(Color? color, decimal all, decimal depth)
: this(color, all, all, all, all, depth)
{
}

public BorderStyle(Color color, decimal left, decimal top, decimal right, decimal bottom, decimal depth)
public BorderStyle(Color? color, decimal left, decimal top, decimal right, decimal bottom, decimal depth)
{
this.Color = color;
this.Left = left;
Expand All @@ -22,7 +24,7 @@ public BorderStyle(Color color, decimal left, decimal top, decimal right, decima
this.Depth = depth;
}

public Color Color
public Color? Color
{
get;
}
Expand Down
Loading
Loading