Skip to content

Commit

Permalink
#19 - delinting / tinkering
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeclayton committed Jul 18, 2023
1 parent 95c642f commit c454ff4
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 124 deletions.
2 changes: 1 addition & 1 deletion src/FancyMouse.UnitTests/Helpers/DrawingHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public void RunTestCases(TestCase data)
screens: data.Screens.Select(s => s.Bounds).ToList(),
activatedLocation: data.ActivatedLocation);
using var actual = DrawingHelper.RenderPreview(previewLayout, desktopHdc);
actual.Save(".\\actual.png", ImageFormat.Png);
/*
actual.Save(".\\actual.png", ImageFormat.Png);
var expected = data.ExpectedResult;
Assert.AreEqual(expected.X, actual.X);
Assert.AreEqual(expected.Y, actual.Y);
Expand Down
12 changes: 2 additions & 10 deletions src/FancyMouse.UnitTests/Models/Settings/AppSettingsReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,7 @@ public void Version2WithAllValuesShouldParse()
new
{
version = 2,
hotkey = new
{
key = Keys.X.ToString(),
modifiers = (KeyModifiers.Control | KeyModifiers.Alt).ToString(),
},
hotkey = "CTRL + ALT + X",
preview = new
{
size = new
Expand Down Expand Up @@ -259,11 +255,7 @@ public void PerformanceTest()
new
{
version = 2,
hotkey = new
{
key = Keys.X.ToString(),
modifiers = (KeyModifiers.Control | KeyModifiers.Alt).ToString(),
},
hotkey = "CTRL + ALT + X",
preview = new
{
size = new
Expand Down
4 changes: 4 additions & 0 deletions src/FancyMouse/FancyMouse.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<ApplicationHighDpiMode>PerMonitorV2</ApplicationHighDpiMode>
</PropertyGroup>

<ItemGroup>
<None Remove="appSettings.styles.json" />
</ItemGroup>

<ItemGroup>
<Content Include="appSettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand Down
36 changes: 18 additions & 18 deletions src/FancyMouse/Helpers/DrawingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,27 @@ internal static Bitmap RenderPreview(
previewLayout.PreviewBounds,
Enumerable.Empty<RectangleInfo>());

// sort the source and target screen areas, putting the activated screen first
// (we need to capture and draw the activated screen before we show the form
// because otherwise we'll capture the form as part of the screenshot!)
var sourceScreens = new List<RectangleInfo> { previewLayout.Screens[previewLayout.ActivatedScreenIndex] }
.Union(previewLayout.Screens.Where((_, idx) => idx != previewLayout.ActivatedScreenIndex))
.ToList();
var targetScreens = new List<BoxBounds> { previewLayout.ScreenshotBounds[previewLayout.ActivatedScreenIndex] }
.Union(previewLayout.ScreenshotBounds.Where((_, idx) => idx != previewLayout.ActivatedScreenIndex))
.ToList();

// draw all the screenshot bezels
foreach (var screenshotBounds in previewLayout.ScreenshotBounds)
{
DrawingHelper.DrawBoxBorder(
previewGraphics, previewLayout.PreviewStyle.ScreenshotStyle, screenshotBounds);
}

var previewHdc = HDC.Null;
var imageUpdated = false;
try
{
// sort the source and target screen areas, putting the activated screen first
// (we need to capture and draw the activated screen before we show the form
// because otherwise we'll capture the form as part of the screenshot!)
var sourceScreens = new List<RectangleInfo> { previewLayout.Screens[previewLayout.ActivatedScreenIndex] }
.Union(previewLayout.Screens.Where((_, idx) => idx != previewLayout.ActivatedScreenIndex))
.ToList();
var targetScreens = new List<BoxBounds> { previewLayout.ScreenshotBounds[previewLayout.ActivatedScreenIndex] }
.Union(previewLayout.ScreenshotBounds.Where((_, idx) => idx != previewLayout.ActivatedScreenIndex))
.ToList();

// draw all the screenshot bezels
foreach (var screenshotBounds in previewLayout.ScreenshotBounds)
{
DrawingHelper.DrawBoxBorder(
previewGraphics, previewLayout.PreviewStyle.ScreenshotStyle, screenshotBounds);
}

var placeholdersDrawn = false;
for (var i = 0; i < sourceScreens.Count; i++)
{
Expand All @@ -86,7 +86,7 @@ internal static Bitmap RenderPreview(
DrawingHelper.DrawPlaceholders(
previewGraphics,
previewLayout.PreviewStyle.ScreenshotStyle,
targetScreens.Where((_, idx) => idx > i).ToList());
targetScreens.GetRange(i + 1, targetScreens.Count - i - 1));
placeholdersDrawn = true;
}

Expand Down
2 changes: 2 additions & 0 deletions src/FancyMouse/Models/Settings/AppSettingsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public static AppSettings ParseJson(string? configJson)
try
{
var configNode = JsonNode.Parse(configJson);

// if the version isn't specified we'll default to v1
configVersion = configNode?["version"]?.GetValue<int>() ?? 1;
}
catch
Expand Down
15 changes: 9 additions & 6 deletions src/FancyMouse/Models/Settings/V1/SettingsConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ public static AppSettings ParseAppSettings(string json)
};
var appConfig = JsonSerializer.Deserialize<AppConfig>(json, options)
?? throw new InvalidOperationException();
var hotkey = appConfig?.FancyMouse?.Hotkey is null
? AppSettings.DefaultSettings.Hotkey
: Keystroke.Parse(appConfig.FancyMouse.Hotkey);
var previewStyle = appConfig?.FancyMouse?.PreviewSize is null
? AppSettings.DefaultSettings.PreviewStyle
: SettingsConverter.ConvertToPreviewStyle(appConfig.FancyMouse.PreviewSize);
var hotkey = SettingsConverter.ConvertToKeystroke(appConfig?.FancyMouse?.Hotkey);
var previewStyle = SettingsConverter.ConvertToPreviewStyle(appConfig?.FancyMouse?.PreviewSize);
var appSettings = new AppSettings(hotkey, previewStyle);
return appSettings;
}

public static Keystroke ConvertToKeystroke(string? hotkey)
{
return (hotkey == null)
? AppSettings.DefaultSettings.Hotkey
: Keystroke.Parse(hotkey);
}

public static PreviewStyle ConvertToPreviewStyle(string? previewSize)
{
if (previewSize is null)
Expand Down
4 changes: 2 additions & 2 deletions src/FancyMouse/Models/Settings/V2/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
internal sealed class AppConfig
{
public AppConfig(
HotkeySettings? hotkey,
string? hotkey,
PreviewSettings? preview)
{
this.Hotkey = hotkey;
this.Preview = preview;
}

public HotkeySettings? Hotkey
public string? Hotkey
{
get;
}
Expand Down
28 changes: 0 additions & 28 deletions src/FancyMouse/Models/Settings/V2/HotkeySettings.cs

This file was deleted.

18 changes: 9 additions & 9 deletions src/FancyMouse/Models/Settings/V2/PreviewSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ namespace FancyMouse.Models.Settings.V2;
public class PreviewSettings
{
public PreviewSettings(
CanvasSizeSettings canvasSize,
CanvasStyleSettings canvasStyle,
ScreenshotStyleSettings screenshotStyle)
CanvasSizeSettings? canvasSize,
CanvasStyleSettings? canvasStyle,
ScreenshotStyleSettings? screenshotStyle)
{
this.CanvasSize = canvasSize ?? throw new ArgumentNullException(nameof(canvasSize));
this.CanvasStyle = canvasStyle ?? throw new ArgumentNullException(nameof(canvasStyle));
this.ScreenshotStyle = screenshotStyle ?? throw new ArgumentNullException(nameof(screenshotStyle));
this.CanvasSize = canvasSize;
this.CanvasStyle = canvasStyle;
this.ScreenshotStyle = screenshotStyle;
}

[JsonPropertyName("size")]
public CanvasSizeSettings CanvasSize
public CanvasSizeSettings? CanvasSize
{
get;
}

[JsonPropertyName("canvas")]
public CanvasStyleSettings CanvasStyle
public CanvasStyleSettings? CanvasStyle
{
get;
}

[JsonPropertyName("screenshot")]
public ScreenshotStyleSettings ScreenshotStyle
public ScreenshotStyleSettings? ScreenshotStyle
{
get;
}
Expand Down
109 changes: 59 additions & 50 deletions src/FancyMouse/Models/Settings/V2/SettingsConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using FancyMouse.Models.Styles;
using FancyMouse.WindowsHotKeys;
using BorderStyle = FancyMouse.Models.Styles.BorderStyle;

namespace FancyMouse.Models.Settings.V2;

Expand All @@ -17,88 +17,97 @@ public static AppSettings ParseAppSettings(string json)
};
var appConfig = JsonSerializer.Deserialize<AppConfig>(json, options)
?? throw new InvalidOperationException();
var appSettings = new AppSettings(
hotkey: SettingsConverter.ConvertToKeystroke(appConfig.Hotkey),
previewStyle: SettingsConverter.ConvertToPreviewStyle(appConfig.Preview));
var hotkey = V1.SettingsConverter.ConvertToKeystroke(appConfig.Hotkey);
var previewStyle = SettingsConverter.ConvertToPreviewStyle(appConfig.Preview);
var appSettings = new AppSettings(hotkey, previewStyle);
return appSettings;
}

public static Keystroke ConvertToKeystroke(HotkeySettings? hotkeySettings)
{
if (hotkeySettings is null)
{
return AppSettings.DefaultSettings.Hotkey;
}

var key = Enum.TryParse<WindowsHotKeys.Keys>(hotkeySettings.Key, out var outKey)
? outKey
: AppSettings.DefaultSettings.Hotkey.Key;
var modifiers = Enum.TryParse<KeyModifiers>(hotkeySettings.Modifiers, out var outModifiers)
? outModifiers
: AppSettings.DefaultSettings.Hotkey.Modifiers;
return new Keystroke(key, modifiers);
}

public static PreviewStyle ConvertToPreviewStyle(PreviewSettings? previewSettings)
{
if (previewSettings is null)
{
return AppSettings.DefaultSettings.PreviewStyle;
}

var defaultStyle = AppSettings.DefaultSettings.PreviewStyle;
var canvasStyle = AppSettings.DefaultSettings.PreviewStyle.CanvasStyle;
var screenshotStyle = AppSettings.DefaultSettings.PreviewStyle.ScreenshotStyle;
return new PreviewStyle(
canvasSize: new(
width: Math.Clamp(previewSettings.CanvasSize.Width, 50, 99999),
height: Math.Clamp(previewSettings.CanvasSize.Height, 50, 99999)
width: Math.Clamp(
value: previewSettings?.CanvasSize?.Width ?? defaultStyle.CanvasSize.Width,
min: 50,
max: 99999),
height: Math.Clamp(
value: previewSettings?.CanvasSize?.Height ?? defaultStyle.CanvasSize.Height,
min: 50,
max: 99999)
),
canvasStyle: new(
marginStyle: MarginStyle.Empty,
borderStyle: new(
color: SettingsConverter.ParseColorSettings(
value: previewSettings.CanvasStyle.BorderStyle.Color,
@default: canvasStyle.BorderStyle.Color),
all: Math.Clamp(previewSettings.CanvasStyle.BorderStyle.Width, 0, 99),
depth: Math.Clamp(previewSettings.CanvasStyle.BorderStyle.Depth, 0, 99)
),
borderStyle: SettingsConverter.ConvertToBorderStyle(previewSettings?.CanvasStyle?.BorderStyle, defaultStyle.CanvasStyle.BorderStyle),
paddingStyle: new(
all: Math.Clamp(previewSettings.CanvasStyle.PaddingStyle.Width, 0, 99)
all: Math.Clamp(
value: previewSettings?.CanvasStyle?.PaddingStyle?.Width ?? canvasStyle.PaddingStyle.Top,
min: 0,
max: 99)
),
backgroundStyle: new(
color1: SettingsConverter.ParseColorSettings(
value: previewSettings.CanvasStyle.BackgroundStyle.Color1,
@default: canvasStyle.BackgroundStyle.Color1),
value: previewSettings?.CanvasStyle?.BackgroundStyle?.Color1,
defaultValue: canvasStyle.BackgroundStyle.Color1),
color2: SettingsConverter.ParseColorSettings(
value: previewSettings.CanvasStyle.BackgroundStyle.Color2,
@default: canvasStyle.BackgroundStyle.Color2)
value: previewSettings?.CanvasStyle?.BackgroundStyle?.Color2,
defaultValue: canvasStyle.BackgroundStyle.Color2)
)
),
screenshotStyle: new(
marginStyle: new(
Math.Clamp(previewSettings.ScreenshotStyle.MarginStyle.Width, 0, 99)
),
borderStyle: new(
color: SettingsConverter.ParseColorSettings(
value: previewSettings.ScreenshotStyle.BorderStyle.Color,
@default: screenshotStyle.BorderStyle.Color),
all: previewSettings.ScreenshotStyle.BorderStyle.Width,
depth: previewSettings.ScreenshotStyle.BorderStyle.Depth
Math.Clamp(
value: previewSettings?.ScreenshotStyle?.MarginStyle?.Width ?? screenshotStyle.MarginStyle.Top,
min: 0,
max: 99)
),
borderStyle: SettingsConverter.ConvertToBorderStyle(previewSettings?.ScreenshotStyle?.BorderStyle, defaultStyle.CanvasStyle.BorderStyle),
paddingStyle: PaddingStyle.Empty,
backgroundStyle: new(
color1: SettingsConverter.ParseColorSettings(
previewSettings.ScreenshotStyle.BackgroundStyle.Color1,
@default: screenshotStyle.BackgroundStyle.Color1),
previewSettings?.ScreenshotStyle?.BackgroundStyle?.Color1,
defaultValue: screenshotStyle.BackgroundStyle.Color1),
color2: SettingsConverter.ParseColorSettings(
value: previewSettings.ScreenshotStyle.BackgroundStyle.Color2,
@default: screenshotStyle.BackgroundStyle.Color2)
value: previewSettings?.ScreenshotStyle?.BackgroundStyle?.Color2,
defaultValue: screenshotStyle.BackgroundStyle.Color2)
)
));
}

public static Color ParseColorSettings(string value, Color @default)
private static BorderStyle ConvertToBorderStyle(BorderStyleSettings? settings, BorderStyle defaultStyle)
{
return new(
color: settings?.Color is null
? defaultStyle.Color
: SettingsConverter.ParseColorSettings(
value: settings.Color,
defaultValue: defaultStyle.Color),
all: Math.Clamp(
value: settings?.Width ?? defaultStyle.Top,
min: 0,
max: 99),
depth: Math.Clamp(
value: settings?.Depth ?? defaultStyle.Depth,
min: 0,
max: 99)
);
}

private static Color ParseColorSettings(string? value, Color defaultValue)
{
if (string.IsNullOrEmpty(value))
{
return defaultValue;
}

var comparison = StringComparison.InvariantCulture;
if (value.StartsWith("#", comparison))
{
Expand All @@ -120,7 +129,7 @@ public static Color ParseColorSettings(string value, Color @default)
if (property is not null)
{
var propertyValue = property.GetValue(null, null);
return (propertyValue is null) ? @default : (Color)propertyValue;
return (propertyValue is null) ? defaultValue : (Color)propertyValue;
}
}

Expand All @@ -132,10 +141,10 @@ public static Color ParseColorSettings(string value, Color @default)
if (property is not null)
{
var propertyValue = property.GetValue(null, null);
return (propertyValue is null) ? @default : (Color)propertyValue;
return (propertyValue is null) ? defaultValue : (Color)propertyValue;
}
}

return @default;
return defaultValue;
}
}

0 comments on commit c454ff4

Please sign in to comment.