Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 9a70d1e

Browse files
Nathan Sobomaxbrunsfeld
authored andcommitted
WIP: Start on making tree view a dock item
1 parent eebc217 commit 9a70d1e

File tree

5 files changed

+96
-218
lines changed

5 files changed

+96
-218
lines changed

lib/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.TREE_VIEW_URI = 'atom://tree-view'

lib/tree-view-package.js

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,33 @@ const path = require('path')
33

44
const FileIcons = require('./file-icons')
55
const TreeView = require('./tree-view')
6+
const {TREE_VIEW_URI} = require('./constants')
67

78
module.exports =
89
class TreeViewPackage {
9-
constructor () {
10-
this.treeView = null
11-
}
12-
13-
activate (state) {
14-
this.state = state
10+
activate () {
1511
this.disposables = new CompositeDisposable()
16-
if (this.shouldAttach()) {
17-
if (this.state.attached == null) {
18-
this.state.attached = true
19-
}
20-
}
2112

22-
if (this.state.attached) {
23-
this.createView()
24-
}
13+
this.disposables.add(atom.commands.add('atom-workspace', {
14+
'tree-view:show': () => this.getTreeViewInstance().show(),
15+
'tree-view:toggle': () => this.getTreeViewInstance().toggle(),
16+
'tree-view:toggle-focus': () => this.getTreeViewInstance().toggleFocus(),
17+
'tree-view:reveal-active-file': () => this.getTreeViewInstance().revealActiveFile(),
18+
'tree-view:toggle-side': () => this.getTreeViewInstance().toggleSide(),
19+
'tree-view:add-file': () => this.getTreeViewInstance().add(true),
20+
'tree-view:add-folder': () => this.getTreeViewInstance().add(false),
21+
'tree-view:duplicate': () => this.getTreeViewInstance().copySelectedEntry(),
22+
'tree-view:remove': () => this.getTreeViewInstance().removeSelectedEntries(),
23+
'tree-view:rename': () => this.getTreeViewInstance().moveSelectedEntry(),
24+
'tree-view:show-current-file-in-file-manager': () => this.getTreeViewInstance().showCurrentFileInFileManager()
25+
}))
2526

26-
return this.disposables.add(atom.commands.add('atom-workspace', {
27-
'tree-view:show': () => this.createView().show(),
28-
'tree-view:toggle': () => this.createView().toggle(),
29-
'tree-view:toggle-focus': () => this.createView().toggleFocus(),
30-
'tree-view:reveal-active-file': () => this.createView().revealActiveFile(),
31-
'tree-view:toggle-side': () => this.createView().toggleSide(),
32-
'tree-view:add-file': () => this.createView().add(true),
33-
'tree-view:add-folder': () => this.createView().add(false),
34-
'tree-view:duplicate': () => this.createView().copySelectedEntry(),
35-
'tree-view:remove': () => this.createView().removeSelectedEntries(),
36-
'tree-view:rename': () => this.createView().moveSelectedEntry(),
37-
'tree-view:show-current-file-in-file-manager': () => this.createView().showCurrentFileInFileManager()
38-
})
39-
)
27+
this.disposables.add(atom.workspace.addOpener((URI) => {
28+
if (URI === TREE_VIEW_URI) return this.getTreeViewInstance()
29+
}))
30+
31+
this.disposables.add(atom.project.onDidChangePaths(this.createOrDestroyTreeViewIfNeeded.bind(this)))
32+
this.createOrDestroyTreeViewIfNeeded()
4033
}
4134

4235
deactivate () {
@@ -56,33 +49,36 @@ class TreeViewPackage {
5649
)
5750
}
5851

59-
serialize () {
60-
if (this.treeView != null) {
61-
return this.treeView.serialize()
62-
} else {
63-
return this.state
52+
getTreeViewInstance (state = {}) {
53+
if (this.treeView == null) {
54+
this.treeView = new TreeView(state)
6455
}
56+
return this.treeView
6557
}
6658

67-
createView () {
68-
if (this.treeView == null) {
69-
this.treeView = new TreeView(this.state)
59+
createOrDestroyTreeViewIfNeeded () {
60+
if (this.shouldAttachTreeView()) {
61+
// TODO: Pass activate: false here
62+
// {activate: !atom.workspace.getActivePaneItem()}
63+
atom.workspace.open(TREE_VIEW_URI)
64+
} else {
65+
if (this.treeView) this.treeView.destroy()
7066
}
71-
return this.treeView
7267
}
7368

74-
shouldAttach () {
75-
const projectPath = atom.project.getPaths()[0] || ''
69+
shouldAttachTreeView () {
70+
if (atom.project.getPaths().length === 0) return false
7671

77-
if (atom.workspace.getActivePaneItem()) {
78-
return false
79-
} else if (path.basename(projectPath) === '.git') {
80-
// Only attach when the project path matches the path to open signifying
81-
// the .git folder was opened explicitly and not by using Atom as the Git
82-
// editor.
83-
return projectPath === atom.getLoadSettings().pathToOpen
84-
} else {
85-
return true
72+
// Avoid opening the tree view if Atom was opened as the Git editor...
73+
// Only show it if the .git folder was explicitly opened.
74+
if (path.basename(atom.project.getPaths()[0]) === '.git') {
75+
return atom.project.getPaths()[0] === atom.getLoadSettings().pathToOpen
8676
}
77+
78+
return true
79+
}
80+
81+
shouldShowTreeViewAfterAttaching () {
82+
if (atom.workspace.getActivePaneItem()) return false
8783
}
8884
}

0 commit comments

Comments
 (0)