1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-10 14:58:34 +00:00

[federated_plugin] adds support for Windows (#533)

This commit is contained in:
Ayush Bherwani
2020-09-18 04:48:20 +05:30
committed by GitHub
parent 9b731e8556
commit 358485d7fa
36 changed files with 1364 additions and 2 deletions

View File

@@ -0,0 +1,17 @@
flutter/
# Visual Studio user-specific files.
*.suo
*.user
*.userosscache
*.sln.docstates
# Visual Studio build-related files.
x64/
x86/
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/

View File

@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.15)
set(PROJECT_NAME "federated_plugin_windows")
project(${PROJECT_NAME} LANGUAGES CXX)
set(PLUGIN_NAME "${PROJECT_NAME}_plugin")
add_library(${PLUGIN_NAME} SHARED
"${PLUGIN_NAME}.cpp"
)
apply_standard_settings(${PLUGIN_NAME})
set_target_properties(${PLUGIN_NAME} PROPERTIES
CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
target_include_directories(${PLUGIN_NAME} INTERFACE
"${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin)
# List of absolute paths to libraries that should be bundled with the plugin
set(federated_plugin_windows_bundled_libraries
""
PARENT_SCOPE
)

View File

@@ -0,0 +1,87 @@
// Copyright 2020 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "include/federated_plugin_windows/federated_plugin_windows_plugin.h"
#include <windows.h>
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#include <flutter/standard_method_codec.h>
#include <map>
#include <memory>
namespace {
class FederatedPluginWindowsPlugin : public flutter::Plugin {
public:
static void RegisterWithRegistrar(flutter::PluginRegistrarWindows* registrar);
FederatedPluginWindowsPlugin();
virtual ~FederatedPluginWindowsPlugin();
private:
// Called when a method is called on this plugin's channel from Dart.
void HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue>& method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
};
void FederatedPluginWindowsPlugin::RegisterWithRegistrar(
flutter::PluginRegistrarWindows* registrar) {
auto channel =
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
registrar->messenger(), "battery",
&flutter::StandardMethodCodec::GetInstance());
auto plugin = std::make_unique<FederatedPluginWindowsPlugin>();
channel->SetMethodCallHandler(
[plugin_pointer = plugin.get()](const auto& call, auto result) {
plugin_pointer->HandleMethodCall(call, std::move(result));
});
registrar->AddPlugin(std::move(plugin));
}
FederatedPluginWindowsPlugin::FederatedPluginWindowsPlugin() {}
FederatedPluginWindowsPlugin::~FederatedPluginWindowsPlugin() {}
void FederatedPluginWindowsPlugin::HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue>& method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
if (method_call.method_name().compare("getBatteryLevel") == 0) {
SYSTEM_POWER_STATUS systemPower;
// GetSystemPowerStatus will retrieve the power status of the system.
if (GetSystemPowerStatus(&systemPower)) {
int batteryLevel = systemPower.BatteryLifePercent;
// The batteryLevel value in the range 0 to 100, or 255 if status is unknown.
if (batteryLevel != 255) {
flutter::EncodableValue response(batteryLevel);
result->Success(&response);
}
else {
result->Error("STATUS_UNAVAILABLE", "Not able to determine battery level.");
}
}
else {
result->Error("STATUS_UNAVAILABLE", "Not able to determine battery level.");
}
}
else {
result->NotImplemented();
}
}
}
void FederatedPluginWindowsPluginRegisterWithRegistrar(
FlutterDesktopPluginRegistrarRef registrar) {
FederatedPluginWindowsPlugin::RegisterWithRegistrar(
flutter::PluginRegistrarManager::GetInstance()
->GetRegistrar<flutter::PluginRegistrarWindows>(registrar));
}

View File

@@ -0,0 +1,23 @@
#ifndef FLUTTER_PLUGIN_FEDERATED_PLUGIN_WINDOWS_PLUGIN_H_
#define FLUTTER_PLUGIN_FEDERATED_PLUGIN_WINDOWS_PLUGIN_H_
#include <flutter_plugin_registrar.h>
#ifdef FLUTTER_PLUGIN_IMPL
#define FLUTTER_PLUGIN_EXPORT __declspec(dllexport)
#else
#define FLUTTER_PLUGIN_EXPORT __declspec(dllimport)
#endif
#if defined(__cplusplus)
extern "C" {
#endif
FLUTTER_PLUGIN_EXPORT void FederatedPluginWindowsPluginRegisterWithRegistrar(
FlutterDesktopPluginRegistrarRef registrar);
#if defined(__cplusplus)
} // extern "C"
#endif
#endif // FLUTTER_PLUGIN_FEDERATED_PLUGIN_WINDOWS_PLUGIN_H_