Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/media-preview/src/imagePreview/zoomStatusBarEntry.ts
4774 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 * as vscode from 'vscode';
7
import { PreviewStatusBarEntry as OwnedStatusBarEntry } from '../ownedStatusBarEntry';
8
9
10
const selectZoomLevelCommandId = '_imagePreview.selectZoomLevel';
11
12
export type Scale = number | 'fit';
13
14
export class ZoomStatusBarEntry extends OwnedStatusBarEntry {
15
16
private readonly _onDidChangeScale = this._register(new vscode.EventEmitter<{ scale: Scale }>());
17
public readonly onDidChangeScale = this._onDidChangeScale.event;
18
19
constructor() {
20
super('status.imagePreview.zoom', vscode.l10n.t("Image Zoom"), vscode.StatusBarAlignment.Right, 102 /* to the left of editor size entry (101) */);
21
22
this._register(vscode.commands.registerCommand(selectZoomLevelCommandId, async () => {
23
type MyPickItem = vscode.QuickPickItem & { scale: Scale };
24
25
const scales: Scale[] = [10, 5, 2, 1, 0.5, 0.2, 'fit'];
26
const options = scales.map((scale): MyPickItem => ({
27
label: this.zoomLabel(scale),
28
scale
29
}));
30
31
const pick = await vscode.window.showQuickPick(options, {
32
placeHolder: vscode.l10n.t("Select zoom level")
33
});
34
if (pick) {
35
this._onDidChangeScale.fire({ scale: pick.scale });
36
}
37
}));
38
39
this.entry.command = selectZoomLevelCommandId;
40
}
41
42
public show(owner: unknown, scale: Scale) {
43
this.showItem(owner, this.zoomLabel(scale));
44
}
45
46
private zoomLabel(scale: Scale): string {
47
return scale === 'fit'
48
? vscode.l10n.t("Whole Image")
49
: `${Math.round(scale * 100)}%`;
50
}
51
}
52
53