Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for rank to the top area #7278

Merged
merged 4 commits into from Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 44 additions & 12 deletions packages/application/src/shell.ts
Expand Up @@ -175,13 +175,13 @@ export class LabShell extends Widget implements JupyterFrontEnd.IShell {
this.addClass(APPLICATION_SHELL_CLASS);
this.id = 'main';

let headerPanel = (this._headerPanel = new Panel());
let topHandler = (this._topHandler = new Private.PanelHandler());
let bottomPanel = (this._bottomPanel = new BoxPanel());
let topPanel = (this._topPanel = new Panel());
let hboxPanel = new BoxPanel();
let dockPanel = (this._dockPanel = new DockPanelSvg({
kind: 'dockPanelBar'
}));
let headerPanel = (this._headerPanel = new Panel());
MessageLoop.installMessageHook(dockPanel, this._dockChildHook);

let hsplitPanel = new SplitPanel();
Expand All @@ -191,12 +191,12 @@ export class LabShell extends Widget implements JupyterFrontEnd.IShell {
));
let rootLayout = new BoxLayout();

headerPanel.id = 'jp-header-panel';
topHandler.panel.id = 'jp-top-panel';
bottomPanel.id = 'jp-bottom-panel';
topPanel.id = 'jp-top-panel';
hboxPanel.id = 'jp-main-content-panel';
dockPanel.id = 'jp-main-dock-panel';
hsplitPanel.id = 'jp-main-split-panel';
headerPanel.id = 'jp-header-panel';

leftHandler.sideBar.addClass(SIDEBAR_CLASS);
leftHandler.sideBar.addClass('jp-mod-left');
Expand All @@ -206,13 +206,13 @@ export class LabShell extends Widget implements JupyterFrontEnd.IShell {
rightHandler.sideBar.addClass('jp-mod-right');
rightHandler.stackedPanel.id = 'jp-right-stack';

bottomPanel.direction = 'bottom-to-top';
hboxPanel.spacing = 0;
dockPanel.spacing = 5;
hsplitPanel.spacing = 1;

hboxPanel.direction = 'left-to-right';
hsplitPanel.orientation = 'horizontal';
bottomPanel.direction = 'bottom-to-top';

SplitPanel.setStretch(leftHandler.stackedPanel, 0);
SplitPanel.setStretch(dockPanel, 1);
Expand All @@ -238,12 +238,12 @@ export class LabShell extends Widget implements JupyterFrontEnd.IShell {
hsplitPanel.setRelativeSizes([1, 2.5, 1]);

BoxLayout.setStretch(headerPanel, 0);
BoxLayout.setStretch(topPanel, 0);
BoxLayout.setStretch(topHandler.panel, 0);
BoxLayout.setStretch(hboxPanel, 1);
BoxLayout.setStretch(bottomPanel, 0);

rootLayout.addWidget(headerPanel);
rootLayout.addWidget(topPanel);
rootLayout.addWidget(topHandler.panel);
rootLayout.addWidget(hboxPanel);
rootLayout.addWidget(bottomPanel);

Expand Down Expand Up @@ -597,7 +597,7 @@ export class LabShell extends Widget implements JupyterFrontEnd.IShell {
case 'header':
return this._headerPanel.widgets.length === 0;
case 'top':
return this._topPanel.widgets.length === 0;
return this._topHandler.panel.widgets.length === 0;
case 'bottom':
return this._bottomPanel.widgets.length === 0;
case 'right':
Expand Down Expand Up @@ -681,7 +681,7 @@ export class LabShell extends Widget implements JupyterFrontEnd.IShell {
case 'header':
return this._headerPanel.children();
case 'top':
return this._topPanel.children();
return this._topHandler.panel.children();
case 'bottom':
return this._bottomPanel.children();
default:
Expand Down Expand Up @@ -803,9 +803,13 @@ export class LabShell extends Widget implements JupyterFrontEnd.IShell {
console.error('Widgets added to app shell must have unique id property.');
return;
}
// Temporary: widgets are added to the panel in order of insertion.
this._topPanel.addWidget(widget);
options = options || {};
const rank = 'rank' in options ? options.rank : DEFAULT_RANK;
this._topHandler.addWidget(widget, rank);
this._onLayoutModified();
if (this._topHandler.panel.isHidden) {
this._topHandler.panel.show();
}
}

/**
Expand Down Expand Up @@ -974,7 +978,7 @@ export class LabShell extends Widget implements JupyterFrontEnd.IShell {
private _rightHandler: Private.SideBarHandler;
private _tracker = new FocusTracker<Widget>();
private _headerPanel: Panel;
private _topPanel: Panel;
private _topHandler: Private.PanelHandler;
private _bottomPanel: Panel;
private _mainOptionsCache = new Map<Widget, DocumentRegistry.IOpenOptions>();
private _sideOptionsCache = new Map<Widget, DocumentRegistry.IOpenOptions>();
Expand Down Expand Up @@ -1024,6 +1028,34 @@ namespace Private {
});
}

/**
* A class which manages a panel and sorts its widgets by rank.
*/
export class PanelHandler {
/**
* Get the panel managed by the handler.
*/
get panel() {
return this._panel;
}

/**
* Add a widget to the panel.
*
* If the widget is already added, it will be moved.
*/
addWidget(widget: Widget, rank: number): void {
widget.parent = null;
const item = { widget, rank };
const index = ArrayExt.upperBound(this._items, item, Private.itemCmp);
ArrayExt.insert(this._items, index, item);
this._panel.insertWidget(index, widget);
}

private _items = new Array<Private.IRankItem>();
private _panel = new Panel();
}

/**
* A class which manages a side bar and related stacked panel.
*/
Expand Down
1 change: 1 addition & 0 deletions tests/test-application/package.json
Expand Up @@ -16,6 +16,7 @@
"@jupyterlab/apputils": "^2.0.0-alpha.0",
"@jupyterlab/coreutils": "^4.0.0-alpha.0",
"@jupyterlab/testutils": "^2.0.0-alpha.0",
"@phosphor/algorithm": "^1.2.0",
"@phosphor/commands": "^1.7.0",
"@phosphor/coreutils": "^1.3.1",
"@phosphor/messaging": "^1.3.0",
Expand Down
12 changes: 12 additions & 0 deletions tests/test-application/src/shell.spec.ts
Expand Up @@ -5,6 +5,8 @@ import { expect } from 'chai';

import { framePromise } from '@jupyterlab/testutils';

import { toArray } from '@phosphor/algorithm';

import { Message } from '@phosphor/messaging';

import { Widget } from '@phosphor/widgets';
Expand Down Expand Up @@ -166,6 +168,16 @@ describe('LabShell', () => {
shell.add(widget, 'top', { rank: 10 });
expect(shell.isEmpty('top')).to.equal(false);
});

it('should add widgets according to their ranks', () => {
const foo = new Widget();
const bar = new Widget();
foo.id = 'foo';
bar.id = 'bar';
shell.add(foo, 'top', { rank: 20 });
shell.add(bar, 'top', { rank: 10 });
expect(toArray(shell.widgets('top'))).to.deep.equal([bar, foo]);
});
});

describe('#add(widget, "left")', () => {
Expand Down