@jupyterlab/ui-components 4.1.0-alpha.3 → 4.1.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,13 @@
1
1
  // Copyright (c) Jupyter Development Team.
2
2
  // Distributed under the terms of the Modified BSD License.
3
3
 
4
+ import { Button } from '@jupyter/react-components';
5
+ import {
6
+ addJupyterLabThemeChangeListener,
7
+ jpButton,
8
+ jpToolbar,
9
+ provideJupyterDesignSystem
10
+ } from '@jupyter/web-components';
4
11
  import { ITranslator, nullTranslator } from '@jupyterlab/translation';
5
12
  import { find, map, some } from '@lumino/algorithm';
6
13
  import { CommandRegistry } from '@lumino/commands';
@@ -10,11 +17,13 @@ import { AttachedProperty } from '@lumino/properties';
10
17
  import { Layout, PanelLayout, Widget } from '@lumino/widgets';
11
18
  import { Throttler } from '@lumino/polling';
12
19
  import * as React from 'react';
13
- import { Button } from './button';
14
20
  import { ellipsesIcon, LabIcon } from '../icon';
15
21
  import { classes } from '../utils';
16
22
  import { ReactWidget, UseSignal } from './vdom';
17
23
 
24
+ provideJupyterDesignSystem().register([jpButton(), jpToolbar()]);
25
+ addJupyterLabThemeChangeListener();
26
+
18
27
  /**
19
28
  * The class name added to toolbars.
20
29
  */
@@ -165,7 +174,7 @@ export class Toolbar<T extends Widget = Widget> extends Widget {
165
174
  * Construct a new toolbar widget.
166
175
  */
167
176
  constructor(options: Toolbar.IOptions = {}) {
168
- super();
177
+ super({ node: document.createElement('jp-toolbar') });
169
178
  this.addClass(TOOLBAR_CLASS);
170
179
  this.layout = options.layout ?? new ToolbarLayout();
171
180
  }
@@ -362,7 +371,9 @@ export class ReactiveToolbar extends Toolbar<Widget> {
362
371
  super();
363
372
  this.insertItem(0, TOOLBAR_OPENER_NAME, this.popupOpener);
364
373
  this.popupOpener.hide();
365
- this._resizer = new Throttler(this._onResize.bind(this), 500);
374
+ this._resizer = new Throttler(async () => {
375
+ await this._onResize();
376
+ }, 300);
366
377
  }
367
378
 
368
379
  /**
@@ -421,15 +432,22 @@ export class ReactiveToolbar extends Toolbar<Widget> {
421
432
  * The item can be removed from the toolbar by setting its parent to `null`.
422
433
  */
423
434
  insertItem(index: number, name: string, widget: Widget): boolean {
435
+ let status: boolean;
424
436
  if (widget instanceof ToolbarPopupOpener) {
425
- return super.insertItem(index, name, widget);
437
+ status = super.insertItem(index, name, widget);
426
438
  } else {
427
439
  const j = Math.max(
428
440
  0,
429
441
  Math.min(index, (this.layout as ToolbarLayout).widgets.length - 1)
430
442
  );
431
- return super.insertItem(j, name, widget);
443
+ status = super.insertItem(j, name, widget);
444
+ }
445
+
446
+ // Save the widgets position when a new widget is inserted.
447
+ if (this._widgetPositions.get(name) === undefined) {
448
+ this._saveWidgetPosition();
432
449
  }
450
+ return status;
433
451
  }
434
452
 
435
453
  /**
@@ -444,92 +462,142 @@ export class ReactiveToolbar extends Toolbar<Widget> {
444
462
 
445
463
  protected onResize(msg: Widget.ResizeMessage): void {
446
464
  super.onResize(msg);
465
+
466
+ // Check if the resize event is due to a zoom change.
467
+ const zoom = Math.round((window.outerWidth / window.innerWidth) * 100);
468
+ if (zoom !== this._zoom) {
469
+ this._zoomChanged = true;
470
+ this._zoom = zoom;
471
+ }
447
472
  if (msg.width > 0 && this._resizer) {
448
473
  void this._resizer.invoke();
449
474
  }
450
475
  }
451
476
 
452
477
  private _onResize() {
453
- if (this.parent && this.parent.isAttached) {
454
- const toolbarWidth = this.node.clientWidth;
455
- const opener = this.popupOpener;
456
- const openerWidth = 30;
457
- const toolbarPadding = 2;
458
- const layout = this.layout as ToolbarLayout;
459
-
460
- let width = opener.isHidden
461
- ? toolbarPadding
462
- : toolbarPadding + openerWidth;
463
- let index = 0;
464
- const widgetsToRemove = [];
465
- const toIndex = layout.widgets.length - 1;
466
-
467
- while (index < toIndex) {
468
- const widget = layout.widgets[index];
469
- this._saveWidgetWidth(widget);
470
- width += this._getWidgetWidth(widget);
471
- if (
472
- widgetsToRemove.length === 0 &&
473
- opener.isHidden &&
474
- width + openerWidth > toolbarWidth
475
- ) {
476
- width += openerWidth;
477
- }
478
- if (width > toolbarWidth) {
479
- widgetsToRemove.push(widget);
478
+ if (!(this.parent && this.parent.isAttached)) {
479
+ return;
480
+ }
481
+ const toolbarWidth = this.node.clientWidth;
482
+ const opener = this.popupOpener;
483
+ const openerWidth = 30;
484
+ // left and right padding.
485
+ const toolbarPadding = 2 + 5;
486
+ let width = opener.isHidden ? toolbarPadding : toolbarPadding + openerWidth;
487
+
488
+ this._getWidgetsToRemove(width, toolbarWidth, openerWidth)
489
+ .then(values => {
490
+ let { width, widgetsToRemove } = values;
491
+ while (widgetsToRemove.length > 0) {
492
+ // Insert the widget in the right position in the opener popup.
493
+ const widget = widgetsToRemove.pop() as Widget;
494
+ const name = Private.nameProperty.get(widget);
495
+ width -= this._widgetWidths![name];
496
+ const position = this._widgetPositions.get(name) ?? 0;
497
+ const index =
498
+ opener.widgetCount() + position - this._widgetPositions.size;
499
+ opener.insertWidget(index, widget);
480
500
  }
481
- index++;
482
- }
483
-
484
- while (widgetsToRemove.length > 0) {
485
- const widget = widgetsToRemove.pop() as Widget;
486
- width -= this._getWidgetWidth(widget);
487
- opener.addWidget(widget);
488
- }
501
+ if (opener.widgetCount() > 0) {
502
+ const widgetsToAdd = [];
503
+ let index = 0;
504
+ let widget = opener.widgetAt(index);
505
+ const widgetCount = opener.widgetCount();
489
506
 
490
- if (opener.widgetCount() > 0) {
491
- const widgetsToAdd = [];
492
- let index = 0;
493
- let widget = opener.widgetAt(index);
494
- const widgetCount = opener.widgetCount();
507
+ width += this._getWidgetWidth(widget);
495
508
 
496
- width += this._getWidgetWidth(widget);
509
+ if (widgetCount === 1 && width - openerWidth <= toolbarWidth) {
510
+ width -= openerWidth;
511
+ }
497
512
 
498
- if (widgetCount === 1 && width - openerWidth <= toolbarWidth) {
499
- width -= openerWidth;
500
- }
513
+ while (width < toolbarWidth && index < widgetCount) {
514
+ widgetsToAdd.push(widget);
515
+ index++;
516
+ widget = opener.widgetAt(index);
517
+ if (widget) {
518
+ width += this._getWidgetWidth(widget);
519
+ } else {
520
+ break;
521
+ }
522
+ }
501
523
 
502
- while (width < toolbarWidth && index < widgetCount) {
503
- widgetsToAdd.push(widget);
504
- index++;
505
- widget = opener.widgetAt(index);
506
- if (widget) {
507
- width += this._getWidgetWidth(widget);
508
- } else {
509
- break;
524
+ while (widgetsToAdd.length > 0) {
525
+ // Insert the widget in the right position in the toolbar.
526
+ const widget = widgetsToAdd.shift()!;
527
+ const name = Private.nameProperty.get(widget);
528
+ const index = this._widgetPositions.get(name) ?? 0;
529
+ this.insertItem(index, name, widget);
510
530
  }
511
531
  }
512
532
 
513
- while (widgetsToAdd.length > 0) {
514
- const widget = widgetsToAdd.shift()!;
515
- this.addItem(Private.nameProperty.get(widget), widget);
533
+ if (opener.widgetCount() > 0) {
534
+ opener.updatePopup();
535
+ opener.show();
536
+ } else {
537
+ opener.hide();
516
538
  }
517
- }
539
+ })
540
+ .catch(msg => {
541
+ console.error('Error while computing the ReactiveToolbar', msg);
542
+ });
543
+ }
518
544
 
519
- if (opener.widgetCount() > 0) {
520
- opener.updatePopup();
521
- opener.show();
545
+ private async _getWidgetsToRemove(
546
+ width: number,
547
+ toolbarWidth: number,
548
+ openerWidth: number
549
+ ) {
550
+ const opener = this.popupOpener;
551
+ const layout = this.layout as ToolbarLayout;
552
+ const toIndex = layout.widgets.length - 1;
553
+
554
+ const widgetsToRemove = [];
555
+
556
+ let index = 0;
557
+ while (index < toIndex) {
558
+ const widget = layout.widgets[index];
559
+ // Compute the widget size only if
560
+ // - the zoom has changed.
561
+ // - the widget size has not been computed yet.
562
+ let widgetWidth: number;
563
+ if (this._zoomChanged) {
564
+ widgetWidth = await this._saveWidgetWidth(widget);
522
565
  } else {
523
- opener.hide();
566
+ widgetWidth =
567
+ this._getWidgetWidth(widget) ?? (await this._saveWidgetWidth(widget));
568
+ }
569
+ width += widgetWidth;
570
+ if (
571
+ widgetsToRemove.length === 0 &&
572
+ opener.isHidden &&
573
+ width + openerWidth > toolbarWidth
574
+ ) {
575
+ width += openerWidth;
576
+ }
577
+ if (width > toolbarWidth) {
578
+ widgetsToRemove.push(widget);
524
579
  }
580
+ index++;
525
581
  }
582
+
583
+ this._zoomChanged = false;
584
+ return {
585
+ width: width,
586
+ widgetsToRemove: widgetsToRemove
587
+ };
526
588
  }
527
589
 
528
- private _saveWidgetWidth(widget: Widget) {
590
+ private async _saveWidgetWidth(widget: Widget): Promise<number> {
591
+ if (widget instanceof ReactWidget) {
592
+ await widget.renderPromise;
593
+ }
529
594
  const widgetName = Private.nameProperty.get(widget);
530
- this._widgetWidths![widgetName] = widget.hasClass(TOOLBAR_SPACER_CLASS)
595
+
596
+ const widgetWidth = widget.hasClass(TOOLBAR_SPACER_CLASS)
531
597
  ? 2
532
598
  : widget.node.clientWidth;
599
+ this._widgetWidths![widgetName] = widgetWidth;
600
+ return widgetWidth;
533
601
  }
534
602
 
535
603
  private _getWidgetWidth(widget: Widget): number {
@@ -537,9 +605,21 @@ export class ReactiveToolbar extends Toolbar<Widget> {
537
605
  return this._widgetWidths![widgetName];
538
606
  }
539
607
 
608
+ private _saveWidgetPosition(): void {
609
+ this._widgetPositions.clear();
610
+ (this.layout as ToolbarLayout).widgets.forEach((widget, index) => {
611
+ this._widgetPositions.set(Private.nameProperty.get(widget), index);
612
+ });
613
+ }
614
+
540
615
  protected readonly popupOpener: ToolbarPopupOpener = new ToolbarPopupOpener();
541
616
  private readonly _widgetWidths: { [key: string]: number } = {};
542
617
  private readonly _resizer: Throttler;
618
+ private _widgetPositions = new Map<string, number>();
619
+ // The zoom property is not the real browser zoom, but a value proportional to
620
+ // the zoom, which is modified when the zoom changes.
621
+ private _zoom: number;
622
+ private _zoomChanged = true;
543
623
  }
544
624
 
545
625
  /**
@@ -626,17 +706,32 @@ export namespace ToolbarButtonComponent {
626
706
  export function ToolbarButtonComponent(
627
707
  props: ToolbarButtonComponent.IProps
628
708
  ): JSX.Element {
709
+ const actualOnClick = props.actualOnClick ?? false;
710
+
629
711
  // In some browsers, a button click event moves the focus from the main
630
712
  // content to the button (see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus).
631
713
  // We avoid a click event by calling preventDefault in mousedown, and
632
714
  // we bind the button action to `mousedown`.
633
- const handleMouseDown = (event: React.MouseEvent) => {
634
- // Fire action only when left button is pressed.
635
- if (event.button === 0) {
636
- event.preventDefault();
637
- props.onClick?.();
638
- }
639
- };
715
+ const handleMouseDown = actualOnClick
716
+ ? undefined
717
+ : (event: React.MouseEvent) => {
718
+ // Fire action only when left button is pressed.
719
+ if (event.button === 0) {
720
+ event.preventDefault();
721
+ props.onClick?.();
722
+ }
723
+ };
724
+
725
+ const handleClick = actualOnClick
726
+ ? (event: React.MouseEvent) => {
727
+ if (event.button === 0) {
728
+ props.onClick?.();
729
+ }
730
+ }
731
+ : (event: React.MouseEvent) => {
732
+ // Prevent the toolbar widget to steal focus
733
+ event.stopPropagation();
734
+ };
640
735
 
641
736
  const handleKeyDown = (event: React.KeyboardEvent) => {
642
737
  const { key } = event;
@@ -645,12 +740,6 @@ export function ToolbarButtonComponent(
645
740
  }
646
741
  };
647
742
 
648
- const handleClick = (event: React.MouseEvent) => {
649
- if (event.button === 0) {
650
- props.onClick?.();
651
- }
652
- };
653
-
654
743
  const getTooltip = () => {
655
744
  if (props.enabled === false && props.disabledTooltip) {
656
745
  return props.disabledTooltip;
@@ -663,6 +752,7 @@ export function ToolbarButtonComponent(
663
752
 
664
753
  return (
665
754
  <Button
755
+ appearance="stealth"
666
756
  className={
667
757
  props.className
668
758
  ? props.className + ' jp-ToolbarButtonComponent'
@@ -672,24 +762,20 @@ export function ToolbarButtonComponent(
672
762
  aria-disabled={props.enabled === false}
673
763
  {...props.dataset}
674
764
  disabled={props.enabled === false}
675
- onClick={props.actualOnClick ?? false ? handleClick : undefined}
676
- onMouseDown={
677
- !(props.actualOnClick ?? false) ? handleMouseDown : undefined
678
- }
765
+ onClick={handleClick}
766
+ onMouseDown={handleMouseDown}
679
767
  onKeyDown={handleKeyDown}
680
768
  title={getTooltip()}
681
769
  minimal
682
770
  >
683
771
  {(props.icon || props.iconClass) && (
684
772
  <LabIcon.resolveReact
685
- icon={props.pressed ? props.pressedIcon : props.icon}
773
+ icon={props.pressed ? props.pressedIcon ?? props.icon : props.icon}
686
774
  iconClass={
687
775
  // add some extra classes for proper support of icons-as-css-background
688
776
  classes(props.iconClass, 'jp-Icon')
689
777
  }
690
- className="jp-ToolbarButtonComponent-icon"
691
- tag="span"
692
- stylesheet="toolbarButton"
778
+ tag={null}
693
779
  />
694
780
  )}
695
781
  {props.label && (
@@ -849,7 +935,11 @@ export function CommandToolbarButtonComponent(
849
935
  args.type === 'many-changed'
850
936
  }
851
937
  >
852
- {() => <ToolbarButtonComponent {...Private.propsFromCommand(props)} />}
938
+ {() =>
939
+ props.commands.listCommands().includes(props.id) ? (
940
+ <ToolbarButtonComponent {...Private.propsFromCommand(props)} />
941
+ ) : null
942
+ }
853
943
  </UseSignal>
854
944
  );
855
945
  }
@@ -873,7 +963,39 @@ export class CommandToolbarButton extends ReactWidget {
873
963
  */
874
964
  constructor(private props: CommandToolbarButtonComponent.IProps) {
875
965
  super();
966
+ const { commands, id, args } = props;
876
967
  addCommandToolbarButtonClass(this);
968
+ this.setCommandAttributes(commands, id, args);
969
+ commands.commandChanged.connect((_, change) => {
970
+ if (change.id === props.id) {
971
+ this.setCommandAttributes(commands, id, args);
972
+ }
973
+ }, this);
974
+ }
975
+ protected setCommandAttributes(
976
+ commands: CommandRegistry,
977
+ id: string,
978
+ args: ReadonlyJSONObject | undefined
979
+ ): void {
980
+ if (commands.isToggled(id, args)) {
981
+ this.addClass('lm-mod-toggled');
982
+ } else {
983
+ this.removeClass('lm-mod-toggled');
984
+ }
985
+ if (commands.isVisible(id, args)) {
986
+ this.removeClass('lm-mod-hidden');
987
+ } else {
988
+ this.addClass('lm-mod-hidden');
989
+ }
990
+ if (commands.isEnabled(id, args)) {
991
+ if ('disabled' in this.node) {
992
+ this.node.disabled = false;
993
+ }
994
+ } else {
995
+ if ('disabled' in this.node) {
996
+ this.node.disabled = true;
997
+ }
998
+ }
877
999
  }
878
1000
  render(): JSX.Element {
879
1001
  return <CommandToolbarButtonComponent {...this.props} />;
@@ -892,7 +1014,8 @@ class ToolbarPopup extends Widget {
892
1014
  * Construct a new ToolbarPopup
893
1015
  */
894
1016
  constructor() {
895
- super();
1017
+ super({ node: document.createElement('jp-toolbar') });
1018
+ this.addClass('jp-Toolbar');
896
1019
  this.addClass('jp-Toolbar-responsive-popup');
897
1020
  this.layout = new PanelLayout();
898
1021
  Widget.attach(this, document.body);
@@ -937,7 +1060,7 @@ class ToolbarPopup extends Widget {
937
1060
  * @param widget widget to add
938
1061
  */
939
1062
  insertWidget(index: number, widget: Widget) {
940
- (this.layout as PanelLayout).insertWidget(0, widget);
1063
+ (this.layout as PanelLayout).insertWidget(index, widget);
941
1064
  }
942
1065
 
943
1066
  /**
@@ -987,6 +1110,14 @@ class ToolbarPopupOpener extends ToolbarButton {
987
1110
  this.popup.insertWidget(0, widget);
988
1111
  }
989
1112
 
1113
+ /**
1114
+ * Insert widget to the popup.
1115
+ * @param widget the widget to add
1116
+ */
1117
+ insertWidget(index: number, widget: Widget) {
1118
+ this.popup.insertWidget(index, widget);
1119
+ }
1120
+
990
1121
  /**
991
1122
  * Dispose of the widget and its descendant widgets.
992
1123
  *
@@ -1067,9 +1198,13 @@ namespace Private {
1067
1198
 
1068
1199
  const label = commands.label(id, args);
1069
1200
  let className = commands.className(id, args);
1070
- // Add the boolean state classes.
1071
- if (commands.isToggled(id, args)) {
1072
- className += ' lm-mod-toggled';
1201
+ // Add the boolean state classes and aria attributes.
1202
+ let pressed;
1203
+ if (commands.isToggleable(id, args)) {
1204
+ pressed = commands.isToggled(id, args);
1205
+ if (pressed) {
1206
+ className += ' lm-mod-toggled';
1207
+ }
1073
1208
  }
1074
1209
  if (!commands.isVisible(id, args)) {
1075
1210
  className += ' lm-mod-hidden';
@@ -1096,7 +1231,8 @@ namespace Private {
1096
1231
  tooltip: options.caption ?? tooltip,
1097
1232
  onClick,
1098
1233
  enabled,
1099
- label: options.label ?? label
1234
+ label: options.label ?? label,
1235
+ pressed
1100
1236
  };
1101
1237
  }
1102
1238