index.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { ToolbarButton } from '@jupyterlab/apputils';
  4. import { CommandRegistry } from '@lumino/commands';
  5. import { Panel, Widget } from '@lumino/widgets';
  6. import { IDebugger } from '../../tokens';
  7. import { VariablesBodyGrid, VariablesGrid } from './grid';
  8. import { VariablesHeader } from './header';
  9. import { VariablesModel } from './model';
  10. import { VariablesBodyTree } from './tree';
  11. /**
  12. * A Panel to show a variable explorer.
  13. */
  14. export class Variables extends Panel {
  15. /**
  16. * Instantiate a new Variables Panel.
  17. *
  18. * @param options The instantiation options for a Variables Panel.
  19. */
  20. constructor(options: Variables.IOptions) {
  21. super();
  22. const { model, service, commands } = options;
  23. this._header = new VariablesHeader();
  24. this._tree = new VariablesBodyTree({ model, service });
  25. this._table = new VariablesBodyGrid({ model, commands });
  26. this._table.hide();
  27. const onClick = (): void => {
  28. if (this._table.isHidden) {
  29. this._tree.hide();
  30. this._table.show();
  31. this.node.setAttribute('data-jp-table', 'true');
  32. } else {
  33. this._tree.show();
  34. this._table.hide();
  35. this.node.removeAttribute('data-jp-table');
  36. }
  37. this.update();
  38. };
  39. this._header.toolbar.addItem(
  40. 'view-VariableSwitch',
  41. new ToolbarButton({
  42. iconClass: 'jp-ToggleSwitch',
  43. onClick,
  44. tooltip: 'Table / Tree View'
  45. })
  46. );
  47. this.addWidget(this._header);
  48. this.addWidget(this._tree);
  49. this.addWidget(this._table);
  50. this.addClass('jp-DebuggerVariables');
  51. }
  52. /**
  53. * Set the variable filter for both the tree and table views.
  54. */
  55. set filter(filter: Set<string>) {
  56. this._tree.filter = filter;
  57. this._table.filter = filter;
  58. }
  59. /**
  60. * Set the theme for the variable table.
  61. */
  62. set theme(theme: VariablesGrid.Theme) {
  63. this._table.theme = theme;
  64. }
  65. /**
  66. * A message handler invoked on a `'resize'` message.
  67. *
  68. * @param msg The Lumino message to process.
  69. */
  70. protected onResize(msg: Widget.ResizeMessage): void {
  71. super.onResize(msg);
  72. this._resizeBody(msg);
  73. }
  74. /**
  75. * Resize the body.
  76. *
  77. * @param msg The resize message.
  78. */
  79. private _resizeBody(msg: Widget.ResizeMessage): void {
  80. const height = msg.height - this._header.node.offsetHeight;
  81. this._tree.node.style.height = `${height}px`;
  82. }
  83. private _header: VariablesHeader;
  84. private _tree: VariablesBodyTree;
  85. private _table: VariablesBodyGrid;
  86. }
  87. /**
  88. * Convert a variable to a primitive type.
  89. *
  90. * @param variable The variable.
  91. */
  92. export const convertType = (
  93. variable: VariablesModel.IVariable
  94. ): string | number => {
  95. const { type, value } = variable;
  96. switch (type) {
  97. case 'int':
  98. return parseInt(value, 10);
  99. case 'float':
  100. return parseFloat(value);
  101. case 'bool':
  102. return value;
  103. case 'str':
  104. return value.slice(1, value.length - 1);
  105. default:
  106. return type;
  107. }
  108. };
  109. /**
  110. * A namespace for Variables `statics`.
  111. */
  112. export namespace Variables {
  113. /**
  114. * Instantiation options for `Variables`.
  115. */
  116. export interface IOptions extends Panel.IOptions {
  117. /**
  118. * The variables model.
  119. */
  120. model: IDebugger.UI.IVariables;
  121. /**
  122. * The debugger service.
  123. */
  124. service: IDebugger;
  125. /**
  126. * The commands registry.
  127. */
  128. commands: CommandRegistry;
  129. }
  130. }