-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPage.cpp
More file actions
160 lines (134 loc) · 5.97 KB
/
MainPage.cpp
File metadata and controls
160 lines (134 loc) · 5.97 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
#include "pch.h"
#include "MainPage.h"
#include "MainPage.g.cpp"
#include "Internals/ExtensionManager.h"
// Includes necessários para Gamepad e Teclado Virtual
#include <winrt/Windows.Gaming.Input.h>
#include <winrt/Windows.UI.ViewManagement.Core.h>
using namespace winrt;
using namespace winrt::Windows::UI::Xaml;
using namespace winrt::Windows::UI::Xaml::Media;
using namespace winrt::Windows::UI::ViewManagement;
using namespace winrt::Windows::UI::ViewManagement::Core;
using namespace winrt::Windows::Gaming::Input;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Data::Json;
ExtensionManager* g_extensionManager = nullptr;
namespace winrt::UWPWebView2::implementation
{
MainPage::MainPage()
: internals(*this)
{
Loaded({ this, &MainPage::OnLoaded });
ApplicationView::GetForCurrentView().SetDesiredBoundsMode(ApplicationViewBoundsMode::UseCoreWindow);
if (!ApplicationViewScaling::TrySetDisableLayoutScaling(true))
{
OutputDebugString(L"Error: Failed to disable layout scaling.");
}
}
const std::string& MainPage::GetAppBaseUrl() { return appBaseUrl; }
const std::string& MainPage::GetWebResourceFolder() { return webResourceFolder; }
WebView2& MainPage::GetWebView2() { return webView; }
fire_and_forget MainPage::InitializeWebView()
{
webView.Background(SolidColorBrush(Windows::UI::ColorHelper::FromArgb(255, 0, 0, 0)));
co_await webView.EnsureCoreWebView2Async();
if (coreWV2 = webView.CoreWebView2())
{
auto settings = coreWV2.Settings();
settings.IsZoomControlEnabled(false);
settings.IsPinchZoomEnabled(false);
webView.NavigationCompleted({ this, &MainPage::OnNavigationCompleted });
webView.WebMessageReceived({ this, &MainPage::OnWebMessageReceived });
webView.NavigationStarting({ &internals, &WV2Internals::OnNavigationStarting });
coreWV2.PermissionRequested({ &internals, &WV2Internals::OnPermissionRequested });
coreWV2.NewWindowRequested({ &internals, &WV2Internals::OnNewWindowRequested });
coreWV2.WindowCloseRequested({ &internals, &WV2Internals::OnWindowCloseRequested });
std::string requestFilter = appBaseUrl + "*";
coreWV2.AddWebResourceRequestedFilter(winrt::to_hstring(requestFilter), CoreWebView2WebResourceContext::All);
coreWV2.WebResourceRequested({ &internals, &WV2Internals::OnWebResourceRequested });
webView.Source(Uri{ L"https://app.localhost/index.html" });
}
}
void MainPage::OnLoaded(const IInspectable&, const RoutedEventArgs&)
{
std::string appFolder = winrt::to_string(Windows::ApplicationModel::Package::Current().InstalledLocation().Path());
std::string userDataFolder = winrt::to_string(Windows::Storage::ApplicationData::Current().LocalFolder().Path());
g_extensionManager = new ExtensionManager(appFolder, webResourceFolder, userDataFolder);
g_extensionManager->SetMainPage(this);
g_extensionManager->Init();
webView = WebView2();
InitializeWebView();
// --- MONITORAMENTO DO GAMEPAD ---
DispatcherTimer timer;
timer.Interval(std::chrono::milliseconds(32)); // ~30fps é suficiente para detecção de botões
timer.Tick([this](auto&&, auto&&) {
auto gamepads = Gamepad::Gamepads();
if (gamepads.Size() > 0)
{
auto reading = gamepads.GetAt(0).GetCurrentReading();
// Bitwise check para Menu (Start) e View (Select/Back)
bool menuPressed = (reading.Buttons & GamepadButtons::Menu) == GamepadButtons::Menu;
bool viewPressed = (reading.Buttons & GamepadButtons::View) == GamepadButtons::View;
static bool wasPressed = false;
if (menuPressed && viewPressed)
{
if (!wasPressed)
{
// Abre o teclado do Xbox
CoreInputView::GetForCurrentView().TryShow(CoreInputViewKind::Keyboard);
wasPressed = true;
}
}
else
{
wasPressed = false;
}
}
});
timer.Start();
}
void MainPage::OnNavigationCompleted(const WebView2&, const CoreWebView2NavigationCompletedEventArgs& args)
{
if (!webView.Parent())
{
if (args.IsSuccess())
{
Content(webView);
webView.Focus(FocusState::Programmatic);
}
}
}
void MainPage::OnWebMessageReceived(const WebView2&, const CoreWebView2WebMessageReceivedEventArgs& args)
{
hstring jsonStr{ args.TryGetWebMessageAsString() };
JsonObject json{};
if (!JsonObject::TryParse(jsonStr, json)) return;
hstring type{ json.GetNamedString(L"type") };
if (type == L"wrapper-init")
{
HandleRuntimeInitMessage();
}
else if (type == L"extension-message")
{
g_extensionManager->OnExtensionMessage(winrt::to_string(jsonStr));
}
}
void MainPage::HandleRuntimeInitMessage()
{
const std::vector<std::string>& allRegisteredComponentIds = g_extensionManager->GetAllRegisteredComponentIds();
JsonArray idArr{};
for (const std::string& id : allRegisteredComponentIds)
{
idArr.Append(JsonValue::CreateStringValue(winrt::to_hstring(id)));
}
JsonObject sendJson{};
sendJson.SetNamedValue(L"type", JsonValue::CreateStringValue(L"wrapper-init-response"));
sendJson.SetNamedValue(L"registeredComponentIds", idArr);
SendWebMessage(winrt::to_string(sendJson.Stringify()));
}
void MainPage::SendWebMessage(const std::string& jsonStr)
{
if (coreWV2) coreWV2.PostWebMessageAsJson(winrt::to_hstring(jsonStr));
}
}