55'use strict' ;
66
77import {
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' ;
2122import { configuration } from '../util/configuration/config' ;
23+ import { Logger } from '../util/logger' ;
2224import { NodeTypes , ViewNode } from './nodes/nodes' ;
2325
2426export 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