Skip to content

Commit

Permalink
#19 - load style settings every preview form activation
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeclayton committed Jul 16, 2023
1 parent 7b3d3f5 commit d95f19a
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Drawing;
using System.Diagnostics;
using System.Drawing;
using System.Text.Json;
using FancyMouse.Models.Settings;
using FancyMouse.Models.Styles;
Expand Down Expand Up @@ -250,6 +251,79 @@ public void Version2WithAllValuesShouldParse()
JsonSerializer.Serialize(actual));
}

[TestMethod]
public void PerformanceTest()
{
var json = ParseJsonTests.SerializeAnonymousType(
new
{
version = 2,
hotkey = new
{
key = Keys.X.ToString(),
modifiers = (KeyModifiers.Control | KeyModifiers.Alt).ToString(),
},
preview = new
{
size = new
{
width = 800,
height = 600,
},
canvas = new
{
border = new
{
color = $"{nameof(SystemColors)}.{nameof(SystemColors.Control)}",
width = 5,
depth = 2,
},
padding = new
{
width = 2,
},
background = new
{
color1 = $"{nameof(Color)}.{nameof(Color.Green)}",
color2 = $"{nameof(Color)}.{nameof(Color.Blue)}",
},
},
screenshot = new
{
margin = new
{
width = 10,
},
border = new
{
color = $"{nameof(SystemColors)}.{nameof(SystemColors.Control)}",
width = 5,
depth = 2,
},
background = new
{
color1 = $"{nameof(Color)}.{nameof(Color.Yellow)}",
color2 = $"{nameof(Color)}.{nameof(Color.Pink)}",
},
},
},
});
var times = new List<long>();
for (var i = 0; i < 10000; i++)
{
var stopwatch = Stopwatch.StartNew();
AppSettingsReader.ParseJson(json);
stopwatch.Stop();
times.Add(stopwatch.ElapsedTicks);
}

const int ticksPerMs = 10000;
var averageMs = (decimal)times.Sum() / times.Count / ticksPerMs;
Console.WriteLine($"{averageMs} ms");

Assert.IsTrue(averageMs < 1);
}

private static string SerializeAnonymousType<T>(T value)
{
var options = new JsonSerializerOptions
Expand Down
1 change: 0 additions & 1 deletion src/FancyMouse/Helpers/DrawingHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using FancyMouse.Models.Drawing;
Expand Down
4 changes: 2 additions & 2 deletions src/FancyMouse/Models/Settings/V2/SettingsConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static Color ParseColorSettings(string value, Color @default)

if (value.StartsWith("Color.", comparison))
{
var propertyName = value.Substring("Color.".Length);
var propertyName = value["Color.".Length..];
var property = typeof(Color).GetProperties()
.SingleOrDefault(property => property.Name == propertyName);
if (property is not null)
Expand All @@ -126,7 +126,7 @@ public static Color ParseColorSettings(string value, Color @default)

if (value.StartsWith("SystemColors.", comparison))
{
var propertyName = value.Substring("SystemColors.".Length);
var propertyName = value["SystemColors.".Length..];
var property = typeof(SystemColors).GetProperties()
.SingleOrDefault(property => property.Name == propertyName);
if (property is not null)
Expand Down
29 changes: 0 additions & 29 deletions src/FancyMouse/Models/Styles/PreviewStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,6 @@ namespace FancyMouse.Models.Styles;

public class PreviewStyle
{
public sealed class Builder
{
public SizeInfo? CanvasSize
{
get;
set;
}

public BoxStyle? CanvasStyle
{
get;
set;
}

public BoxStyle? ScreenshotStyle
{
get;
set;
}

public PreviewStyle Build()
{
return new PreviewStyle(
canvasSize: this.CanvasSize ?? throw new InvalidOperationException($"{nameof(this.CanvasSize)} must be initialized before calling {nameof(this.Build)}."),
canvasStyle: this.CanvasStyle ?? throw new InvalidOperationException($"{nameof(this.CanvasStyle)} must be initialized before calling {nameof(this.Build)}."),
screenshotStyle: this.ScreenshotStyle ?? throw new InvalidOperationException($"{nameof(this.ScreenshotStyle)} must be initialized before calling {nameof(this.Build)}."));
}
}

public PreviewStyle(
SizeInfo canvasSize,
BoxStyle canvasStyle,
Expand Down
3 changes: 1 addition & 2 deletions src/FancyMouse/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ private static void Main()
// logger: LogManager.LoadConfiguration(".\\NLog.config").GetCurrentClassLogger(),
var dialog = new FancyMouseDialog(
new FancyMouseDialogOptions(
logger: LogManager.CreateNullLogger(),
previewStyle: appSettings.PreviewStyle));
logger: LogManager.CreateNullLogger()));

var hotKeyManager = new HotKeyManager(appSettings.Hotkey);
hotKeyManager.HotKeyPressed +=
Expand Down
12 changes: 2 additions & 10 deletions src/FancyMouse/UI/FancyMouseDialogOptions.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
using FancyMouse.Models.Styles;
using NLog;
using NLog;

namespace FancyMouse.UI;

internal sealed class FancyMouseDialogOptions
{
public FancyMouseDialogOptions(
ILogger logger,
PreviewStyle previewStyle)
ILogger logger)
{
this.Logger = logger ?? throw new ArgumentNullException(nameof(logger));
this.PreviewStyle = previewStyle ?? throw new ArgumentNullException(nameof(previewStyle));
}

public ILogger Logger
{
get;
}

public PreviewStyle PreviewStyle
{
get;
}
}
6 changes: 5 additions & 1 deletion src/FancyMouse/UI/FancyMouseForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using FancyMouse.Helpers;
using FancyMouse.Models.Drawing;
using FancyMouse.Models.Layout;
using FancyMouse.Models.Settings;
using static FancyMouse.NativeMethods.Core;

namespace FancyMouse.UI;
Expand Down Expand Up @@ -173,12 +174,15 @@ public void ShowPreview()
// hide the form while we redraw it...
this.Visible = false;

// load the config so it can be changed without having to restart the app
var appSettings = AppSettingsReader.ReadFile(".\\appSettings.json");

var stopwatch = Stopwatch.StartNew();

var screens = ScreenHelper.GetAllScreens().Select(screen => screen.DisplayArea).ToList();
var activatedLocation = MouseHelper.GetCursorPosition();
this.PreviewLayout = LayoutHelper.GetPreviewLayout(
previewStyle: this.Options.PreviewStyle,
previewStyle: appSettings.PreviewStyle,
screens: screens,
activatedLocation: activatedLocation);

Expand Down

0 comments on commit d95f19a

Please sign in to comment.