-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.xaml.cs
106 lines (92 loc) · 3.87 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Hardcodet.Wpf.TaskbarNotification;
using System;
using System.Linq;
using System.Windows;
using Meziantou.Framework.Win32;
namespace BetterOutlookReminder
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private readonly NotificationWindow notificationWindow = new NotificationWindow(null);
private readonly AppointmentChangePoller poller = new AppointmentChangePoller();
private readonly NotificationTimer timer = new NotificationTimer();
private bool first = true;
private TaskbarIcon notifyIcon;
private readonly ImageSource connectedIcon = new BitmapImage(new Uri("pack://application:,,,/clock_green.ico"));
private readonly ImageSource disconnectedIcon = new BitmapImage(new Uri("pack://application:,,,/clock_red.ico"));
protected override async void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
notifyIcon = (TaskbarIcon)FindResource("NotificationIcon");
notifyIcon.TrayLeftMouseUp += NotifyIconOnTrayLeftMouseUp;
poller.NextAppointmentChanged += PollerOnNextAppointmentChanged;
await poller.Start();
timer.NotificationDue += TimerOnNotificationDue;
//ScreenUtils.PausedForFullScreen += (s, e2) => notifyIcon.ShowBalloonTip(null, "Paused calendar reminders while full screen", BalloonIcon.None);
//ScreenUtils.ResumedFromFullScreen += (s, e2) => notifyIcon.ShowBalloonTip(null, "Calendar reminders resumed", BalloonIcon.None);
}
protected override void OnExit(ExitEventArgs e)
{
notifyIcon.Dispose();
base.OnExit(e);
}
private void TimerOnNotificationDue(object sender, NotificationTimer.NotificationDueEventArgs args)
{
if (ScreenUtils.CanShowPopup())
{
notificationWindow.Show(args.Appointments);
}
UpdateTooltip(args.Appointments);
}
private async void NotifyIconOnTrayLeftMouseUp(object sender, RoutedEventArgs routedEventArgs)
{
// qq should really be able to see current appointment when clicking here
await poller.Force();
notificationWindow.Show(poller.CurrentValue);
}
private void PollerOnNextAppointmentChanged(object sender,
AppointmentChangePoller.NextAppointmentChangedEventHandlerArgs args)
{
AppointmentGroup appts = args.NextAppointments;
timer.updateNextAppointment(appts);
UpdateTooltip(appts);
if (first)
{
notificationWindow.Show(appts);
first = false;
}
}
private void MenuItem_OnClick(object sender, RoutedEventArgs e)
{
Shutdown();
}
private void UpdateTooltip(AppointmentGroup appointments)
{
if (appointments != null)
{
notifyIcon.IconSource = connectedIcon;
Appointment next = appointments.Next.FirstOrDefault(a => a.Start > DateTime.Now);
if (next != null)
{
notifyIcon.ToolTipText = string.Format("Next: {0} - {1}",
next.Start.ToShortTimeString(),
next.Subject);
}
else
{
notifyIcon.ToolTipText = "No further appointments today";
}
}
else
{
notifyIcon.ToolTipText = "Unable to contact exchange server";
notifyIcon.IconSource = disconnectedIcon;
}
}
}
}