Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/services/clipboard/electron-browser/clipboardService.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IClipboardService } from '../../../../platform/clipboard/common/clipboardService.js';
7
import { URI } from '../../../../base/common/uri.js';
8
import { isMacintosh } from '../../../../base/common/platform.js';
9
import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js';
10
import { INativeHostService } from '../../../../platform/native/common/native.js';
11
import { VSBuffer } from '../../../../base/common/buffer.js';
12
import { ILogService } from '../../../../platform/log/common/log.js';
13
14
export class NativeClipboardService implements IClipboardService {
15
16
private static readonly FILE_FORMAT = 'code/file-list'; // Clipboard format for files
17
18
declare readonly _serviceBrand: undefined;
19
20
constructor(
21
@INativeHostService private readonly nativeHostService: INativeHostService,
22
@ILogService private readonly logService: ILogService
23
) { }
24
25
async triggerPaste(targetWindowId: number): Promise<void> {
26
this.logService.trace('NativeClipboardService#triggerPaste called');
27
return this.nativeHostService.triggerPaste({ targetWindowId });
28
}
29
30
async readImage(): Promise<Uint8Array> {
31
return this.nativeHostService.readImage();
32
}
33
34
async writeText(text: string, type?: 'selection' | 'clipboard'): Promise<void> {
35
this.logService.trace('NativeClipboardService#writeText called with type:', type, ' with text.length:', text.length);
36
return this.nativeHostService.writeClipboardText(text, type);
37
}
38
39
async readText(type?: 'selection' | 'clipboard'): Promise<string> {
40
this.logService.trace('NativeClipboardService#readText called with type:', type);
41
return this.nativeHostService.readClipboardText(type);
42
}
43
44
async readFindText(): Promise<string> {
45
if (isMacintosh) {
46
return this.nativeHostService.readClipboardFindText();
47
}
48
49
return '';
50
}
51
52
async writeFindText(text: string): Promise<void> {
53
if (isMacintosh) {
54
return this.nativeHostService.writeClipboardFindText(text);
55
}
56
}
57
58
async writeResources(resources: URI[]): Promise<void> {
59
if (resources.length) {
60
return this.nativeHostService.writeClipboardBuffer(NativeClipboardService.FILE_FORMAT, this.resourcesToBuffer(resources));
61
}
62
}
63
64
async readResources(): Promise<URI[]> {
65
return this.bufferToResources(await this.nativeHostService.readClipboardBuffer(NativeClipboardService.FILE_FORMAT));
66
}
67
68
async hasResources(): Promise<boolean> {
69
return this.nativeHostService.hasClipboard(NativeClipboardService.FILE_FORMAT);
70
}
71
72
private resourcesToBuffer(resources: URI[]): VSBuffer {
73
return VSBuffer.fromString(resources.map(r => r.toString()).join('\n'));
74
}
75
76
private bufferToResources(buffer: VSBuffer): URI[] {
77
if (!buffer) {
78
return [];
79
}
80
81
const bufferValue = buffer.toString();
82
if (!bufferValue) {
83
return [];
84
}
85
86
try {
87
return bufferValue.split('\n').map(f => URI.parse(f));
88
} catch (error) {
89
return []; // do not trust clipboard data
90
}
91
}
92
}
93
94
registerSingleton(IClipboardService, NativeClipboardService, InstantiationType.Delayed);
95
96