flutter_window.cpp 1.9 KB

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