-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
151 lines (124 loc) · 3.9 KB
/
MainWindow.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using CommunityToolkit.Mvvm.ComponentModel;
using Sandman.Properties;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
namespace Sandman;
[ObservableObject]
public partial class MainWindow : Window
{
[ObservableProperty]
private StringBuilder _consoleLog = new();
public static readonly string ExecutableFolder = string.Empty;
private readonly Shared.NotificationIcon _notificationIcon;
static MainWindow()
{
var asm = System.Reflection.Assembly.GetEntryAssembly();
if (asm is not null)
{
ExecutableFolder = Path.GetDirectoryName(asm.Location) ?? string.Empty;
}
}
public MainWindow()
{
/*
* If necessary, migrate app.config settings to new app.config file.
*
* Note: This works for an "in-place" upgrade, such as a build,
* running setup.exe, or using ClickOnce.
* The reason is that .NET creates a hash from the path to determine
* where to store user settings.
* In "in-place" upgrades, all user.config files are in
* version-specific directories under a single app folder.
*
* REF: Upgrade() https://stackoverflow.com/questions/534261/how-do-you-keep-user-config-settings-across-different-assembly-versions-in-net/534335#534335
* REF: Reload() https://stackoverflow.com/questions/23924183/keep-users-settings-after-altering-assembly-file-version/47921377#47921377
*/
if (Settings.Default.UpgradeRequired)
{
try
{
Settings.Default.Upgrade();
Settings.Default.Reload();
Settings.Default.UpgradeRequired = false;
Settings.Default.Save();
}
catch (Exception)
{
}
}
// remove original default trace listener
Trace.Listeners.Clear();
// form path to log file
string pathLog = Path.Combine(ExecutableFolder, "Sandman.log");
// add new default listener and set the filename
DefaultTraceListener listener = new()
{
LogFileName = pathLog
};
Trace.Listeners.Add(listener);
Trace.AutoFlush = true;
/*
* Set up notification area icon
*/
_notificationIcon = new Shared.NotificationIcon(this, Properties.Resources.Sandman, nameof(Sandman), menuItems: []);
InitializeComponent();
DataContext = this;
}
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
await WatchAndWait.StartAsync(this).ConfigureAwait(continueOnCapturedContext: false);
}
private void Window_Closing(object sender, CancelEventArgs e)
{
Settings.Default.Save();
if (_notificationIcon.ShouldClose())
{
/// Calling <see cref="AddToConsoleLog(string)"/> while shutting down
/// will cause a <seealso cref="TaskCanceledException"/>.
/// So, we cancel the wait task before exiting.
WatchAndWait.CancelWaitTaskAsync().Wait(TimeSpan.FromSeconds(1));
Application.Current.Shutdown();
return;
}
e.Cancel = true;
Hide();
Trace.Flush();
}
private async void RestartButton_Click(object sender, RoutedEventArgs e)
{
await WatchAndWait.RestartWaitAsync().ConfigureAwait(continueOnCapturedContext: false);
}
public void WriteInformation(string s)
{
string timestampedMessage = TimestampMessage(s);
Trace.TraceInformation(timestampedMessage);
AddToConsoleLog(timestampedMessage);
}
public void WriteWarning(string s)
{
string timestampedMessage = TimestampMessage(s);
Trace.TraceWarning(timestampedMessage);
AddToConsoleLog(timestampedMessage);
}
public void WriteError(string s)
{
string timestampedMessage = TimestampMessage(s);
Trace.TraceError(timestampedMessage);
AddToConsoleLog(timestampedMessage);
}
private void AddToConsoleLog(string timestampedMessage)
{
ConsoleLog.AppendLine(timestampedMessage);
OnPropertyChanged(nameof(ConsoleLog));
Dispatcher.Invoke(() =>
{
CtlLog.CaretIndex = CtlLog.Text.Length;
CtlLog.ScrollToEnd();
});
}
private static string TimestampMessage(string s) => $"[{DateTimeOffset.Now:yyyy-MM-dd HH:mm:ss}] (Thread {Environment.CurrentManagedThreadId:00}) {s}";
}