flutter_window.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. return true;
  23. }
  24. void FlutterWindow::OnDestroy() {
  25. if (flutter_controller_) {
  26. flutter_controller_ = nullptr;
  27. }
  28. Win32Window::OnDestroy();
  29. }
  30. LRESULT
  31. FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
  32. WPARAM const wparam,
  33. LPARAM const lparam) noexcept {
  34. // Give Flutter, including plugins, an opportunity to handle window messages.
  35. if (flutter_controller_) {
  36. std::optional<LRESULT> result =
  37. flutter_controller_->HandleTopLevelWindowProc(hwnd, message, wparam,
  38. lparam);
  39. if (result) {
  40. return *result;
  41. }
  42. }
  43. switch (message) {
  44. case WM_FONTCHANGE:
  45. flutter_controller_->engine()->ReloadSystemFonts();
  46. break;
  47. }
  48. return Win32Window::MessageHandler(hwnd, message, wparam, lparam);
  49. }