|
1 | 1 | #include "flutter_window.h" |
2 | 2 |
|
| 3 | +#include <optional> |
| 4 | + |
3 | 5 | #include "flutter/generated_plugin_registrant.h" |
4 | 6 |
|
5 | | -FlutterWindow::FlutterWindow(RunLoop* run_loop, |
6 | | - const flutter::DartProject& project) |
7 | | - : run_loop_(run_loop), project_(project) {} |
| 7 | +FlutterWindow::FlutterWindow(const flutter::DartProject& project) |
| 8 | + : project_(project) {} |
8 | 9 |
|
9 | 10 | FlutterWindow::~FlutterWindow() {} |
10 | 11 |
|
11 | | -void FlutterWindow::OnCreate() { |
12 | | - Win32Window::OnCreate(); |
| 12 | +bool FlutterWindow::OnCreate() { |
| 13 | + if (!Win32Window::OnCreate()) { |
| 14 | + return false; |
| 15 | + } |
| 16 | + |
| 17 | + RECT frame = GetClientArea(); |
13 | 18 |
|
14 | | - // The size here is arbitrary since SetChildContent will resize it. |
15 | | - flutter_controller_ = |
16 | | - std::make_unique<flutter::FlutterViewController>(100, 100, project_); |
17 | | - RegisterPlugins(flutter_controller_.get()); |
18 | | - run_loop_->RegisterFlutterInstance(flutter_controller_.get()); |
| 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()); |
19 | 28 | SetChildContent(flutter_controller_->view()->GetNativeWindow()); |
| 29 | + return true; |
20 | 30 | } |
21 | 31 |
|
22 | 32 | void FlutterWindow::OnDestroy() { |
23 | 33 | if (flutter_controller_) { |
24 | | - run_loop_->UnregisterFlutterInstance(flutter_controller_.get()); |
25 | 34 | flutter_controller_ = nullptr; |
26 | 35 | } |
27 | 36 |
|
28 | 37 | Win32Window::OnDestroy(); |
29 | 38 | } |
| 39 | + |
| 40 | +LRESULT |
| 41 | +FlutterWindow::MessageHandler(HWND hwnd, UINT const message, |
| 42 | + WPARAM const wparam, |
| 43 | + LPARAM const lparam) noexcept { |
| 44 | + // Give Flutter, including plugins, an opportunity to handle window messages. |
| 45 | + if (flutter_controller_) { |
| 46 | + std::optional<LRESULT> result = |
| 47 | + flutter_controller_->HandleTopLevelWindowProc(hwnd, message, wparam, |
| 48 | + lparam); |
| 49 | + if (result) { |
| 50 | + return *result; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + switch (message) { |
| 55 | + case WM_FONTCHANGE: |
| 56 | + flutter_controller_->engine()->ReloadSystemFonts(); |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + return Win32Window::MessageHandler(hwnd, message, wparam, lparam); |
| 61 | +} |
0 commit comments