feat(windows): create my_plugin_windows (#5)
This commit is contained in:
parent
7ccf9524aa
commit
b09c1243c9
21
.github/workflows/my_plugin_windows.yaml
vendored
Normal file
21
.github/workflows/my_plugin_windows.yaml
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
name: my_plugin_windows
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
paths:
|
||||
- ".github/workflows/my_plugin_windows.yaml"
|
||||
- "src/my_plugin_windows/**"
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
paths:
|
||||
- ".github/workflows/my_plugin_windows.yaml"
|
||||
- "src/my_plugin_windows/**"
|
||||
|
||||
jobs:
|
||||
build:
|
||||
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/flutter_package.yml@v1
|
||||
with:
|
||||
flutter_channel: stable
|
||||
flutter_version: 2.10.1
|
||||
working_directory: src/my_plugin_windows
|
29
src/my_plugin_windows/.gitignore
vendored
Normal file
29
src/my_plugin_windows/.gitignore
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
# Miscellaneous
|
||||
*.class
|
||||
*.log
|
||||
*.pyc
|
||||
*.swp
|
||||
.DS_Store
|
||||
.atom/
|
||||
.buildlog/
|
||||
.history
|
||||
.svn/
|
||||
|
||||
# IntelliJ related
|
||||
*.iml
|
||||
*.ipr
|
||||
*.iws
|
||||
.idea/
|
||||
|
||||
# The .vscode folder contains launch configuration and tasks you configure in
|
||||
# VS Code which you may wish to be included in version control, so this line
|
||||
# is commented out by default.
|
||||
#.vscode/
|
||||
|
||||
# Flutter/Dart/Pub related
|
||||
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
|
||||
/pubspec.lock
|
||||
**/doc/api/
|
||||
.dart_tool/
|
||||
.packages
|
||||
build/
|
10
src/my_plugin_windows/.metadata
Normal file
10
src/my_plugin_windows/.metadata
Normal file
@ -0,0 +1,10 @@
|
||||
# This file tracks properties of this Flutter project.
|
||||
# Used by Flutter tool to assess capabilities and perform upgrades etc.
|
||||
#
|
||||
# This file should be version controlled and should not be manually edited.
|
||||
|
||||
version:
|
||||
revision: 77d935af4db863f6abd0b9c31c7e6df2a13de57b
|
||||
channel: stable
|
||||
|
||||
project_type: plugin
|
3
src/my_plugin_windows/CHANGELOG.md
Normal file
3
src/my_plugin_windows/CHANGELOG.md
Normal file
@ -0,0 +1,3 @@
|
||||
# 0.1.0+1
|
||||
|
||||
- Initial release of this plugin.
|
21
src/my_plugin_windows/LICENSE
Normal file
21
src/my_plugin_windows/LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 Very Good Ventures
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
14
src/my_plugin_windows/README.md
Normal file
14
src/my_plugin_windows/README.md
Normal file
@ -0,0 +1,14 @@
|
||||
# my_plugin_windows
|
||||
|
||||
[![style: very good analysis][very_good_analysis_badge]][very_good_analysis_link]
|
||||
|
||||
The windows implementation of `my_plugin`.
|
||||
|
||||
## Usage
|
||||
|
||||
This package is [endorsed][endorsed_link], which means you can simply use `my_plugin`
|
||||
normally. This package will be automatically included in your app when you do.
|
||||
|
||||
[endorsed_link]: https://flutter.dev/docs/development/packages-and-plugins/developing-packages#endorsed-federated-plugin
|
||||
[very_good_analysis_badge]: https://img.shields.io/badge/style-very_good_analysis-B22C89.svg
|
||||
[very_good_analysis_link]: https://pub.dev/packages/very_good_analysis
|
1
src/my_plugin_windows/analysis_options.yaml
Normal file
1
src/my_plugin_windows/analysis_options.yaml
Normal file
@ -0,0 +1 @@
|
||||
include: package:very_good_analysis/analysis_options.2.4.0.yaml
|
20
src/my_plugin_windows/lib/my_plugin_windows.dart
Normal file
20
src/my_plugin_windows/lib/my_plugin_windows.dart
Normal file
@ -0,0 +1,20 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:my_plugin_platform_interface/my_plugin_platform_interface.dart';
|
||||
|
||||
/// The Windows implementation of [MyPluginPlatform].
|
||||
class MyPluginWindows extends MyPluginPlatform {
|
||||
/// The method channel used to interact with the native platform.
|
||||
@visibleForTesting
|
||||
final methodChannel = const MethodChannel('my_plugin_windows');
|
||||
|
||||
/// Registers this class as the default instance of [MyPluginPlatform]
|
||||
static void registerWith() {
|
||||
MyPluginPlatform.instance = MyPluginWindows();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> getPlatformName() {
|
||||
return methodChannel.invokeMethod<String>('getPlatformName');
|
||||
}
|
||||
}
|
27
src/my_plugin_windows/pubspec.yaml
Normal file
27
src/my_plugin_windows/pubspec.yaml
Normal file
@ -0,0 +1,27 @@
|
||||
name: my_plugin_windows
|
||||
description: Windows implementation of the my_plugin plugin
|
||||
version: 0.1.0+1
|
||||
publish_to: none
|
||||
|
||||
environment:
|
||||
sdk: ">=2.12.0 <3.0.0"
|
||||
flutter: ">=2.0.0"
|
||||
|
||||
flutter:
|
||||
plugin:
|
||||
implements: my_plugin
|
||||
platforms:
|
||||
windows:
|
||||
pluginClass: MyPluginWindows
|
||||
dartPluginClass: MyPluginWindows
|
||||
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
my_plugin_platform_interface:
|
||||
path: ../my_plugin_platform_interface
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
very_good_analysis: ^2.4.0
|
44
src/my_plugin_windows/test/my_plugin_windows_test.dart
Normal file
44
src/my_plugin_windows/test/my_plugin_windows_test.dart
Normal file
@ -0,0 +1,44 @@
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:my_plugin_platform_interface/my_plugin_platform_interface.dart';
|
||||
import 'package:my_plugin_windows/my_plugin_windows.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('MyPluginWindows', () {
|
||||
const kPlatformName = 'Windows';
|
||||
late MyPluginWindows myPlugin;
|
||||
late List<MethodCall> log;
|
||||
|
||||
setUp(() async {
|
||||
myPlugin = MyPluginWindows();
|
||||
|
||||
log = <MethodCall>[];
|
||||
TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(myPlugin.methodChannel, (methodCall) async {
|
||||
log.add(methodCall);
|
||||
switch (methodCall.method) {
|
||||
case 'getPlatformName':
|
||||
return kPlatformName;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('can be registered', () {
|
||||
MyPluginWindows.registerWith();
|
||||
expect(MyPluginPlatform.instance, isA<MyPluginWindows>());
|
||||
});
|
||||
|
||||
test('getPlatformName returns correct name', () async {
|
||||
final name = await myPlugin.getPlatformName();
|
||||
expect(
|
||||
log,
|
||||
<Matcher>[isMethodCall('getPlatformName', arguments: null)],
|
||||
);
|
||||
expect(name, equals(kPlatformName));
|
||||
});
|
||||
});
|
||||
}
|
17
src/my_plugin_windows/windows/.gitignore
vendored
Normal file
17
src/my_plugin_windows/windows/.gitignore
vendored
Normal 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/
|
22
src/my_plugin_windows/windows/CMakeLists.txt
Normal file
22
src/my_plugin_windows/windows/CMakeLists.txt
Normal file
@ -0,0 +1,22 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
set(PROJECT_NAME "my_plugin_windows")
|
||||
project(${PROJECT_NAME} LANGUAGES CXX)
|
||||
|
||||
set(PLUGIN_NAME "${PROJECT_NAME}_plugin")
|
||||
|
||||
add_library(${PLUGIN_NAME} SHARED
|
||||
"my_plugin_windows_plugin.cpp"
|
||||
"include/my_plugin_windows/my_plugin_windows.h"
|
||||
)
|
||||
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(my_plugin_bundled_libraries
|
||||
""
|
||||
PARENT_SCOPE
|
||||
)
|
@ -0,0 +1,23 @@
|
||||
#ifndef FLUTTER_PLUGIN_MY_PLUGIN_WINDOWS_PLUGIN_H_
|
||||
#define FLUTTER_PLUGIN_MY_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 MyPluginWindowsRegisterWithRegistrar(
|
||||
FlutterDesktopPluginRegistrarRef registrar);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#endif // FLUTTER_PLUGIN_MY_PLUGIN_WINDOWS_PLUGIN_H_
|
72
src/my_plugin_windows/windows/my_plugin_windows_plugin.cpp
Normal file
72
src/my_plugin_windows/windows/my_plugin_windows_plugin.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
#include "include/my_plugin_windows/my_plugin_windows.h"
|
||||
|
||||
// This must be included before many other Windows headers.
|
||||
#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 {
|
||||
|
||||
using flutter::EncodableValue;
|
||||
|
||||
class MyPluginWindows : public flutter::Plugin {
|
||||
public:
|
||||
static void RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar);
|
||||
|
||||
MyPluginWindows();
|
||||
|
||||
virtual ~MyPluginWindows();
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
// static
|
||||
void MyPluginWindows::RegisterWithRegistrar(
|
||||
flutter::PluginRegistrarWindows *registrar) {
|
||||
auto channel =
|
||||
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
|
||||
registrar->messenger(), "my_plugin_windows",
|
||||
&flutter::StandardMethodCodec::GetInstance());
|
||||
|
||||
auto plugin = std::make_unique<MyPluginWindows>();
|
||||
|
||||
channel->SetMethodCallHandler(
|
||||
[plugin_pointer = plugin.get()](const auto &call, auto result) {
|
||||
plugin_pointer->HandleMethodCall(call, std::move(result));
|
||||
});
|
||||
|
||||
registrar->AddPlugin(std::move(plugin));
|
||||
}
|
||||
|
||||
MyPluginWindows::MyPluginWindows() {}
|
||||
|
||||
MyPluginWindows::~MyPluginWindows() {}
|
||||
|
||||
void MyPluginWindows::HandleMethodCall(
|
||||
const flutter::MethodCall<flutter::EncodableValue> &method_call,
|
||||
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
if (method_call.method_name().compare("getPlatformName") == 0) {
|
||||
result->Success(EncodableValue("Windows"));
|
||||
}
|
||||
else {
|
||||
result->NotImplemented();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void MyPluginWindowsRegisterWithRegistrar(
|
||||
FlutterDesktopPluginRegistrarRef registrar) {
|
||||
MyPluginWindows::RegisterWithRegistrar(
|
||||
flutter::PluginRegistrarManager::GetInstance()
|
||||
->GetRegistrar<flutter::PluginRegistrarWindows>(registrar));
|
||||
}
|
Reference in New Issue
Block a user