-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
184 lines (174 loc) · 6.88 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
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
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
using Pyrux.LevelIO;
using System.Threading.Tasks;
namespace Pyrux
{
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
public string TitleText
{
get => "[PRE-ALPHA] Pyrux v" + StaticDataStore.VersionNumber;
}
public static MainWindow Instance;
public int NavViewSelectedIndex { get; private set; } = -1;
/// <summary>
/// List of pages in the navigation menu.
/// </summary>
public List<(string Tag, Type Page)> contentDictionary = new()
{
("levelSelect",typeof(LevelSelectPage)),
("exerciseView",typeof(ExercisePage)),
("goalView",typeof(GoalPageView)),
("hint",typeof(HintPage)),
("docs",typeof(DocsPage)),
("about",typeof(AboutPage)),
("devtools",typeof(DevToolsPage))
};
public MainWindow()
{
this.InitializeComponent();
ExtendsContentIntoTitleBar = true;
SetTitleBar(AppTitleBar);
Instance = this;
}
/// <summary>
/// Initialize the basic layout of the navigation view.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void ngvMainWindow_Loaded(object sender, RoutedEventArgs e)
{
await CheckAppdataIntegrity();
if (!AppdataManagement.AppdataCorrupted)
{
ngvMainWindow.SelectedItem = ngvMainWindow.MenuItems[0];
NavViewNavigate("levelSelect", new Microsoft.UI.Xaml.Media.Animation.EntranceNavigationTransitionInfo());
}
else
{
DisplayAppdataError();
}
}
/// <summary>
/// Run when an item is invoked on the navigationview.
/// Switch the content frame to the page selected.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void ngvMainWindow_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
{
if (args.IsSettingsInvoked)
{
NavViewNavigate("settings", args.RecommendedNavigationTransitionInfo);
}
else if (args.InvokedItemContainer != null)
{
string navItemTag = args.InvokedItemContainer.Tag.ToString();
NavViewNavigate(navItemTag, args.RecommendedNavigationTransitionInfo);
}
}
/// <summary>
/// Navigate the content frame to the selected page.
/// </summary>
/// <param name="navItemTag">Tag of the selected page.</param>
/// <param name="transitionInfo">Transitioninfo for transition animation.</param>
public void NavViewNavigate(string navItemTag, Microsoft.UI.Xaml.Media.Animation.NavigationTransitionInfo transitionInfo)
{
if (ExercisePage.Instance != null)
{
ExercisePage.Instance.CancelScriptExecution();
ExercisePage.Instance.NavigationLayoutReset();
}
Type page = null;
if (navItemTag == "settings")
{
page = typeof(SettingsPage);
}
else
{
(string Tag, Type Page) item = contentDictionary.FirstOrDefault(p => p.Tag.Equals(navItemTag));
page = item.Page;
NavViewSelectedIndex = contentDictionary.IndexOf(item);
}
Type preNavPageType = ctfMain.CurrentSourcePageType;
if (!(page is null) && !Type.Equals(preNavPageType, page))
{
ctfMain.Navigate(page, null, transitionInfo);
}
}
/// <summary>
/// Set the navigation view selection to the given index.
/// </summary>
/// <param name="index">Index to move the selection to.</param>
public void NavViewSetSelection(int index)
{
ngvMainWindow.SelectedItem = ngvMainWindow.MenuItems[index];
}
/// <summary>
/// Set the navigation view selection to the given footer index.
/// </summary>
/// <param name="index">Index to move the selection to.</param>
public void NavViewSetSelectionInFooter(int index)
{
ngvMainWindow.SelectedItem = ngvMainWindow.MenuItems[index];
}
private void Window_Activated(object sender, WindowActivatedEventArgs args)
{
}
/// <summary>
/// Run a check to see if the appdata folder is still intact.
/// </summary>
private async Task CheckAppdataIntegrity()
{
await AppdataManagement.CheckAppdata();
}
/// <summary>
/// Set the navigation view enabled or disabled.
/// </summary>
/// <param name="enabled">Enable or disable the nav view.</param>
public void NavViewSetEnabled(bool enabled)
{
ngvMainWindow.IsEnabled = enabled;
}
/// <summary>
/// Display an error indicating that the appdata folder has been corrupted.
/// </summary>
public async void DisplayAppdataError()
{
if (AppdataManagement.AppdataCorrupted)
{
ContentDialog appdataErrorDialogue = new();
appdataErrorDialogue.XamlRoot = this.Content.XamlRoot;
appdataErrorDialogue.Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style;
appdataErrorDialogue.Title = "Appdata corrupted.";
appdataErrorDialogue.PrimaryButtonText = "RESET";
appdataErrorDialogue.SecondaryButtonText = "Exit";
appdataErrorDialogue.DefaultButton = ContentDialogButton.Primary;
appdataErrorDialogue.Content = new Pages.ContentDialogs.ConfirmRepairAppdataFolderDialogue();
ContentDialogResult result = await appdataErrorDialogue.ShowAsync();
if (result == ContentDialogResult.Primary)
{
await AppdataManagement.ResetAppdata();
ngvMainWindow_Loaded(null, null);
}
else
{
Environment.Exit(0);
}
}
}
private void wndMain_Closed(object sender, WindowEventArgs args)
{
if (ApplicationWindows.DocumentationWindow.Instance != null)
{
ApplicationWindows.DocumentationWindow.Instance.Close();
}
}
}
}