opentabs.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { ILabShell } from '@jupyterlab/application';
  4. import { DocumentWidget } from '@jupyterlab/docregistry';
  5. import { IRunningSessionManagers, IRunningSessions } from '@jupyterlab/running';
  6. import { ITranslator } from '@jupyterlab/translation';
  7. import { fileIcon, LabIcon } from '@jupyterlab/ui-components';
  8. import { toArray } from '@lumino/algorithm';
  9. import { ISignal, Signal } from '@lumino/signaling';
  10. import { Widget } from '@lumino/widgets';
  11. /**
  12. * A class used to consolidate the signals used to rerender the open tabs section.
  13. */
  14. class OpenTabsSignaler {
  15. constructor(labShell: ILabShell) {
  16. this._labShell = labShell;
  17. this._labShell.layoutModified.connect(this._emitTabsChanged, this);
  18. }
  19. /**
  20. * A signal that fires when the open tabs section should be rerendered.
  21. */
  22. get tabsChanged(): ISignal<this, void> {
  23. return this._tabsChanged;
  24. }
  25. /**
  26. * Add a widget to watch for title changing.
  27. *
  28. * @param widget A widget whose title may change.
  29. */
  30. addWidget(widget: Widget): void {
  31. widget.title.changed.connect(this._emitTabsChanged, this);
  32. this._widgets.push(widget);
  33. }
  34. /**
  35. * Emit the main signal that indicates the open tabs should be rerendered.
  36. */
  37. private _emitTabsChanged(): void {
  38. this._widgets.forEach(widget => {
  39. widget.title.changed.disconnect(this._emitTabsChanged, this);
  40. });
  41. this._widgets = [];
  42. this._tabsChanged.emit(void 0);
  43. }
  44. private _tabsChanged = new Signal<this, void>(this);
  45. private _labShell: ILabShell;
  46. private _widgets: Widget[] = [];
  47. }
  48. /**
  49. * Add the open tabs section to the running panel.
  50. *
  51. * @param managers - The IRunningSessionManagers used to register this section.
  52. * @param translator - The translator to use.
  53. * @param labShell - The ILabShell.
  54. */
  55. export function addOpenTabsSessionManager(
  56. managers: IRunningSessionManagers,
  57. translator: ITranslator,
  58. labShell: ILabShell
  59. ) {
  60. const signaler = new OpenTabsSignaler(labShell);
  61. const trans = translator.load('jupyterlab');
  62. managers.add({
  63. name: trans.__('Open Tabs'),
  64. running: () => {
  65. return toArray(labShell.widgets('main')).map((widget: Widget) => {
  66. signaler.addWidget(widget);
  67. return new OpenTab(widget);
  68. });
  69. },
  70. shutdownAll: () => {
  71. toArray(labShell.widgets('main')).forEach((widget: Widget) => {
  72. widget.close();
  73. });
  74. },
  75. refreshRunning: () => {
  76. return void 0;
  77. },
  78. runningChanged: signaler.tabsChanged,
  79. shutdownLabel: trans.__('Close'),
  80. shutdownAllLabel: trans.__('Close All'),
  81. shutdownAllConfirmationText: trans.__(
  82. 'Are you sure you want to close all open tabs?'
  83. )
  84. });
  85. class OpenTab implements IRunningSessions.IRunningItem {
  86. constructor(widget: Widget) {
  87. this._widget = widget;
  88. }
  89. open() {
  90. labShell.activateById(this._widget.id);
  91. }
  92. shutdown() {
  93. this._widget.close();
  94. }
  95. icon() {
  96. const widgetIcon = this._widget.title.icon;
  97. return widgetIcon instanceof LabIcon ? widgetIcon : fileIcon;
  98. }
  99. label() {
  100. return this._widget.title.label;
  101. }
  102. labelTitle() {
  103. let labelTitle: string;
  104. if (this._widget instanceof DocumentWidget) {
  105. labelTitle = this._widget.context.path;
  106. } else {
  107. labelTitle = this._widget.title.label;
  108. }
  109. return labelTitle;
  110. }
  111. private _widget: Widget;
  112. }
  113. }