Skip to content

Commit 6a97fa9

Browse files
committed
Refactored registerCommands. Added show on refresh click
1 parent d8f9bdc commit 6a97fa9

File tree

3 files changed

+88
-55
lines changed

3 files changed

+88
-55
lines changed

src/views/navTreeView.ts

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import {
77
commands,
88
ConfigurationChangeEvent,
9+
Disposable,
910
Range,
1011
Selection,
1112
TextDocumentChangeEvent,
@@ -49,9 +50,6 @@ export class NavTreeView extends GView<NavTreeNode> {
4950
// Initialize View
5051
this.initialize();
5152

52-
// Register View Commands
53-
this.registerCommands();
54-
5553
// Initialize StatusBar
5654
this._statusbar = Control.statusBarController;
5755

@@ -141,30 +139,36 @@ export class NavTreeView extends GView<NavTreeNode> {
141139
}
142140
}
143141

144-
protected registerCommands() {
145-
// Refresh Command
146-
commands.registerCommand(
147-
ViewCommands.RefreshTree,
148-
() => {
149-
if (this._editor && this._editor.document) {
150-
if (this._editor.document.languageId === constants.langId) {
151-
void Control.setContext(Contexts.ViewsNavTreeEnabled, true);
152-
}
142+
protected registerCommands(): Disposable[] {
143+
return [
144+
// Refresh Command
145+
commands.registerCommand(
146+
ViewCommands.RefreshTree,
147+
() => {
148+
if (this._editor && this._editor.document) {
149+
if (this._editor.document.languageId === constants.langId) {
150+
void Control.setContext(Contexts.ViewsNavTreeEnabled, true);
151+
}
153152

154-
void this.refresh();
155-
}
156-
},
157-
this,
158-
);
159-
160-
// Selection Command
161-
commands.registerCommand(
162-
ViewCommands.TreeSelect,
163-
range => {
164-
this.select(range);
165-
},
166-
this,
167-
);
153+
// Refresh View
154+
void this.refresh();
155+
156+
// Show Tree View
157+
void this.show();
158+
}
159+
},
160+
this,
161+
),
162+
163+
// Selection Command
164+
commands.registerCommand(
165+
ViewCommands.TreeSelect,
166+
range => {
167+
this.select(range);
168+
},
169+
this,
170+
),
171+
];
168172
}
169173

170174
protected async refresh(element?: NavTreeNode): Promise<void> {

src/views/statsView.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import {
88
commands,
99
ConfigurationChangeEvent,
10+
Disposable,
1011
TextDocumentChangeEvent,
1112
ThemeIcon,
1213
TreeItemCollapsibleState,
@@ -44,12 +45,8 @@ export class StatsView extends GView<StatsNode> {
4445
constructor() {
4546
super(StatsViewInfo.ViewId, StatsViewInfo.ViewName);
4647

47-
this._editor = window.activeTextEditor;
48-
4948
this.initialize();
5049

51-
this.registerCommands();
52-
5350
this._autoRefresh = configuration.getParam(StatsViewInfo.Config.AutoRefresh);
5451

5552
if (this._autoRefresh) {
@@ -140,19 +137,25 @@ export class StatsView extends GView<StatsNode> {
140137
}
141138
}
142139

143-
protected registerCommands() {
144-
// Refresh Command
145-
commands.registerCommand(
146-
ViewCommands.RefreshStats,
147-
() => {
148-
if (window.activeTextEditor?.document.languageId === constants.langId) {
149-
void Control.setContext(Contexts.ViewsStatsEnabled, true);
150-
}
140+
protected registerCommands(): Disposable[] {
141+
return [
142+
// Refresh Command
143+
commands.registerCommand(
144+
ViewCommands.RefreshStats,
145+
() => {
146+
if (window.activeTextEditor?.document.languageId === constants.langId) {
147+
void Control.setContext(Contexts.ViewsStatsEnabled, true);
148+
}
151149

152-
void this.refresh();
153-
},
154-
this,
155-
);
150+
// Refresh View
151+
void this.refresh();
152+
153+
// Show Stats View
154+
void this.show();
155+
},
156+
this,
157+
),
158+
];
156159
}
157160

158161
protected async refresh(element?: StatsNode): Promise<void> {

src/views/views.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'use strict';
66

77
import {
8+
commands,
89
ConfigurationChangeEvent,
910
Disposable,
1011
Event,
@@ -14,15 +15,16 @@ import {
1415
TreeDataProvider,
1516
TreeItem,
1617
TreeView,
18+
TreeViewVisibilityChangeEvent,
1719
window,
1820
workspace,
1921
} from 'vscode';
20-
import { Control } from '../control';
2122
import { configuration } from '../util/configuration/config';
23+
import { Logger } from '../util/logger';
2224
import { NodeTypes, ViewNode } from './nodes/nodes';
2325

2426
export abstract class GView<TRoot extends ViewNode<NodeTypes>> implements TreeDataProvider<ViewNode>, Disposable {
25-
protected _disposable: Disposable | undefined;
27+
protected _disposables: Disposable[] = [];
2628
protected _root: TRoot | undefined;
2729
protected _tree: TreeView<ViewNode> | undefined;
2830
protected _editor: TextEditor | undefined;
@@ -33,9 +35,17 @@ export abstract class GView<TRoot extends ViewNode<NodeTypes>> implements TreeDa
3335
return this._onDidChangeTreeData.event;
3436
}
3537

38+
private _onDidChangeVisibility: EventEmitter<TreeViewVisibilityChangeEvent> =
39+
new EventEmitter<TreeViewVisibilityChangeEvent>();
40+
get onDidChangeVisibility(): Event<TreeViewVisibilityChangeEvent> {
41+
return this._onDidChangeVisibility.event;
42+
}
43+
3644
protected abstract getRoot(): ViewNode | undefined;
3745

38-
constructor(public readonly id: string, public readonly name: string) {}
46+
constructor(public readonly id: string, public readonly name: string) {
47+
this._disposables.push(...this.registerCommands());
48+
}
3949

4050
protected ensureRoot() {
4151
if (this._root === undefined) {
@@ -46,26 +56,40 @@ export abstract class GView<TRoot extends ViewNode<NodeTypes>> implements TreeDa
4656
}
4757

4858
protected initialize(options: { showCollapseAll?: boolean } = {}) {
49-
if (this._disposable) {
50-
this._disposable.dispose();
51-
this._onDidChangeTreeData = new EventEmitter<ViewNode>();
52-
}
53-
5459
this._tree = window.createTreeView(this.id, {
5560
...options,
5661
treeDataProvider: this,
5762
});
5863

59-
Control.context.subscriptions.push(configuration.onDidChange(this.onConfigurationChanged, this));
64+
this._disposables.push(
65+
configuration.onDidChange(this.onConfigurationChanged, this),
66+
window.onDidChangeActiveTextEditor(() => this.onActiveEditorChanged()),
67+
workspace.onDidChangeTextDocument(e => this.onDocumentChanged(e)),
68+
this._tree,
69+
this._tree.onDidChangeVisibility(this.onVisibilityChanged, this),
70+
);
71+
}
6072

61-
window.onDidChangeActiveTextEditor(() => this.onActiveEditorChanged());
62-
workspace.onDidChangeTextDocument(e => this.onDocumentChanged(e));
73+
protected onVisibilityChanged(e: TreeViewVisibilityChangeEvent) {
74+
this._onDidChangeVisibility.fire(e);
75+
}
6376

64-
this._disposable = Disposable.from(this._tree);
77+
get visible(): boolean {
78+
return this._tree?.visible ?? false;
79+
}
80+
81+
protected async show() {
82+
if (!this.visible) {
83+
try {
84+
void (await commands.executeCommand(`${this.id}.focus`));
85+
} catch (err) {
86+
Logger.error(err, 'Error focusing view');
87+
}
88+
}
6589
}
6690

6791
dispose() {
68-
this._disposable && this._disposable.dispose();
92+
Disposable.from(...this._disposables).dispose();
6993
}
7094

7195
getTreeItem(element: ViewNode): TreeItem | Promise<TreeItem> {
@@ -85,6 +109,8 @@ export abstract class GView<TRoot extends ViewNode<NodeTypes>> implements TreeDa
85109
return element.getParent();
86110
}
87111

112+
protected abstract registerCommands(): Disposable[];
113+
88114
protected abstract refresh(element?: ViewNode): void;
89115

90116
protected abstract onConfigurationChanged(e: ConfigurationChangeEvent): void;

0 commit comments

Comments
 (0)