flutter_window.cpp 2.1 KB

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