main
1#include "flutter_window.h"
2
3#include <optional>
4
5#include "flutter/generated_plugin_registrant.h"
6
7FlutterWindow::FlutterWindow(const flutter::DartProject& project)
8 : project_(project) {}
9
10FlutterWindow::~FlutterWindow() {}
11
12bool FlutterWindow::OnCreate() {
13 if (!Win32Window::OnCreate()) {
14 return false;
15 }
16
17 RECT frame = GetClientArea();
18
19 // The size here must match the window dimensions to avoid unnecessary surface
20 // creation / destruction in the startup path.
21 flutter_controller_ = std::make_unique<flutter::FlutterViewController>(
22 frame.right - frame.left, frame.bottom - frame.top, project_);
23 // Ensure that basic setup of the controller was successful.
24 if (!flutter_controller_->engine() || !flutter_controller_->view()) {
25 return false;
26 }
27 RegisterPlugins(flutter_controller_->engine());
28 SetChildContent(flutter_controller_->view()->GetNativeWindow());
29
30 flutter_controller_->engine()->SetNextFrameCallback([&]() {
31 this->Show();
32 });
33
34 return true;
35}
36
37void FlutterWindow::OnDestroy() {
38 if (flutter_controller_) {
39 flutter_controller_ = nullptr;
40 }
41
42 Win32Window::OnDestroy();
43}
44
45LRESULT
46FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
47 WPARAM const wparam,
48 LPARAM const lparam) noexcept {
49 // Give Flutter, including plugins, an opportunity to handle window messages.
50 if (flutter_controller_) {
51 std::optional<LRESULT> result =
52 flutter_controller_->HandleTopLevelWindowProc(hwnd, message, wparam,
53 lparam);
54 if (result) {
55 return *result;
56 }
57 }
58
59 switch (message) {
60 case WM_FONTCHANGE:
61 flutter_controller_->engine()->ReloadSystemFonts();
62 break;
63 }
64
65 return Win32Window::MessageHandler(hwnd, message, wparam, lparam);
66}