flutter_window.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. return true;
  26. }
  27. void FlutterWindow::OnDestroy() {
  28. if (flutter_controller_) {
  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. }