More cleanups

This commit is contained in:
Jeremy Tuloup 2022-10-05 10:08:35 +02:00
parent 7375ee1208
commit 07720adaa6
3 changed files with 52 additions and 42 deletions

View File

@ -42,11 +42,13 @@ import {
import { jupyterIcon } from '@jupyter-notebook/ui-components';
import { each } from '@lumino/algorithm';
import { PromiseDelegate } from '@lumino/coreutils';
import { DisposableDelegate, DisposableSet } from '@lumino/disposable';
import {
DisposableDelegate,
DisposableSet,
IDisposable
} from '@lumino/disposable';
import { Menu, Widget } from '@lumino/widgets';
@ -605,12 +607,12 @@ const sidebarVisibility: JupyterFrontEndPlugin<void> = {
) => {
const trans = translator.load('notebook');
const sideBarMenu: SideBarPanel.sideBarMenu = {
const sideBarMenu: { [area in SideBarPanel.Area]: IDisposable | null } = {
left: null,
right: null
};
var sideBarPalette: SideBarPalette | null = null;
let sideBarPalette: SideBarPalette | null = null;
/* Arguments for togglePanel command:
* side, left or right area
@ -640,7 +642,9 @@ const sidebarVisibility: JupyterFrontEndPlugin<void> = {
case 'left':
if (notebookShell.leftCollapsed) {
notebookShell.expandLeft(args.id as string);
} else if (notebookShell.leftHandler.current?.id !== args.id) {
} else if (
notebookShell.leftHandler.currentWidget?.id !== args.id
) {
notebookShell.expandLeft(args.id as string);
} else {
notebookShell.collapseLeft();
@ -652,7 +656,9 @@ const sidebarVisibility: JupyterFrontEndPlugin<void> = {
case 'right':
if (notebookShell.rightCollapsed) {
notebookShell.expandRight(args.id as string);
} else if (notebookShell.rightHandler.current?.id !== args.id) {
} else if (
notebookShell.rightHandler.currentWidget?.id !== args.id
) {
notebookShell.expandRight(args.id as string);
} else {
notebookShell.collapseRight();
@ -664,31 +670,31 @@ const sidebarVisibility: JupyterFrontEndPlugin<void> = {
}
},
isToggled: args => {
var currentWidget = null;
switch (args['side'] as string) {
case 'left':
case 'left': {
if (notebookShell.leftCollapsed) {
return false;
}
currentWidget = notebookShell.leftHandler.current;
const currentWidget = notebookShell.leftHandler.currentWidget;
if (!currentWidget) {
return false;
}
return currentWidget.id === (args['id'] as string);
case 'right':
}
case 'right': {
if (notebookShell.rightCollapsed) {
return false;
}
currentWidget = notebookShell.rightHandler.current;
const currentWidget = notebookShell.rightHandler.currentWidget;
if (!currentWidget) {
return false;
}
return currentWidget.id === (args['id'] as string);
default:
return false;
}
}
return false;
}
});
@ -713,7 +719,7 @@ const sidebarVisibility: JupyterFrontEndPlugin<void> = {
const widgets = notebookShell.widgets(area);
let menuToAdd = false;
each(widgets, widget => {
for (let widget of widgets) {
newMenu.addItem({
command: CommandIDs.togglePanel,
args: {
@ -723,7 +729,7 @@ const sidebarVisibility: JupyterFrontEndPlugin<void> = {
}
});
menuToAdd = true;
});
}
// If there are widgets, add the menu to the main menu entry.
if (menuToAdd) {
@ -734,7 +740,13 @@ const sidebarVisibility: JupyterFrontEndPlugin<void> = {
}
};
const getSidebarLabel = (area: SideBarPanel.Area) => {
/**
* Get a label for a sidebar panel.
*
* @param area - 'left' or 'right', the area of the side bar.
* @returns the label for the sidebar menu entry.
*/
const getSidebarLabel = (area: SideBarPanel.Area): string => {
if (area === 'left') {
return trans.__(`Left Sidebar`);
} else {

View File

@ -7,7 +7,6 @@ import { closeIcon } from '@jupyterlab/ui-components';
import { ArrayExt, find } from '@lumino/algorithm';
import { PromiseDelegate, Token } from '@lumino/coreutils';
import { IDisposable } from '@lumino/disposable';
import { Message, MessageLoop, IMessageHandler } from '@lumino/messaging';
import { ISignal, Signal } from '@lumino/signaling';
@ -176,7 +175,7 @@ export class NotebookShell extends Widget implements JupyterFrontEnd.IShell {
}
/**
* Promise that resolves when main widget is loaded
* Promise that resolves when the main widget is loaded
*/
get restored(): Promise<void> {
return this._mainWidgetLoaded.promise;
@ -214,7 +213,7 @@ export class NotebookShell extends Widget implements JupyterFrontEnd.IShell {
*/
add(
widget: Widget,
area?: string,
area?: Shell.Area,
options?: DocumentRegistry.IOpenOptions
): void {
const rank = options?.rank ?? DEFAULT_RANK;
@ -359,13 +358,6 @@ export namespace SideBarPanel {
* The areas of the sidebar panel
*/
export type Area = 'left' | 'right';
/**
* The sidebar menu type, one for each area
*/
export type sideBarMenu = {
[area in Area]: IDisposable | null;
};
}
/**
@ -380,8 +372,8 @@ export class SideBarHandler {
this._panel = new Panel();
this._panel.hide();
this._current = null;
this._lastCurrent = null;
this._currentWidget = null;
this._lastCurrentWidget = null;
this._widgetPanel = new StackedPanel();
this._widgetPanel.widgetRemoved.connect(this._onWidgetRemoved, this);
@ -402,10 +394,13 @@ export class SideBarHandler {
this._panel.addWidget(this._widgetPanel);
}
get current(): Widget | null {
/**
* Get the current widget in the sidebar panel.
*/
get currentWidget(): Widget | null {
return (
this._current ||
this._lastCurrent ||
this._currentWidget ||
this._lastCurrentWidget ||
(this._items.length > 0 ? this._items[0].widget : null)
);
}
@ -460,15 +455,15 @@ export class SideBarHandler {
* if there is no most recently used.
*/
expand(id?: string): void {
if (this._current) {
if (this._currentWidget) {
this.collapse();
}
if (id) {
this.activate(id);
} else {
const visibleWidget = this.current;
const visibleWidget = this.currentWidget;
if (visibleWidget) {
this._current = visibleWidget;
this._currentWidget = visibleWidget;
this.activate(visibleWidget.id);
}
}
@ -482,7 +477,7 @@ export class SideBarHandler {
activate(id: string): void {
const widget = this._findWidgetByID(id);
if (widget) {
this._current = widget;
this._currentWidget = widget;
widget.show();
widget.activate();
}
@ -499,8 +494,8 @@ export class SideBarHandler {
* Collapse the sidebar so no items are expanded.
*/
collapse(): void {
this._current?.hide();
this._current = null;
this._currentWidget?.hide();
this._currentWidget = null;
}
/**
@ -570,8 +565,8 @@ export class SideBarHandler {
* Handle the `widgetRemoved` signal from the panel.
*/
private _onWidgetRemoved(sender: StackedPanel, widget: Widget): void {
if (widget === this._lastCurrent) {
this._lastCurrent = null;
if (widget === this._lastCurrentWidget) {
this._lastCurrentWidget = null;
}
ArrayExt.removeAt(this._items, this._findWidgetIndex(widget));
@ -585,11 +580,12 @@ export class SideBarHandler {
private _items = new Array<Private.IRankItem>();
private _panel: Panel;
private _widgetPanel: StackedPanel;
private _current: Widget | null;
private _lastCurrent: Widget | null;
private _currentWidget: Widget | null;
private _lastCurrentWidget: Widget | null;
private _widgetAdded: Signal<SideBarHandler, Widget> = new Signal(this);
private _widgetRemoved: Signal<SideBarHandler, Widget> = new Signal(this);
}
/**
* A namespace for private module data.
*/

View File

@ -30,6 +30,8 @@
#jp-right-stack .jp-SidePanel-collapse {
display: flex;
flex: 0 0 auto;
min-height: 0;
padding: 0;
}
#jp-left-stack .jp-SidePanel-collapse {