Skip to content

Commit 14e6ad3

Browse files
shizengzhoumjbvz
authored andcommitted
* Fix microsoft#84111 * Replace fs with vscode's fs * Put binary size in the binarySizeStatusBar
1 parent 427beb6 commit 14e6ad3

3 files changed

Lines changed: 83 additions & 2 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 { Disposable } from './dispose';
8+
import * as nls from 'vscode-nls';
9+
10+
const localize = nls.loadMessageBundle();
11+
12+
class BinarySize {
13+
static readonly KB = 1024;
14+
static readonly MB = BinarySize.KB * BinarySize.KB;
15+
static readonly GB = BinarySize.MB * BinarySize.KB;
16+
static readonly TB = BinarySize.GB * BinarySize.KB;
17+
18+
static formatSize(size: number): string {
19+
if (size < BinarySize.KB) {
20+
return localize('sizeB', "{0}B", size);
21+
}
22+
23+
if (size < BinarySize.MB) {
24+
return localize('sizeKB', "{0}KB", (size / BinarySize.KB).toFixed(2));
25+
}
26+
27+
if (size < BinarySize.GB) {
28+
return localize('sizeMB', "{0}MB", (size / BinarySize.MB).toFixed(2));
29+
}
30+
31+
if (size < BinarySize.TB) {
32+
return localize('sizeGB', "{0}GB", (size / BinarySize.GB).toFixed(2));
33+
}
34+
35+
return localize('sizeTB', "{0}TB", (size / BinarySize.TB).toFixed(2));
36+
}
37+
}
38+
39+
export class BinarySizeStatusBarEntry extends Disposable {
40+
private readonly _entry: vscode.StatusBarItem;
41+
42+
private _showingOwner: string | undefined;
43+
44+
constructor() {
45+
super();
46+
this._entry = this._register(vscode.window.createStatusBarItem({
47+
id: 'imagePreview.binarySize',
48+
name: localize('sizeStatusBar.name', "Image Binary Size"),
49+
alignment: vscode.StatusBarAlignment.Right,
50+
priority: 100,
51+
}));
52+
}
53+
54+
public show(owner: string, size: number) {
55+
this._showingOwner = owner;
56+
this._entry.text = size < 0 ? '' : BinarySize.formatSize(size);
57+
this._entry.show();
58+
}
59+
60+
public hide(owner: string) {
61+
if (owner === this._showingOwner) {
62+
this._entry.hide();
63+
this._showingOwner = undefined;
64+
}
65+
}
66+
}

extensions/image-preview/src/extension.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import * as vscode from 'vscode';
77
import { PreviewManager } from './preview';
88
import { SizeStatusBarEntry } from './sizeStatusBarEntry';
9+
import { BinarySizeStatusBarEntry } from './binarySizeStatusBarEntry';
910
import { ZoomStatusBarEntry } from './zoomStatusBarEntry';
1011

1112
export function activate(context: vscode.ExtensionContext) {
@@ -14,10 +15,13 @@ export function activate(context: vscode.ExtensionContext) {
1415
const sizeStatusBarEntry = new SizeStatusBarEntry();
1516
context.subscriptions.push(sizeStatusBarEntry);
1617

18+
const binarySizeStatusBarEntry = new BinarySizeStatusBarEntry();
19+
context.subscriptions.push(binarySizeStatusBarEntry);
20+
1721
const zoomStatusBarEntry = new ZoomStatusBarEntry();
1822
context.subscriptions.push(zoomStatusBarEntry);
1923

20-
const previewManager = new PreviewManager(extensionRoot, sizeStatusBarEntry, zoomStatusBarEntry);
24+
const previewManager = new PreviewManager(extensionRoot, sizeStatusBarEntry, binarySizeStatusBarEntry, zoomStatusBarEntry);
2125

2226
context.subscriptions.push(vscode.window.registerWebviewEditorProvider(
2327
PreviewManager.viewType,

extensions/image-preview/src/preview.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as nls from 'vscode-nls';
88
import { Disposable } from './dispose';
99
import { SizeStatusBarEntry } from './sizeStatusBarEntry';
1010
import { Scale, ZoomStatusBarEntry } from './zoomStatusBarEntry';
11+
import { BinarySizeStatusBarEntry } from './binarySizeStatusBarEntry';
1112

1213
const localize = nls.loadMessageBundle();
1314

@@ -22,14 +23,15 @@ export class PreviewManager {
2223
constructor(
2324
private readonly extensionRoot: vscode.Uri,
2425
private readonly sizeStatusBarEntry: SizeStatusBarEntry,
26+
private readonly binarySizeStatusBarEntry: BinarySizeStatusBarEntry,
2527
private readonly zoomStatusBarEntry: ZoomStatusBarEntry,
2628
) { }
2729

2830
public resolve(
2931
resource: vscode.Uri,
3032
webviewEditor: vscode.WebviewPanel,
3133
): vscode.WebviewEditorCapabilities {
32-
const preview = new Preview(this.extensionRoot, resource, webviewEditor, this.sizeStatusBarEntry, this.zoomStatusBarEntry);
34+
const preview = new Preview(this.extensionRoot, resource, webviewEditor, this.sizeStatusBarEntry, this.binarySizeStatusBarEntry, this.zoomStatusBarEntry);
3335
this._previews.add(preview);
3436
this.setActivePreview(preview);
3537

@@ -72,13 +74,15 @@ class Preview extends Disposable implements vscode.WebviewEditorEditingCapabilit
7274

7375
private _previewState = PreviewState.Visible;
7476
private _imageSize: string | undefined;
77+
private _imageBinarySize: number | undefined;
7578
private _imageZoom: Scale | undefined;
7679

7780
constructor(
7881
private readonly extensionRoot: vscode.Uri,
7982
private readonly resource: vscode.Uri,
8083
private readonly webviewEditor: vscode.WebviewPanel,
8184
private readonly sizeStatusBarEntry: SizeStatusBarEntry,
85+
private readonly binarySizeStatusBarEntry: BinarySizeStatusBarEntry,
8286
private readonly zoomStatusBarEntry: ZoomStatusBarEntry,
8387
) {
8488
super();
@@ -125,6 +129,7 @@ class Preview extends Disposable implements vscode.WebviewEditorEditingCapabilit
125129
this._register(webviewEditor.onDidDispose(() => {
126130
if (this._previewState === PreviewState.Active) {
127131
this.sizeStatusBarEntry.hide(this.id);
132+
this.binarySizeStatusBarEntry.hide(this.id);
128133
this.zoomStatusBarEntry.hide(this.id);
129134
}
130135
this._previewState = PreviewState.Disposed;
@@ -142,6 +147,10 @@ class Preview extends Disposable implements vscode.WebviewEditorEditingCapabilit
142147
}
143148
}));
144149

150+
(async () => {
151+
const { size } = await vscode.workspace.fs.stat(resource);
152+
this._imageBinarySize = size;
153+
})();
145154
this.render();
146155
this.update();
147156
this.webviewEditor.webview.postMessage({ type: 'setActive', value: this.webviewEditor.active });
@@ -173,10 +182,12 @@ class Preview extends Disposable implements vscode.WebviewEditorEditingCapabilit
173182
if (this.webviewEditor.active) {
174183
this._previewState = PreviewState.Active;
175184
this.sizeStatusBarEntry.show(this.id, this._imageSize || '');
185+
this.binarySizeStatusBarEntry.show(this.id, this._imageBinarySize || -1);
176186
this.zoomStatusBarEntry.show(this.id, this._imageZoom || 'fit');
177187
} else {
178188
if (this._previewState === PreviewState.Active) {
179189
this.sizeStatusBarEntry.hide(this.id);
190+
this.binarySizeStatusBarEntry.hide(this.id);
180191
this.zoomStatusBarEntry.hide(this.id);
181192
}
182193
this._previewState = PreviewState.Visible;

0 commit comments

Comments
 (0)