From 1711d10af2b5a538d32eecb36a3633631210a329 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 6 Apr 2017 16:35:25 -0600 Subject: [PATCH] Aggregate onDidStopChangingActivePaneItem events across all containers --- spec/pane-container-spec.coffee | 23 ----------- spec/workspace-spec.js | 21 ++++++++++- src/pane-container.js | 14 ------- src/workspace.js | 67 +++++++++++++++++++-------------- 4 files changed, 58 insertions(+), 67 deletions(-) diff --git a/spec/pane-container-spec.coffee b/spec/pane-container-spec.coffee index 7e84d6f96..a232eaecd 100644 --- a/spec/pane-container-spec.coffee +++ b/spec/pane-container-spec.coffee @@ -156,29 +156,6 @@ describe "PaneContainer", -> pane2.activate() expect(activatedPanes).toEqual([pane1, pane1, pane2, pane2]) - describe "::onDidStopChangingActivePaneItem()", -> - [container, pane1, pane2, observed] = [] - - beforeEach -> - container = new PaneContainer(root: new Pane(items: [new Object, new Object])) - container.getRoot().splitRight(items: [new Object, new Object]) - [pane1, pane2] = container.getPanes() - - observed = [] - container.onDidStopChangingActivePaneItem (item) -> observed.push(item) - - it "invokes observers when the active item of the active pane stops changing", -> - pane2.activateNextItem() - pane2.activateNextItem() - advanceClock(100) - expect(observed).toEqual [pane2.itemAtIndex(0)] - - it "invokes observers when the active pane stops changing", -> - pane1.activate() - pane2.activate() - advanceClock(100) - expect(observed).toEqual [pane2.itemAtIndex(0)] - describe "::observePanes()", -> it "invokes observers with all current and future panes", -> container = new PaneContainer(params) diff --git a/spec/workspace-spec.js b/spec/workspace-spec.js index 5437e4fb2..ecefe4741 100644 --- a/spec/workspace-spec.js +++ b/spec/workspace-spec.js @@ -1076,7 +1076,7 @@ describe('Workspace', () => { }) }) - describe('pane containers', () => { + describe('active pane containers', () => { it('maintains the active pane and item globally across active pane containers', () => { const leftDock = workspace.getLeftDock() const leftItem1 = {element: document.createElement('div')} @@ -1197,6 +1197,25 @@ describe('Workspace', () => { }) }) + describe('::onDidStopChangingActivePaneItem()', function () { + it('invokes observers when the active item of the active pane stops changing', function () { + const pane1 = atom.workspace.getCenter().getActivePane() + const pane2 = pane1.splitRight({items: [document.createElement('div'), document.createElement('div')]}); + atom.workspace.getLeftDock().getActivePane().addItem(document.createElement('div')) + + emittedItems = [] + atom.workspace.onDidStopChangingActivePaneItem(item => emittedItems.push(item)) + + pane2.activateNextItem() + pane2.activateNextItem() + pane1.activate() + atom.workspace.getLeftDock().activate() + + advanceClock(100) + expect(emittedItems).toEqual([atom.workspace.getLeftDock().getActivePaneItem()]) + }) + }) + 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/pane-container.js b/src/pane-container.js index fce117c0f..88922c000 100644 --- a/src/pane-container.js +++ b/src/pane-container.js @@ -5,7 +5,6 @@ const ItemRegistry = require('./item-registry') const PaneContainerElement = require('./pane-container-element') const SERIALIZATION_VERSION = 1 -const STOPPED_CHANGING_ACTIVE_PANE_ITEM_DELAY = 100 module.exports = class PaneContainer { @@ -16,7 +15,6 @@ class PaneContainer { this.subscriptions = new CompositeDisposable() this.itemRegistry = new ItemRegistry() this.alive = true - this.stoppedChangingActivePaneItemTimeout = null this.setRoot(new Pane({container: this, config: this.config, applicationDelegate, notificationManager, deserializerManager, viewRegistry: this.viewRegistry})) this.didActivatePane(this.getRoot()) @@ -30,7 +28,6 @@ class PaneContainer { destroy () { this.alive = false - this.cancelStoppedChangingActivePaneItemTimeout() for (let pane of this.getRoot().getPanes()) { pane.destroy() } this.subscriptions.dispose() this.emitter.dispose() @@ -277,17 +274,6 @@ class PaneContainer { didChangeActiveItemOnPane (pane, activeItem) { if (pane === this.getActivePane()) { this.emitter.emit('did-change-active-pane-item', activeItem) - this.cancelStoppedChangingActivePaneItemTimeout() - this.stoppedChangingActivePaneItemTimeout = setTimeout(() => { - this.stoppedChangingActivePaneItemTimeout = null - this.emitter.emit('did-stop-changing-active-pane-item', activeItem) - }, STOPPED_CHANGING_ACTIVE_PANE_ITEM_DELAY) - } - } - - cancelStoppedChangingActivePaneItemTimeout () { - if (this.stoppedChangingActivePaneItemTimeout != null) { - clearTimeout(this.stoppedChangingActivePaneItemTimeout) } } } diff --git a/src/workspace.js b/src/workspace.js index 1e4eb6484..e3356d521 100644 --- a/src/workspace.js +++ b/src/workspace.js @@ -19,6 +19,8 @@ const Task = require('./task') const WorkspaceCenter = require('./workspace-center') const WorkspaceElement = require('./workspace-element') +const STOPPED_CHANGING_ACTIVE_PANE_ITEM_DELAY = 100 + // Essential: Represents the state of the user interface for the entire window. // An instance of this class is available via the `atom.workspace` global. // @@ -35,7 +37,6 @@ module.exports = class Workspace extends Model { this.updateWindowTitle = this.updateWindowTitle.bind(this) this.updateDocumentEdited = this.updateDocumentEdited.bind(this) this.didDestroyPaneItem = this.didDestroyPaneItem.bind(this) - this.didChangeActivePaneItem = this.didChangeActivePaneItem.bind(this) this.didChangeActivePaneOnPaneContainer = this.didChangeActivePaneOnPaneContainer.bind(this) this.didChangeActivePaneItemOnPaneContainer = this.didChangeActivePaneItemOnPaneContainer.bind(this) this.didActivatePaneContainer = this.didActivatePaneContainer.bind(this) @@ -59,6 +60,7 @@ module.exports = class Workspace extends Model { this.emitter = new Emitter() this.openers = [] this.destroyedItemURIs = [] + this.stoppedChangingActivePaneItemTimeout = null this.paneContainer = new PaneContainer({ location: 'center', @@ -177,7 +179,7 @@ module.exports = class Workspace extends Model { } subscribeToEvents () { - this.subscribeToActiveItem() + this.project.onDidChangePaths(this.updateWindowTitle) this.subscribeToFontSize() this.subscribeToAddedItems() this.subscribeToMovedItems() @@ -259,7 +261,7 @@ module.exports = class Workspace extends Model { didActivatePaneContainer (paneContainer) { if (paneContainer !== this.getActivePaneContainer()) { this.activePaneContainer = paneContainer - if (global.debug) debugger + this.didChangeActivePaneItem(this.activePaneContainer.getActivePaneItem()) this.emitter.emit('did-change-active-pane-container', this.activePaneContainer) this.emitter.emit('did-change-active-pane', this.activePaneContainer.getActivePane()) this.emitter.emit('did-change-active-pane-item', this.activePaneContainer.getActivePaneItem()) @@ -274,38 +276,15 @@ module.exports = class Workspace extends Model { didChangeActivePaneItemOnPaneContainer (paneContainer, item) { if (paneContainer === this.getActivePaneContainer()) { + this.didChangeActivePaneItem(item) this.emitter.emit('did-change-active-pane-item', item) } } - didHideDock () { - this.getCenter().activate() - } - - setHoveredDock (hoveredDock) { - this.hoveredDock = hoveredDock - _.values(this.docks).forEach(dock => { - dock.setHovered(dock === hoveredDock) - }) - } - - setDraggingItem (draggingItem) { - _.values(this.docks).forEach(dock => { - dock.setDraggingItem(draggingItem) - }) - } - - subscribeToActiveItem () { - this.project.onDidChangePaths(this.updateWindowTitle) - this.onDidChangeActivePaneItem(this.didChangeActivePaneItem) - } - didChangeActivePaneItem (item) { this.updateWindowTitle() this.updateDocumentEdited() - if (this.activeItemSubscriptions != null) { - this.activeItemSubscriptions.dispose() - } + if (this.activeItemSubscriptions) this.activeItemSubscriptions.dispose() this.activeItemSubscriptions = new CompositeDisposable() let modifiedSubscription, titleSubscription @@ -334,6 +313,35 @@ module.exports = class Workspace extends Model { if (titleSubscription != null) { this.activeItemSubscriptions.add(titleSubscription) } if (modifiedSubscription != null) { this.activeItemSubscriptions.add(modifiedSubscription) } + + this.cancelStoppedChangingActivePaneItemTimeout() + this.stoppedChangingActivePaneItemTimeout = setTimeout(() => { + this.stoppedChangingActivePaneItemTimeout = null + this.emitter.emit('did-stop-changing-active-pane-item', item) + }, STOPPED_CHANGING_ACTIVE_PANE_ITEM_DELAY) + } + + cancelStoppedChangingActivePaneItemTimeout () { + if (this.stoppedChangingActivePaneItemTimeout != null) { + clearTimeout(this.stoppedChangingActivePaneItemTimeout) + } + } + + didHideDock () { + this.getCenter().activate() + } + + setHoveredDock (hoveredDock) { + this.hoveredDock = hoveredDock + _.values(this.docks).forEach(dock => { + dock.setHovered(dock === hoveredDock) + }) + } + + setDraggingItem (draggingItem) { + _.values(this.docks).forEach(dock => { + dock.setDraggingItem(draggingItem) + }) } subscribeToAddedItems () { @@ -497,7 +505,7 @@ module.exports = class Workspace extends Model { // // Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. onDidStopChangingActivePaneItem (callback) { - return this.paneContainer.onDidStopChangingActivePaneItem(callback) + return this.emitter.on('did-stop-changing-active-pane-item', callback) } // Essential: Invoke the given callback with the current active pane item and @@ -1352,6 +1360,7 @@ module.exports = class Workspace extends Model { // Called by Model superclass when destroyed destroyed () { this.paneContainer.destroy() + this.cancelStoppedChangingActivePaneItemTimeout() if (this.activeItemSubscriptions != null) { this.activeItemSubscriptions.dispose() }