Korero – Rev 1

Subversion Repositories:
Rev:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Korero.Notifications
{
    public class NotificationManager : IDisposable
    {
        #region Private Delegates, Events, Enums, Properties, Indexers and Fields

        private readonly TaskScheduler _uiScheduler;

        private List<NotificationForm> _notificationList;

        private Rectangle _screenWorkingArea;

        #endregion

        #region Constructors, Destructors and Finalizers

        public NotificationManager(TaskScheduler uiScheduler) : this()
        {
            _uiScheduler = uiScheduler;
        }

        private NotificationManager()
        {
            UpdateScreenResolution();

            SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
        }

        public void Dispose()
        {
            SystemEvents.DisplaySettingsChanged -= SystemEvents_DisplaySettingsChanged;
        }

        #endregion

        #region Event Handlers

        private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
        {
            UpdateScreenResolution();
        }

        private void NotificationForm_Closing(object sender, CancelEventArgs e)
        {
            var notificationForm = (NotificationForm) sender;
            notificationForm.Closing += NotificationForm_Closing;
            _notificationList[notificationForm.Index] = null;
        }

        #endregion

        #region Public Methods

        public void UpdateScreenResolution()
        {
            _notificationList = new List<NotificationForm>();

            _screenWorkingArea = Screen.GetWorkingArea(new Point(0, 0));

            using (var notificationForm = new NotificationForm())
            {
                var items = _screenWorkingArea.Bottom / notificationForm.Height;
                foreach (var _ in Enumerable.Range(0, items))
                {
                    _notificationList.Add(null);
                }
            }
        }

        public void ShowNotification(string title, string message, int timeout)
        {
            Task.Factory.StartNew(() =>
            {
                var i = _notificationList.FindIndex(item => item == null);
                if (i == -1)
                {
                    return;
                }

                var notificationForm =
                    new NotificationForm(i, title, message, timeout);

                _notificationList[i] = notificationForm;

                var notificationX = _screenWorkingArea.Right - notificationForm.Width;
                var notificationY = _screenWorkingArea.Bottom - (i + 1) * notificationForm.Height;
                notificationForm.UpdateLocation(notificationX, notificationY);

                notificationForm.Closing += NotificationForm_Closing;
                notificationForm.Show();
            }, CancellationToken.None, TaskCreationOptions.None, _uiScheduler);
        }

        public void ShowNotification(string title, string message, UnmanagedMemoryStream sound, int timeout)
        {
            Task.Factory.StartNew(() =>
            {
                var i = _notificationList.FindIndex(item => item == null);
                if (i == -1)
                {
                    return;
                }

                var notificationForm =
                    new NotificationForm(i, title, message, timeout, sound);

                _notificationList[i] = notificationForm;

                var notificationX = _screenWorkingArea.Right - notificationForm.Width;
                var notificationY = _screenWorkingArea.Bottom - (i + 1) * notificationForm.Height;
                notificationForm.UpdateLocation(notificationX, notificationY);

                notificationForm.Closing += NotificationForm_Closing;
                notificationForm.Show();
            }, CancellationToken.None, TaskCreationOptions.None, _uiScheduler);
        }

        public void ShowNotification(string title, string message, UnmanagedMemoryStream sound, int timeout,
                                     Action action)
        {
            Task.Factory.StartNew(() =>
            {
                var i = _notificationList.FindIndex(item => item == null);
                if (i == -1)
                {
                    return;
                }

                var notificationForm =
                    new NotificationForm(i, title, message, timeout, sound, action);

                _notificationList[i] = notificationForm;

                var notificationX = _screenWorkingArea.Right - notificationForm.Width;
                var notificationY = _screenWorkingArea.Bottom - (i + 1) * notificationForm.Height;
                notificationForm.UpdateLocation(notificationX, notificationY);

                notificationForm.Closing += NotificationForm_Closing;
                notificationForm.Show();
            }, CancellationToken.None, TaskCreationOptions.None, _uiScheduler);
        }

        #endregion
    }
}

Generated by GNU Enscript 1.6.5.90.