using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace BodyshopUploader.Utils.Growls { public partial class GrowlNotification { private const byte MAX_NOTIFICATIONS = 4; private int count; public Notifications Notifications = new Notifications(); private readonly Notifications buffer = new Notifications(); public ViewModels.MainViewModel ShellVm; public GrowlNotification(ViewModels.MainViewModel shellViewModel) { InitializeComponent(); ShellVm = shellViewModel; NotificationsControl.DataContext = Notifications; var desktopWorkingArea = System.Windows.SystemParameters.WorkArea; this.Left = desktopWorkingArea.Right - 425; //this.Top = desktopWorkingArea.Bottom - 425; this.Top = 50; } public void AddNotification(Notification notification) { notification.Id = count++; if (Notifications.Count + 1 > MAX_NOTIFICATIONS) buffer.Add(notification); else Notifications.Add(notification); //Show window if there're notifications if (Notifications.Count > 0 && !IsActive) Show(); } public void RemoveNotification(Notification notification) { if (Notifications.Contains(notification)) Notifications.Remove(notification); if (buffer.Count > 0) { Notifications.Add(buffer[0]); buffer.RemoveAt(0); } //Close window if there's nothing to show if (Notifications.Count < 1) Hide(); } private void NotificationWindowSizeChanged(object sender, SizeChangedEventArgs e) { if (e.NewSize.Height != 0.0) return; var element = sender as Grid; RemoveNotification(Notifications.First( n => n.Id == Int32.Parse(element.Tag.ToString()))); } private void NotificationWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Notification n = (Notification)((Grid)sender).DataContext; BringWindowToFront(); RemoveNotification(n); } static void BringWindowToFront() { var currentProcess = Process.GetCurrentProcess(); var processes = Process.GetProcessesByName(currentProcess.ProcessName); var process = processes.FirstOrDefault(p => p.Id == currentProcess.Id); if (process == null) return; SetForegroundWindow(process.MainWindowHandle); } [DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr hWnd); } }