123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import {
- ILabShell,
- JupyterFrontEnd,
- JupyterFrontEndPlugin
- } from '@jupyterlab/application';
- import {
- ISessionContext,
- ICommandPalette,
- ISessionContextDialogs,
- sessionContextDialogs
- } from '@jupyterlab/apputils';
- import { Cell, CodeCell } from '@jupyterlab/cells';
- import {
- CodeConsole,
- ConsolePanel,
- IConsoleTracker
- } from '@jupyterlab/console';
- import { IDocumentWidget } from '@jupyterlab/docregistry';
- import { FileEditor, IEditorTracker } from '@jupyterlab/fileeditor';
- import {
- INotebookTracker,
- Notebook,
- NotebookPanel
- } from '@jupyterlab/notebook';
- import {
- IStatusBar,
- KernelStatus,
- LineCol,
- MemoryUsage,
- RunningSessions,
- StatusBar
- } from '@jupyterlab/statusbar';
- import { IMainMenu } from '@jupyterlab/mainmenu';
- import { ISettingRegistry } from '@jupyterlab/settingregistry';
- import { Title, Widget } from '@lumino/widgets';
- export const STATUSBAR_PLUGIN_ID = '@jupyterlab/statusbar-extension:plugin';
- /**
- * Initialization data for the statusbar extension.
- */
- const statusBar: JupyterFrontEndPlugin<IStatusBar> = {
- id: STATUSBAR_PLUGIN_ID,
- provides: IStatusBar,
- autoStart: true,
- activate: (
- app: JupyterFrontEnd,
- labShell: ILabShell | null,
- settingRegistry: ISettingRegistry | null,
- mainMenu: IMainMenu | null,
- palette: ICommandPalette | null
- ) => {
- const statusBar = new StatusBar();
- statusBar.id = 'jp-main-statusbar';
- app.shell.add(statusBar, 'bottom');
- // If available, connect to the shell's layout modified signal.
- if (labShell) {
- labShell.layoutModified.connect(() => {
- statusBar.update();
- });
- }
- const category: string = 'Main Area';
- const command: string = 'statusbar:toggle';
- app.commands.addCommand(command, {
- label: 'Show Status Bar',
- execute: (args: any) => {
- statusBar.setHidden(statusBar.isVisible);
- if (settingRegistry) {
- void settingRegistry.set(
- STATUSBAR_PLUGIN_ID,
- 'visible',
- statusBar.isVisible
- );
- }
- },
- isToggled: () => statusBar.isVisible
- });
- if (palette) {
- palette.addItem({ command, category });
- }
- if (mainMenu) {
- mainMenu.viewMenu.addGroup([{ command }], 1);
- }
- if (settingRegistry) {
- const updateSettings = (settings: ISettingRegistry.ISettings): void => {
- const visible = settings.get('visible').composite as boolean;
- statusBar.setHidden(!visible);
- };
- Promise.all([settingRegistry.load(STATUSBAR_PLUGIN_ID), app.restored])
- .then(([settings]) => {
- updateSettings(settings);
- settings.changed.connect(settings => {
- updateSettings(settings);
- });
- })
- .catch((reason: Error) => {
- console.error(reason.message);
- });
- }
- return statusBar;
- },
- optional: [ILabShell, ISettingRegistry, IMainMenu, ICommandPalette]
- };
- /**
- * A plugin that provides a kernel status item to the status bar.
- */
- export const kernelStatus: JupyterFrontEndPlugin<void> = {
- id: '@jupyterlab/statusbar-extension:kernel-status',
- autoStart: true,
- requires: [IStatusBar, INotebookTracker, IConsoleTracker, ILabShell],
- optional: [ISessionContextDialogs],
- activate: (
- app: JupyterFrontEnd,
- statusBar: IStatusBar,
- notebookTracker: INotebookTracker,
- consoleTracker: IConsoleTracker,
- labShell: ILabShell,
- sessionDialogs: ISessionContextDialogs | null
- ) => {
- // When the status item is clicked, launch the kernel
- // selection dialog for the current session.
- let currentSession: ISessionContext | null = null;
- const changeKernel = async () => {
- if (!currentSession) {
- return;
- }
- await (sessionDialogs || sessionContextDialogs).selectKernel(
- currentSession
- );
- };
- // Create the status item.
- const item = new KernelStatus({
- onClick: changeKernel
- });
- // When the title of the active widget changes, update the label
- // of the hover text.
- const onTitleChanged = (title: Title<Widget>) => {
- item.model!.activityName = title.label;
- };
- // Keep the session object on the status item up-to-date.
- labShell.currentChanged.connect((_, change) => {
- const { oldValue, newValue } = change;
- // Clean up after the old value if it exists,
- // listen for changes to the title of the activity
- if (oldValue) {
- oldValue.title.changed.disconnect(onTitleChanged);
- }
- if (newValue) {
- newValue.title.changed.connect(onTitleChanged);
- }
- // Grab the session off of the current widget, if it exists.
- if (newValue && consoleTracker.has(newValue)) {
- currentSession = (newValue as ConsolePanel).sessionContext;
- } else if (newValue && notebookTracker.has(newValue)) {
- currentSession = (newValue as NotebookPanel).sessionContext;
- } else {
- currentSession = null;
- }
- item.model!.sessionContext = currentSession;
- });
- statusBar.registerStatusItem(
- '@jupyterlab/statusbar-extension:kernel-status',
- {
- item,
- align: 'left',
- rank: 1,
- isActive: () => {
- const current = labShell.currentWidget;
- return (
- !!current &&
- (notebookTracker.has(current) || consoleTracker.has(current))
- );
- }
- }
- );
- }
- };
- /**
- * A plugin providing a line/column status item to the application.
- */
- export const lineColItem: JupyterFrontEndPlugin<void> = {
- id: '@jupyterlab/statusbar-extension:line-col-status',
- autoStart: true,
- requires: [
- IStatusBar,
- INotebookTracker,
- IEditorTracker,
- IConsoleTracker,
- ILabShell
- ],
- activate: (
- _: JupyterFrontEnd,
- statusBar: IStatusBar,
- notebookTracker: INotebookTracker,
- editorTracker: IEditorTracker,
- consoleTracker: IConsoleTracker,
- labShell: ILabShell
- ) => {
- const item = new LineCol();
- const onActiveCellChanged = (notebook: Notebook, cell: Cell) => {
- item.model!.editor = cell && cell.editor;
- };
- const onPromptCreated = (console: CodeConsole, prompt: CodeCell) => {
- item.model!.editor = prompt && prompt.editor;
- };
- labShell.currentChanged.connect((_, change) => {
- const { oldValue, newValue } = change;
- // Check if we need to disconnect the console listener
- // or the notebook active cell listener
- if (oldValue && consoleTracker.has(oldValue)) {
- (oldValue as ConsolePanel).console.promptCellCreated.disconnect(
- onPromptCreated
- );
- } else if (oldValue && notebookTracker.has(oldValue)) {
- (oldValue as NotebookPanel).content.activeCellChanged.disconnect(
- onActiveCellChanged
- );
- }
- // Wire up the new editor to the model if it exists
- if (newValue && consoleTracker.has(newValue)) {
- (newValue as ConsolePanel).console.promptCellCreated.connect(
- onPromptCreated
- );
- const prompt = (newValue as ConsolePanel).console.promptCell;
- item.model!.editor = prompt && prompt.editor;
- } else if (newValue && notebookTracker.has(newValue)) {
- (newValue as NotebookPanel).content.activeCellChanged.connect(
- onActiveCellChanged
- );
- const cell = (newValue as NotebookPanel).content.activeCell;
- item.model!.editor = cell && cell.editor;
- } else if (newValue && editorTracker.has(newValue)) {
- item.model!.editor = (newValue as IDocumentWidget<
- FileEditor
- >).content.editor;
- } else {
- item.model!.editor = null;
- }
- });
- // Add the status item to the status bar.
- statusBar.registerStatusItem(
- '@jupyterlab/statusbar-extension:line-col-status',
- {
- item,
- align: 'right',
- rank: 2,
- isActive: () => {
- const current = labShell.currentWidget;
- return (
- !!current &&
- (notebookTracker.has(current) ||
- editorTracker.has(current) ||
- consoleTracker.has(current))
- );
- }
- }
- );
- }
- };
- /**
- * A plugin providing memory usage statistics to the application.
- *
- * #### Notes
- * This plugin will not work unless the memory usage server extension
- * is installed.
- */
- export const memoryUsageItem: JupyterFrontEndPlugin<void> = {
- id: '@jupyterlab/statusbar-extension:memory-usage-status',
- autoStart: true,
- requires: [IStatusBar],
- activate: (app: JupyterFrontEnd, statusBar: IStatusBar) => {
- let item = new MemoryUsage();
- statusBar.registerStatusItem(
- '@jupyterlab/statusbar-extension:memory-usage-status',
- {
- item,
- align: 'left',
- rank: 2,
- isActive: () => item.model!.metricsAvailable,
- activeStateChanged: item.model!.stateChanged
- }
- );
- }
- };
- /*
- * A plugin providing running terminals and sessions information
- * to the status bar.
- */
- export const runningSessionsItem: JupyterFrontEndPlugin<void> = {
- id: '@jupyterlab/statusbar-extension:running-sessions-status',
- autoStart: true,
- requires: [IStatusBar],
- activate: (app: JupyterFrontEnd, statusBar: IStatusBar) => {
- const item = new RunningSessions({
- onClick: () => app.shell.activateById('jp-running-sessions'),
- serviceManager: app.serviceManager
- });
- statusBar.registerStatusItem(
- '@jupyterlab/statusbar-extension:running-sessions-status',
- {
- item,
- align: 'left',
- rank: 0
- }
- );
- }
- };
- const plugins: JupyterFrontEndPlugin<any>[] = [
- statusBar,
- lineColItem,
- kernelStatus,
- runningSessionsItem,
- memoryUsageItem
- ];
- export default plugins;
|