Files
swiftcontrol/windows/runner/flutter_window.cpp
Jonas Bark c39d4191f6 overlay/desktop: switch to multi_window_native
Drops desktop_multi_window 0.3.0 in favour of multi_window_native 1.0.4
on a new branch (feature/trainer-overlay-multi-window-native).

Mechanism is different but the responsibilities map cleanly:
- `MultiWindowNative.createWindow([routeName, argsJson, theme])` spawns
  the sub-window; main() detects it via the first arg.
- Communication is broadcast by method name through
  `notifyAllWindows` + `registerListener`. Conventions:
    main → overlay: trainerOverlay.state
    overlay → main: trainerOverlay.action / ready / positionChanged
  Each side filters by method name; the API has no per-window targeting.
- The overlay window grabs its windowId via `windowManager.getId()` and
  broadcasts `trainerOverlay.ready` so the controller can later close it.
- `notifyUiRendered()` is called after the first frame, per the package's
  black-screen workaround on macOS/Windows.

Native shims:
- macOS MainFlutterWindow.swift: re-register plugins on every new
  sub-engine via `MultiWindowNativePlugin.onEngineCreatedCallback`. Kept
  the `.statusBar` / `.fullScreenAuxiliary` elevation for visibility
  above fullscreened trainer apps; the callback now finds the new
  sub-window's NSWindow by matching the FlutterViewController's engine.
- Windows flutter_window.cpp: reverted the desktop_multi_window callback;
  multi_window_native's Windows wiring requires a more invasive
  flutter_window.h / cpp change (SetOnCloseCallback, GetFlutterViewController,
  callback registration in main.cpp) which is TBD. The overlay is only
  verified on macOS in this branch.

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

77 lines
2.3 KiB
C++

#include "flutter_window.h"
#include <optional>
#include "flutter/generated_plugin_registrant.h"
// NOTE: multi_window_native ^1.0.4 requires Windows runner edits
// (SetOnCloseCallback / GetFlutterViewController on FlutterWindow plus a
// callback registration here) that aren't applied yet. The desktop overlay
// is currently only verified on macOS; Windows wiring is TBD.
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());
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);
}