forked from microsoft/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.xaml.cs
201 lines (176 loc) · 7.46 KB
/
App.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// App.xaml.h
// Declaration of the App class.
//
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Storage;
using Windows.UI.StartScreen;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using CalculatorApp.Utils;
using CalculatorApp.ViewModel.Common;
using CalculatorApp.ViewModel.Common.Automation;
namespace CalculatorApp
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public sealed partial class App
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
InitializeComponent();
NarratorNotifier.RegisterDependencyProperties();
// TODO: MSFT 14645325: Set this directly from XAML.
// Currently this is bugged so the property is only respected from code-behind.
HighContrastAdjustment = ApplicationHighContrastAdjustment.None;
Suspending += OnSuspending;
#if DEBUG
DebugSettings.IsBindingTracingEnabled = true;
DebugSettings.BindingFailed += (sender, args) =>
{
if (Debugger.IsAttached)
{
string errorMessage = args.Message;
Debugger.Break();
}
};
#endif
}
/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used when the application is launched to open a specific file, to display
/// search results, and so forth.
/// </summary>
/// <param name="e">Details about the launch request and process.</param>
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
NavCategoryStates.SetCurrentUser(args.User.NonRoamableId);
// It takes time to check GraphingMode at the very first time. Warm up in a background thread.
Task.Run(() => NavCategoryStates.IsViewModeEnabled(ViewMode.Graphing));
OnAppLaunch(args, args.Arguments, args.PrelaunchActivated);
}
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.Protocol)
{
if (args.IsSnapshotProtocol())
{
var protoArgs = (IProtocolActivatedEventArgs)args;
OnAppLaunch(args,
new SnapshotLaunchArguments
{
ActivityId = protoArgs.Uri.GetActivityId(),
LaunchUri = protoArgs.Uri
},
false);
}
else
{
// handle any unknown protocol launch as a normal app launch.
OnAppLaunch(args, null, false);
}
}
}
private void OnAppLaunch(IActivatedEventArgs args, object arguments, bool isPreLaunch)
{
// Uncomment the following lines to display frame-rate and per-frame CPU usage info.
//#if DEBUG
// if (IsDebuggerPresent())
// {
// DebugSettings.EnableFrameRateCounter = true;
// }
//#endif
args.SplashScreen.Dismissed += async (_, __) => await SetupJumpListAsync();
var minWindowWidth = Convert.ToSingle(Resources["AppMinWindowWidth"]);
var minWindowHeight = Convert.ToSingle(Resources["AppMinWindowHeight"]);
var minWindowSize = SizeHelper.FromDimensions(minWindowWidth, minWindowHeight);
var appView = ApplicationView.GetForCurrentView();
var localSettings = ApplicationData.Current.LocalSettings;
// SetPreferredMinSize should always be called before Window.Activate
appView.SetPreferredMinSize(minWindowSize);
// For very first launch, set the size of the calc as size of the default standard mode
if (!localSettings.Values.ContainsKey("VeryFirstLaunch"))
{
localSettings.Values["VeryFirstLaunch"] = false;
appView.TryResizeView(minWindowSize); // the requested size must not be less than the min size.
}
else
{
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Auto;
}
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
var rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame
{
FlowDirection = LocalizationService.GetInstance().GetFlowDirection()
};
}
if (isPreLaunch)
{
return;
}
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
if (rootFrame.Content == null && !rootFrame.Navigate(typeof(MainPage), arguments))
{
// We couldn't navigate to the main page, kill the app so we have a good
// stack to debug
throw new SystemException("6d430286-eb5d-4f8d-95d2-3d1059552968");
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
ThemeHelper.InitializeAppTheme();
Window.Current.Activate();
}
private void OnSuspending(object sender, SuspendingEventArgs args)
{
TraceLogger.GetInstance().LogButtonUsage();
}
private async Task SetupJumpListAsync()
{
try
{
var calculatorOptions = NavCategoryStates.CreateCalculatorCategoryGroup();
var jumpList = await JumpList.LoadCurrentAsync();
jumpList.SystemGroupKind = JumpListSystemGroupKind.None;
jumpList.Items.Clear();
foreach (NavCategory option in calculatorOptions.Categories)
{
if (!NavCategoryStates.IsViewModeEnabled(option.ViewMode))
{
continue;
}
ViewMode mode = option.ViewMode;
var item = JumpListItem.CreateWithArguments(((int)mode).ToString(), "ms-resource:///Resources/" + NavCategoryStates.GetNameResourceKey(mode));
item.Description = "ms-resource:///Resources/" + NavCategoryStates.GetNameResourceKey(mode);
item.Logo = new Uri("ms-appx:///Assets/" + mode + ".png");
jumpList.Items.Add(item);
}
await jumpList.SaveAsync();
}
catch (Exception ex)
{
TraceLogger.GetInstance().LogError(ViewMode.None, nameof(SetupJumpListAsync), ex.ToString());
#if DEBUG
throw;
#endif
}
}
}
}