Widow

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 10  →  ?path2? @ 11
/trunk/Widow/WindowManipulation.cs
@@ -1,6 +1,9 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using Windows;
using Widow.Properties;
 
namespace Widow
@@ -7,6 +10,12 @@
{
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; }
@@ -21,12 +30,18 @@
 
private MainForm Form { get; }
 
private Windows.Windows Windows { get; }
private Windows.Windows WindowsToManipulate { get; }
 
private CancellationTokenSource CancellationTokenSource { get; set; }
 
private Task ApplyEveryTask { get; }
 
private BufferBlock<Window> WindowsBufferBlock { get; }
 
private ActionBlock<Window> WindowsActionBlock { get; }
 
private IDisposable WindowsLink { get; set; }
 
#endregion
 
#region Constructors, Destructors and Finalizers
@@ -34,6 +49,10 @@
public WindowManipulation()
{
CancellationTokenSource = new CancellationTokenSource();
WindowsBufferBlock = new BufferBlock<Window>();
WindowsActionBlock = new ActionBlock<Window>(ManipulateWindows);
WindowsLink = WindowsBufferBlock.LinkTo(WindowsActionBlock);
 
ApplyEveryTask = ApplyEvery(CancellationTokenSource.Token);
}
 
@@ -44,13 +63,13 @@
Form.WindowCreated += Form_WindowCreated;
}
 
public WindowManipulation(MainForm mainForm, Windows.Windows windows) : this(mainForm)
public WindowManipulation(MainForm mainForm, Windows.Windows windowsToManipulate) : this(mainForm)
{
Windows = windows;
WindowsToManipulate = windowsToManipulate;
}
 
public WindowManipulation(MainForm mainForm, Windows.Windows windows, Settings settings) : this(mainForm,
windows)
public WindowManipulation(MainForm mainForm, Windows.Windows windowsToManipulate, Settings settings) : this(mainForm,
windowsToManipulate)
{
OnWindowCreate = settings.OnWindowCreate;
ApplyEveryTimeEnabled = settings.ApplyEveryTimeEnabled;
@@ -62,6 +81,9 @@
 
public void Dispose()
{
WindowsLink?.Dispose();
WindowsLink = null;
 
Form.WindowCreated -= Form_WindowCreated;
CancellationTokenSource.Cancel();
ApplyEveryTask.Wait();
@@ -73,7 +95,7 @@
 
#region Event Handlers
 
private void Form_WindowCreated(object sender, WindowCreatedEventArgs e)
private async void Form_WindowCreated(object sender, WindowCreatedEventArgs e)
{
if (!OnWindowCreate)
{
@@ -80,12 +102,12 @@
return;
}
 
if (!Windows.Contains(e.Name))
if (!WindowsToManipulate.Contains(e.Title))
{
return;
}
 
Apply();
await Apply();
}
 
#endregion
@@ -92,11 +114,19 @@
 
#region Public Methods
 
public void Apply()
public async Task Apply()
{
foreach (var window in Windows.Window)
await Task.WhenAll(WindowsToManipulate.Window.Select(window => WindowsBufferBlock.SendAsync(window)));
}
 
#endregion
 
#region Private Methods
 
private void ManipulateWindows(Window window)
{
foreach (var hWnd in Helpers.FindWindowsWithText(window.Title))
{
var hWnd = Helpers.FindWindowByTitle(window.Name);
if (hWnd == IntPtr.Zero)
{
continue;
@@ -131,14 +161,12 @@
height = rect.Top - rect.Bottom;
}
 
Natives.MoveWindow(hWnd, left, top, width, height, true);
var success = Natives.MoveWindow(hWnd, left, top, width, height, true);
 
WindowManipulated?.Invoke(this, new WindowManipulatedEventArgs(success, window));
}
}
 
#endregion
 
#region Private Methods
 
private async Task ApplyEvery(CancellationToken cancellationToken)
{
do
@@ -151,7 +179,7 @@
 
await Task.Delay(ApplyEveryTime, cancellationToken);
 
Apply();
await Apply();
} while (!cancellationToken.IsCancellationRequested);
}