Widow – Rev 1

Subversion Repositories:
Rev:
using System;
using System.ComponentModel;
using System.Configuration;
using System.Threading.Tasks;
using System.Windows.Forms;
using Widow.Properties;
using Widow.Serialization;

namespace Widow
{
    public partial class Form1 : Form
    {
        #region Public Events & Delegates

        public event EventHandler<WindowCreatedEventArgs> WindowCreated;

        public event EventHandler<WindowDestroyedEventArgs> WindowDestroyed;

        #endregion

        #region Public Enums, Properties and Fields

        public Windows.Windows Windows { get; set; }

        public RuleEditForm RuleEditForm { get; set; }

        public Apply Apply { get; set; }

        #endregion

        #region Constructors, Destructors and Finalizers

        public Form1()
        {
            InitializeComponent();
            // Bind to settings changed event.
            Settings.Default.SettingsLoaded += Default_SettingsLoaded;
            Settings.Default.SettingsSaving += Default_SettingsSaving;
            Settings.Default.PropertyChanged += Default_PropertyChanged;

            Natives.RegisterShellHookWindow(Handle);

            LoadWindows().ContinueWith(task =>
            {
                Apply = new Apply(this, Windows);
            });
        }

        /// <summary>
        ///     Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && components != null)
            {
                Settings.Default.SettingsLoaded -= Default_SettingsLoaded;
                Settings.Default.SettingsSaving -= Default_SettingsSaving;
                Settings.Default.PropertyChanged -= Default_PropertyChanged;

                components.Dispose();
            }

            base.Dispose(disposing);
        }

        #endregion

        #region Private Overrides

        protected override void WndProc(ref Message m)
        {
            var handle = m.LParam;
            var windowName = Helpers.GetWindowTitle(handle);

            switch (m.WParam.ToInt32())
            {
                case (int) Natives.WM.HSHELL_WINDOWCREATED:
                    WindowCreated?.Invoke(this, new WindowCreatedEventArgs(windowName, handle));
                    break;
                case (int) Natives.WM.HSHELL_WINDOWDESTROYED:
                    WindowDestroyed?.Invoke(this, new WindowDestroyedEventArgs(windowName, handle));
                    break;
            }

            base.WndProc(ref m);
        }

        #endregion

        #region Event Handlers

        private void Default_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Settings.Default.Save();
        }

        private void Default_SettingsSaving(object sender, CancelEventArgs e)
        {
        }

        private void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e)
        {
        }

        private void NewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RuleEditForm = new RuleEditForm(this, Windows);
            RuleEditForm.Closed += RuleEditForm_Closed;
            RuleEditForm.Show();
        }

        private async void RuleEditForm_Closed(object sender, EventArgs e)
        {
            RuleEditForm.Closed -= RuleEditForm_Closed;
            RuleEditForm.Dispose();
            RuleEditForm = null;

            switch (await WindowsSerialization.Serialize(Windows, "Windows.xml"))
            {
                case SerializationSuccess serializationSuccess:
                    break;
                case SerializationFailure serializationFailure:
                    break;
            }
        }

        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void LaunchOnBootToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
        {
            Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked;

            LaunchOnBoot.Set(Settings.Default.LaunchOnBoot);
        }

        #endregion

        #region Private Methods

        private async Task LoadWindows()
        {
            switch (await WindowsSerialization.Deserialize("Windows.xml"))
            {
                case SerializationSuccess serializationSuccess:
                    Windows = serializationSuccess.Windows;
                    break;
                case SerializationFailure serializationFailure:
                    Windows = new Windows.Windows();
                    break;
            }
        }

        #endregion

        private void OnWindowCreateToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
        {
            Settings.Default.OnWindowCreate = ((ToolStripMenuItem) sender).Checked;

            Apply.OnWindowCreate = Settings.Default.OnWindowCreate;
        }
    }
}

Generated by GNU Enscript 1.6.5.90.