|
| 1 | +import { |
| 2 | + ILabShell, |
| 3 | + JupyterFrontEnd, |
| 4 | + JupyterFrontEndPlugin |
| 5 | +} from '@jupyterlab/application'; |
| 6 | +import { ICommandPalette, MainAreaWidget } from '@jupyterlab/apputils'; |
| 7 | +import { FileBrowserModel, IFileBrowserFactory } from '@jupyterlab/filebrowser'; |
| 8 | +import { ILauncher, Launcher, LauncherModel } from '@jupyterlab/launcher'; |
| 9 | +import { ITranslator } from '@jupyterlab/translation'; |
| 10 | +import { addIcon, launcherIcon } from '@jupyterlab/ui-components'; |
| 11 | +import { find } from '@lumino/algorithm'; |
| 12 | +import { ReadonlyPartialJSONObject } from '@lumino/coreutils'; |
| 13 | +import { DockPanel, TabBar, Widget } from '@lumino/widgets'; |
| 14 | + |
| 15 | +/** |
| 16 | + * The command IDs used by the launcher plugin. |
| 17 | + */ |
| 18 | +namespace CommandIDs { |
| 19 | + export const launcher = 'launcher:create'; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * A service providing an interface to the the launcher. |
| 24 | + */ |
| 25 | +export const launcherPlugin: JupyterFrontEndPlugin<ILauncher> = { |
| 26 | + activate, |
| 27 | + id: 'jupyter-drives:launcher-extension-plugin', |
| 28 | + description: 'Provides the launcher tab service for the file browsers.', |
| 29 | + requires: [ITranslator], |
| 30 | + optional: [ILabShell, ICommandPalette, IFileBrowserFactory], |
| 31 | + provides: ILauncher, |
| 32 | + autoStart: true |
| 33 | +}; |
| 34 | + |
| 35 | +/** |
| 36 | + * Activate the launcher. |
| 37 | + */ |
| 38 | +function activate( |
| 39 | + app: JupyterFrontEnd, |
| 40 | + translator: ITranslator, |
| 41 | + labShell: ILabShell | null, |
| 42 | + palette: ICommandPalette | null, |
| 43 | + factory: IFileBrowserFactory | null |
| 44 | +): ILauncher { |
| 45 | + const { commands, shell } = app; |
| 46 | + const trans = translator.load('jupyter-drives'); |
| 47 | + const model = new LauncherModel(); |
| 48 | + |
| 49 | + commands.addCommand(CommandIDs.launcher, { |
| 50 | + label: trans.__('New Launcher'), |
| 51 | + icon: args => (args.toolbar ? addIcon : undefined), |
| 52 | + execute: (args: ReadonlyPartialJSONObject) => { |
| 53 | + // get current file browser used |
| 54 | + const currentBrowser = factory?.tracker.currentWidget; |
| 55 | + const cwd = (args['cwd'] as string) ?? currentBrowser?.model.path ?? ''; |
| 56 | + const id = `launcher-${Private.id++}`; |
| 57 | + const callback = (item: Widget) => { |
| 58 | + // If widget is attached to the main area replace the launcher |
| 59 | + if (find(shell.widgets('main'), w => w === item)) { |
| 60 | + shell.add(item, 'main', { ref: id }); |
| 61 | + launcher.dispose(); |
| 62 | + } |
| 63 | + }; |
| 64 | + const launcher = new Launcher({ |
| 65 | + model, |
| 66 | + cwd, |
| 67 | + callback, |
| 68 | + commands, |
| 69 | + translator |
| 70 | + }); |
| 71 | + |
| 72 | + launcher.model = model; |
| 73 | + launcher.title.icon = launcherIcon; |
| 74 | + launcher.title.label = trans.__('Launcher'); |
| 75 | + |
| 76 | + const main = new MainAreaWidget({ content: launcher }); |
| 77 | + |
| 78 | + // If there are any other widgets open, remove the launcher close icon. |
| 79 | + main.title.closable = !!Array.from(shell.widgets('main')).length; |
| 80 | + main.id = id; |
| 81 | + |
| 82 | + shell.add(main, 'main', { |
| 83 | + activate: args['activate'] as boolean, |
| 84 | + ref: args['ref'] as string |
| 85 | + }); |
| 86 | + |
| 87 | + if (labShell) { |
| 88 | + labShell.layoutModified.connect(() => { |
| 89 | + // If there is only a launcher open, remove the close icon. |
| 90 | + main.title.closable = Array.from(labShell.widgets('main')).length > 1; |
| 91 | + }, main); |
| 92 | + } |
| 93 | + |
| 94 | + if (currentBrowser) { |
| 95 | + const onPathChanged = (model: FileBrowserModel) => { |
| 96 | + launcher.cwd = model.path; |
| 97 | + }; |
| 98 | + currentBrowser.model.pathChanged.connect(onPathChanged); |
| 99 | + launcher.disposed.connect(() => { |
| 100 | + currentBrowser.model.pathChanged.disconnect(onPathChanged); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + return main; |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + if (labShell) { |
| 109 | + const currentBrowser = factory?.tracker.currentWidget; |
| 110 | + void Promise.all([app.restored, currentBrowser?.model.restored]).then( |
| 111 | + () => { |
| 112 | + function maybeCreate() { |
| 113 | + // Create a launcher if there are no open items. |
| 114 | + if (labShell!.isEmpty('main')) { |
| 115 | + void commands.execute(CommandIDs.launcher); |
| 116 | + } |
| 117 | + } |
| 118 | + // When layout is modified, create a launcher if there are no open items. |
| 119 | + labShell.layoutModified.connect(() => { |
| 120 | + maybeCreate(); |
| 121 | + }); |
| 122 | + } |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + if (palette) { |
| 127 | + palette.addItem({ |
| 128 | + command: CommandIDs.launcher, |
| 129 | + category: trans.__('Launcher') |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + if (labShell) { |
| 134 | + labShell.addButtonEnabled = true; |
| 135 | + labShell.addRequested.connect((sender: DockPanel, arg: TabBar<Widget>) => { |
| 136 | + // Get the ref for the current tab of the tabbar which the add button was clicked |
| 137 | + const ref = |
| 138 | + arg.currentTitle?.owner.id || |
| 139 | + arg.titles[arg.titles.length - 1].owner.id; |
| 140 | + |
| 141 | + return commands.execute(CommandIDs.launcher, { ref }); |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + return model; |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * The namespace for module private data. |
| 150 | + */ |
| 151 | +namespace Private { |
| 152 | + /** |
| 153 | + * The incrementing id used for launcher widgets. |
| 154 | + */ |
| 155 | + export let id = 0; |
| 156 | +} |
0 commit comments