-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
143 lines (126 loc) · 4.36 KB
/
App.xaml.cs
File metadata and controls
143 lines (126 loc) · 4.36 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
using System;
using System.Diagnostics;
using System.Security.Principal;
using System.Windows;
using Microsoft.Win32;
using TweakHub.Services;
namespace TweakHub;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
// Initialize theme service FIRST to ensure proper icon colors from startup
var themeService = ThemeService.Instance;
// Follow Windows theme at startup (Light/Dark based on AppsUseLightTheme)
themeService.CurrentTheme = AppTheme.System;
// Live Windows theme changes
SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
// Check if running as administrator
if (!IsRunningAsAdministrator())
{
var result = MessageBox.Show(
"TweakHub requires administrator privileges to function properly.\n\n" +
"Many system tweaks and optimizations require elevated permissions.\n\n" +
"Would you like to restart TweakHub as administrator?",
"Administrator Privileges Required",
MessageBoxButton.YesNo,
MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
RestartAsAdministrator();
return; // Exit current instance
}
else
{
MessageBox.Show(
"TweakHub will continue to run, but some features may not work correctly without administrator privileges.",
"Limited Functionality Warning",
MessageBoxButton.OK,
MessageBoxImage.Warning);
}
}
base.OnStartup(e);
// Set up global exception handling
DispatcherUnhandledException += App_DispatcherUnhandledException;
// Set up exit handling to ensure clean shutdown
Exit += App_Exit;
}
private void App_Exit(object sender, ExitEventArgs e)
{
try
{
SystemEvents.UserPreferenceChanged -= SystemEvents_UserPreferenceChanged;
}
catch
{
// Ignore unhook errors
}
// Ensure all services are properly stopped
try
{
HardwareMonitoringService.Instance.StopMonitoring();
SystemMonitoringService.Instance.Stop();
}
catch
{
// Ignore errors during shutdown
}
}
private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
// Theme changes usually surface as preference changes; refresh theme if we follow System.
try
{
Dispatcher.Invoke(() => ThemeService.Instance.RefreshSystemThemeIfNeeded());
}
catch
{
// Ignore theme refresh errors
}
}
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show($"An unexpected error occurred: {e.Exception.Message}", "TweakHub Error",
MessageBoxButton.OK, MessageBoxImage.Error);
e.Handled = true;
}
private bool IsRunningAsAdministrator()
{
try
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch
{
return false;
}
}
private void RestartAsAdministrator()
{
try
{
var processInfo = new ProcessStartInfo
{
FileName = Environment.ProcessPath ?? Process.GetCurrentProcess().MainModule?.FileName ?? "TweakHub.exe",
UseShellExecute = true,
Verb = "runas" // This triggers UAC elevation
};
Process.Start(processInfo);
Current.Shutdown();
}
catch (Exception ex)
{
MessageBox.Show(
$"Failed to restart as administrator:\n\n{ex.Message}\n\n" +
"Please manually run TweakHub as administrator for full functionality.",
"Restart Failed",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
}