Skip to content

Commit 71c3136

Browse files
committed
Extract out common status bar entry management code
1 parent aaa01ea commit 71c3136

4 files changed

Lines changed: 47 additions & 55 deletions

File tree

extensions/image-preview/src/binarySizeStatusBarEntry.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as vscode from 'vscode';
7-
import { Disposable } from './dispose';
87
import * as nls from 'vscode-nls';
8+
import { PreviewStatusBarEntry } from './statusBarEntry';
99

1010
const localize = nls.loadMessageBundle();
1111

@@ -36,35 +36,22 @@ class BinarySize {
3636
}
3737
}
3838

39-
export class BinarySizeStatusBarEntry extends Disposable {
40-
private readonly _entry: vscode.StatusBarItem;
41-
42-
private _showingOwner: string | undefined;
39+
export class BinarySizeStatusBarEntry extends PreviewStatusBarEntry {
4340

4441
constructor() {
45-
super();
46-
this._entry = this._register(vscode.window.createStatusBarItem({
42+
super({
4743
id: 'imagePreview.binarySize',
4844
name: localize('sizeStatusBar.name', "Image Binary Size"),
4945
alignment: vscode.StatusBarAlignment.Right,
5046
priority: 100,
51-
}));
47+
});
5248
}
5349

5450
public show(owner: string, size: number | undefined) {
55-
this._showingOwner = owner;
5651
if (typeof size === 'number') {
57-
this._entry.text = BinarySize.formatSize(size);
58-
this._entry.show();
52+
super.showItem(owner, BinarySize.formatSize(size));
5953
} else {
6054
this.hide(owner);
6155
}
6256
}
63-
64-
public hide(owner: string) {
65-
if (owner === this._showingOwner) {
66-
this._entry.hide();
67-
this._showingOwner = undefined;
68-
}
69-
}
7057
}

extensions/image-preview/src/sizeStatusBarEntry.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,23 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as vscode from 'vscode';
7-
import { Disposable } from './dispose';
87
import * as nls from 'vscode-nls';
8+
import { PreviewStatusBarEntry } from './statusBarEntry';
99

1010
const localize = nls.loadMessageBundle();
1111

12-
export class SizeStatusBarEntry extends Disposable {
13-
private readonly _entry: vscode.StatusBarItem;
14-
15-
private _showingOwner: string | undefined;
12+
export class SizeStatusBarEntry extends PreviewStatusBarEntry {
1613

1714
constructor() {
18-
super();
19-
this._entry = this._register(vscode.window.createStatusBarItem({
15+
super({
2016
id: 'imagePreview.size',
2117
name: localize('sizeStatusBar.name', "Image Size"),
2218
alignment: vscode.StatusBarAlignment.Right,
2319
priority: 101 /* to the left of editor status (100) */,
24-
}));
20+
});
2521
}
2622

2723
public show(owner: string, text: string) {
28-
this._showingOwner = owner;
29-
this._entry.text = text;
30-
this._entry.show();
31-
}
32-
33-
public hide(owner: string) {
34-
if (owner === this._showingOwner) {
35-
this._entry.hide();
36-
this._showingOwner = undefined;
37-
}
24+
this.showItem(owner, text);
3825
}
3926
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
9+
export abstract class PreviewStatusBarEntry extends Disposable {
10+
private _showOwner: string | undefined;
11+
12+
protected readonly entry: vscode.StatusBarItem;
13+
14+
constructor(options: vscode.window.StatusBarItemOptions) {
15+
super();
16+
this.entry = this._register(vscode.window.createStatusBarItem(options));
17+
}
18+
19+
protected showItem(owner: string, text: string) {
20+
this._showOwner = owner;
21+
this.entry.text = text;
22+
this.entry.show();
23+
}
24+
25+
public hide(owner: string) {
26+
if (owner === this._showOwner) {
27+
this.entry.hide();
28+
this._showOwner = undefined;
29+
}
30+
}
31+
}

extensions/image-preview/src/zoomStatusBarEntry.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,26 @@
55

66
import * as vscode from 'vscode';
77
import * as nls from 'vscode-nls';
8-
import { Disposable } from './dispose';
8+
import { PreviewStatusBarEntry } from './statusBarEntry';
99

1010
const localize = nls.loadMessageBundle();
1111

1212
const selectZoomLevelCommandId = '_imagePreview.selectZoomLevel';
1313

1414
export type Scale = number | 'fit';
1515

16-
export class ZoomStatusBarEntry extends Disposable {
17-
private readonly _entry: vscode.StatusBarItem;
16+
export class ZoomStatusBarEntry extends PreviewStatusBarEntry {
1817

1918
private readonly _onDidChangeScale = this._register(new vscode.EventEmitter<{ scale: Scale }>());
2019
public readonly onDidChangeScale = this._onDidChangeScale.event;
2120

22-
private _showOwner: string | undefined;
23-
2421
constructor() {
25-
super();
26-
this._entry = this._register(vscode.window.createStatusBarItem({
22+
super({
2723
id: 'imagePreview.zoom',
2824
name: localize('zoomStatusBar.name', "Image Zoom"),
2925
alignment: vscode.StatusBarAlignment.Right,
3026
priority: 102 /* to the left of editor size entry (101) */,
31-
}));
27+
});
3228

3329
this._register(vscode.commands.registerCommand(selectZoomLevelCommandId, async () => {
3430
type MyPickItem = vscode.QuickPickItem & { scale: Scale };
@@ -47,20 +43,11 @@ export class ZoomStatusBarEntry extends Disposable {
4743
}
4844
}));
4945

50-
this._entry.command = selectZoomLevelCommandId;
46+
this.entry.command = selectZoomLevelCommandId;
5147
}
5248

5349
public show(owner: string, scale: Scale) {
54-
this._showOwner = owner;
55-
this._entry.text = this.zoomLabel(scale);
56-
this._entry.show();
57-
}
58-
59-
public hide(owner: string) {
60-
if (owner === this._showOwner) {
61-
this._entry.hide();
62-
this._showOwner = undefined;
63-
}
50+
this.showItem(owner, this.zoomLabel(scale));
6451
}
6552

6653
private zoomLabel(scale: Scale): string {

0 commit comments

Comments
 (0)