forked from microsoft/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowFrameService.cpp
178 lines (155 loc) · 6.17 KB
/
WindowFrameService.cpp
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "pch.h"
#include "WindowFrameService.h"
#include "CalcViewModel/Common/KeyboardShortcutManager.h"
#include "CalcViewModel/Common/TraceLogger.h"
using namespace concurrency;
using namespace Platform;
using namespace CalculatorApp;
using namespace CalculatorApp::Common;
using namespace std;
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::Storage;
using namespace Windows::UI::Core;
using namespace Windows::UI::ViewManagement;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Interop;
using namespace Windows::UI::Xaml::Navigation;
namespace CalculatorApp
{
WindowFrameService ^ WindowFrameService::CreateNewWindowFrameService(_In_ Frame ^ viewFrame, bool createdByUs, Platform::WeakReference parent)
{
assert(CoreWindow::GetForCurrentThread() != nullptr);
auto frameService = ref new WindowFrameService(viewFrame, parent);
frameService->InitializeFrameService(createdByUs);
return frameService;
}
WindowFrameService::WindowFrameService(_In_ Frame ^ frame, WeakReference parent)
: m_currentWindow(CoreWindow::GetForCurrentThread())
, m_coreDispatcher(m_currentWindow->Dispatcher)
, m_frame(frame)
, m_parent(parent)
, m_viewId(ApplicationView::GetApplicationViewIdForWindow(m_currentWindow.Get()))
{
}
void WindowFrameService::InitializeFrameService(bool createdByUs)
{
assert(createdByUs == (!CoreApplication::GetCurrentView()->IsHosted && !CoreApplication::GetCurrentView()->IsMain));
if (createdByUs)
{
ApplicationView::GetForCurrentView()->Consolidated +=
ref new TypedEventHandler<ApplicationView ^, ApplicationViewConsolidatedEventArgs ^>(this, &WindowFrameService::OnConsolidated);
}
else
{
CoreWindow::GetForCurrentThread()->Closed += ref new TypedEventHandler<CoreWindow ^, CoreWindowEventArgs ^>(this, &WindowFrameService::OnClosed);
}
}
CoreDispatcher ^ WindowFrameService::GetCoreDispatcher()
{
return m_coreDispatcher.Get();
}
int WindowFrameService::GetViewId()
{
return m_viewId;
}
Frame ^ WindowFrameService::GetFrame()
{
return m_frame;
}
Page ^ WindowFrameService::GetCurrentPage()
{
return dynamic_cast<Page ^>(m_frame->Content);
}
void WindowFrameService::SetNewFrame(_In_ Windows::UI::Xaml::Controls::Frame ^ frame)
{
assert(frame->BackStackDepth == 0);
m_frame = frame;
}
void WindowFrameService::RegisterOnWindowClosingHandler(function<void()> onWindowClosingHandler)
{
m_onWindowClosingHandlers.push_back(onWindowClosingHandler);
}
void WindowFrameService::InvokeWindowClosingHandlers()
{
// Should be called only once just before we kill the window.
for (auto& handler : m_onWindowClosingHandlers)
{
handler();
}
m_onWindowClosingHandlers.clear();
}
task<void> WindowFrameService::HandleViewRelease()
{
auto that(this);
task_completion_event<void> closingHandlersCompletedEvent;
m_coreDispatcher->RunAsync(CoreDispatcherPriority::Low, ref new DispatchedHandler([that, closingHandlersCompletedEvent]() {
KeyboardShortcutManager::OnWindowClosed(that->m_viewId);
Window::Current->Content = nullptr;
that->InvokeWindowClosingHandlers();
// This is to ensure InvokeWindowClosingHandlers is be done before RemoveWindowFromMap
// If InvokeWindowClosingHandlers throws any exception we want it to crash the application
// so we are OK not setting closingHandlersCompletedEvent in that case
closingHandlersCompletedEvent.set();
that->m_coreDispatcher->StopProcessEvents();
Window::Current->Close();
}));
return create_task(closingHandlersCompletedEvent);
}
void WindowFrameService::OnConsolidated(_In_ ApplicationView ^ sender, _In_ ApplicationViewConsolidatedEventArgs ^ e)
{
TraceLogger::GetInstance()->DecreaseWindowCount();
auto parent = m_parent.Resolve<App>();
if (parent != nullptr)
{
parent->RemoveWindow(this);
}
}
void WindowFrameService::OnClosed(_In_ CoreWindow ^ sender, _In_ CoreWindowEventArgs ^ args)
{
auto parent = m_parent.Resolve<App>();
if (parent != nullptr)
{
parent->RemoveSecondaryWindow(this);
}
}
void WindowFrameService::RegisterRuntimeWindowService(TypeName serviceId, _In_opt_ Object ^ service)
{
if (TryResolveRuntimeWindowService(serviceId))
{
throw ref new InvalidArgumentException(serviceId + L" already registered");
}
m_runtimeServicesMap[serviceId.Name] = service;
}
bool WindowFrameService::RemoveRuntimeWindowService(TypeName serviceId)
{
return m_runtimeServicesMap.erase(serviceId.Name) > 0;
}
Object ^ WindowFrameService::ResolveRuntimeWindowService(TypeName serviceId)
{
auto service = TryResolveRuntimeWindowService(serviceId);
if (service)
{
return service;
}
else
{
throw ref new InvalidArgumentException(serviceId.Name + L" not found");
}
}
_Ret_maybenull_ Object ^ WindowFrameService::TryResolveRuntimeWindowService(TypeName serviceId)
{
auto entry = m_runtimeServicesMap.find(serviceId.Name);
if (entry != m_runtimeServicesMap.end())
{
return entry->second;
}
return nullptr;
}
}