108 lines
3.7 KiB
C++
108 lines
3.7 KiB
C++
#include <flutter/dart_project.h>
|
|
#include <flutter/flutter_view_controller.h>
|
|
#include <windows.h>
|
|
|
|
#include "app_links/app_links_plugin_c_api.h"
|
|
#include "flutter_window.h"
|
|
#include "utils.h"
|
|
|
|
int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
|
|
_In_ wchar_t *command_line, _In_ int show_command) {
|
|
// Diagnostic breadcrumb: protocol activations carry the link as the only
|
|
// command-line argument; regular launches have none.
|
|
int argc = 0;
|
|
wchar_t** argv = ::CommandLineToArgvW(::GetCommandLineW(), &argc);
|
|
if (argv != nullptr) {
|
|
std::string cmdline;
|
|
for (int i = 0; i < argc; ++i) {
|
|
cmdline += Utf8FromUtf16(argv[i]);
|
|
cmdline += ' ';
|
|
}
|
|
::LocalFree(argv);
|
|
LogAppLink("launched with: " + cmdline);
|
|
}
|
|
|
|
const bool protocol_launch = HasAppLinkArgument();
|
|
const bool is_primary = AcquireSingleInstanceMutex();
|
|
|
|
// (Re)register the per-user custom scheme so OAuth / magic-link redirects
|
|
// launched by the browser can activate this app.
|
|
RegisterAppLinkProtocol();
|
|
|
|
// Exactly one instance may own a window: the PKCE code verifier lives in
|
|
// shared secure storage, so a login callback delivered to a window other
|
|
// than the one that initiated the sign-in would silently complete the
|
|
// exchange in the WRONG window while the user keeps staring at a login
|
|
// screen. Duplicate normal launches surface the existing instance and
|
|
// exit instead of starting a competing window.
|
|
if (!protocol_launch && !is_primary) {
|
|
::SendAppLinkToInstance(); // best-effort: surface the existing window
|
|
LogAppLink("another instance owns the app; exiting");
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
// Protocol activation while an instance is already running hands the link
|
|
// to that instance (over WM_COPYDATA, consumed by the app_links plugin)
|
|
// and exits, instead of spawning a second window.
|
|
if (::SendAppLinkToInstance()) {
|
|
LogAppLink("link forwarded to the running instance; exiting");
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
if (protocol_launch) {
|
|
LogAppLink(is_primary
|
|
? "cold start with link; handling it directly in this "
|
|
"process"
|
|
: "forwarding failed; handling the link in this secondary "
|
|
"process");
|
|
}
|
|
|
|
// Attach to console when present (e.g., 'flutter run') or create a
|
|
// new console when running with a debugger.
|
|
if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
|
|
CreateAndAttachConsole();
|
|
}
|
|
|
|
// Initialize COM, so that it is available for use in the library and/or
|
|
// plugins.
|
|
::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
|
|
|
|
flutter::DartProject project(L"data");
|
|
|
|
std::vector<std::string> command_line_arguments =
|
|
GetCommandLineArguments();
|
|
|
|
project.set_dart_entrypoint_arguments(std::move(command_line_arguments));
|
|
|
|
FlutterWindow window(project);
|
|
Win32Window::Point origin(10, 10);
|
|
Win32Window::Size size(1280, 720);
|
|
if (!window.Create(L"glpcompanion", origin, size)) {
|
|
return EXIT_FAILURE;
|
|
}
|
|
window.SetQuitOnClose(true);
|
|
|
|
if (protocol_launch) {
|
|
// Windows protocol activation must not look like a hang: surface the
|
|
// window that will complete the exchange (Windows blocks background
|
|
// processes from stealing focus, so fall back to a taskbar flash).
|
|
HWND hwnd = window.GetHandle();
|
|
if (hwnd != nullptr) {
|
|
::ShowWindow(hwnd, SW_SHOW);
|
|
if (!::SetForegroundWindow(hwnd)) {
|
|
FLASHWINFO flash = {sizeof(FLASHWINFO), hwnd, FLASHW_ALL, 5, 0};
|
|
::FlashWindowEx(&flash);
|
|
}
|
|
}
|
|
}
|
|
|
|
::MSG msg;
|
|
while (::GetMessage(&msg, nullptr, 0, 0)) {
|
|
::TranslateMessage(&msg);
|
|
::DispatchMessage(&msg);
|
|
}
|
|
|
|
::CoUninitialize();
|
|
return EXIT_SUCCESS;
|
|
}
|