Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomoreno committed Sep 20, 2018
1 parent 941c0e5 commit c9a0382
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
22 changes: 17 additions & 5 deletions src/vs/base/browser/ui/grid/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import 'vs/css!./gridview';
import { Orientation } from 'vs/base/browser/ui/sash/sash';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { tail2 as tail } from 'vs/base/common/arrays';
import { tail2 as tail, equals } from 'vs/base/common/arrays';
import { orthogonal, IView, GridView, Sizing as GridViewSizing, Box, IGridViewStyles } from './gridview';
import { Event } from 'vs/base/common/event';

Expand Down Expand Up @@ -256,15 +256,27 @@ export class Grid<T extends IView> implements IDisposable {
throw new Error('Can\'t remove last view');
}

if (!this.views.has(view)) {
throw new Error('View not found');
}

const location = this.getViewLocation(view);
this.gridview.removeView(location, sizing === Sizing.Distribute ? GridViewSizing.Distribute : undefined);
this.views.delete(view);
}

moveView(view: T, sizing: number | Sizing, referenceView: T, direction: Direction): void {
const sourceLocation = this.getViewLocation(view);
const [sourceParentLocation, from] = tail(sourceLocation);

const referenceLocation = this.getViewLocation(referenceView);
const targetLocation = getRelativeLocation(this.gridview.orientation, referenceLocation, direction);
const [targetParentLocation, to] = tail(targetLocation);

if (equals(sourceParentLocation, targetParentLocation)) {
this.gridview.moveView(sourceParentLocation, from, to);
} else {
this.removeView(view, typeof sizing === 'number' ? undefined : sizing);
this.addView(view, sizing, referenceView, direction);
}
}

swapViews(from: T, to: T): void {
const fromLocation = this.getViewLocation(from);
const toLocation = this.getViewLocation(to);
Expand Down
57 changes: 49 additions & 8 deletions src/vs/base/browser/ui/grid/gridview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,14 @@ class BranchNode implements ISplitView, IDisposable {
throw new Error('Invalid index');
}

const first = index === 0;
const last = index === this.splitview.length;
this.splitview.addView(node, size, index);
this._addChild(node, index);
this.onDidChildrenChange();
}

private _addChild(node: Node, index: number): void {
const first = index === 0;
const last = index === this.children.length;
this.children.splice(index, 0, node);
node.orthogonalStartSash = this.splitview.sashes[index - 1];
node.orthogonalEndSash = this.splitview.sashes[index];
Expand All @@ -195,19 +200,22 @@ class BranchNode implements ISplitView, IDisposable {
if (!last) {
this.children[index + 1].orthogonalStartSash = this.splitview.sashes[index];
}

this.onDidChildrenChange();
}

removeChild(index: number, sizing?: Sizing): void {
if (index < 0 || index >= this.children.length) {
throw new Error('Invalid index');
}

const first = index === 0;
const last = index === this.splitview.length - 1;
this.splitview.removeView(index, sizing);
this.children.splice(index, 1);
this._removeChild(index);
this.onDidChildrenChange();
}

private _removeChild(index: number): Node {
const first = index === 0;
const last = index === this.children.length - 1;
const [child] = this.children.splice(index, 1);

if (!first) {
this.children[index - 1].orthogonalEndSash = this.splitview.sashes[index - 1];
Expand All @@ -217,7 +225,30 @@ class BranchNode implements ISplitView, IDisposable {
this.children[index].orthogonalStartSash = this.splitview.sashes[Math.max(index - 1, 0)];
}

this.onDidChildrenChange();
return child;
}

moveChild(from: number, to: number): void {
if (from === to) {
return;
}

if (from < 0 || from >= this.children.length) {
throw new Error('Invalid from index');
}

if (to < 0 || to > this.children.length) {
throw new Error('Invalid to index');
}

if (from < to) {
to--;
}

this.splitview.moveView(from, to);

const child = this._removeChild(from);
this._addChild(child, to);
}

swapChildren(from: number, to: number): void {
Expand Down Expand Up @@ -651,6 +682,16 @@ export class GridView implements IDisposable {
return node.view;
}

moveView(parentLocation: number[], from: number, to: number): void {
const [, parent] = this.getNode(parentLocation);

if (!(parent instanceof BranchNode)) {
throw new Error('Invalid location');
}

parent.moveChild(from, to);
}

swapViews(from: number[], to: number[]): void {
const [fromRest, fromIndex] = tail(from);
const [, fromParent] = this.getNode(fromRest);
Expand Down
3 changes: 1 addition & 2 deletions src/vs/workbench/browser/parts/editor/editorPart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,7 @@ export class EditorPart extends Part implements EditorGroupsServiceImpl, IEditor
const groupHasFocus = isAncestor(document.activeElement, sourceView.element);

// Move is a simple remove and add of the same view
this.gridWidget.removeView(sourceView, Sizing.Distribute);
this.gridWidget.addView(sourceView, Sizing.Distribute, targetView, this.toGridViewDirection(direction));
this.gridWidget.moveView(sourceView, Sizing.Distribute, targetView, this.toGridViewDirection(direction));

// Restore focus if we had it previously (we run this after gridWidget.removeView() is called
// because removing a view can mean to reparent it and thus focus would be removed otherwise)
Expand Down

0 comments on commit c9a0382

Please sign in to comment.