From 285c04ea62a869f910fbdfb5201084f51568bed6 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 20 Mar 2012 12:29:47 -0600 Subject: [PATCH] WIP: Split panes can be closed with meta-w MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Still some issues… view gets screwed up after closing panes in certain layout arrangements. --- spec/atom/root-view-spec.coffee | 29 +++++++++++++++++++++++-- src/atom/editor.coffee | 10 ++++++++- src/atom/root-view.coffee | 20 +++++++++++++++-- src/stdlib/underscore-extensions.coffee | 3 ++- 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/spec/atom/root-view-spec.coffee b/spec/atom/root-view-spec.coffee index 23751bbc9..a63226187 100644 --- a/spec/atom/root-view-spec.coffee +++ b/spec/atom/root-view-spec.coffee @@ -198,14 +198,39 @@ describe "RootView", -> expect(editor5.position().top).toBe editor4.outerHeight() expect(editor5.outerHeight()).toBe Math.floor(1/3 * rootView.height()) + describe "when close is triggered on an editor pane", -> + it "adjusts the layout, focuses the next most-recently active editor, and closes the window when there are no remaining editors", -> + spyOn(window, 'close') + editor = rootView.find('.editor').view() + editor.trigger 'split-right' + editor.trigger 'split-right' + editor.trigger 'split-right' + [editor1, editor2, editor3, editor4] = rootView.find('.editor').map -> $(this).view() + editor2.focus() + editor1.focus() + editor3.focus() + editor4.focus() + editor4.trigger 'close' + expect(editor3.isFocused).toBeTruthy() + expect(editor1.outerWidth()).toBe Math.floor(rootView.width() / 3) + expect(editor2.outerWidth()).toBe Math.floor(rootView.width() / 3) + expect(editor3.outerWidth()).toBe Math.floor(rootView.width() / 3) + editor3.trigger 'close' + expect(editor1.isFocused).toBeTruthy() + expect(editor1.outerWidth()).toBe Math.floor(rootView.width() / 2) + expect(editor2.outerWidth()).toBe Math.floor(rootView.width() / 2) + editor1.trigger 'close' + expect(editor2.isFocused).toBeTruthy() + expect(editor2.outerWidth()).toBe Math.floor(rootView.width()) - - + expect(window.close).not.toHaveBeenCalled() + editor2.trigger 'close' + expect(window.close).toHaveBeenCalled() describe ".addPane(view)", -> it "adds the given view to the rootView (at the bottom by default)", -> diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index eb6252c59..629f9649f 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -99,6 +99,7 @@ class Editor extends View @on 'split-right', => @splitRight() @on 'split-up', => @splitUp() @on 'split-down', => @splitDown() + @on 'close', => @remove(); false buildCursorAndSelection: -> @cursor = new Cursor(this) @@ -113,6 +114,7 @@ class Editor extends View false @hiddenInput.on 'focus', => + @rootView()?.editorFocused(this) @isFocused = true @addClass 'focused' @@ -157,6 +159,9 @@ class Editor extends View @setMaxLineLength() if @softWrap @focus() + rootView: -> + @parents('#root-view').view() + selectTextOnMouseMovement: -> moveHandler = (e) => @selectToScreenPosition(@screenPositionFromMouseEvent(e)) @on 'mousemove', moveHandler @@ -440,8 +445,11 @@ class Editor extends View @parents('#root-view').view().adjustSplitPanes() remove: (selector, keepData) -> - @unsubscribeFromBuffer() unless keepData + return super if keepData + @unsubscribeFromBuffer() + rootView = @rootView() super + rootView?.editorRemoved(this) unsubscribeFromBuffer: -> @buffer.off ".editor#{@id}" diff --git a/src/atom/root-view.coffee b/src/atom/root-view.coffee index 8ed1ded4c..542982078 100644 --- a/src/atom/root-view.coffee +++ b/src/atom/root-view.coffee @@ -15,7 +15,10 @@ class RootView extends View @div id: 'root-view', => @subview 'editor', new Editor + editors: null + initialize: ({url}) -> + @editors = [] @editor.keyEventHandler = window.keymap @createProject(url) @@ -36,10 +39,24 @@ class RootView extends View addPane: (view) -> @append(view) + editorFocused: (editor) -> + _.remove(@editors, editor) + @editors.push(editor) + + editorRemoved: (editor) -> + _.remove(@editors, editor) + @adjustSplitPanes() + if @editors.length + @focusLastActiveEditor() + else + window.close() + + focusLastActiveEditor: -> + _.last(@editors).focus() + adjustSplitPanes: (element = @children(':first'))-> if element.hasClass('row') totalUnits = @horizontalGridUnits(element) - console.log totalUnits unitsSoFar = 0 for child in element.children() child = $(child) @@ -54,7 +71,6 @@ class RootView extends View else if element.hasClass('column') totalUnits = @verticalGridUnits(element) - console.log "total vertical", totalUnits unitsSoFar = 0 for child in element.children() child = $(child) diff --git a/src/stdlib/underscore-extensions.coffee b/src/stdlib/underscore-extensions.coffee index 32d3ff078..204d2aea6 100644 --- a/src/stdlib/underscore-extensions.coffee +++ b/src/stdlib/underscore-extensions.coffee @@ -2,7 +2,8 @@ _ = require 'underscore' _.mixin remove: (array, element) -> - array.splice(array.indexOf(element), 1) + index = array.indexOf(element) + array.splice(index, 1) if index >= 0 sum: (array) -> sum = 0