Skip to content

Commit

Permalink
fix: fix cannot take screenshot on Windows. #34
Browse files Browse the repository at this point in the history
  • Loading branch information
lijy91 committed Jun 23, 2024
1 parent ebd6018 commit b153f46
Show file tree
Hide file tree
Showing 20 changed files with 99 additions and 75 deletions.
4 changes: 4 additions & 0 deletions packages/screen_capturer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/screen_capturer/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include: package:mostly_reasonable_lints/flutter.yaml
include: package:mostly_reasonable_lints/analysis_options.yaml
2 changes: 1 addition & 1 deletion packages/screen_capturer/example/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include: package:mostly_reasonable_lints/flutter.yaml
include: package:mostly_reasonable_lints/analysis_options.yaml
7 changes: 2 additions & 5 deletions packages/screen_capturer/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -19,10 +18,8 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
builder: BotToastInit(),
navigatorObservers: [BotToastNavigatorObserver()],
home: const HomePage(),
return const MaterialApp(
home: HomePage(),
);
}
}
62 changes: 38 additions & 24 deletions packages/screen_capturer/example/lib/pages/home.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -63,83 +60,98 @@ class _HomePageState extends State<HomePage> {
}

Widget _buildBody(BuildContext context) {
return PreferenceList(
return ListView(
children: <Widget>[
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 =
await screenCapturer.readImageFromClipboard();
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!;
setState(() {});
},
),
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);
Expand Down Expand Up @@ -176,9 +188,11 @@ class _HomePageState extends State<HomePage> {
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),
);
}
}
13 changes: 3 additions & 10 deletions packages/screen_capturer/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 6 additions & 6 deletions packages/screen_capturer/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions packages/screen_capturer_linux/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.2

* chore: Bump `mostly_reasonable_lints` to 0.1.2.

## 0.2.0

* First release.
2 changes: 1 addition & 1 deletion packages/screen_capturer_linux/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include: package:mostly_reasonable_lints/flutter.yaml
include: package:mostly_reasonable_lints/analysis_options.yaml
8 changes: 4 additions & 4 deletions packages/screen_capturer_linux/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions packages/screen_capturer_macos/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.2

* chore: Bump `mostly_reasonable_lints` to 0.1.2.

## 0.2.0

* First release.
2 changes: 1 addition & 1 deletion packages/screen_capturer_macos/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include: package:mostly_reasonable_lints/flutter.yaml
include: package:mostly_reasonable_lints/analysis_options.yaml
8 changes: 4 additions & 4 deletions packages/screen_capturer_macos/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions packages/screen_capturer_platform_interface/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.2

* chore: Bump `mostly_reasonable_lints` to 0.1.2.

## 0.2.0

* First release.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include: package:mostly_reasonable_lints/flutter.yaml
include: package:mostly_reasonable_lints/analysis_options.yaml
4 changes: 2 additions & 2 deletions packages/screen_capturer_platform_interface/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
4 changes: 4 additions & 0 deletions packages/screen_capturer_windows/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.2

* fix cannot take screenshot on Windows. #34

## 0.2.0

* First release.
2 changes: 1 addition & 1 deletion packages/screen_capturer_windows/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include: package:mostly_reasonable_lints/flutter.yaml
include: package:mostly_reasonable_lints/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -43,10 +44,11 @@ bool _isScreenClipping() {
for (var i = 0; i < (cbNeeded.value ~/ sizeOf<HMODULE>()); 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;
}
Expand All @@ -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));

Expand Down
Loading

0 comments on commit b153f46

Please sign in to comment.