Widow – Rev 12

Subversion Repositories:
Rev:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using Windows;
using Widow.Properties;

namespace Widow
{
    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; }

        public int ApplyEveryTime { get; set; }

        public bool ApplyEveryTimeEnabled { get; set; }

        #endregion

        #region Private Delegates, Events, Enums, Properties, Indexers and Fields

        private MainForm Form { 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

        public WindowManipulation()
        {
            CancellationTokenSource = new CancellationTokenSource();
            WindowsBufferBlock = new BufferBlock<Window>();
            WindowsActionBlock = new ActionBlock<Window>(ManipulateWindows);
            WindowsLink = WindowsBufferBlock.LinkTo(WindowsActionBlock);

            ApplyEveryTask = ApplyEvery(CancellationTokenSource.Token);
        }

        public WindowManipulation(MainForm mainForm) : this()
        {
            Form = mainForm;

            Form.WindowCreated += Form_WindowCreated;
        }

        public WindowManipulation(MainForm mainForm, Windows.Windows windowsToManipulate) : this(mainForm)
        {
            WindowsToManipulate = windowsToManipulate;
        }

        public WindowManipulation(MainForm mainForm, Windows.Windows windowsToManipulate, Settings settings) : this(
            mainForm,
            windowsToManipulate)
        {
            OnWindowCreate = settings.OnWindowCreate;
            ApplyEveryTimeEnabled = settings.ApplyEveryTimeEnabled;
            if (int.TryParse(settings.ApplyEveryTime, out var time))
            {
                ApplyEveryTime = time;
            }
        }

        public void Dispose()
        {
            WindowsLink?.Dispose();
            WindowsLink = null;

            Form.WindowCreated -= Form_WindowCreated;
            CancellationTokenSource.Cancel();
            ApplyEveryTask.Wait();
            CancellationTokenSource.Dispose();
            CancellationTokenSource = null;
        }

        #endregion

        #region Event Handlers

        private async void Form_WindowCreated(object sender, WindowCreatedEventArgs e)
        {
            if (!OnWindowCreate)
            {
                return;
            }

            if (!WindowsToManipulate.TryGetWindow(e.Title, out _))
            {
                return;
            }

            await Apply();
        }

        #endregion

        #region Public Methods

        public async Task Apply()
        {
            var windows = new List<Window>();
            foreach (var hWnd in Helpers.EnumerateWindows())
            {
                var title = Helpers.GetWindowTitle(hWnd);

                if (string.IsNullOrEmpty(title))
                {
                    continue;
                }

                if (!WindowsToManipulate.TryGetWindow(title, out var window))
                {
                    continue;
                }

                windows.Add(window);
            }

            await Task.WhenAll(windows.Select(window => WindowsBufferBlock.SendAsync(window)));
        }

        #endregion

        #region Private Methods

        private void ManipulateWindows(Window window)
        {
            foreach (var hWnd in Helpers.FindWindowsWithText(window.Title))
            {
                if (hWnd == IntPtr.Zero)
                {
                    continue;
                }

                if (!Natives.GetWindowRect(hWnd, out var rect))
                {
                    continue;
                }

                var left = window.Left;
                if (window.IgnoreLeft)
                {
                    left = rect.Left;
                }

                var top = window.Top;
                if (window.IgnoreTop)
                {
                    top = rect.Top;
                }

                var width = window.Width;
                if (window.IgnoreWidth)
                {
                    width = rect.Left - rect.Right;
                }

                var height = window.Height;
                if (window.IgnoreHeight)
                {
                    height = rect.Top - rect.Bottom;
                }

                var success = Natives.MoveWindow(hWnd, left, top, width, height, true);

                WindowManipulated?.Invoke(this, new WindowManipulatedEventArgs(success, window));
            }
        }

        private async Task ApplyEvery(CancellationToken cancellationToken)
        {
            do
            {
                if (!ApplyEveryTimeEnabled)
                {
                    await Task.Delay(1000, cancellationToken);
                    continue;
                }

                await Task.Delay(ApplyEveryTime, cancellationToken);

                await Apply();
            } while (!cancellationToken.IsCancellationRequested);
        }

        #endregion
    }
}

Generated by GNU Enscript 1.6.5.90.