Skip to content

Commit

Permalink
Added ActionDisposable, renamed Rect to RECT
Browse files Browse the repository at this point in the history
  • Loading branch information
Roemer committed Dec 8, 2019
1 parent ed85278 commit 38e4ec8
Show file tree
Hide file tree
Showing 7 changed files with 2,431 additions and 2,415 deletions.
31 changes: 31 additions & 0 deletions src/FlaUI.Core/ActionDisposable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Threading;

namespace FlaUI.Core
{
/// <summary>
/// An <see cref="IDisposable"/> implementation that executes an <see cref="Action"/> upon disposal.
/// </summary>
public class ActionDisposable : IDisposable
{
private volatile Action disposeAction;

/// <summary>
/// Constructs a new disposable with the given action used for disposal.
/// </summary>
/// <param name="disposeAction">The action that is called upon disposal.</param>
public ActionDisposable(Action disposeAction)
{
this.disposeAction = disposeAction;
}

/// <summary>
/// Calls the defined <see cref="Action"/>.
/// </summary>
public void Dispose()
{
// Set the action to null to make sure it is only called once
Interlocked.Exchange(ref disposeAction, null)?.Invoke();
}
}
}
6 changes: 3 additions & 3 deletions src/FlaUI.Core/Capturing/Capture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ public static CaptureImage Rectangle(Rectangle bounds, CaptureSettings settings
private static Rectangle GetBoundsByScreenIndex(int screenIndex)
{
var monitors = new List<MonitorInfo>();
User32.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, MonitorDelegate , IntPtr.Zero);
User32.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, MonitorDelegate, IntPtr.Zero);
var monitorRect = monitors[screenIndex].monitor;
return new Rectangle(monitorRect.left, monitorRect.top, monitorRect.right - monitorRect.left, monitorRect.bottom - monitorRect.top);
bool MonitorDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData)

bool MonitorDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData)
{
var mi = new MonitorInfo();
mi.size = (uint)Marshal.SizeOf(mi);
Expand Down
39 changes: 12 additions & 27 deletions src/FlaUI.Core/Input/Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,18 @@ public static void ReleaseVirtualKeyCode(ushort virtualKeyCode)
/// </summary>
public static IDisposable Pressing(params VirtualKeyShort[] virtualKeys)
{
return new KeyPressingActivation(virtualKeys);
foreach (var key in virtualKeys)
{
Press(key);
}

return new ActionDisposable(() =>
{
foreach (var key in virtualKeys.Reverse())
{
Release(key);
}
});
}

/// <summary>
Expand Down Expand Up @@ -257,31 +268,5 @@ private static void SendInput(ushort keyCode, bool isKeyDown, bool isScanCode, b
Logger.Default.Warn("Could not send keyboard input. ErrorCode: {0}", errorCode);
}
}

/// <summary>
/// Disposable class which presses the keys on creation
/// and disposes them on destruction.
/// </summary>
private class KeyPressingActivation : IDisposable
{
private readonly VirtualKeyShort[] _virtualKeys;

public KeyPressingActivation(VirtualKeyShort[] virtualKeys)
{
_virtualKeys = virtualKeys;
foreach (var key in _virtualKeys)
{
Press(key);
}
}

public void Dispose()
{
foreach (var key in _virtualKeys.Reverse())
{
Release(key);
}
}
}
}
}
Loading

0 comments on commit 38e4ec8

Please sign in to comment.