From b153f46613a941d6dade003a891ab095311f9888 Mon Sep 17 00:00:00 2001 From: LiJianying Date: Sun, 23 Jun 2024 17:32:57 +0800 Subject: [PATCH] fix: fix cannot take screenshot on Windows. #34 --- packages/screen_capturer/CHANGELOG.md | 4 ++ .../screen_capturer/analysis_options.yaml | 2 +- .../example/analysis_options.yaml | 2 +- .../screen_capturer/example/lib/main.dart | 7 +-- .../example/lib/pages/home.dart | 62 ++++++++++++------- packages/screen_capturer/example/pubspec.yaml | 13 +--- packages/screen_capturer/pubspec.yaml | 12 ++-- packages/screen_capturer_linux/CHANGELOG.md | 4 ++ .../analysis_options.yaml | 2 +- packages/screen_capturer_linux/pubspec.yaml | 8 +-- packages/screen_capturer_macos/CHANGELOG.md | 4 ++ .../analysis_options.yaml | 2 +- packages/screen_capturer_macos/pubspec.yaml | 8 +-- .../CHANGELOG.md | 4 ++ .../analysis_options.yaml | 2 +- .../pubspec.yaml | 4 +- packages/screen_capturer_windows/CHANGELOG.md | 4 ++ .../analysis_options.yaml | 2 +- .../lib/src/commands/ms_screenclip.dart | 22 +++---- packages/screen_capturer_windows/pubspec.yaml | 6 +- 20 files changed, 99 insertions(+), 75 deletions(-) diff --git a/packages/screen_capturer/CHANGELOG.md b/packages/screen_capturer/CHANGELOG.md index 4c0158a..fc5c16c 100644 --- a/packages/screen_capturer/CHANGELOG.md +++ b/packages/screen_capturer/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.2.2 + +* chore: Bump `mostly_reasonable_lints` to 0.1.2. + ## 0.2.1 * feat: Convert to federated plugin #29 diff --git a/packages/screen_capturer/analysis_options.yaml b/packages/screen_capturer/analysis_options.yaml index 095b1d6..9033bb2 100644 --- a/packages/screen_capturer/analysis_options.yaml +++ b/packages/screen_capturer/analysis_options.yaml @@ -1 +1 @@ -include: package:mostly_reasonable_lints/flutter.yaml +include: package:mostly_reasonable_lints/analysis_options.yaml diff --git a/packages/screen_capturer/example/analysis_options.yaml b/packages/screen_capturer/example/analysis_options.yaml index 095b1d6..9033bb2 100644 --- a/packages/screen_capturer/example/analysis_options.yaml +++ b/packages/screen_capturer/example/analysis_options.yaml @@ -1 +1 @@ -include: package:mostly_reasonable_lints/flutter.yaml +include: package:mostly_reasonable_lints/analysis_options.yaml diff --git a/packages/screen_capturer/example/lib/main.dart b/packages/screen_capturer/example/lib/main.dart index b2da3dd..b6ba673 100644 --- a/packages/screen_capturer/example/lib/main.dart +++ b/packages/screen_capturer/example/lib/main.dart @@ -1,4 +1,3 @@ -import 'package:bot_toast/bot_toast.dart'; import 'package:flutter/material.dart'; import 'package:screen_capturer_example/pages/home.dart'; import 'package:screen_capturer_example/utils/default_shell_executor.dart'; @@ -19,10 +18,8 @@ class MyApp extends StatefulWidget { class _MyAppState extends State { @override Widget build(BuildContext context) { - return MaterialApp( - builder: BotToastInit(), - navigatorObservers: [BotToastNavigatorObserver()], - home: const HomePage(), + return const MaterialApp( + home: HomePage(), ); } } diff --git a/packages/screen_capturer/example/lib/pages/home.dart b/packages/screen_capturer/example/lib/pages/home.dart index 2317a71..cf50785 100644 --- a/packages/screen_capturer/example/lib/pages/home.dart +++ b/packages/screen_capturer/example/lib/pages/home.dart @@ -1,12 +1,9 @@ import 'dart:io'; -import 'package:bot_toast/bot_toast.dart'; -import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:path_provider/path_provider.dart'; -import 'package:preference_list/preference_list.dart'; import 'package:screen_capturer/screen_capturer.dart'; class HomePage extends StatefulWidget { @@ -63,46 +60,58 @@ class _HomePageState extends State { } Widget _buildBody(BuildContext context) { - return PreferenceList( + return ListView( children: [ if (_screenshotsDirectory != null) - PreferenceListSection( + Column( children: [ - PreferenceListItem( + ListTile( title: const Text('Output Directory'), - accessoryView: Text(_screenshotsDirectory!.path), + trailing: Text(_screenshotsDirectory!.path), onTap: () { Clipboard.setData( ClipboardData(text: _screenshotsDirectory!.path), ); - BotToast.showText(text: 'Copied to clipboard'); + ScaffoldMessenger.of(context) + .showSnackBar(const SnackBar(content: Text('Copied'))); }, ), ], ), - PreferenceListSection( - title: const Text('METHODS'), + Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, children: [ + Container( + margin: const EdgeInsets.only(top: 32, bottom: 8, left: 16), + child: const Text( + 'METHODS', + style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600), + ), + ), if (!kIsWeb && Platform.isMacOS) ...[ - PreferenceListItem( + ListTile( title: const Text('isAccessAllowed'), - accessoryView: Text('$_isAccessAllowed'), + trailing: Text('$_isAccessAllowed'), onTap: () async { bool allowed = await screenCapturer.isAccessAllowed(); - BotToast.showText(text: 'allowed: $allowed'); + // ignore: use_build_context_synchronously + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('allowed: $allowed')), + ); setState(() { _isAccessAllowed = allowed; }); }, ), - PreferenceListItem( + ListTile( title: const Text('requestAccess'), onTap: () async { await screenCapturer.requestAccess(); }, ), ], - PreferenceListItem( + ListTile( title: const Text('readImageFromClipboard'), onTap: () async { _imageBytesFromClipboard = @@ -110,13 +119,14 @@ class _HomePageState extends State { setState(() {}); }, ), - PreferenceListItem( + ListTile( title: const Text('capture'), - accessoryView: Row( + trailing: Row( + mainAxisSize: MainAxisSize.min, children: [ Row( children: [ - CupertinoCheckbox( + Checkbox( value: _copyToClipboard, onChanged: (value) { _copyToClipboard = value!; @@ -124,22 +134,24 @@ class _HomePageState extends State { }, ), const Text('copyToClipboard'), - const SizedBox(width: 10), ], ), - CupertinoButton( + const SizedBox(width: 10), + FilledButton( child: const Text('region'), onPressed: () { _handleClickCapture(CaptureMode.region); }, ), - CupertinoButton( + const SizedBox(width: 10), + FilledButton( child: const Text('screen'), onPressed: () { _handleClickCapture(CaptureMode.screen); }, ), - CupertinoButton( + const SizedBox(width: 10), + FilledButton( child: const Text('window'), onPressed: () { _handleClickCapture(CaptureMode.window); @@ -176,9 +188,11 @@ class _HomePageState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: const Text('Plugin example app'), + title: const Text('screen_capturer_example'), + ), + body: Container( + child: _buildBody(context), ), - body: _buildBody(context), ); } } diff --git a/packages/screen_capturer/example/pubspec.yaml b/packages/screen_capturer/example/pubspec.yaml index 7240314..4787078 100644 --- a/packages/screen_capturer/example/pubspec.yaml +++ b/packages/screen_capturer/example/pubspec.yaml @@ -3,29 +3,22 @@ description: "Demonstrates how to use the screen_capturer plugin." publish_to: "none" environment: - sdk: ">=3.2.4 <4.0.0" + sdk: ">=3.3.0 <4.0.0" dependencies: - bot_toast: ^4.1.3 flutter: sdk: flutter path_provider: ^2.0.15 - preference_list: ^0.0.1 screen_capturer: - # When depending on this package from a real application you should use: - # screen_capturer: ^x.y.z - # See https://dart.dev/tools/pub/dependencies#version-constraints - # The example app is bundled with the plugin so we use a path dependency on - # the parent directory to use the current plugin's version. path: ../ - shell_executor: ^0.1.4 + shell_executor: ^0.1.6 dev_dependencies: flutter_test: sdk: flutter integration_test: sdk: flutter - mostly_reasonable_lints: ^0.1.1 + mostly_reasonable_lints: ^0.1.2 flutter: uses-material-design: true diff --git a/packages/screen_capturer/pubspec.yaml b/packages/screen_capturer/pubspec.yaml index 2e7e791..019cf4e 100644 --- a/packages/screen_capturer/pubspec.yaml +++ b/packages/screen_capturer/pubspec.yaml @@ -1,6 +1,6 @@ name: screen_capturer description: This plugin allows Flutter desktop apps to capture screenshots. -version: 0.2.1 +version: 0.2.2 homepage: https://github.com/leanflutter/screen_capturer platforms: @@ -15,15 +15,15 @@ environment: dependencies: flutter: sdk: flutter - screen_capturer_linux: ^0.2.0 - screen_capturer_macos: ^0.2.0 - screen_capturer_platform_interface: ^0.2.0 - screen_capturer_windows: ^0.2.0 + screen_capturer_linux: ^0.2.2 + screen_capturer_macos: ^0.2.2 + screen_capturer_platform_interface: ^0.2.2 + screen_capturer_windows: ^0.2.2 dev_dependencies: flutter_test: sdk: flutter - mostly_reasonable_lints: ^0.1.1 + mostly_reasonable_lints: ^0.1.2 plugin_platform_interface: ^2.1.8 flutter: diff --git a/packages/screen_capturer_linux/CHANGELOG.md b/packages/screen_capturer_linux/CHANGELOG.md index 2f145ce..a02328a 100644 --- a/packages/screen_capturer_linux/CHANGELOG.md +++ b/packages/screen_capturer_linux/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.2.2 + +* chore: Bump `mostly_reasonable_lints` to 0.1.2. + ## 0.2.0 * First release. diff --git a/packages/screen_capturer_linux/analysis_options.yaml b/packages/screen_capturer_linux/analysis_options.yaml index 095b1d6..9033bb2 100644 --- a/packages/screen_capturer_linux/analysis_options.yaml +++ b/packages/screen_capturer_linux/analysis_options.yaml @@ -1 +1 @@ -include: package:mostly_reasonable_lints/flutter.yaml +include: package:mostly_reasonable_lints/analysis_options.yaml diff --git a/packages/screen_capturer_linux/pubspec.yaml b/packages/screen_capturer_linux/pubspec.yaml index 88e945f..d4c4d67 100644 --- a/packages/screen_capturer_linux/pubspec.yaml +++ b/packages/screen_capturer_linux/pubspec.yaml @@ -1,6 +1,6 @@ name: screen_capturer_linux description: Linux implementation of the screen_capturer plugin. -version: 0.2.0 +version: 0.2.2 repository: https://github.com/leanflutter/screen_capturer/tree/main/packages/screen_capturer_linux environment: @@ -10,13 +10,13 @@ environment: dependencies: flutter: sdk: flutter - screen_capturer_platform_interface: ^0.2.0 - shell_executor: ^0.1.4 + screen_capturer_platform_interface: ^0.2.2 + shell_executor: ^0.1.6 dev_dependencies: flutter_test: sdk: flutter - mostly_reasonable_lints: ^0.1.1 + mostly_reasonable_lints: ^0.1.2 flutter: plugin: diff --git a/packages/screen_capturer_macos/CHANGELOG.md b/packages/screen_capturer_macos/CHANGELOG.md index 2f145ce..a02328a 100644 --- a/packages/screen_capturer_macos/CHANGELOG.md +++ b/packages/screen_capturer_macos/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.2.2 + +* chore: Bump `mostly_reasonable_lints` to 0.1.2. + ## 0.2.0 * First release. diff --git a/packages/screen_capturer_macos/analysis_options.yaml b/packages/screen_capturer_macos/analysis_options.yaml index 095b1d6..9033bb2 100644 --- a/packages/screen_capturer_macos/analysis_options.yaml +++ b/packages/screen_capturer_macos/analysis_options.yaml @@ -1 +1 @@ -include: package:mostly_reasonable_lints/flutter.yaml +include: package:mostly_reasonable_lints/analysis_options.yaml diff --git a/packages/screen_capturer_macos/pubspec.yaml b/packages/screen_capturer_macos/pubspec.yaml index 76e910f..642dcf5 100644 --- a/packages/screen_capturer_macos/pubspec.yaml +++ b/packages/screen_capturer_macos/pubspec.yaml @@ -1,6 +1,6 @@ name: screen_capturer_macos description: macOS implementation of the screen_capturer plugin. -version: 0.2.0 +version: 0.2.2 repository: https://github.com/leanflutter/screen_capturer/tree/main/packages/screen_capturer_macos environment: @@ -10,13 +10,13 @@ environment: dependencies: flutter: sdk: flutter - screen_capturer_platform_interface: ^0.2.0 - shell_executor: ^0.1.4 + screen_capturer_platform_interface: ^0.2.2 + shell_executor: ^0.1.6 dev_dependencies: flutter_test: sdk: flutter - mostly_reasonable_lints: ^0.1.1 + mostly_reasonable_lints: ^0.1.2 flutter: plugin: diff --git a/packages/screen_capturer_platform_interface/CHANGELOG.md b/packages/screen_capturer_platform_interface/CHANGELOG.md index 2f145ce..a02328a 100644 --- a/packages/screen_capturer_platform_interface/CHANGELOG.md +++ b/packages/screen_capturer_platform_interface/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.2.2 + +* chore: Bump `mostly_reasonable_lints` to 0.1.2. + ## 0.2.0 * First release. diff --git a/packages/screen_capturer_platform_interface/analysis_options.yaml b/packages/screen_capturer_platform_interface/analysis_options.yaml index 095b1d6..9033bb2 100644 --- a/packages/screen_capturer_platform_interface/analysis_options.yaml +++ b/packages/screen_capturer_platform_interface/analysis_options.yaml @@ -1 +1 @@ -include: package:mostly_reasonable_lints/flutter.yaml +include: package:mostly_reasonable_lints/analysis_options.yaml diff --git a/packages/screen_capturer_platform_interface/pubspec.yaml b/packages/screen_capturer_platform_interface/pubspec.yaml index 952b1bc..6147bf0 100644 --- a/packages/screen_capturer_platform_interface/pubspec.yaml +++ b/packages/screen_capturer_platform_interface/pubspec.yaml @@ -1,6 +1,6 @@ name: screen_capturer_platform_interface description: A common platform interface for the screen_capturer plugin. -version: 0.2.0 +version: 0.2.2 homepage: https://github.com/leanflutter/screen_capturer/blob/main/packages/screen_capturer_platform_interface environment: @@ -18,4 +18,4 @@ dev_dependencies: flutter_test: sdk: flutter json_serializable: ^6.6.0 - mostly_reasonable_lints: ^0.1.1 + mostly_reasonable_lints: ^0.1.2 diff --git a/packages/screen_capturer_windows/CHANGELOG.md b/packages/screen_capturer_windows/CHANGELOG.md index 2f145ce..6d341e0 100644 --- a/packages/screen_capturer_windows/CHANGELOG.md +++ b/packages/screen_capturer_windows/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.2.2 + +* fix cannot take screenshot on Windows. #34 + ## 0.2.0 * First release. diff --git a/packages/screen_capturer_windows/analysis_options.yaml b/packages/screen_capturer_windows/analysis_options.yaml index 095b1d6..9033bb2 100644 --- a/packages/screen_capturer_windows/analysis_options.yaml +++ b/packages/screen_capturer_windows/analysis_options.yaml @@ -1 +1 @@ -include: package:mostly_reasonable_lints/flutter.yaml +include: package:mostly_reasonable_lints/analysis_options.yaml diff --git a/packages/screen_capturer_windows/lib/src/commands/ms_screenclip.dart b/packages/screen_capturer_windows/lib/src/commands/ms_screenclip.dart index 0deea3f..056c564 100644 --- a/packages/screen_capturer_windows/lib/src/commands/ms_screenclip.dart +++ b/packages/screen_capturer_windows/lib/src/commands/ms_screenclip.dart @@ -18,7 +18,8 @@ bool _isScreenClipping() { GetWindowThreadProcessId(hWnd, lpdwProcessId); // Get a handle to the process. final hProcess = OpenProcess( - PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, + PROCESS_ACCESS_RIGHTS.PROCESS_QUERY_INFORMATION | + PROCESS_ACCESS_RIGHTS.PROCESS_VM_READ, FALSE, lpdwProcessId.value, ); @@ -43,10 +44,11 @@ bool _isScreenClipping() { for (var i = 0; i < (cbNeeded.value ~/ sizeOf()); i++) { final szModName = wsalloc(MAX_PATH); // Get the full path to the module's file. - final hModule = hModules.elementAt(i).value; + final hModule = (hModules + i).value; if (GetModuleFileNameEx(hProcess, hModule, szModName, MAX_PATH) != 0) { String moduleName = szModName.toDartString(); - if (moduleName.contains('ScreenClippingHost.exe')) { + if (moduleName.contains('ScreenClippingHost.exe') || + moduleName.contains('SnippingTool.exe')) { free(szModName); return true; } @@ -71,22 +73,20 @@ class _MsScreenclip with SystemScreenCapturer { bool copyToClipboard = true, bool silent = true, }) async { + String url = 'ms-screenclip://?'; if (mode == CaptureMode.screen) { - assert(imagePath != null); - await ScreenCapturerPlatform.instance.captureScreen( - imagePath: imagePath!, - ); - return; + url += 'type=snapshot'; + } else { + url += 'clippingMode=${_knownCaptureModeArgs[mode]}'; } await Clipboard.setData(const ClipboardData(text: '')); ShellExecute( 0, 'open'.toNativeUtf16(), - 'ms-screenclip://?clippingMode=${_knownCaptureModeArgs[mode]}' - .toNativeUtf16(), + url.toNativeUtf16(), nullptr, nullptr, - SW_SHOWNORMAL, + SHOW_WINDOW_CMD.SW_SHOWNORMAL, ); await Future.delayed(const Duration(seconds: 1)); diff --git a/packages/screen_capturer_windows/pubspec.yaml b/packages/screen_capturer_windows/pubspec.yaml index 51be7ab..646313b 100644 --- a/packages/screen_capturer_windows/pubspec.yaml +++ b/packages/screen_capturer_windows/pubspec.yaml @@ -1,6 +1,6 @@ name: screen_capturer_windows description: Windows implementation of the screen_capturer plugin. -version: 0.2.0 +version: 0.2.2 repository: https://github.com/leanflutter/screen_capturer/tree/main/packages/screen_capturer_windows environment: @@ -11,13 +11,13 @@ dependencies: ffi: ^2.0.2 flutter: sdk: flutter - screen_capturer_platform_interface: ^0.2.0 + screen_capturer_platform_interface: ^0.2.2 win32: '>=2.6.0 <6.0.0' dev_dependencies: flutter_test: sdk: flutter - mostly_reasonable_lints: ^0.1.1 + mostly_reasonable_lints: ^0.1.2 flutter: plugin: