context.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { IInstanceTracker } from '@jupyterlab/apputils';
  4. import { Widget } from '@phosphor/widgets';
  5. import { ApplicationShell } from '@jupyterlab/application';
  6. export interface IStatusContext<T extends Widget> {
  7. tracker: IInstanceTracker<T>;
  8. isEnabled?: (widget: T) => boolean;
  9. }
  10. export namespace IStatusContext {
  11. function findContext<E extends IStatusContext<Widget>>(
  12. widget: Widget,
  13. contexts: Array<E>
  14. ): E | undefined {
  15. return contexts.find((context: E) => {
  16. return context.tracker.has(widget);
  17. });
  18. }
  19. export function delegateActive<E extends IStatusContext<Widget>>(
  20. shell: ApplicationShell,
  21. contexts: Array<E>
  22. ): () => boolean {
  23. return () => {
  24. const currentWidget: Widget | null = shell.currentWidget;
  25. if (currentWidget !== null) {
  26. const context = findContext(currentWidget, contexts);
  27. return (
  28. !!context &&
  29. (context.isEnabled ? context.isEnabled(currentWidget) : true)
  30. );
  31. } else if (contexts.length === 0) {
  32. return true;
  33. } else {
  34. return false;
  35. }
  36. };
  37. }
  38. }