-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
198 lines (175 loc) · 6.38 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
198 lines (175 loc) · 6.38 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Libertix.Helpers;
using Libertix.Dialogs;
using Libertix.Pages;
using Libertix.Models;
namespace Libertix
{
public partial class MainWindow : Window
{
private readonly InstallationState _installationState;
private readonly TrayIconController _trayIcon;
private string _selectedLanguageCode;
private bool _hiddenInTray;
private bool _allowClose;
public MainWindow()
{
var application = (App)Application.Current;
_installationState = application.InstallationState;
InitializeComponent();
if (application.Filepool.IsDevelopmentMode)
{
DevelopmentModeBanner.Visibility = Visibility.Visible;
}
_installationState.InstallationRunningChanged += InstallationState_InstallationRunningChanged;
string windowsLang = Localization.GetWindowsLanguageCode();
Localization.SetLanguage(windowsLang);
_selectedLanguageCode = windowsLang;
_trayIcon = new TrayIconController(
RestoreFromTray,
ResourceText("TrayOpenLibertix", "Open Libertix"));
MainFrame.Content = CreateWelcomePage();
if (_installationState.UefiRecoveryStatePath is string recoveryStatePath &&
!string.IsNullOrWhiteSpace(recoveryStatePath))
{
Dispatcher.BeginInvoke(new Action(() =>
NavigationHelper.NavigateWithAnimationInFrame(
MainFrame,
new UefiBootFallback(_installationState),
TimeSpan.Zero)));
}
}
protected override void OnClosing(CancelEventArgs e)
{
if (_allowClose)
{
base.OnClosing(e);
return;
}
if (_installationState.IsInstallationRunning)
{
e.Cancel = true;
HideInTrayDuringInstallation();
return;
}
e.Cancel = true;
bool confirmed = LocalizedConfirmationDialog.Show(
this,
ResourceText("CloseConfirmationTitle", "Close Libertix"),
ResourceText("CloseConfirmationMessage", "Do you really want to close Libertix?"),
ResourceText("ConfirmationYes", "Yes"),
ResourceText("ConfirmationNo", "No"));
if (!confirmed)
{
return;
}
_allowClose = true;
e.Cancel = false;
base.OnClosing(e);
}
protected override void OnClosed(EventArgs e)
{
_installationState.InstallationRunningChanged -= InstallationState_InstallationRunningChanged;
_trayIcon.Dispose();
base.OnClosed(e);
}
public void PrepareForSystemRestart()
{
_allowClose = true;
}
public void CancelSystemRestartPreparation()
{
_allowClose = false;
}
private void HideInTrayDuringInstallation()
{
_hiddenInTray = true;
Hide();
_trayIcon.Show(
ResourceText("TrayInstallTitle", "Libertix installation in progress"),
ResourceText(
"TrayInstallMessage",
"The installation is still running. Double-click the Libertix icon near the clock to reopen the window."));
}
private void RestoreFromTray()
{
Dispatcher.BeginInvoke(new Action(() =>
{
_hiddenInTray = false;
_trayIcon.Hide();
Show();
WindowState = WindowState.Normal;
Activate();
Topmost = true;
Topmost = false;
Focus();
}));
}
private void InstallationState_InstallationRunningChanged(object sender, EventArgs e)
{
if (!_installationState.IsInstallationRunning && _hiddenInTray)
RestoreFromTray();
}
private static string ResourceText(string key, string fallback)
{
return Localization.GetString(key, fallback);
}
private void StartInstallation()
{
_installationState.SelectedDistro = null;
_installationState.SelectedLinuxSizeGiB = null;
_installationState.Account = null;
NavigationHelper.NavigateWithAnimationInFrame(
MainFrame,
new CompatibilityCheck(_installationState),
TimeSpan.FromSeconds(0.3));
}
private void OpenAbout()
{
NavigationHelper.NavigateWithAnimationInFrame(
MainFrame,
new About(),
TimeSpan.FromSeconds(0.3));
}
/// <summary>Restores a fresh welcome page outside the installation journal.</summary>
public void ReturnToWelcome()
{
NavigationService navigation = MainFrame.NavigationService;
NavigatedEventHandler navigated = null;
navigated = (sender, args) =>
{
navigation.Navigated -= navigated;
// Auxiliary pages must not become part of the installation journal.
// Clear About only after the fresh welcome page has become current.
while (navigation.RemoveBackEntry() != null)
{
}
};
navigation.Navigated += navigated;
NavigationHelper.NavigateWithAnimationInFrame(
MainFrame,
CreateWelcomePage(),
TimeSpan.FromSeconds(0.3),
slideLeft: false);
}
private Welcome CreateWelcomePage()
{
return new Welcome(
_selectedLanguageCode,
StartInstallation,
OpenAbout,
ChangeLanguage);
}
private void ChangeLanguage(string cultureName)
{
_selectedLanguageCode = cultureName;
Localization.SetLanguage(cultureName);
_trayIcon?.SetOpenLabel(ResourceText("TrayOpenLibertix", "Open Libertix"));
}
}
}