appflowy_backend_plugin.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // This must be included before many other Windows headers.
  2. #include <windows.h>
  3. // For getPlatformVersion; remove unless needed for your plugin implementation.
  4. #include <VersionHelpers.h>
  5. #include <flutter/method_channel.h>
  6. #include <flutter/plugin_registrar_windows.h>
  7. #include <flutter/standard_method_codec.h>
  8. #include <map>
  9. #include <memory>
  10. #include <sstream>
  11. #include "include/appflowy_backend/app_flowy_backend_plugin.h"
  12. namespace
  13. {
  14. class AppFlowyBackendPlugin : public flutter::Plugin
  15. {
  16. public:
  17. static void RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar);
  18. AppFlowyBackendPlugin();
  19. virtual ~AppFlowyBackendPlugin();
  20. private:
  21. // Called when a method is called on this plugin's channel from Dart.
  22. void HandleMethodCall(
  23. const flutter::MethodCall<flutter::EncodableValue> &method_call,
  24. std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
  25. };
  26. // static
  27. void AppFlowyBackendPlugin::RegisterWithRegistrar(
  28. flutter::PluginRegistrarWindows *registrar)
  29. {
  30. auto channel =
  31. std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
  32. registrar->messenger(), "appflowy_backend",
  33. &flutter::StandardMethodCodec::GetInstance());
  34. auto plugin = std::make_unique<AppFlowyBackendPlugin>();
  35. channel->SetMethodCallHandler(
  36. [plugin_pointer = plugin.get()](const auto &call, auto result)
  37. {
  38. plugin_pointer->HandleMethodCall(call, std::move(result));
  39. });
  40. registrar->AddPlugin(std::move(plugin));
  41. }
  42. AppFlowyBackendPlugin::AppFlowyBackendPlugin() {}
  43. AppFlowyBackendPlugin::~AppFlowyBackendPlugin() {}
  44. void AppFlowyBackendPlugin::HandleMethodCall(
  45. const flutter::MethodCall<flutter::EncodableValue> &method_call,
  46. std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result)
  47. {
  48. if (method_call.method_name().compare("getPlatformVersion") == 0)
  49. {
  50. std::ostringstream version_stream;
  51. version_stream << "Windows ";
  52. if (IsWindows10OrGreater())
  53. {
  54. version_stream << "10+";
  55. }
  56. else if (IsWindows8OrGreater())
  57. {
  58. version_stream << "8";
  59. }
  60. else if (IsWindows7OrGreater())
  61. {
  62. version_stream << "7";
  63. }
  64. result->Success(flutter::EncodableValue(version_stream.str()));
  65. }
  66. else
  67. {
  68. result->NotImplemented();
  69. }
  70. }
  71. } // namespace
  72. void AppFlowyBackendPluginRegisterWithRegistrar(
  73. FlutterDesktopPluginRegistrarRef registrar)
  74. {
  75. AppFlowyBackendPlugin::RegisterWithRegistrar(
  76. flutter::PluginRegistrarManager::GetInstance()
  77. ->GetRegistrar<flutter::PluginRegistrarWindows>(registrar));
  78. }