Skip to content

Commit 4517fcf

Browse files
committed
feat(share_plus)!: native share dialog for windows
* Platform specific implementation for using DataTransferManager on Windows * DataTransferManager is only used on (& available for) Windows 10 builds 17763 or higher. * On older Windows versions, existing old implementation is used i.e. launching mailto: URIs with subject & body query parameters. * Getting the share result is not supported on Windows. Thus, shareWithResult & shareFilesWithResult always return ShareResultStatus.unavailable. * This platform specific implementation beings supports for all existing methods available in package:share_plus to Windows, namely: share, shareFiles, shareWithResult, shareFilesWithResult, shareXFiles.
1 parent 98d31e6 commit 4517fcf

File tree

11 files changed

+1096
-6
lines changed

11 files changed

+1096
-6
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Defines the Chromium style for automatic reformatting.
2+
# http://clang.llvm.org/docs/ClangFormatStyleOptions.html
3+
BasedOnStyle: Chromium
4+
# This defaults to 'Auto'. Explicitly set it for a while, so that
5+
# 'vector<vector<int> >' in existing files gets formatted to
6+
# 'vector<vector<int>>'. ('Auto' means that clang-format will only use
7+
# 'int>>' if the file already contains at least one such instance.)
8+
Standard: Cpp11
9+
SortIncludes: true
10+
---
11+
Language: ObjC
12+
ColumnLimit: 100

packages/share_plus/share_plus_windows/lib/share_plus_windows.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ library share_plus_windows;
33

44
import 'dart:ui';
55

6+
import 'package:share_plus_windows/src/version_helper.dart';
67
import 'package:url_launcher/url_launcher.dart';
78
import 'package:share_plus_platform_interface/share_plus_platform_interface.dart';
89

9-
/// The Windows implementation of SharePlatform.
10+
/// The fallback Windows implementation of [SharePlatform], for older Windows versions.
11+
///
1012
class ShareWindows extends SharePlatform {
11-
/// Register this dart class as the platform implementation for linux
13+
/// If the modern Share UI i.e. `DataTransferManager` is not available, then use this Dart class instead of platform specific implementation.
14+
///
1215
static void registerWith() {
13-
SharePlatform.instance = ShareWindows();
16+
if (!VersionHelper.instance.isWindows10RS5OrGreater) {
17+
SharePlatform.instance = ShareWindows();
18+
}
1419
}
1520

1621
/// Share text.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import 'dart:io';
2+
import 'dart:ffi';
3+
import 'package:ffi/ffi.dart';
4+
import 'package:win32/win32.dart';
5+
6+
class VersionHelper {
7+
static VersionHelper instance = VersionHelper._();
8+
9+
/// Whether the current OS is Windows 10 Redstone 5 or later.
10+
/// This is used to determine whether the modern Share UI i.e. `DataTransferManager` is available or not.
11+
///
12+
/// References: https://en.wikipedia.org/wiki/Windows_10_version_history
13+
/// https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-osversioninfoexa
14+
///
15+
bool isWindows10RS5OrGreater = false;
16+
17+
static const int _kWindows10RS5BuildNumber = 17763;
18+
19+
VersionHelper._() {
20+
if (Platform.isWindows) {
21+
final pointer = calloc<OSVERSIONINFOEX>();
22+
pointer.ref
23+
..dwOSVersionInfoSize = sizeOf<OSVERSIONINFOEX>()
24+
..dwBuildNumber = 0
25+
..dwMajorVersion = 0
26+
..dwMinorVersion = 0
27+
..dwPlatformId = 0
28+
..szCSDVersion = ''
29+
..wServicePackMajor = 0
30+
..wServicePackMinor = 0
31+
..wSuiteMask = 0
32+
..wProductType = 0
33+
..wReserved = 0;
34+
final rtlGetVersion = DynamicLibrary.open('ntdll.dll').lookupFunction<
35+
Void Function(Pointer<OSVERSIONINFOEX>),
36+
void Function(Pointer<OSVERSIONINFOEX>)>('RtlGetVersion');
37+
rtlGetVersion(pointer);
38+
isWindows10RS5OrGreater =
39+
pointer.ref.dwBuildNumber >= _kWindows10RS5BuildNumber;
40+
calloc.free(pointer);
41+
}
42+
}
43+
}

packages/share_plus/share_plus_windows/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ dependencies:
1414
sdk: flutter
1515
meta: ^1.7.0
1616
url_launcher: ^6.1.2
17+
ffi: ^2.0.1
18+
win32: ^2.3.8
1719

1820
dev_dependencies:
1921
flutter_test:
@@ -23,8 +25,6 @@ dev_dependencies:
2325

2426
flutter:
2527
plugin:
26-
implements: share_plus
2728
platforms:
2829
windows:
29-
dartPluginClass: ShareWindows
30-
pluginClass: none
30+
pluginClass: SharePlusWindowsPluginCApi
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
flutter/
2+
3+
# Visual Studio user-specific files.
4+
*.suo
5+
*.user
6+
*.userosscache
7+
*.sln.docstates
8+
9+
# Visual Studio build-related files.
10+
x64/
11+
x86/
12+
13+
# Visual Studio cache files
14+
# files ending in .cache can be ignored
15+
*.[Cc]ache
16+
# but keep track of directories ending in .cache
17+
!*.[Cc]ache/
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# The Flutter tooling requires that developers have a version of Visual Studio
2+
# installed that includes CMake 3.14 or later. You should not increase this
3+
# version, as doing so will cause the plugin to fail to compile for some
4+
# customers of the plugin.
5+
cmake_minimum_required(VERSION 3.14)
6+
7+
# Project-level configuration.
8+
set(PROJECT_NAME "share_plus_windows")
9+
project(${PROJECT_NAME} LANGUAGES CXX)
10+
11+
# This value is used when generating builds using this plugin, so it must
12+
# not be changed
13+
set(PLUGIN_NAME "share_plus_windows_plugin")
14+
15+
# Any new source files that you add to the plugin should be added here.
16+
list(APPEND PLUGIN_SOURCES
17+
"share_plus_windows_plugin.cpp"
18+
"share_plus_windows_plugin.h"
19+
)
20+
21+
# Define the plugin library target. Its name must not be changed (see comment
22+
# on PLUGIN_NAME above).
23+
add_library(${PLUGIN_NAME} SHARED
24+
"include/share_plus_windows/share_plus_windows_plugin_c_api.h"
25+
"share_plus_windows_plugin_c_api.cpp"
26+
${PLUGIN_SOURCES}
27+
)
28+
29+
# Apply a standard set of build settings that are configured in the
30+
# application-level CMakeLists.txt. This can be removed for plugins that want
31+
# full control over build settings.
32+
apply_standard_settings(${PLUGIN_NAME})
33+
34+
# Symbols are hidden by default to reduce the chance of accidental conflicts
35+
# between plugins. This should not be removed; any symbols that should be
36+
# exported should be explicitly exported with the FLUTTER_PLUGIN_EXPORT macro.
37+
set_target_properties(${PLUGIN_NAME} PROPERTIES
38+
CXX_VISIBILITY_PRESET hidden)
39+
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
40+
41+
# Source include directories and library dependencies. Add any plugin-specific
42+
# dependencies here.
43+
target_include_directories(${PLUGIN_NAME} INTERFACE
44+
"${CMAKE_CURRENT_SOURCE_DIR}/include")
45+
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin)
46+
47+
# List of absolute paths to libraries that should be bundled with the plugin.
48+
# This list could contain prebuilt libraries, or libraries created by an
49+
# external build triggered from this build file.
50+
set(share_plus_windows_bundled_libraries
51+
""
52+
PARENT_SCOPE
53+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef FLUTTER_PLUGIN_SHARE_PLUS_WINDOWS_PLUGIN_C_API_H_
2+
#define FLUTTER_PLUGIN_SHARE_PLUS_WINDOWS_PLUGIN_C_API_H_
3+
4+
#include <flutter_plugin_registrar.h>
5+
6+
#ifdef FLUTTER_PLUGIN_IMPL
7+
#define FLUTTER_PLUGIN_EXPORT __declspec(dllexport)
8+
#else
9+
#define FLUTTER_PLUGIN_EXPORT __declspec(dllimport)
10+
#endif
11+
12+
#if defined(__cplusplus)
13+
extern "C" {
14+
#endif
15+
16+
FLUTTER_PLUGIN_EXPORT void SharePlusWindowsPluginCApiRegisterWithRegistrar(
17+
FlutterDesktopPluginRegistrarRef registrar);
18+
19+
#if defined(__cplusplus)
20+
} // extern "C"
21+
#endif
22+
23+
#endif // FLUTTER_PLUGIN_SHARE_PLUS_WINDOWS_PLUGIN_C_API_H_

0 commit comments

Comments
 (0)