-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
164 lines (139 loc) · 6.34 KB
/
App.xaml.cs
File metadata and controls
164 lines (139 loc) · 6.34 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
using AutoOS.Views.Installer.Actions;
using Microsoft.UI.Windowing;
using Microsoft.Win32;
using Microsoft.Windows.AppLifecycle;
using Windows.Graphics;
using Windows.Storage;
using WinRT.Interop;
using AutoOS.Views.Startup;
namespace AutoOS
{
public partial class App : Application
{
private readonly ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
public new static App Current => (App)Application.Current;
public static Window MainWindow = Window.Current;
public JsonNavigationService NavService { get; set; }
public IThemeService ThemeService { get; set; }
internal static bool IsInstalled { get; private set; }
internal static double Scaling { get; set; }
public App()
{
InitializeComponent();
NavService = new JsonNavigationService();
IsInstalled = (Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\AutoOS", "IsInstalled", 0) as int? ?? 0) == 1 || Registry.CurrentUser.OpenSubKey(@"SOFTWARE\AutoOS")?.GetValue("Stage") as string == "Installed";
Application.Current.UnhandledException += Current_UnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
}
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
if (IsInstalled)
{
AppActivationArguments appActivationArguments = AppInstance.GetCurrent().GetActivatedEventArgs();
if (appActivationArguments.Kind is ExtendedActivationKind.StartupTask)
{
MainWindow = new StartupWindow();
MainWindow.Title = MainWindow.AppWindow.Title = "AutoOS Startup";
MainWindow.AppWindow.SetIcon("Assets/AppIcon.ico");
Window window = MainWindow;
var monitor = DisplayMonitorHelper.GetMonitorInfo(window);
int X = (int)monitor.RectMonitor.Width;
int Y = (int)monitor.RectMonitor.Height;
int windowWidth = (int)(340 * Scaling);
int windowHeight = (int)(130 * Scaling);
int posX = X - windowWidth - (int)(10 * Scaling);
int posY = Y - windowHeight - (int)(53 * Scaling);
MainWindow.AppWindow.MoveAndResize(new RectInt32(posX, posY, windowWidth, windowHeight));
if (!localSettings.Values.TryGetValue("LaunchMinimized", out object value) || (int)value == 0)
{
MainWindow.Activate();
}
}
else
{
MainWindow = new MainWindow();
MainWindow.Title = MainWindow.AppWindow.Title = "AutoOS Settings";
MainWindow.AppWindow.SetIcon("Assets/AppIcon.ico");
ThemeService = new ThemeService().Initialize(MainWindow);
WindowHelper.ResizeAndCenterWindowToPercentageOfWorkArea(MainWindow, 92);
MainWindow.Activate();
}
}
else
{
AppActivationArguments appActivationArguments = AppInstance.GetCurrent().GetActivatedEventArgs();
if (appActivationArguments.Kind is ExtendedActivationKind.StartupTask)
{
Application.Current.Exit();
}
else
{
MainWindow = new MainWindow();
MainWindow.Title = MainWindow.AppWindow.Title = "AutoOS Installer";
MainWindow.AppWindow.SetIcon("Assets/AppIcon.ico");
AppWindow.GetFromWindowId(Win32Interop.GetWindowIdFromWindow(WindowNative.GetWindowHandle(MainWindow))).Closing += AppWindow_Closing;
ThemeService = new ThemeService().Initialize(MainWindow);
MainWindow.Activate();
}
}
}
private async void Current_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
e.Handled = true;
await ShowErrorMessage(e.Exception);
}
private void CurrentDomain_UnhandledException(object sender, System.UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception ex)
{
_ = ShowErrorMessage(ex);
}
}
private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
e.SetObserved();
if (e.Exception != null && !e.Exception.Message.Contains("Response body is unavailable for redirect responses"))
_ = ShowErrorMessage(e.Exception);
}
internal static async Task ShowErrorMessage(Exception ex)
{
try
{
await ProcessActions.LogError(ex);
}
catch { }
if (MainWindow?.DispatcherQueue != null)
{
MainWindow.DispatcherQueue.TryEnqueue(async () =>
{
MessageBoxOptions options = MessageBoxOptions.Default;
options.Underlay = UnderlayMode.SmokeLayer;
await MessageBox.ShowAsync(true, MainWindow, ex.Message, "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, options);
});
}
}
private async void AppWindow_Closing(AppWindow sender, AppWindowClosingEventArgs args)
{
args.Cancel = true;
var dialog = new ContentDialog
{
Title = "Close AutoOS?",
Content = "Are you sure that you want to close AutoOS?",
PrimaryButtonText = "Yes",
CloseButtonText = "No",
DefaultButton = ContentDialogButton.Close,
XamlRoot = MainWindow.Content.XamlRoot
};
try
{
var result = await dialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
Current.Exit();
}
}
catch { }
}
}
}