flutter_window.cpp (2122B)
1 #include "flutter_window.h" 2 3 #include <optional> 4 5 #include "flutter/generated_plugin_registrant.h" 6 7 FlutterWindow::FlutterWindow(const flutter::DartProject& project) 8 : project_(project) {} 9 10 FlutterWindow::~FlutterWindow() {} 11 12 bool 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 // Flutter can complete the first frame before the "show window" callback is 35 // registered. The following call ensures a frame is pending to ensure the 36 // window is shown. It is a no-op if the first frame hasn't completed yet. 37 flutter_controller_->ForceRedraw(); 38 39 return true; 40 } 41 42 void FlutterWindow::OnDestroy() { 43 if (flutter_controller_) { 44 flutter_controller_ = nullptr; 45 } 46 47 Win32Window::OnDestroy(); 48 } 49 50 LRESULT 51 FlutterWindow::MessageHandler(HWND hwnd, UINT const message, 52 WPARAM const wparam, 53 LPARAM const lparam) noexcept { 54 // Give Flutter, including plugins, an opportunity to handle window messages. 55 if (flutter_controller_) { 56 std::optional<LRESULT> result = 57 flutter_controller_->HandleTopLevelWindowProc(hwnd, message, wparam, 58 lparam); 59 if (result) { 60 return *result; 61 } 62 } 63 64 switch (message) { 65 case WM_FONTCHANGE: 66 flutter_controller_->engine()->ReloadSystemFonts(); 67 break; 68 } 69 70 return Win32Window::MessageHandler(hwnd, message, wparam, lparam); 71 }