-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
112 lines (90 loc) · 3.11 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
107
108
109
110
111
112
using System;
using System.Threading;
using System.Windows;
using Pinny_Notes.Components;
using Pinny_Notes.Helpers;
using Pinny_Notes.Properties;
using Pinny_Notes.Views;
namespace Pinny_Notes;
public partial class App : Application
{
#if DEBUG
public const bool IsDebugMode = true;
#else
public const bool IsDebugMode = false;
#endif
private const string UniqueEventName = (IsDebugMode) ? "176fc692-28c2-4ed0-ba64-60fbd7165018" : "b1bc1a95-e142-4031-a239-dd0e14568a3c";
private const string UniqueMutexName = (IsDebugMode) ? "e21c6456-5a11-4f37-a08d-83661b642abe" : "a46c6290-525a-40d8-9880-c95d35a49057";
private Mutex _mutex = null!;
private EventWaitHandle _eventWaitHandle = null!;
private SettingsWindow? _settingsWindow;
private NotifyIconComponent? NotifyIcon;
protected override void OnStartup(StartupEventArgs e)
{
_mutex = new(true, UniqueMutexName, out bool createdNew);
_eventWaitHandle = new(false, EventResetMode.AutoReset, UniqueEventName);
if (!createdNew)
{
_eventWaitHandle.Set();
Shutdown();
return;
}
base.OnStartup(e);
// Spawn a thread which will be waiting for our event
Thread thread = new(
() => {
while (_eventWaitHandle.WaitOne())
Current.Dispatcher.BeginInvoke(
() => CreateNewNote()
);
}
)
{
// It is important mark it as background otherwise it will prevent app from exiting.
IsBackground = true
};
thread.Start();
if (Settings.Default.ShowTrayIcon)
{
NotifyIcon = new();
ShutdownMode = ShutdownMode.OnExplicitShutdown;
}
CreateNewNote();
CheckForNewRelease();
}
protected override void OnExit(ExitEventArgs e)
{
NotifyIcon?.Dispose();
base.OnExit(e);
}
public void CreateNewNote()
{
new NoteWindow().Show();
}
public void ShowSettingsWindow(Window? owner = null)
{
if (_settingsWindow == null || !_settingsWindow.IsLoaded)
_settingsWindow = new SettingsWindow();
_settingsWindow.Owner = owner;
if (_settingsWindow.IsVisible)
_settingsWindow.Activate();
else
_settingsWindow.Show();
}
private async void CheckForNewRelease()
{
DateTimeOffset date = DateTimeOffset.UtcNow;
if (Settings.Default.CheckForUpdates && Settings.Default.LastUpdateCheck < date.AddDays(-7).ToUnixTimeSeconds())
{
Settings.Default.LastUpdateCheck = date.ToUnixTimeSeconds();
Settings.Default.Save();
if (await VersionHelper.IsNewVersionAvailable())
MessageBox.Show(
$"A new version of Pinny Notes is available;{Environment.NewLine}https://github.com/63BeetleSmurf/PinnyNotes/releases/latest",
"Update available",
MessageBoxButton.OK,
MessageBoxImage.Information
);
}
}
}