flutter_window.cpp 1.81 KB
Newer Older
1 2
#include "flutter_window.h"

3 4
#include <optional>

5 6
#include "flutter/generated_plugin_registrant.h"

7 8
FlutterWindow::FlutterWindow(const flutter::DartProject& project)
    : project_(project) {}
9 10 11

FlutterWindow::~FlutterWindow() {}

12 13 14 15
bool FlutterWindow::OnCreate() {
  if (!Win32Window::OnCreate()) {
    return false;
  }
16

17 18
  RECT frame = GetClientArea();

19 20 21 22
  // The size here must match the window dimensions to avoid unnecessary surface
  // creation / destruction in the startup path.
  flutter_controller_ = std::make_unique<flutter::FlutterViewController>(
      frame.right - frame.left, frame.bottom - frame.top, project_);
23 24 25 26
  // Ensure that basic setup of the controller was successful.
  if (!flutter_controller_->engine() || !flutter_controller_->view()) {
    return false;
  }
27
  RegisterPlugins(flutter_controller_->engine());
28
  SetChildContent(flutter_controller_->view()->GetNativeWindow());
29 30 31 32 33

  flutter_controller_->engine()->SetNextFrameCallback([&]() {
    this->Show();
  });

34
  return true;
35 36 37 38 39 40 41 42 43
}

void FlutterWindow::OnDestroy() {
  if (flutter_controller_) {
    flutter_controller_ = nullptr;
  }

  Win32Window::OnDestroy();
}
44 45 46 47 48

LRESULT
FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
                              WPARAM const wparam,
                              LPARAM const lparam) noexcept {
49
  // Give Flutter, including plugins, an opportunity to handle window messages.
50 51 52 53 54 55 56 57
  if (flutter_controller_) {
    std::optional<LRESULT> result =
        flutter_controller_->HandleTopLevelWindowProc(hwnd, message, wparam,
                                                      lparam);
    if (result) {
      return *result;
    }
  }
58 59 60 61 62 63 64

  switch (message) {
    case WM_FONTCHANGE:
      flutter_controller_->engine()->ReloadSystemFonts();
      break;
  }

65 66
  return Win32Window::MessageHandler(hwnd, message, wparam, lparam);
}