Toasts – Rev 29

Subversion Repositories:
Rev:
using System;
using System.Drawing;
using System.IO;
using System.Media;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
using Toasts;

namespace Test
{
    public partial class TestForm : Form
    {
        #region Private Delegates, Events, Enums, Properties, Indexers and Fields

        private bool _initialLoad = true;

        #endregion

        #region Constructors, Destructors and Finalizers

        public TestForm()
        {
            InitializeComponent();
            PopulateComboBoxes();
        }

        #endregion

        #region Event Handlers

        private void ButtonShowNotification_Click(object sender, EventArgs e)
        {
            ShowNotification();
        }

        private async void ComboBoxSound_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!_initialLoad)
            {
                await PlayNotificationSound($"Test.Sounds.{comboBoxSound.Text}.wav");
            }
        }

        #endregion

        #region Private Methods

        private void PopulateComboBoxes()
        {
            foreach (FormAnimator.AnimationMethod method in Enum.GetValues(typeof(FormAnimator.AnimationMethod)))
                comboBoxAnimation.Items.Add(method.ToString());

            comboBoxAnimation.SelectedIndex = 2;

            foreach (FormAnimator.AnimationDirection direction in Enum.GetValues(
                         typeof(FormAnimator.AnimationDirection)))
                comboBoxAnimationDirection.Items.Add(direction.ToString());

            comboBoxAnimationDirection.SelectedIndex = 3;

            var soundsFolder = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Sounds"));
            foreach (var file in soundsFolder.GetFiles())
                comboBoxSound.Items.Add(Path.GetFileNameWithoutExtension(file.FullName));

            comboBoxSound.SelectedIndex = 5;
            _initialLoad = false;

            comboBoxDuration.SelectedIndex = 0;
        }

        private async void ShowNotification()
        {
            int duration;
            int.TryParse(comboBoxDuration.SelectedItem.ToString(), out duration);
            if (duration <= 0) duration = -1;

            var animationMethod = FormAnimator.AnimationMethod.Slide;
            foreach (FormAnimator.AnimationMethod method in Enum.GetValues(typeof(FormAnimator.AnimationMethod)))
                if (Equals(method.ToString(), comboBoxAnimation.SelectedItem))
                {
                    animationMethod = method;
                    break;
                }

            var animationDirection = FormAnimator.AnimationDirection.Up;
            foreach (FormAnimator.AnimationDirection direction in Enum.GetValues(
                         typeof(FormAnimator.AnimationDirection)))
                if (Equals(direction.ToString(), comboBoxAnimationDirection.SelectedItem))
                {
                    animationDirection = direction;
                    break;
                }

            var imagesFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images");

            var backgroundFile = Path.Combine(imagesFolder, "background.png");
            var background = new Bitmap(backgroundFile);

            var logoFile = Path.Combine(imagesFolder, "was.png");
            var logo = new Bitmap(logoFile);

            var sound = await LoadResource($"Test.Sounds.{comboBoxSound.Text}.wav");

            var toastNotification = new ToastForm(textBoxTitle.Text, textBoxBody.Text);
            
            toastNotification.Show();
        }

        private static async Task<byte[]> LoadResource(string resource)
        {
            var assembly = Assembly.GetExecutingAssembly();

            using (var manifestResourceStream = assembly.GetManifestResourceStream(resource))
            {
                if (manifestResourceStream == null)
                {
                    return null;
                }

                var memoryStream = new MemoryStream();

                await manifestResourceStream.CopyToAsync(memoryStream);

                memoryStream.Position = 0L;

                return memoryStream.ToArray();
            }
        }

        private static async Task PlayNotificationSound(string sound)
        {
            var resource = await LoadResource(sound);

            using (var memoryStream = new MemoryStream(resource))
            {
                using (var player = new SoundPlayer(memoryStream))
                {
                    player.Play();
                }
            }
        }

        #endregion
    }
}

Generated by GNU Enscript 1.6.5.90.