Widow – Rev 8

Subversion Repositories:
Rev:
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Widow
{
    public class WindowRescale : IDisposable
    {
        #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 Windows { get; }

        private CancellationTokenSource CancellationTokenSource { get; set; }

        private Task ApplyEveryTask { get; }

        #endregion

        #region Constructors, Destructors and Finalizers

        public WindowRescale()
        {
            CancellationTokenSource = new CancellationTokenSource();
            ApplyEveryTask = ApplyEvery(CancellationTokenSource.Token);
        }

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

            Form.WindowCreated += Form_WindowCreated;
        }

        public WindowRescale(MainForm mainForm, Windows.Windows windows) : this(mainForm)
        {
            Windows = windows;
        }

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

        #endregion

        #region Event Handlers

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

            foreach (var window in Windows.Window)
            {
                if (window.Name != e.Name)
                {
                    continue;
                }

                var hWnd = Helpers.FindWindowByTitle(window.Name);
                if (hWnd == IntPtr.Zero)
                {
                    continue;
                }

                Natives.MoveWindow(hWnd, window.Left, window.Top, window.Width, window.Height, true);
            }
        }

        #endregion

        #region Public Methods

        public void Apply()
        {
            foreach (var window in Windows.Window)
            {
                var hWnd = Helpers.FindWindowByTitle(window.Name);
                if (hWnd == IntPtr.Zero)
                {
                    continue;
                }

                Natives.MoveWindow(hWnd, window.Left, window.Top, window.Width, window.Height, true);
            }
        }

        #endregion

        #region Private Methods

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

                await Task.Delay(ApplyEveryTime, cancellationToken);

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

        #endregion
    }
}

Generated by GNU Enscript 1.6.5.90.