Advertisement
FlyFar

flutter_window.cpp

Jul 25th, 2023
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | Cybersecurity | 0 0
  1. #include "flutter_window.h"
  2.  
  3. #include <optional>
  4.  
  5. #include "flutter/generated_plugin_registrant.h"
  6.  
  7. FlutterWindow::FlutterWindow(const flutter::DartProject& project)
  8.     : project_(project) {}
  9.  
  10. FlutterWindow::~FlutterWindow() {}
  11.  
  12. bool FlutterWindow::OnCreate() {
  13.   if (!Win32Window::OnCreate()) {
  14.     return false;
  15.   }
  16.  
  17.   RECT frame = GetClientArea();
  18.  
  19.   // The size here must match the window dimensions to avoid unnecessary surface
  20.   // creation / destruction in the startup path.
  21.   flutter_controller_ = std::make_unique<flutter::FlutterViewController>(
  22.       frame.right - frame.left, frame.bottom - frame.top, project_);
  23.   // Ensure that basic setup of the controller was successful.
  24.   if (!flutter_controller_->engine() || !flutter_controller_->view()) {
  25.     return false;
  26.   }
  27.   RegisterPlugins(flutter_controller_->engine());
  28.   SetChildContent(flutter_controller_->view()->GetNativeWindow());
  29.   return true;
  30. }
  31.  
  32. void FlutterWindow::OnDestroy() {
  33.   if (flutter_controller_) {
  34.     flutter_controller_ = nullptr;
  35.   }
  36.  
  37.   Win32Window::OnDestroy();
  38. }
  39.  
  40. LRESULT
  41. FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
  42.                               WPARAM const wparam,
  43.                               LPARAM const lparam) noexcept {
  44.   // Give Flutter, including plugins, an opportunity to handle window messages.
  45.   if (flutter_controller_) {
  46.     std::optional<LRESULT> result =
  47.         flutter_controller_->HandleTopLevelWindowProc(hwnd, message, wparam,
  48.                                                       lparam);
  49.     if (result) {
  50.       return *result;
  51.     }
  52.   }
  53.  
  54.   switch (message) {
  55.     case WM_FONTCHANGE:
  56.       flutter_controller_->engine()->ReloadSystemFonts();
  57.       break;
  58.   }
  59.  
  60.   return Win32Window::MessageHandler(hwnd, message, wparam, lparam);
  61. }
Tags: flutter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement