Skip to content

Commit daa4b8f

Browse files
committed
fix: tests
1 parent 97d9971 commit daa4b8f

File tree

7 files changed

+100
-80
lines changed

7 files changed

+100
-80
lines changed

Src/GhostDraw/Core/ServiceConfiguration.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
using System.IO;
2+
using GhostDraw.Helpers;
3+
using GhostDraw.Managers;
4+
using GhostDraw.Services;
5+
using GhostDraw.ViewModels;
6+
using GhostDraw.Views;
17
using Microsoft.Extensions.DependencyInjection;
28
using Microsoft.Extensions.Logging;
39
using Serilog;
410
using Serilog.Events;
5-
using System.IO;
6-
using GhostDraw.Services;
7-
using GhostDraw.Managers;
8-
using GhostDraw.Helpers;
9-
using GhostDraw.Views;
10-
using GhostDraw.ViewModels;
1111

1212
namespace GhostDraw.Core;
1313

@@ -58,15 +58,16 @@ public static ServiceProvider ConfigureServices()
5858
services.AddSingleton<AppSettingsService>();
5959
services.AddSingleton<CursorHelper>();
6060
services.AddSingleton<ScreenshotService>();
61-
61+
6262
// Register drawing tools
6363
services.AddSingleton<GhostDraw.Tools.PenTool>();
6464
services.AddSingleton<GhostDraw.Tools.LineTool>();
6565
services.AddSingleton<GhostDraw.Tools.EraserTool>();
6666
services.AddSingleton<GhostDraw.Tools.RectangleTool>();
6767
services.AddSingleton<GhostDraw.Tools.CircleTool>();
68-
68+
6969
services.AddSingleton<OverlayWindow>();
70+
services.AddSingleton<IOverlayWindow>(sp => sp.GetRequiredService<OverlayWindow>());
7071
services.AddSingleton<GlobalKeyboardHook>();
7172
services.AddSingleton<DrawingManager>();
7273
services.AddSingleton<LoggingSettingsService>();

Src/GhostDraw/Managers/DrawingManager.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace GhostDraw.Managers;
88
public class DrawingManager
99
{
1010
private readonly ILogger<DrawingManager> _logger;
11-
private readonly OverlayWindow _overlayWindow;
11+
private readonly IOverlayWindow _overlayWindow;
1212
private readonly AppSettingsService _appSettings;
1313
private readonly ScreenshotService _screenshotService;
1414
private readonly GlobalKeyboardHook _keyboardHook;
@@ -19,7 +19,7 @@ public class DrawingManager
1919

2020
public bool IsDrawingMode => _overlayWindow.IsVisible || _isDrawingLocked;
2121

22-
public DrawingManager(ILogger<DrawingManager> logger, OverlayWindow overlayWindow,
22+
public DrawingManager(ILogger<DrawingManager> logger, IOverlayWindow overlayWindow,
2323
AppSettingsService appSettings, ScreenshotService screenshotService,
2424
GlobalKeyboardHook keyboardHook)
2525
{
@@ -50,7 +50,7 @@ public void EnableDrawing()
5050
_isDrawingLocked = false;
5151
_overlayWindow.DisableDrawing();
5252
_overlayWindow.Hide();
53-
53+
5454
// Notify hook that drawing mode is inactive
5555
_keyboardHook.SetDrawingModeActive(false);
5656
}
@@ -62,7 +62,7 @@ public void EnableDrawing()
6262
_overlayWindow.Show();
6363
_overlayWindow.Activate();
6464
_overlayWindow.Focus();
65-
65+
6666
// Notify hook that drawing mode is active
6767
_keyboardHook.SetDrawingModeActive(true);
6868
}
@@ -75,7 +75,7 @@ public void EnableDrawing()
7575
_overlayWindow.Show();
7676
_overlayWindow.Activate();
7777
_overlayWindow.Focus();
78-
78+
7979
// Notify hook that drawing mode is active
8080
_keyboardHook.SetDrawingModeActive(true);
8181
}
@@ -121,10 +121,10 @@ public void DisableDrawing()
121121
_logger.LogInformation("Disabling drawing mode (hold released)");
122122
_overlayWindow.DisableDrawing();
123123
_overlayWindow.Hide();
124-
124+
125125
// Notify hook that drawing mode is inactive
126126
_keyboardHook.SetDrawingModeActive(false);
127-
127+
128128
_logger.LogDebug("Overlay hidden");
129129
}
130130
catch (Exception ex)
@@ -149,21 +149,21 @@ public void ForceDisableDrawing()
149149
try
150150
{
151151
_logger.LogInformation("ESC pressed - checking help visibility");
152-
152+
153153
// Check if help is visible and handle accordingly
154154
bool shouldExitDrawingMode = _overlayWindow.HandleEscapeKey();
155-
155+
156156
if (shouldExitDrawingMode)
157157
{
158158
// Help was not visible, or user wants to exit - force disable drawing mode
159159
_logger.LogDebug("Force disabling drawing mode");
160160
_isDrawingLocked = false;
161161
_overlayWindow.DisableDrawing();
162162
_overlayWindow.Hide();
163-
163+
164164
// Notify hook that drawing mode is inactive
165165
_keyboardHook.SetDrawingModeActive(false);
166-
166+
167167
_logger.LogDebug("Drawing mode force disabled");
168168
}
169169
else
@@ -200,7 +200,7 @@ public void DisableDrawingMode()
200200
_isDrawingLocked = false;
201201
_overlayWindow.DisableDrawing();
202202
_overlayWindow.Hide();
203-
203+
204204
// Notify hook that drawing mode is inactive
205205
_keyboardHook.SetDrawingModeActive(false);
206206
}
@@ -418,13 +418,13 @@ public void CaptureFullScreenshot()
418418
{
419419
_logger.LogInformation("====== CaptureFullScreenshot CALLED ======");
420420
_logger.LogInformation("Overlay visible: {IsVisible}", _overlayWindow.IsVisible);
421-
421+
422422
if (_overlayWindow.IsVisible)
423423
{
424424
_logger.LogInformation("Capturing full screenshot (Ctrl+S) - calling ScreenshotService");
425-
var filePath = _screenshotService.CaptureFullScreen(_overlayWindow);
425+
var filePath = _screenshotService.CaptureFullScreen();
426426
_logger.LogInformation("ScreenshotService returned file path: {FilePath}", filePath ?? "(null)");
427-
427+
428428
if (filePath != null)
429429
{
430430
_logger.LogInformation("Screenshot saved successfully, showing toast notification");

Src/GhostDraw/Services/ScreenshotService.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ public ScreenshotService(ILogger<ScreenshotService> logger, AppSettingsService a
2424
}
2525

2626
/// <summary>
27-
/// Captures a fullscreen screenshot including the overlay drawing
27+
/// Captures a fullscreen screenshot including the overlay drawing.
28+
/// The capture is taken for the entire virtual screen and does not
29+
/// depend on a specific window instance.
2830
/// </summary>
29-
/// <param name="overlayWindow">The overlay window containing the drawing</param>
3031
/// <returns>Path to saved screenshot file, or null if failed</returns>
31-
public string? CaptureFullScreen(Window overlayWindow)
32+
public string? CaptureFullScreen()
3233
{
3334
try
3435
{
@@ -42,13 +43,13 @@ public ScreenshotService(ILogger<ScreenshotService> logger, AppSettingsService a
4243
(int)SystemParameters.VirtualScreenWidth,
4344
(int)SystemParameters.VirtualScreenHeight);
4445

45-
_logger.LogInformation("Screen bounds: X={X}, Y={Y}, Width={Width}, Height={Height}",
46+
_logger.LogInformation("Screen bounds: X={X}, Y={Y}, Width={Width}, Height={Height}",
4647
bounds.X, bounds.Y, bounds.Width, bounds.Height);
4748

4849
// Create bitmap for screen capture
4950
_logger.LogInformation("Creating bitmap for screen capture");
5051
using var bitmap = new System.Drawing.Bitmap(bounds.Width, bounds.Height);
51-
52+
5253
_logger.LogInformation("Copying screen to bitmap using Graphics.CopyFromScreen");
5354
using (var graphics = System.Drawing.Graphics.FromImage(bitmap))
5455
{
@@ -67,7 +68,7 @@ public ScreenshotService(ILogger<ScreenshotService> logger, AppSettingsService a
6768
var settings = _appSettings.CurrentSettings;
6869
_logger.LogInformation("Screenshot settings - CopyToClipboard: {Copy}, OpenFolder: {Open}, PlaySound: {Sound}",
6970
settings.CopyScreenshotToClipboard, settings.OpenFolderAfterScreenshot, settings.PlayShutterSound);
70-
71+
7172
// Copy to clipboard if enabled
7273
if (settings.CopyScreenshotToClipboard)
7374
{
@@ -114,7 +115,7 @@ public ScreenshotService(ILogger<ScreenshotService> logger, AppSettingsService a
114115
_logger.LogInformation("====== SaveScreenshot CALLED ======");
115116
var settings = _appSettings.CurrentSettings;
116117
var savePath = settings.ScreenshotSavePath;
117-
118+
118119
_logger.LogInformation("Screenshot save path from settings: {SavePath}", savePath);
119120
_logger.LogInformation("Bitmap dimensions: {Width}x{Height}", bitmap.Width, bitmap.Height);
120121

@@ -134,15 +135,15 @@ public ScreenshotService(ILogger<ScreenshotService> logger, AppSettingsService a
134135
var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
135136
var fileName = $"GhostDraw_{timestamp}.png";
136137
var filePath = Path.Combine(savePath, fileName);
137-
138+
138139
_logger.LogInformation("Generated filename: {FileName}", fileName);
139140
_logger.LogInformation("Full file path: {FilePath}", filePath);
140141

141142
// Save as PNG
142143
_logger.LogInformation("Saving bitmap to file as PNG...");
143144
bitmap.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
144145
_logger.LogInformation("Bitmap saved successfully!");
145-
146+
146147
// Verify file exists
147148
if (File.Exists(filePath))
148149
{
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using GhostDraw.Core;
2+
3+
namespace GhostDraw.Views;
4+
5+
/// <summary>
6+
/// Abstraction over the overlay window to make drawing logic testable
7+
/// without depending on WPF-specific implementation details.
8+
/// </summary>
9+
public interface IOverlayWindow
10+
{
11+
bool IsVisible { get; }
12+
bool IsActive { get; }
13+
bool IsFocused { get; }
14+
15+
void EnableDrawing();
16+
void DisableDrawing();
17+
void Show();
18+
void Hide();
19+
bool Activate();
20+
bool Focus();
21+
22+
void OnToolChanged(DrawTool newTool);
23+
void ClearCanvas();
24+
void ToggleHelp();
25+
26+
/// <summary>
27+
/// Handles ESC key press. Returns true if drawing mode should exit,
28+
/// false if only the help overlay was closed.
29+
/// </summary>
30+
bool HandleEscapeKey();
31+
32+
void ShowScreenshotSaved();
33+
}

Src/GhostDraw/Views/OverlayWindow.xaml.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace GhostDraw.Views;
1515

16-
public partial class OverlayWindow : Window
16+
public partial class OverlayWindow : Window, IOverlayWindow
1717
{
1818
private readonly ILogger<OverlayWindow> _logger;
1919
private readonly AppSettingsService _appSettings;
@@ -53,7 +53,7 @@ public partial class OverlayWindow : Window
5353
/// Duration for help popup fade-out animation (used when hiding help)
5454
/// </summary>
5555
private readonly TimeSpan _helpFadeOutDuration = TimeSpan.FromMilliseconds(500);
56-
56+
5757
/// <summary>
5858
/// Duration for help popup fade-in animation (used when showing help)
5959
/// </summary>
@@ -169,10 +169,10 @@ public void EnableDrawing()
169169
_activeTool.OnThicknessChanged(settings.BrushThickness);
170170

171171
UpdateCursor();
172-
172+
173173
// Show the drawing mode hint
174174
ShowDrawingModeHint();
175-
175+
176176
_logger.LogDebug("IsHitTestVisible={IsHitTestVisible}, Focusable={Focusable}",
177177
IsHitTestVisible, Focusable);
178178
}
@@ -207,7 +207,7 @@ private void UpdateCursor()
207207
try
208208
{
209209
var settings = _appSettings.CurrentSettings;
210-
210+
211211
this.Cursor = settings.ActiveTool switch
212212
{
213213
DrawTool.Pen => _cursorHelper.CreateColoredPencilCursor(settings.ActiveBrush),
@@ -217,7 +217,7 @@ private void UpdateCursor()
217217
DrawTool.Circle => _cursorHelper.CreateCircleCursor(settings.ActiveBrush),
218218
_ => WpfCursors.Cross
219219
};
220-
220+
221221
_logger.LogDebug("Updated cursor for tool {Tool} with color {Color}", settings.ActiveTool, settings.ActiveBrush);
222222
}
223223
catch (Exception ex)
@@ -240,7 +240,7 @@ private void OverlayWindow_MouseLeftButtonDown(object sender, MouseButtonEventAr
240240
private void OverlayWindow_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
241241
{
242242
_logger.LogDebug("MouseLeftButtonUp - isDrawing:{IsDrawing}", _isDrawing);
243-
243+
244244
if (_isDrawing && _activeTool != null)
245245
{
246246
var position = e.GetPosition(DrawingCanvas);
@@ -337,7 +337,7 @@ public void OnToolChanged(DrawTool newTool)
337337
_activeTool.Cancel(DrawingCanvas);
338338
_activeTool.OnDeactivated();
339339
}
340-
340+
341341
// Switch to new tool
342342
_activeTool = newTool switch
343343
{
@@ -354,10 +354,10 @@ public void OnToolChanged(DrawTool newTool)
354354
_activeTool.OnActivated();
355355
_activeTool.OnColorChanged(settings.ActiveBrush);
356356
_activeTool.OnThicknessChanged(settings.BrushThickness);
357-
357+
358358
// Update cursor for new tool
359359
UpdateCursor();
360-
360+
361361
_logger.LogInformation("Tool changed to {Tool}", newTool);
362362
}
363363
catch (Exception ex)
@@ -460,7 +460,7 @@ public void ClearCanvas()
460460
try
461461
{
462462
int strokeCount = DrawingCanvas.Children.Count;
463-
463+
464464
if (strokeCount > 0)
465465
{
466466
_logger.LogInformation("Clearing {StrokeCount} strokes from canvas via R key", strokeCount);
@@ -470,7 +470,7 @@ public void ClearCanvas()
470470
{
471471
_logger.LogDebug("Canvas already empty, clear acknowledged");
472472
}
473-
473+
474474
// Always show feedback so user knows R key was recognized
475475
ShowCanvasClearedToast();
476476
}
@@ -686,7 +686,7 @@ private void ShowHelp()
686686
};
687687

688688
HelpPopupBorder.BeginAnimation(OpacityProperty, fadeInAnimation);
689-
689+
690690
_isHelpVisible = true;
691691

692692
_logger.LogDebug("Help popup shown (toggle mode)");
@@ -717,7 +717,7 @@ private void HideHelp()
717717
};
718718

719719
HelpPopupBorder.BeginAnimation(OpacityProperty, fadeOutAnimation);
720-
720+
721721
_isHelpVisible = false;
722722

723723
_logger.LogDebug("Help popup hidden");

0 commit comments

Comments
 (0)