Simplify pane:active-item-changed events

Panes now trigger an event every time their active item changes,
regardless of whether the pane itself is active. Panes also trigger
events when the become active and when they are removed. The rootView
now scopes its active-item-changed event listener only to active panes,
and also listens to listens to pane activation and removal events to
update the title when switching active panes and removing the last
pane.
This commit is contained in:
Nathan Sobo
2013-02-25 16:26:50 -07:00
committed by probablycorey
parent 892ff0c51f
commit 15d8a6cada
4 changed files with 40 additions and 26 deletions

View File

@@ -27,7 +27,7 @@ describe "Pane", ->
expect(view2.css('display')).toBe ''
expect(pane.currentItem).toBe view2
it "triggers 'pane:active-item-changed' if the pane is active and the item isn't already the currentItem", ->
it "triggers 'pane:active-item-changed' if the item isn't already the currentItem", ->
pane.makeActive()
itemChangedHandler = jasmine.createSpy("itemChangedHandler")
container.on 'pane:active-item-changed', itemChangedHandler
@@ -44,10 +44,6 @@ describe "Pane", ->
expect(itemChangedHandler.argsForCall[0][1]).toBe editSession1
itemChangedHandler.reset()
pane.makeInactive()
pane.showItem(editSession2)
expect(itemChangedHandler).not.toHaveBeenCalled()
describe "when the given item isn't yet in the items list on the pane", ->
it "adds it to the items list after the current item", ->
view3 = $$ -> @div id: 'view-3', "View 3"
@@ -144,6 +140,13 @@ describe "Pane", ->
expect(editSession1.destroyed).toBeTruthy()
expect(editSession2.destroyed).toBeTruthy()
it "triggers a 'pane:removed' event with the pane", ->
removedHandler = jasmine.createSpy("removedHandler")
container.on 'pane:removed', removedHandler
pane.remove()
expect(removedHandler).toHaveBeenCalled()
expect(removedHandler.argsForCall[0][1]).toBe pane
describe "when there are other panes", ->
[paneToLeft, paneToRight] = []
@@ -187,13 +190,6 @@ describe "Pane", ->
expect(container.getPanes().length).toBe 1
window.rootView = focus: jasmine.createSpy("rootView.focus")
it "triggers a 'pane:active-item-changed' event with null", ->
itemChangedHandler = jasmine.createSpy("itemChangedHandler")
container.on 'pane:active-item-changed', itemChangedHandler
pane.remove()
expect(itemChangedHandler).toHaveBeenCalled()
expect(itemChangedHandler.argsForCall[0][1]).toBeNull()
describe "when the removed pane is focused", ->
it "calls focus on rootView so we don't lose focus", ->
container.attachToDom()
@@ -214,17 +210,16 @@ describe "Pane", ->
pane.focus()
expect(focusHandler).toHaveBeenCalled()
it "triggers 'pane:active-item-changed' if it was not previously active", ->
itemChangedHandler = jasmine.createSpy("itemChangedHandler")
container.on 'pane:active-item-changed', itemChangedHandler
it "triggers 'pane:became-active' if it was not previously active", ->
becameActiveHandler = jasmine.createSpy("becameActiveHandler")
container.on 'pane:became-active', becameActiveHandler
expect(pane.isActive()).toBeFalsy()
pane.focusin()
expect(pane.isActive()).toBeTruthy()
pane.focusin()
expect(itemChangedHandler.callCount).toBe 1
expect(itemChangedHandler.argsForCall[0][1]).toBe pane.currentItem
expect(becameActiveHandler.callCount).toBe 1
describe "split methods", ->
[pane1, view3, view4] = []

View File

@@ -165,17 +165,34 @@ describe "RootView", ->
expect(rootView.title).toBe 'untitled'
describe "when the project has a path", ->
describe "when there is no active pane item", ->
it "sets the title to the project's path", ->
rootView.getActivePane().remove()
expect(rootView.getActivePaneItem()).toBeUndefined()
expect(rootView.title).toBe project.getPath()
beforeEach ->
rootView.open('b')
describe "when there is an active pane item", ->
it "sets the title to the pane item's title plus the project path", ->
item = rootView.getActivePaneItem()
expect(rootView.title).toBe "#{item.getTitle()} - #{project.getPath()}"
describe "when the active pane's item changes", ->
it "updates the title to the new item's title plus the project path", ->
rootView.getActivePane().showNextItem()
item = rootView.getActivePaneItem()
expect(rootView.title).toBe "#{item.getTitle()} - #{project.getPath()}"
describe "when the last pane item is removed", ->
it "sets the title to the project's path", ->
rootView.getActivePane().remove()
expect(rootView.getActivePaneItem()).toBeUndefined()
expect(rootView.title).toBe project.getPath()
describe "when an inactive pane's item changes", ->
it "does not update the title", ->
pane = rootView.getActivePane()
pane.splitRight()
initialTitle = rootView.title
pane.showNextItem()
expect(rootView.title).toBe initialTitle
describe "font size adjustment", ->
it "increases/decreases font size when increase/decrease-font-size events are triggered", ->
fontSizeBefore = config.get('editor.fontSize')

View File

@@ -40,7 +40,7 @@ class Pane extends View
pane.makeInactive()
wasActive = @isActive()
@addClass('active')
@trigger 'pane:active-item-changed', [@currentItem] unless wasActive
@trigger 'pane:became-active' unless wasActive
makeInactive: ->
@removeClass('active')
@@ -80,7 +80,7 @@ class Pane extends View
@currentItem = item
@currentView = view
@currentView.show()
@trigger 'pane:active-item-changed', [item] if @isActive()
@trigger 'pane:active-item-changed', [item]
addItem: (item) ->
return if _.include(@items, item)
@@ -187,7 +187,7 @@ class Pane extends View
sibling = parentAxis.children().detach()
parentAxis.replaceWith(sibling)
container.adjustPaneDimensions()
container.trigger 'pane:active-item-changed', [null] unless container.getActivePaneItem()
container.trigger 'pane:removed', [this]
afterRemove: ->
item.destroy?() for item in @getItems()

View File

@@ -40,7 +40,9 @@ class RootView extends View
@handleFocus(e) if document.activeElement is document.body
project.on 'path-changed', => @updateTitle()
@on 'pane:active-item-changed', => @updateTitle()
@on 'pane:became-active', => @updateTitle()
@on 'pane:active-item-changed', '.active.pane', => @updateTitle()
@on 'pane:removed', => @updateTitle() unless @getActivePane()
@command 'window:increase-font-size', =>
config.set("editor.fontSize", config.get("editor.fontSize") + 1)