Skip to content

Commit

Permalink
Merge pull request atom#1315 from atom/wl-add-projects-view
Browse files Browse the repository at this point in the history
Always show Tree View
  • Loading branch information
Nathan Sobo authored Jun 20, 2019
2 parents 38de0da + 703dd72 commit b0debd0
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 161 deletions.
36 changes: 36 additions & 0 deletions lib/add-projects-view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module.exports =
class AddProjectView {
constructor () {
this.element = document.createElement('div')
this.element.id = 'add-projects-view'

this.icon = document.createElement('div')
this.icon.classList.add('icon', 'icon-large', 'icon-telescope')
this.element.appendChild(this.icon)

this.description = document.createElement('div')
this.description.classList.add('description')
this.description.innerText = 'Your project is currently empty'
this.element.appendChild(this.description)

this.addProjectsButton = document.createElement('button')
this.addProjectsButton.classList.add('btn', 'btn-primary')
this.addProjectsButton.innerText = 'Add folders'
this.addProjectsButton.addEventListener('click', () => {
atom.pickFolder(paths => {
if (paths) {
atom.project.setPaths(paths)
}
})
})
this.element.appendChild(this.addProjectsButton)

this.reopenProjectButton = document.createElement('button')
this.reopenProjectButton.classList.add('btn')
this.reopenProjectButton.innerText = 'Reopen a project'
this.reopenProjectButton.addEventListener('click', () => {
atom.commands.dispatch(this.element, 'application:reopen-project')
})
this.element.appendChild(this.reopenProjectButton)
}
}
11 changes: 9 additions & 2 deletions lib/get-icon-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class IconServices {
}

updateDirectoryIcon (view) {
view.directoryName.className = ''

const classes = ['name', 'icon']
if (this.elementIcons) {
const disposable = this.elementIcons(view.directoryName, view.directory.path)
this.elementIconDisposables.add(disposable)
Expand All @@ -65,11 +68,14 @@ class IconServices {
if (view.directory.submodule) iconClass = 'icon-file-submodule'
}
}
view.directoryName.classList.add(iconClass)
classes.push(iconClass)
}
view.directoryName.classList.add(...classes)
}

updateFileIcon (view) {
view.fileName.className = ''

const classes = ['name', 'icon']
let iconClass
if (this.elementIcons) {
Expand All @@ -81,7 +87,8 @@ class IconServices {
if (iconClass) {
if (!Array.isArray(iconClass)) {
iconClass = iconClass.toString().split(/\s+/g)
} classes.push(...iconClass)
}
classes.push(...iconClass)
}
view.fileName.classList.add(...classes)
}
Expand Down
60 changes: 6 additions & 54 deletions lib/tree-view-package.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const {Disposable, CompositeDisposable} = require('atom')
const path = require('path')

const getIconServices = require('./get-icon-services')
const TreeView = require('./tree-view')
Expand All @@ -21,18 +20,12 @@ class TreeViewPackage {
'tree-view:show-current-file-in-file-manager': () => this.getTreeViewInstance().showCurrentFileInFileManager()
}))

this.disposables.add(atom.project.onDidChangePaths(this.createOrDestroyTreeViewIfNeeded.bind(this)))

if (this.shouldAttachTreeView()) {
const treeView = this.getTreeViewInstance()
const showOnAttach = !atom.workspace.getActivePaneItem()
this.treeViewOpenPromise = atom.workspace.open(treeView, {
activatePane: showOnAttach,
activateItem: showOnAttach
})
} else {
this.treeViewOpenPromise = Promise.resolve()
}
const treeView = this.getTreeViewInstance()
const showOnAttach = !atom.workspace.getActivePaneItem()
this.treeViewOpenPromise = atom.workspace.open(treeView, {
activatePane: showOnAttach,
activateItem: showOnAttach
})
}

async deactivate () {
Expand All @@ -51,10 +44,8 @@ class TreeViewPackage {

consumeFileIcons (service) {
getIconServices().setFileIcons(service)
if (this.treeView) this.treeView.updateRoots()
return new Disposable(() => {
getIconServices().resetFileIcons()
if (this.treeView) this.treeView.updateRoots()
})
}

Expand All @@ -72,43 +63,4 @@ class TreeViewPackage {
}
return this.treeView
}

createOrDestroyTreeViewIfNeeded () {
if (this.shouldAttachTreeView()) {
const treeView = this.getTreeViewInstance()
const paneContainer = atom.workspace.paneContainerForURI(treeView.getURI())
if (paneContainer) {
paneContainer.show()
} else {
atom.workspace.open(treeView, {
activatePane: false,
activateItem: false
}).then(() => {
const paneContainer = atom.workspace.paneContainerForURI(treeView.getURI())
if (paneContainer) paneContainer.show()
})
}
} else {
if (this.treeView) {
const pane = atom.workspace.paneForItem(this.treeView)
if (pane) pane.removeItem(this.treeView)
}
}
}

shouldAttachTreeView () {
if (atom.project.getPaths().length === 0) return false

// Avoid opening the tree view if Atom was opened as the Git editor...
// Only show it if the .git folder was explicitly opened.
if (path.basename(atom.project.getPaths()[0]) === '.git') {
return atom.project.getPaths()[0] === atom.getLoadSettings().pathToOpen
}

return true
}

shouldShowTreeViewAfterAttaching () {
if (atom.workspace.getActivePaneItem()) return false
}
}
69 changes: 41 additions & 28 deletions lib/tree-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ MoveDialog = require './move-dialog'
CopyDialog = require './copy-dialog'
IgnoredNames = null # Defer requiring until actually needed

AddProjectsView = require './add-projects-view'

Directory = require './directory'
DirectoryView = require './directory-view'
RootDragAndDrop = require './root-drag-and-drop'
Expand All @@ -32,7 +34,6 @@ class TreeView

@list = document.createElement('ol')
@list.classList.add('tree-view-root', 'full-menu', 'list-tree', 'has-collapsable-children', 'focusable-panel')
@element.appendChild(@list)

@disposables = new CompositeDisposable
@emitter = new Emitter
Expand Down Expand Up @@ -323,33 +324,45 @@ class TreeView
root.directory.destroy()
root.remove()

IgnoredNames ?= require('./ignored-names')

@roots = for projectPath in atom.project.getPaths()
stats = fs.lstatSyncNoException(projectPath)
continue unless stats
stats = _.pick stats, _.keys(stats)...
for key in ["atime", "birthtime", "ctime", "mtime"]
stats[key] = stats[key].getTime()

directory = new Directory({
name: path.basename(projectPath)
fullPath: projectPath
symlink: false
isRoot: true
expansionState: expansionStates[projectPath] ?
oldExpansionStates[projectPath] ?
{isExpanded: true}
ignoredNames: new IgnoredNames()
@useSyncFS
stats
})
root = new DirectoryView(directory).element
@list.appendChild(root)
root

# The DOM has been recreated; reselect everything
@selectMultipleEntries(@entryForPath(selectedPath)) for selectedPath in selectedPaths
@roots = []

projectPaths = atom.project.getPaths()
if projectPaths.length > 0
@element.appendChild(@list) unless @element.querySelector('tree-view-root')

addProjectsViewElement = @element.querySelector('#add-projects-view')
@element.removeChild(addProjectsViewElement) if addProjectsViewElement

IgnoredNames ?= require('./ignored-names')

@roots = for projectPath in projectPaths
stats = fs.lstatSyncNoException(projectPath)
continue unless stats
stats = _.pick stats, _.keys(stats)...
for key in ["atime", "birthtime", "ctime", "mtime"]
stats[key] = stats[key].getTime()

directory = new Directory({
name: path.basename(projectPath)
fullPath: projectPath
symlink: false
isRoot: true
expansionState: expansionStates[projectPath] ?
oldExpansionStates[projectPath] ?
{isExpanded: true}
ignoredNames: new IgnoredNames()
@useSyncFS
stats
})
root = new DirectoryView(directory).element
@list.appendChild(root)
root

# The DOM has been recreated; reselect everything
@selectMultipleEntries(@entryForPath(selectedPath)) for selectedPath in selectedPaths
else
@element.removeChild(@list) if @element.querySelector('.tree-view-root')
@element.appendChild(new AddProjectsView().element) unless @element.querySelector('#add-projects-view')

getActivePath: -> atom.workspace.getCenter().getActivePaneItem()?.getPath?()

Expand Down
Loading

0 comments on commit b0debd0

Please sign in to comment.