diff --git a/spec/pane-container-spec.coffee b/spec/pane-container-spec.coffee index ad7f80b6a..c87a2fe3c 100644 --- a/spec/pane-container-spec.coffee +++ b/spec/pane-container-spec.coffee @@ -66,3 +66,24 @@ describe "PaneContainer", -> pane2.destroy() expect(container.getActivePane()).toBe pane1 expect(pane1.isActive()).toBe true + + describe "::onDidChangeActivePaneItem()", -> + [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.onDidChangeActivePaneItem (item) -> observed.push(item) + + it "invokes observers when the active item of the active pane changes", -> + pane2.activateNextItem() + pane2.activateNextItem() + expect(observed).toEqual [pane2.itemAtIndex(1), pane2.itemAtIndex(0)] + + it "invokes observers when the active pane changes", -> + pane1.activate() + pane2.activate() + expect(observed).toEqual [pane1.itemAtIndex(0), pane2.itemAtIndex(0)] diff --git a/src/pane-container.coffee b/src/pane-container.coffee index 63e6a5a9a..a3a1f3ba7 100644 --- a/src/pane-container.coffee +++ b/src/pane-container.coffee @@ -1,6 +1,6 @@ {find} = require 'underscore-plus' {Model} = require 'theorist' -{Emitter} = require 'event-kit' +{Emitter, CompositeDisposable} = require 'event-kit' Serializable = require 'serializable' Pane = require './pane' @@ -26,10 +26,13 @@ class PaneContainer extends Model super @emitter = new Emitter + @subscriptions = new CompositeDisposable @subscribe @$root, @onRootChanged @destroyEmptyPanes() if params?.destroyEmptyPanes + @monitorActivePaneItem() + deserializeParams: (params) -> params.root = atom.deserializers.deserialize(params.root, container: this) params.destroyEmptyPanes = atom.config.get('core.destroyEmptyPanes') @@ -43,6 +46,17 @@ class PaneContainer extends Model onDidChangeActivePane: (fn) -> @emitter.on 'did-change-active-pane', fn + observeActivePane: (fn) -> + fn(@getActivePane()) + @onDidChangeActivePane(fn) + + onDidChangeActivePaneItem: (fn) -> + @emitter.on 'did-change-active-pane-item', fn + + observeActivePaneItem: (fn) -> + fn(@getActivePaneItem()) + @onDidChangeActivePaneItem(fn) + getRoot: -> @root replaceChild: (oldChild, newChild) -> @@ -61,6 +75,9 @@ class PaneContainer extends Model @emitter.emit 'did-change-active-pane', @activePane @activePane + getActivePaneItem: -> + @getActivePane().getActiveItem() + paneForUri: (uri) -> find @getPanes(), (pane) -> pane.itemForUri(uri)? @@ -99,7 +116,6 @@ class PaneContainer extends Model root.parent = this root.container = this - @activePane ?= root if root instanceof Pane destroyEmptyPanes: -> @@ -111,3 +127,17 @@ class PaneContainer extends Model # Called by Model superclass when destroyed destroyed: -> pane.destroy() for pane in @getPanes() + @subscriptions.dispose() + @emitter.dispose() + + monitorActivePaneItem: -> + childSubscription = null + @subscriptions.add @observeActivePane (activePane) => + if childSubscription? + @subscriptions.remove(childSubscription) + childSubscription.dispose() + + childSubscription = activePane.observeActiveItem (activeItem) => + @emitter.emit 'did-change-active-pane-item', activeItem + + @subscriptions.add(childSubscription)