Files
swiftcontrol/windows/runner/flutter_window.cpp
Jonas Bark 15a04604ab windows: register only window_manager + desktop_multi_window on sub-engine
Confirmed: calling RegisterPlugins (every plugin) on the secondary
engine deadlocks its boot before Dart main() runs. With registration
skipped entirely, Dart runs and we get MissingPluginException on
window_manager.ensureInitialized() — which means the overlay can now
launch as soon as it has just the plugins it actually uses.

Register only:
- WindowManagerPlugin — needed for size, position, alwaysOnTop, etc.
- DesktopMultiWindowPlugin — needed for the WindowMethodChannel that
  carries state updates from the parent.

The other ~17 plugins (BLE, IAP, key hooks, accessibility, media,
URL launcher, share, screen capture, ...) stay registered on the
MAIN engine only. The overlay never needs them.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 11:03:29 +02:00

98 lines
3.3 KiB
C++

#include "flutter_window.h"
#include <optional>
#include "flutter/generated_plugin_registrant.h"
#include <desktop_multi_window/desktop_multi_window_plugin.h>
#include <window_manager/window_manager_plugin.h>
FlutterWindow::FlutterWindow(const flutter::DartProject& project)
: project_(project) {}
FlutterWindow::~FlutterWindow() {}
bool FlutterWindow::OnCreate() {
if (!Win32Window::OnCreate()) {
return false;
}
RECT frame = GetClientArea();
// The size here must match the window dimensions to avoid unnecessary surface
// creation / destruction in the startup path.
flutter_controller_ = std::make_unique<flutter::FlutterViewController>(
frame.right - frame.left, frame.bottom - frame.top, project_);
// Ensure that basic setup of the controller was successful.
if (!flutter_controller_->engine() || !flutter_controller_->view()) {
return false;
}
RegisterPlugins(flutter_controller_->engine());
// Selectively register ONLY the plugins the trainer overlay sub-window
// actually needs.
//
// Calling `RegisterPlugins` (which calls every plugin's RegisterWith) on
// the secondary engine reliably deadlocks the sub-engine's boot before
// Dart `main()` runs — confirmed by toggling registration on/off as a
// diagnostic. Likely culprits are plugins that own process-singleton
// Windows resources already held by the main engine (windows_iap,
// media_key_detector_windows, bluetooth_low_energy_windows, etc.).
//
// The overlay uses only:
// - desktop_multi_window (WindowMethodChannel to talk to main)
// - window_manager (set window styling, position, alwaysOnTop)
DesktopMultiWindowSetWindowCreatedCallback([](void *controller) {
auto *fvc =
reinterpret_cast<flutter::FlutterViewController *>(controller);
auto *registry = fvc->engine();
WindowManagerPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("WindowManagerPlugin"));
DesktopMultiWindowPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("DesktopMultiWindowPlugin"));
});
SetChildContent(flutter_controller_->view()->GetNativeWindow());
flutter_controller_->engine()->SetNextFrameCallback([&]() {
this->Show();
});
// Flutter can complete the first frame before the "show window" callback is
// registered. The following call ensures a frame is pending to ensure the
// window is shown. It is a no-op if the first frame hasn't completed yet.
flutter_controller_->ForceRedraw();
return true;
}
void FlutterWindow::OnDestroy() {
if (flutter_controller_) {
flutter_controller_ = nullptr;
}
Win32Window::OnDestroy();
}
LRESULT
FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
WPARAM const wparam,
LPARAM const lparam) noexcept {
// Give Flutter, including plugins, an opportunity to handle window messages.
if (flutter_controller_) {
std::optional<LRESULT> result =
flutter_controller_->HandleTopLevelWindowProc(hwnd, message, wparam,
lparam);
if (result) {
return *result;
}
}
switch (message) {
case WM_FONTCHANGE:
flutter_controller_->engine()->ReloadSystemFonts();
break;
}
return Win32Window::MessageHandler(hwnd, message, wparam, lparam);
}