Sfoglia il codice sorgente

Implement launch behavior

Steven Silvester 8 anni fa
parent
commit
f906aa00c6

+ 23 - 3
packages/application/src/shell.ts

@@ -341,13 +341,20 @@ class ApplicationShell extends Widget {
    * All widgets added to the main area should be disposed after removal (or
    * simply disposed in order to remove).
    */
-  addToMainArea(widget: Widget): void {
+  addToMainArea(widget: Widget, options: ApplicationShell.IMainAreaOptions = {}): void {
     if (!widget.id) {
       console.error('Widgets added to app shell must have unique id property.');
       return;
     }
 
-    this._dockPanel.addWidget(widget, { mode: 'tab-after' });
+    let dock = this._dockPanel;
+
+    let ref: Widget | null = null;
+    if (options.ref) {
+      ref = find(dock.widgets(), value => value.id === options.ref);
+    }
+
+    dock.addWidget(widget, { mode: 'tab-after', ref });
     this._tracker.add(widget);
   }
 
@@ -747,6 +754,19 @@ namespace ApplicationShell {
      */
     rank?: number;
   }
+
+  /**
+   * The options for adding a widget to a side area of the shell.
+   */
+  export
+  interface IMainAreaOptions {
+    /**
+     * The reference widget id for the insert location.
+     *
+     * The default is `null`.
+     */
+    ref?: string | null;
+  }
 }
 
 
@@ -974,7 +994,7 @@ namespace Private {
    * A message hook that adds and removes the .jp-Activity class to widgets in the dock panel.
    */
   export
-  var activityClassHook = (handler: IMessageHandler, msg: Message): boolean => {      
+  var activityClassHook = (handler: IMessageHandler, msg: Message): boolean => {
     if (msg.type === 'child-added') {
       (msg as Widget.ChildMessage).child.addClass(ACTIVITY_CLASS)
     } else if (msg.type === 'child-removed') {

+ 3 - 9
packages/console-extension/src/index.ts

@@ -160,14 +160,7 @@ function activateConsole(app: JupyterLab, manager: IServiceManager, rendermime:
 
   // The launcher callback.
   let callback = (cwd: string, name: string) => {
-    return new ConsolePanel({
-      manager,
-      rendermime: rendermime.clone(),
-      contentFactory,
-      mimeTypeService: editorServices.mimeTypeService,
-      basePath: cwd,
-      kernelPreference: { name }
-    });
+    return createConsole({ basePath: cwd, kernelPreference: { name } });
   };
 
   // Add a launcher item if the launcher is available.
@@ -192,7 +185,7 @@ function activateConsole(app: JupyterLab, manager: IServiceManager, rendermime:
   /**
    * Create a console for a given path.
    */
-  function createConsole(options: Partial<ConsolePanel.IOptions>): Promise<void> {
+  function createConsole(options: Partial<ConsolePanel.IOptions>): Promise<ConsolePanel> {
     return manager.ready.then(() => {
       let panel = new ConsolePanel({
         manager,
@@ -206,6 +199,7 @@ function activateConsole(app: JupyterLab, manager: IServiceManager, rendermime:
       tracker.add(panel);
       shell.addToMainArea(panel);
       shell.activateById(panel.id);
+      return panel;
     });
   }
 

+ 2 - 1
packages/launcher-extension/package.json

@@ -16,7 +16,8 @@
     "@jupyterlab/apputils": "^0.6.0",
     "@jupyterlab/filebrowser": "^0.6.1",
     "@jupyterlab/launcher": "^0.6.0",
-    "@jupyterlab/services": "^0.45.0"
+    "@jupyterlab/services": "^0.45.0",
+    "@phosphor/widgets": "^1.2.0"
   },
   "devDependencies": {
     "rimraf": "^2.5.2",

+ 12 - 2
packages/launcher-extension/src/index.ts

@@ -9,6 +9,7 @@ import {
   JupyterLab, JupyterLabPlugin
 } from '@jupyterlab/application';
 
+
 import {
   ICommandPalette, ILayoutRestorer
 } from '@jupyterlab/apputils';
@@ -17,6 +18,10 @@ import {
   ILauncher, LauncherModel, LauncherWidget
 } from '@jupyterlab/launcher';
 
+import {
+  Widget
+} from '@phosphor/widgets';
+
 
 /**
  * The command IDs used by the launcher plugin.
@@ -61,9 +66,14 @@ function activate(app: JupyterLab, services: IServiceManager, palette: ICommandP
     label: 'New Launcher',
     execute: (args) => {
       let cwd = args['cwd'] ? String(args['cwd']) : '';
-      let widget = new LauncherWidget({ cwd });
+      let id = `launcher-${Private.id++}`;
+      let callback = (item: Widget) => {
+        shell.addToMainArea(item, { ref: id });
+        shell.activateById(item.id);
+      };
+      let widget = new LauncherWidget({ cwd, callback });
       widget.model = model;
-      widget.id = `launcher-${Private.id++}`;
+      widget.id = id;
       widget.title.label = 'Launcher';
       widget.title.closable = true;
       shell.addToMainArea(widget);

+ 36 - 9
packages/launcher/src/index.ts

@@ -14,13 +14,17 @@ import {
 } from '@phosphor/disposable';
 
 import {
-  h, VirtualNode
-} from '@phosphor/virtualdom';
+  Message
+} from '@phosphor/messaging';
 
 import {
   Widget
 } from '@phosphor/widgets';
 
+import {
+  h, VirtualNode
+} from '@phosphor/virtualdom';
+
 import {
   VDomModel, VDomRenderer
 } from '@jupyterlab/apputils';
@@ -55,7 +59,7 @@ const LAUNCHER_CLASS = 'jp-LauncherWidget';
 /**
  * The class name added to LauncherWidget image nodes.
  */
-//const IMAGE_CLASS = 'jp-LauncherWidget-image';
+const IMAGE_CLASS = 'jp-LauncherWidget-image';
 
 /**
  * The class name added to LauncherWidget text nodes.
@@ -111,8 +115,8 @@ interface ILauncherItem {
    * The callback invoked to launch the item.
    *
    * The callback is invoked with a current working directory and the
-   * name of the selected launcher item and returns a promise that
-   * resolves to the widget that will replace the launcher widget.
+   * name of the selected launcher item.  When the function returns
+   * the launcher will close.
    */
   callback: (cwd: string, name: string) => Widget | Promise<Widget>;
 
@@ -194,7 +198,7 @@ class LauncherModel extends VDomModel implements ILauncher {
    */
   add(options: ILauncherItem): IDisposable {
     // Create a copy of the options to circumvent mutations to the original.
-    let item = JSON.parse(JSON.stringify(options));
+    let item = {...options};
 
     this._items.push(item);
     this.stateChanged.emit(void 0);
@@ -227,6 +231,7 @@ class LauncherWidget extends VDomRenderer<LauncherModel> {
   constructor(options: LauncherWidget.IOptions) {
     super();
     this.cwd = options.cwd;
+    this._callback = options.callback;
     this.addClass(LAUNCHER_CLASS);
   }
 
@@ -235,17 +240,32 @@ class LauncherWidget extends VDomRenderer<LauncherModel> {
    */
   readonly cwd: string;
 
+  /**
+   * Handle `'activate-request'` messages.
+   */
+  protected onActivateRequest(msg: Message): void {
+    this.node.tabIndex = -1;
+    this.node.focus();
+  }
+
   /**
    * Render the launcher to virtual DOM nodes.
    */
   protected render(): VirtualNode | VirtualNode[] {
     // Create an iterator that yields rendered item nodes.
     let children = map(this.model.items(), item => {
-      let icon = h.div({ className: item.iconClass }, item.iconLabel);
-      let text = h.span({className: TEXT_CLASS }, item.displayName);
       let onclick = () => {
-        console.log('hi clicked', item);
+        let callback = item.callback;
+        let value = callback(this.cwd, item.name);
+        Promise.resolve(value).then(widget => {
+          let callback = this._callback;
+          callback(widget);
+          this.dispose();
+        });
       };
+      let imageClass = `${item.iconClass} ${IMAGE_CLASS}`;
+      let icon = h.div({ className: imageClass }, item.iconLabel);
+      let text = h.span({className: TEXT_CLASS }, item.displayName);
       return h.div({
         onclick,
         className: ITEM_CLASS,
@@ -256,6 +276,8 @@ class LauncherWidget extends VDomRenderer<LauncherModel> {
 
     return h.div({ className: DIALOG_CLASS }, [body]);
   }
+
+  private _callback: (widget: Widget) => void;
 }
 
 
@@ -273,6 +295,11 @@ namespace LauncherWidget {
      * The cwd of the launcher.
      */
     cwd: string;
+
+    /**
+     * The callback used when an item is launched.
+     */
+    callback: (widget: Widget) => void;
   }
 }
 

+ 0 - 1
packages/launcher/style/index.css

@@ -83,7 +83,6 @@
   background-size: 56px 72px;
   background-repeat: no-repeat;
   cursor: pointer;
-
 }
 
 

+ 2 - 6
packages/terminal-extension/src/index.ts

@@ -132,12 +132,7 @@ function activate(app: JupyterLab, services: IServiceManager, mainMenu: IMainMen
       displayName: 'Terminal',
       iconClass: TERMINAL_ICON_CLASS,
       callback: () => {
-        let term = new Terminal();
-        term.title.closable = true;
-        term.title.icon = TERMINAL_ICON_CLASS;
-        term.title.label = '...';
-        services.terminals.startNew();
-        return term;
+        return commands.execute(CommandIDs.createNew);
       }
     });
   }
@@ -182,6 +177,7 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Instan
         term.session = session;
         tracker.add(term);
         shell.activateById(term.id);
+        return term;
       }).catch(() => { term.dispose(); });
     }
   });