Widow

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 13  →  ?path2? @ 14
/trunk/Widow/WindowManipulation.cs
@@ -11,12 +11,6 @@
{
public class WindowManipulation : IDisposable
{
#region Public Events & Delegates
 
public event EventHandler<WindowManipulatedEventArgs> WindowManipulated;
 
#endregion
 
#region Public Enums, Properties and Fields
 
public bool OnWindowCreate { get; set; }
@@ -37,9 +31,9 @@
 
private Task ApplyEveryTask { get; }
 
private BufferBlock<Window> WindowsBufferBlock { get; }
private BufferBlock<WindowModification> WindowsBufferBlock { get; }
 
private ActionBlock<Window> WindowsActionBlock { get; }
private ActionBlock<WindowModification> WindowsActionBlock { get; }
 
private IDisposable WindowsLink { get; set; }
 
@@ -50,8 +44,8 @@
public WindowManipulation()
{
CancellationTokenSource = new CancellationTokenSource();
WindowsBufferBlock = new BufferBlock<Window>();
WindowsActionBlock = new ActionBlock<Window>(ManipulateWindows);
WindowsBufferBlock = new BufferBlock<WindowModification>();
WindowsActionBlock = new ActionBlock<WindowModification>(ManipulateWindows);
WindowsLink = WindowsBufferBlock.LinkTo(WindowsActionBlock);
 
ApplyEveryTask = ApplyEvery(CancellationTokenSource.Token);
@@ -104,7 +98,8 @@
return;
}
 
if (!WindowsToManipulate.TryGetWindow(e.Title, out _))
var @class = Helpers.GetWindowClass(e.Handle);
if (!WindowsToManipulate.TryGetWindow(new WindowHash(e.Title, @class), out _))
{
return;
}
@@ -118,25 +113,32 @@
 
public async Task Apply()
{
var windows = new List<Window>();
foreach (var hWnd in Helpers.EnumerateWindows())
var windows = new List<WindowModification>();
foreach (var hWnd in Helpers.FindWindows((wnd, param) => true))
{
var title = Helpers.GetWindowTitle(hWnd);
var windowTitle = Helpers.GetWindowTitle(hWnd);
 
if (string.IsNullOrEmpty(title))
if (string.IsNullOrEmpty(windowTitle))
{
continue;
}
 
if (!WindowsToManipulate.TryGetWindow(title, out var window))
var windowClass = Helpers.GetWindowClass(hWnd);
if (string.IsNullOrEmpty(windowClass))
{
continue;
}
 
windows.Add(window);
var windowHash = new WindowHash(windowTitle, windowClass);
if (!WindowsToManipulate.TryGetWindow(windowHash, out var window))
{
continue;
}
 
windows.Add(new WindowModification(hWnd, window));
}
 
await Task.WhenAll(windows.Select(window => WindowsBufferBlock.SendAsync(window)));
await Task.WhenAll(windows.Select(modification => WindowsBufferBlock.SendAsync(modification)));
}
 
#endregion
@@ -143,48 +145,43 @@
 
#region Private Methods
 
private void ManipulateWindows(Window window)
private static void ManipulateWindows(WindowModification modification)
{
foreach (var hWnd in Helpers.FindWindowsWithText(window.Title))
if (modification.Handle == IntPtr.Zero)
{
if (hWnd == IntPtr.Zero)
{
continue;
}
return;
}
 
if (!Natives.GetWindowRect(hWnd, out var rect))
{
continue;
}
if (!Natives.GetWindowRect(modification.Handle, out var rect))
{
return;
}
 
var left = window.Left;
if (window.IgnoreLeft)
{
left = rect.Left;
}
var left = modification.Window.Left;
if (modification.Window.IgnoreLeft)
{
left = rect.Left;
}
 
var top = window.Top;
if (window.IgnoreTop)
{
top = rect.Top;
}
var top = modification.Window.Top;
if (modification.Window.IgnoreTop)
{
top = rect.Top;
}
 
var width = window.Width;
if (window.IgnoreWidth)
{
width = rect.Left - rect.Right;
}
var width = modification.Window.Width;
if (modification.Window.IgnoreWidth)
{
width = rect.Left - rect.Right;
}
 
var height = window.Height;
if (window.IgnoreHeight)
{
height = rect.Top - rect.Bottom;
}
var height = modification.Window.Height;
if (modification.Window.IgnoreHeight)
{
height = rect.Top - rect.Bottom;
}
 
var success = Natives.MoveWindow(hWnd, left, top, width, height, true);
 
WindowManipulated?.Invoke(this, new WindowManipulatedEventArgs(success, window));
}
Natives.MoveWindow(modification.Handle, left, top, width, height, true);
}
 
private async Task ApplyEvery(CancellationToken cancellationToken)