flowy_sdk_plugin.cpp 2.4 KB

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