/trunk/Winify/Form1.cs |
@@ -23,6 +23,8 @@ |
|
private GotifyConnectionManager _gotifyConnectionManager; |
|
private NotificationManager _notificationManager; |
|
private SettingsForm _settingsForm; |
|
#endregion |
@@ -49,7 +51,10 @@ |
_servers = new global::Servers.Servers(); |
_servers.Server.CollectionChanged += Server_CollectionChanged; |
|
_notificationManager = new NotificationManager(TaskScheduler.FromCurrentSynchronizationContext()); |
|
_gotifyConnectionManager = new GotifyConnectionManager(_servers); |
_gotifyConnectionManager.GotifyNotification += GotifyConnectionManager_GotifyNotification; |
|
Task.Run(async () => |
{ |
@@ -76,9 +81,13 @@ |
Settings.Default.SettingsSaving -= Default_SettingsSaving; |
Settings.Default.PropertyChanged -= Default_PropertyChanged; |
|
_gotifyConnectionManager.GotifyNotification -= GotifyConnectionManager_GotifyNotification; |
_gotifyConnectionManager?.Dispose(); |
_gotifyConnectionManager = null; |
|
_notificationManager?.Dispose(); |
_notificationManager = null; |
|
components.Dispose(); |
} |
|
@@ -89,6 +98,11 @@ |
|
#region Event Handlers |
|
private void GotifyConnectionManager_GotifyNotification(object sender, GotifyNotificationEventArgs e) |
{ |
_notificationManager.ShowNotification(e); |
} |
|
private async void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
{ |
switch (await ServersSerialization.Serialize(_servers, Constants.ServersFile)) |
/trunk/Winify/Gotify/GotifyConnectionManager.cs |
@@ -8,6 +8,12 @@ |
{ |
public class GotifyConnectionManager : IDisposable |
{ |
#region Public Events & Delegates |
|
public event EventHandler<GotifyNotificationEventArgs> GotifyNotification; |
|
#endregion |
|
#region Private Delegates, Events, Enums, Properties, Indexers and Fields |
|
private readonly ConcurrentDictionary<string, GotifyConnection> _gotifyConnections; |
@@ -70,10 +76,9 @@ |
} |
} |
|
private static void Connection_GotifyNotification(object sender, GotifyNotificationEventArgs e) |
private void Connection_GotifyNotification(object sender, GotifyNotificationEventArgs e) |
{ |
var notification = new NotificationForm(e.Image, e.Notification.Title, e.Notification.Message, 5000); |
notification.ShowDialog(); |
GotifyNotification?.Invoke(sender, e); |
} |
|
#endregion |
/trunk/Winify/NotificationForm.cs |
@@ -1,8 +1,6 @@ |
using System; |
using System.Diagnostics; |
using System.Drawing; |
using System.IO; |
using System.Media; |
using System.Threading.Tasks; |
using System.Windows.Forms; |
using Winify.Utilities; |
@@ -11,8 +9,16 @@ |
{ |
public partial class NotificationForm : Form |
{ |
#region Public Enums, Properties and Fields |
|
public int Index { get; } |
|
#endregion |
|
#region Private Delegates, Events, Enums, Properties, Indexers and Fields |
|
private Point _displayLocation; |
|
private Image _image; |
|
#endregion |
@@ -19,13 +25,15 @@ |
|
#region Constructors, Destructors and Finalizers |
|
private NotificationForm() |
public NotificationForm() |
{ |
InitializeComponent(); |
} |
|
public NotificationForm(Image image, string title, string text, int milliseconds) : this() |
public NotificationForm(int i, Image image, string title, string text, int milliseconds) : this() |
{ |
Index = i; |
|
_image = image; |
|
pictureBox1.InvokeIfRequired(pictureBox => { pictureBox.Image = image; }); |
@@ -37,16 +45,6 @@ |
this.InvokeIfRequired(form => { form.Close(); })); |
} |
|
public NotificationForm(Image image, string title, string text, Stream sound, int milliseconds) : this(image, |
title, text, |
milliseconds) |
{ |
using (var soundPlayer = new SoundPlayer(sound)) |
{ |
soundPlayer.Play(); |
} |
} |
|
/// <summary> |
/// Clean up any resources being used. |
/// </summary> |
@@ -75,8 +73,7 @@ |
|
protected override void OnLoad(EventArgs e) |
{ |
var screen = Screen.FromPoint(Location); |
Location = new Point(screen.WorkingArea.Right - Width, screen.WorkingArea.Bottom - Height); |
Location = _displayLocation; |
base.OnLoad(e); |
} |
|
@@ -109,5 +106,14 @@ |
} |
|
#endregion |
|
#region Public Methods |
|
public void UpdateLocation(int x, int y) |
{ |
_displayLocation = new Point(x, y); |
} |
|
#endregion |
} |
} |
/trunk/Winify/NotificationManager.cs |
@@ -0,0 +1,107 @@ |
using System; |
using System.Collections.Generic; |
using System.ComponentModel; |
using System.Drawing; |
using System.Linq; |
using System.Threading; |
using System.Threading.Tasks; |
using System.Windows.Forms; |
using Microsoft.Win32; |
using Winify.Gotify; |
|
namespace Winify |
{ |
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(GotifyNotificationEventArgs e) |
{ |
Task.Factory.StartNew(() => |
{ |
var i = _notificationList.FindIndex(item => item == null); |
if (i == -1) |
{ |
return; |
} |
|
var notificationForm = |
new NotificationForm(i, e.Image, e.Notification.Title, e.Notification.Message, 5000); |
|
_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 |
} |
} |