From dec52a7384d9f241cae0cd2d0a51f440bafa7677 Mon Sep 17 00:00:00 2001 From: Matthew Dapena-Tretter Date: Thu, 30 Mar 2017 17:37:55 -0700 Subject: [PATCH] Add `atom.workspace.hide()` This adds an method for hiding items by URI and re-implements `toggle()` in terms of it. (tbh it's really just extracting most of `toggle()` into a new function.) --- spec/workspace-spec.js | 59 ++++++++++++++++++++++++++++++++++ src/workspace.js | 72 ++++++++++++++++++++++++------------------ 2 files changed, 100 insertions(+), 31 deletions(-) diff --git a/spec/workspace-spec.js b/spec/workspace-spec.js index 2237dc33e..e58d4ba6e 100644 --- a/spec/workspace-spec.js +++ b/spec/workspace-spec.js @@ -906,6 +906,65 @@ describe('Workspace', () => { }) }) + describe('::hide(uri)', () => { + let item + const URI = 'atom://hide-test' + + beforeEach(() => { + const el = document.createElement('div') + item = { + getTitle: () => 'Item', + getElement: () => el, + getURI: () => URI + } + }) + + it('removes matching items from the center', () => { + const pane = atom.workspace.getActivePane() + pane.addItem(item) + atom.workspace.hide(URI) + expect(pane.getItems().length).toBe(0) + }) + + it('hides the dock when an item matches', () => { + const dock = atom.workspace.getLeftDock() + const pane = dock.getActivePane() + pane.addItem(item) + dock.activate() + expect(dock.isOpen()).toBe(true) + const itemFound = atom.workspace.hide(URI) + expect(itemFound).toBe(true) + expect(dock.isOpen()).toBe(false) + }) + }) + + describe('::toggle(uri)', () => { + it('shows the item and dock if no item matches', () => { + const URI = 'atom://hide-test' + workspace.addOpener(uri => { + if (uri === URI) { + const el = document.createElement('div') + return { + getDefaultLocation: () => 'left', + getTitle: () => 'Item', + getElement: () => el, + getURI: () => URI + } + } + }) + const dock = workspace.getLeftDock() + expect(dock.isOpen()).toBe(false) + waitsFor(done => { + workspace.onDidOpen(({item}) => { + expect(item.getURI()).toBe(URI) + expect(dock.isOpen()).toBe(true) + done() + }) + workspace.toggle(URI) + }) + }) + }) + describe('the grammar-used hook', () => { it('fires when opening a file or changing the grammar of an open file', () => { let editor = null diff --git a/src/workspace.js b/src/workspace.js index 6c9621b28..927818478 100644 --- a/src/workspace.js +++ b/src/workspace.js @@ -749,6 +749,47 @@ module.exports = class Workspace extends Model { return item } + // Essential: Search the workspace for items matching the given URI and hide them. + // + // * `uri` (optional) A {String} containing a URI. + // + // Returns a {boolean} indicating whether any items were found (and hidden). + hide (uri) { + let foundItems = false + + // If any visible item has the given URI, hide it + for (const container of this.getPaneContainers()) { + const isCenter = container === this.getCenter() + if (isCenter || container.isOpen()) { + for (const pane of container.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 { + container.hide() + } + } + } + } + } + } + + return foundItems + } + + // Essential: Search the workspace for items matching the given URI. If any are found, hide them. + // Otherwise, open the URL. + // + // * `uri` (optional) A {String} containing a URI. + toggle (uri) { + if (!this.hide(uri)) this.open(uri, {searchAllPanes: true}) + } + // Open Atom's license in the active pane. openLicense () { return this.open(path.join(process.resourcesPath, 'LICENSE.md')) @@ -1189,37 +1230,6 @@ 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.getPaneContainers()) { - 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