From ab47df19879a5cebb2ed160156631ea864765d65 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 23 Jul 2012 11:29:25 -0600 Subject: [PATCH] Make command-panel sticky like tree view is (fit into the "tool panel" scheme) Esc on the command panel just moves focus back to the editor. Ctrl-0 will move focus back to the panel if it is open but not focused. --- spec/extensions/command-panel-spec.coffee | 63 ++++++++++--------- .../command-panel/command-panel.coffee | 9 ++- src/extensions/command-panel/keymap.coffee | 2 +- 3 files changed, 42 insertions(+), 32 deletions(-) diff --git a/spec/extensions/command-panel-spec.coffee b/spec/extensions/command-panel-spec.coffee index 87a1afeb6..f28fd4e63 100644 --- a/spec/extensions/command-panel-spec.coffee +++ b/spec/extensions/command-panel-spec.coffee @@ -26,31 +26,43 @@ describe "CommandPanel", -> newRootView.remove() - describe "when toggle-command-panel is triggered on the root view", -> - it "toggles the command panel", -> + describe "when command-panel:toggle is triggered on the root view", -> + beforeEach -> rootView.attachToDom() - expect(rootView.find('.command-panel')).not.toExist() - expect(rootView.getActiveEditor().isFocused).toBeTruthy() - expect(commandPanel.miniEditor.isFocused).toBeFalsy() - rootView.trigger 'command-panel:toggle' - window.advanceClock() # Setting the font is in a defer statement - expect(rootView.find('.command-panel').view()).toBe commandPanel - expect(commandPanel.miniEditor.isFocused).toBeTruthy() - # this is currently assigned dynamically since our css scheme lacks variables - expect(commandPanel.prompt.css('font')).toBe commandPanel.miniEditor.css('font') - commandPanel.miniEditor.insertText 's/war/peace/g' + describe "when the command panel is visible", -> + beforeEach -> + commandPanel.attach() - rootView.trigger 'command-panel:toggle' - expect(rootView.find('.command-panel')).not.toExist() - expect(rootView.getActiveEditor().isFocused).toBeTruthy() - expect(commandPanel.miniEditor.isFocused).toBeFalsy() + describe "when the command panel is focused", -> + it "closes the command panel", -> + expect(commandPanel.miniEditor.hiddenInput).toMatchSelector ':focus' + rootView.trigger 'command-panel:toggle' + expect(commandPanel.hasParent()).toBeFalsy() + + describe "when the command panel is not focused", -> + it "focuses the command panel", -> + rootView.focus() + expect(commandPanel.miniEditor.hiddenInput).not.toMatchSelector ':focus' + rootView.trigger 'command-panel:toggle' + expect(commandPanel.hasParent()).toBeTruthy() + expect(commandPanel.miniEditor.hiddenInput).toMatchSelector ':focus' + + describe "when the command panel is not visible", -> + it "shows and focuses the command panel", -> + expect(commandPanel.hasParent()).toBeFalsy() + rootView.trigger 'command-panel:toggle' + expect(commandPanel.hasParent()).toBeTruthy() + + describe "when command-panel:unfocus is triggered on the command panel", -> + it "returns focus to the root view but does not hide the command panel", -> + rootView.attachToDom() + commandPanel.attach() + expect(commandPanel.miniEditor.hiddenInput).toMatchSelector ':focus' + commandPanel.trigger 'command-panel:unfocus' + expect(commandPanel.hasParent()).toBeTruthy() + expect(commandPanel.miniEditor.hiddenInput).not.toMatchSelector ':focus' - rootView.trigger 'command-panel:toggle' - expect(rootView.find('.command-panel').view()).toBe commandPanel - expect(commandPanel.miniEditor.isFocused).toBeTruthy() - expect(commandPanel.miniEditor.getText()).toBe '' - expect(commandPanel.miniEditor.getCursorScreenPosition()).toEqual [0, 0] describe "when command-panel:repeat-relative-address is triggered on the root view", -> it "repeats the last search command if there is one", -> @@ -75,7 +87,7 @@ describe "CommandPanel", -> rootView.trigger 'command-panel:repeat-relative-address' expect(editor.getSelection().getBufferRange()).toEqual [[3,31], [3,38]] - describe "when command-pane:repeat-relative-address-in-reverse is triggered on the root view", -> + describe "when command-panel:repeat-relative-address-in-reverse is triggered on the root view", -> it "it repeats the last relative address in the reverse direction", -> rootView.trigger 'command-panel:repeat-relative-address-in-reverse' @@ -104,13 +116,6 @@ describe "CommandPanel", -> expect(commandPanel.parent).not.toBeEmpty() expect(commandPanel.miniEditor.getText()).toBe "/" - describe "when esc is pressed in the command panel", -> - it "closes the command panel", -> - rootView.trigger 'command-panel:toggle' - expect(rootView.find('.command-panel').view()).toBe commandPanel - commandPanel.miniEditor.hiddenInput.trigger keydownEvent('escape') - expect(rootView.find('.command-panel')).not.toExist() - describe "when return is pressed on the panel's editor", -> it "calls execute", -> spyOn(commandPanel, 'execute') diff --git a/src/extensions/command-panel/command-panel.coffee b/src/extensions/command-panel/command-panel.coffee index eef6f7c0d..4dd941048 100644 --- a/src/extensions/command-panel/command-panel.coffee +++ b/src/extensions/command-panel/command-panel.coffee @@ -38,6 +38,7 @@ class CommandPanel extends View @commandInterpreter = new CommandInterpreter() @history = [] + @on 'command-panel:unfocus', => @rootView.focus() @rootView.on 'command-panel:toggle', => @toggle() @rootView.on 'command-panel:execute', => @execute() @rootView.on 'command-panel:find-in-file', => @attach("/") @@ -50,8 +51,12 @@ class CommandPanel extends View @miniEditor.on 'move-down', => @navigateForwardInHistory() toggle: -> - if @parent().length then @detach() else @attach() - false + if @miniEditor.isFocused + @detach() + @rootView.focus() + else + @attach() unless @hasParent() + @miniEditor.focus() attach: (text='') -> @rootView.append(this) diff --git a/src/extensions/command-panel/keymap.coffee b/src/extensions/command-panel/keymap.coffee index 605ac9b2e..825394d8a 100644 --- a/src/extensions/command-panel/keymap.coffee +++ b/src/extensions/command-panel/keymap.coffee @@ -6,7 +6,7 @@ window.keymap.bindKeys '*' window.keymap.bindKeys '.command-panel .editor input', 'meta-w': 'command-panel:toggle' - escape: 'command-panel:toggle' + escape: 'command-panel:unfocus' enter: 'command-panel:execute' window.keymap.bindKeys '.editor',