From 81b05023dbb6a3048a17ea0e1810f9af687afdc3 Mon Sep 17 00:00:00 2001 From: Jason Rudolph Date: Thu, 25 May 2017 08:44:35 -0400 Subject: [PATCH] Refactor: Move directional pane fns to WorkspaceElement --- src/pane-container-element.coffee | 78 ------------------------- src/workspace-element.js | 95 +++++++++++++++++++++++++++---- 2 files changed, 85 insertions(+), 88 deletions(-) diff --git a/src/pane-container-element.coffee b/src/pane-container-element.coffee index 9054f8b2a..78e2fbad3 100644 --- a/src/pane-container-element.coffee +++ b/src/pane-container-element.coffee @@ -24,83 +24,5 @@ class PaneContainerElement extends HTMLElement hasFocus: -> this is document.activeElement or @contains(document.activeElement) - focusPaneViewAbove: -> - @nearestPaneInDirection('above')?.focus() - - focusPaneViewBelow: -> - @nearestPaneInDirection('below')?.focus() - - focusPaneViewOnLeft: -> - @nearestPaneInDirection('left')?.focus() - - focusPaneViewOnRight: -> - @nearestPaneInDirection('right')?.focus() - - moveActiveItemToPaneAbove: (params) -> - @moveActiveItemToNearestPaneInDirection('above', params) - - moveActiveItemToPaneBelow: (params) -> - @moveActiveItemToNearestPaneInDirection('below', params) - - moveActiveItemToPaneOnLeft: (params) -> - @moveActiveItemToNearestPaneInDirection('left', params) - - moveActiveItemToPaneOnRight: (params) -> - @moveActiveItemToNearestPaneInDirection('right', params) - - moveActiveItemToNearestPaneInDirection: (direction, params) -> - destPane = @nearestPaneInDirection(direction)?.getModel() - return unless destPane? - if params?.keepOriginal - @model.copyActiveItemToPane(destPane) - else - @model.moveActiveItemToPane(destPane) - destPane.focus() - - nearestPaneInDirection: (direction, pane = null) -> - distance = (pointA, pointB) -> - x = pointB.x - pointA.x - y = pointB.y - pointA.y - Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) - - pane = pane or atom.workspace.getActivePane() - paneView = pane.getElement() - box = @boundingBoxForPaneView(paneView) - - visiblePaneContainers = atom.workspace.getPaneContainers() - .filter (container) => - isCenter = container == atom.workspace.getCenter() - isCenter or container.isVisible() - - visiblePanes = _.flatten(visiblePaneContainers.map (container) => container.getPanes()) - - paneViews = visiblePanes - .map (otherPane) => - otherPane.getElement() - .filter (otherPaneView) => - otherBox = @boundingBoxForPaneView(otherPaneView) - switch direction - when 'left' then otherBox.right.x <= box.left.x - when 'right' then otherBox.left.x >= box.right.x - when 'above' then otherBox.bottom.y <= box.top.y - when 'below' then otherBox.top.y >= box.bottom.y - .sort (paneViewA, paneViewB) => - boxA = @boundingBoxForPaneView(paneViewA) - boxB = @boundingBoxForPaneView(paneViewB) - switch direction - when 'left' then distance(box.left, boxA.right) - distance(box.left, boxB.right) - when 'right' then distance(box.right, boxA.left) - distance(box.right, boxB.left) - when 'above' then distance(box.top, boxA.bottom) - distance(box.top, boxB.bottom) - when 'below' then distance(box.bottom, boxA.top) - distance(box.bottom, boxB.top) - - paneViews[0] - - boundingBoxForPaneView: (paneView) -> - boundingBox = paneView.getBoundingClientRect() - - left: {x: boundingBox.left, y: boundingBox.top} - right: {x: boundingBox.right, y: boundingBox.top} - top: {x: boundingBox.left, y: boundingBox.top} - bottom: {x: boundingBox.left, y: boundingBox.bottom} module.exports = PaneContainerElement = document.registerElement 'atom-pane-container', prototype: PaneContainerElement.prototype diff --git a/src/workspace-element.js b/src/workspace-element.js index 42d692aa2..a1e8f3ba7 100644 --- a/src/workspace-element.js +++ b/src/workspace-element.js @@ -222,25 +222,100 @@ class WorkspaceElement extends HTMLElement { this.model.getActivePane().activate() } - focusPaneViewAbove () { this.paneContainer.focusPaneViewAbove() } + focusPaneViewAbove () { this.focusPaneViewInDirection('above') } - focusPaneViewBelow () { this.paneContainer.focusPaneViewBelow() } + focusPaneViewBelow () { this.focusPaneViewInDirection('below') } - focusPaneViewOnLeft () { this.paneContainer.focusPaneViewOnLeft() } + focusPaneViewOnLeft () { this.focusPaneViewInDirection('left') } - focusPaneViewOnRight () { this.paneContainer.focusPaneViewOnRight() } + focusPaneViewOnRight () { this.focusPaneViewInDirection('right') } - moveActiveItemToPaneAbove (params) { this.paneContainer.moveActiveItemToPaneAbove(params) } + focusPaneViewInDirection (direction, pane) { + const activePane = this.model.getActivePane() + const paneToFocus = this.nearestVisiblePaneInDirection(direction, activePane) + paneToFocus && paneToFocus.focus() + } - moveActiveItemToPaneBelow (params) { this.paneContainer.moveActiveItemToPaneBelow(params) } + moveActiveItemToPaneAbove (params) { + this.moveActiveItemToNearestPaneInDirection('above', params) + } - moveActiveItemToPaneOnLeft (params) { this.paneContainer.moveActiveItemToPaneOnLeft(params) } + moveActiveItemToPaneBelow (params) { + this.moveActiveItemToNearestPaneInDirection('below', params) + } - moveActiveItemToPaneOnRight (params) { this.paneContainer.moveActiveItemToPaneOnRight(params) } + moveActiveItemToPaneOnLeft (params) { + this.moveActiveItemToNearestPaneInDirection('left', params) + } + + moveActiveItemToPaneOnRight (params) { + this.moveActiveItemToNearestPaneInDirection('right', params) + } + + moveActiveItemToNearestPaneInDirection (direction, params) { + const activePane = this.model.getActivePane() + const nearestPaneView = this.nearestVisiblePaneInDirection(direction, activePane) + if (nearestPaneView == null) { return } + if (params && params.keepOriginal) { + activePane.getContainer().copyActiveItemToPane(nearestPaneView.getModel()) + } else { + activePane.getContainer().moveActiveItemToPane(nearestPaneView.getModel()) + } + nearestPaneView.focus() + } nearestVisiblePaneInDirection (direction, pane) { - const paneContainerElement = pane.getContainer().getElement() - return paneContainerElement.nearestPaneInDirection(direction, pane) + const distance = function (pointA, pointB) { + const x = pointB.x - pointA.x + const y = pointB.y - pointA.y + return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) + } + + const paneView = pane.getElement() + const box = this.boundingBoxForPaneView(paneView) + + const visiblePaneContainers = atom.workspace.getPaneContainers() + .filter(container => { + const isCenter = container === atom.workspace.getCenter() + return isCenter || container.isVisible() + }) + + const visiblePanes = _.flatten(visiblePaneContainers.map(container => container.getPanes())) + + const paneViews = visiblePanes + .map(otherPane => { + return otherPane.getElement() + }).filter(otherPaneView => { + const otherBox = this.boundingBoxForPaneView(otherPaneView) + switch (direction) { + case 'left': return otherBox.right.x <= box.left.x + case 'right': return otherBox.left.x >= box.right.x + case 'above': return otherBox.bottom.y <= box.top.y + case 'below': return otherBox.top.y >= box.bottom.y + } + }).sort((paneViewA, paneViewB) => { + const boxA = this.boundingBoxForPaneView(paneViewA) + const boxB = this.boundingBoxForPaneView(paneViewB) + switch (direction) { + case 'left': return distance(box.left, boxA.right) - distance(box.left, boxB.right) + case 'right': return distance(box.right, boxA.left) - distance(box.right, boxB.left) + case 'above': return distance(box.top, boxA.bottom) - distance(box.top, boxB.bottom) + case 'below': return distance(box.bottom, boxA.top) - distance(box.bottom, boxB.top) + } + }) + + return paneViews[0] + } + + boundingBoxForPaneView (paneView) { + const boundingBox = paneView.getBoundingClientRect() + + return { + left: {x: boundingBox.left, y: boundingBox.top}, + right: {x: boundingBox.right, y: boundingBox.top}, + top: {x: boundingBox.left, y: boundingBox.top}, + bottom: {x: boundingBox.left, y: boundingBox.bottom} + } } runPackageSpecs () {