Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Clipboard for Windows #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This package comes with a number of examples to demonstrate various features.
| `bar.dart` | Simple demonstration of DrawingCanvas for drawing a vertical bar. |
| `canvas.dart` | Shows use of ConsoleCanvas for character-based positioning. |
| `choice.dart` | Select a multiple choice option. |
| `clipboard.dart` | Simple example of clipboard operation. |
| `clock.dart` | Sophisticated example of using DrawingCanvas to display a clock. |
| `colors.dart` | Setting colored output with the TextPen class. |
| `cube.dart` | Draws a 3D cube with DrawingCanvas. |
Expand Down
18 changes: 18 additions & 0 deletions example/clipboard.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:console/console.dart';

import 'dart:io';

void main() {
var clip = getClipboard();
if (clip == null) {
throw Exception('${Platform.operatingSystem} is not supported.');
}

var prev = clip.getContent();
print('Previous clipboard: $prev');

clip.setContent('Hello!');

var cur = clip.getContent();
print('Current clipboard: $cur');
}
2 changes: 2 additions & 0 deletions lib/console.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ library console;
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';

part 'src/base.dart';
part 'src/clipboard.dart';
Expand Down
29 changes: 29 additions & 0 deletions lib/src/clipboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Clipboard? getClipboard() {
if (Platform.isLinux && File('/usr/bin/xclip').existsSync()) {
return XClipboard();
}
if (Platform.isWindows) return WinClipboard();
return null;
}

Expand Down Expand Up @@ -53,3 +54,31 @@ class XClipboard implements Clipboard {
});
}
}

class WinClipboard implements Clipboard {
@override
String getContent() {
OpenClipboard(NULL);
if (IsClipboardFormatAvailable(CF_TEXT) == FALSE) return '';

var hg = GetClipboardData(CF_TEXT);
if (hg == NULL) return '';

var ptstr = GlobalLock(hg);

GlobalUnlock(hg);
CloseClipboard();

return ptstr.cast<Utf8>().toDartString();
}

@override
void setContent(String content) {
var ptstr = content.toNativeUtf8();

OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_TEXT, ptstr.address);
CloseClipboard();
}
}
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ environment:
sdk: '>=2.12.0 <3.0.0'

dependencies:
ffi: ^1.1.2
vector_math: ^2.1.0
win32: ^2.2.5

dev_dependencies:
test: ^1.16.8
Expand Down