flutter_window.cpp 2.2 KB

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