From d854a88dbbe3613ea2d31b83cfffe4a47e6f86f9 Mon Sep 17 00:00:00 2001 From: Matthew Dapena-Tretter Date: Tue, 14 Mar 2017 16:31:06 -0700 Subject: [PATCH] Add `workspace.toggle()` method --- src/dock.js | 12 +++++++++ src/workspace-center.js | 2 ++ src/workspace.js | 60 ++++++++++++++++++++++++++++++++++------- 3 files changed, 64 insertions(+), 10 deletions(-) diff --git a/src/dock.js b/src/dock.js index e72b1415f..c4d95eecb 100644 --- a/src/dock.js +++ b/src/dock.js @@ -88,10 +88,22 @@ module.exports = class Dock { this.setState({draggingItem}) } + activate () { + this.setState({open: true}) + } + + hide () { + this.setState({open: false}) + } + toggle () { this.setState({open: !this.state.open}) } + isOpen () { + return this.state.open + } + setState (newState) { const prevState = this.state const nextState = Object.assign({}, prevState, newState) diff --git a/src/workspace-center.js b/src/workspace-center.js index 370d37b75..c2c875b74 100644 --- a/src/workspace-center.js +++ b/src/workspace-center.js @@ -7,6 +7,8 @@ module.exports = class WorkspaceCenter { this.paneContainer = paneContainer } + activate () {} + /* Section: Event Subscription */ diff --git a/src/workspace.js b/src/workspace.js index dbbba1a97..786ec85ee 100644 --- a/src/workspace.js +++ b/src/workspace.js @@ -776,22 +776,30 @@ module.exports = class Workspace extends Model { const uri = options.uri == null && typeof item.getURI === 'function' ? item.getURI() : options.uri + let paneLocation + if (pane != null) { + paneLocation = this.getPaneLocations().find(location => location.getPanes().includes(pane)) + } + + // Determine which location to use, unless a split was provided. In that case, make sure it goes + // in the center location (legacy behavior) let location - // If a split was provided, make sure it goes in the center location (legacy behavior) - if (pane == null && split == null) { - if (uri != null) { - location = this.previousLocations.load(uri) - } - if (location == null && typeof item.getDefaultLocation === 'function') { - location = item.getDefaultLocation() - } + if (paneLocation == null && pane == null && split == null && uri != null) { + location = this.previousLocations.load(uri) } return Promise.resolve(location) .then(location => { + if (paneLocation == null) { + if (location == null && typeof item.getDefaultLocation === 'function') { + location = item.getDefaultLocation() + } + paneLocation = this.docks[location] || this.getCenter() + } + }) + .then(() => { if (pane != null) return pane - - pane = this.docks[location] == null ? this.getActivePane() : this.docks[location].getActivePane() + pane = paneLocation.getActivePane() switch (split) { case 'left': return pane.findLeftmostSibling() case 'right': return pane.findOrCreateRightmostSibling() @@ -814,6 +822,7 @@ module.exports = class Workspace extends Model { if (activatePane) { pane.activate() } + paneLocation.activate() let initialColumn = 0 let initialLine = 0 @@ -1188,6 +1197,37 @@ module.exports = class Workspace extends Model { return [this.getCenter(), ..._.values(this.docks)] } + toggle (uri) { + let foundItems = false + + // If any visible item has the given URI, hide it + for (const location of this.getPaneLocations()) { + const isCenter = location === this.getCenter() + if (isCenter || location.isOpen()) { + for (const pane of location.getPanes()) { + const activeItem = pane.getActiveItem() + if (activeItem != null && typeof activeItem.getURI === 'function') { + const itemURI = activeItem.getURI() + if (itemURI === uri) { + foundItems = true + // We can't really hide the center so we just destroy the item. + if (isCenter) { + pane.destroyItem(activeItem) + } else { + location.hide() + } + } + } + } + } + } + + // If no visible items had the URI, show it. + if (!foundItems) { + this.open(uri, {searchAllPanes: true}) + } + } + /* Section: Panels