You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.7 KiB

2 years ago
  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. }