flutter_window.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "flutter_window.h"
  2. #include <optional>
  3. #include "flutter/generated_plugin_registrant.h"
  4. #include "flutter/method_channel.h"
  5. #include "flutter/standard_method_codec.h"
  6. void myMethodChannel(flutter::FlutterEngine*);
  7. FlutterWindow::FlutterWindow(const flutter::DartProject& project)
  8. : project_(project) {}
  9. FlutterWindow::~FlutterWindow() {}
  10. bool FlutterWindow::OnCreate() {
  11. if (!Win32Window::OnCreate()) {
  12. return false;
  13. }
  14. RECT frame = GetClientArea();
  15. // The size here must match the window dimensions to avoid unnecessary surface
  16. // creation / destruction in the startup path.
  17. flutter_controller_ = std::make_unique<flutter::FlutterViewController>(
  18. frame.right - frame.left, frame.bottom - frame.top, project_);
  19. // Ensure that basic setup of the controller was successful.
  20. if (!flutter_controller_->engine() || !flutter_controller_->view()) {
  21. return false;
  22. }
  23. RegisterPlugins(flutter_controller_->engine());
  24. SetChildContent(flutter_controller_->view()->GetNativeWindow());
  25. myMethodChannel(flutter_controller_->engine());
  26. return true;
  27. }
  28. // flutter method register
  29. void myMethodChannel(flutter::FlutterEngine* engine) {
  30. std::string channelName = "pulgin.pc.o2oa.net/notify";
  31. const flutter::StandardMethodCodec& codec = flutter::StandardMethodCodec::GetInstance();
  32. flutter::MethodChannel method_channel_(engine->messenger(), channelName, &codec);
  33. method_channel_.SetMethodCallHandler([](const flutter::MethodCall<>& call,
  34. std::unique_ptr<flutter::MethodResult<>> result) {
  35. if (call.method_name().compare("sendNotify") == 0) {
  36. const auto* arguments = std::get_if<flutter::EncodableMap>(call.arguments());
  37. std::string msgId;
  38. auto msgId_it = arguments->find(flutter::EncodableValue("msgId"));
  39. if (msgId_it != arguments->end()) {
  40. msgId = std::get<std::string>(msgId_it->second);
  41. }
  42. std::string msgTitle;
  43. auto msgTitle_it = arguments->find(flutter::EncodableValue("title"));
  44. if (msgTitle_it != arguments->end()) {
  45. msgTitle = std::get<std::string>(msgTitle_it->second);
  46. }
  47. std::string msgBody;
  48. auto msgBody_it = arguments->find(flutter::EncodableValue("msgBody"));
  49. if (msgBody_it != arguments->end()) {
  50. msgBody = std::get<std::string>(msgBody_it->second);
  51. }
  52. std::cout << "Native:Halo!" << std::endl;
  53. std::cout << "receive msg content : " << msgId << msgTitle << msgBody << std::endl;
  54. result->Success(flutter::EncodableValue("Call Native Successfully!"));
  55. //result->Error(flutter::EncodableValue("Call Native Failed!"));
  56. }
  57. });
  58. }
  59. void FlutterWindow::OnDestroy() {
  60. if (flutter_controller_) {
  61. flutter_controller_ = nullptr;
  62. }
  63. Win32Window::OnDestroy();
  64. }
  65. LRESULT
  66. FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
  67. WPARAM const wparam,
  68. LPARAM const lparam) noexcept {
  69. // Give Flutter, including plugins, an opportunity to handle window messages.
  70. if (flutter_controller_) {
  71. std::optional<LRESULT> result =
  72. flutter_controller_->HandleTopLevelWindowProc(hwnd, message, wparam,
  73. lparam);
  74. if (result) {
  75. return *result;
  76. }
  77. }
  78. switch (message) {
  79. case WM_FONTCHANGE:
  80. flutter_controller_->engine()->ReloadSystemFonts();
  81. break;
  82. }
  83. return Win32Window::MessageHandler(hwnd, message, wparam, lparam);
  84. }