From 15d8a6cada8887dcdded426c93082ddae94fb582 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 25 Feb 2013 16:26:50 -0700 Subject: [PATCH] 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. --- spec/app/pane-spec.coffee | 29 ++++++++++++----------------- spec/app/root-view-spec.coffee | 27 ++++++++++++++++++++++----- src/app/pane.coffee | 6 +++--- src/app/root-view.coffee | 4 +++- 4 files changed, 40 insertions(+), 26 deletions(-) diff --git a/spec/app/pane-spec.coffee b/spec/app/pane-spec.coffee index d761c364c..308a5e7d8 100644 --- a/spec/app/pane-spec.coffee +++ b/spec/app/pane-spec.coffee @@ -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] = [] diff --git a/spec/app/root-view-spec.coffee b/spec/app/root-view-spec.coffee index 85be2c4e0..cc56ee87e 100644 --- a/spec/app/root-view-spec.coffee +++ b/spec/app/root-view-spec.coffee @@ -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') diff --git a/src/app/pane.coffee b/src/app/pane.coffee index 9e018ab3a..5a95cf5a4 100644 --- a/src/app/pane.coffee +++ b/src/app/pane.coffee @@ -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() diff --git a/src/app/root-view.coffee b/src/app/root-view.coffee index f958aa5ba..4515f50e2 100644 --- a/src/app/root-view.coffee +++ b/src/app/root-view.coffee @@ -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)