ea6c370e7393eeb2fb9411f563b27bbc63eccee61fb14117591ddaf4c820dda43d539477aad4a8a5ca6d738ac5748a1b47934d7d6a36bb54acc56ca86987d9 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { TimeoutTimer } from '../../../common/async.js';
  6. import { Disposable } from '../../../common/lifecycle.js';
  7. export class ScrollbarVisibilityController extends Disposable {
  8. constructor(visibility, visibleClassName, invisibleClassName) {
  9. super();
  10. this._visibility = visibility;
  11. this._visibleClassName = visibleClassName;
  12. this._invisibleClassName = invisibleClassName;
  13. this._domNode = null;
  14. this._isVisible = false;
  15. this._isNeeded = false;
  16. this._rawShouldBeVisible = false;
  17. this._shouldBeVisible = false;
  18. this._revealTimer = this._register(new TimeoutTimer());
  19. }
  20. setVisibility(visibility) {
  21. if (this._visibility !== visibility) {
  22. this._visibility = visibility;
  23. this._updateShouldBeVisible();
  24. }
  25. }
  26. // ----------------- Hide / Reveal
  27. setShouldBeVisible(rawShouldBeVisible) {
  28. this._rawShouldBeVisible = rawShouldBeVisible;
  29. this._updateShouldBeVisible();
  30. }
  31. _applyVisibilitySetting() {
  32. if (this._visibility === 2 /* ScrollbarVisibility.Hidden */) {
  33. return false;
  34. }
  35. if (this._visibility === 3 /* ScrollbarVisibility.Visible */) {
  36. return true;
  37. }
  38. return this._rawShouldBeVisible;
  39. }
  40. _updateShouldBeVisible() {
  41. const shouldBeVisible = this._applyVisibilitySetting();
  42. if (this._shouldBeVisible !== shouldBeVisible) {
  43. this._shouldBeVisible = shouldBeVisible;
  44. this.ensureVisibility();
  45. }
  46. }
  47. setIsNeeded(isNeeded) {
  48. if (this._isNeeded !== isNeeded) {
  49. this._isNeeded = isNeeded;
  50. this.ensureVisibility();
  51. }
  52. }
  53. setDomNode(domNode) {
  54. this._domNode = domNode;
  55. this._domNode.setClassName(this._invisibleClassName);
  56. // Now that the flags & the dom node are in a consistent state, ensure the Hidden/Visible configuration
  57. this.setShouldBeVisible(false);
  58. }
  59. ensureVisibility() {
  60. if (!this._isNeeded) {
  61. // Nothing to be rendered
  62. this._hide(false);
  63. return;
  64. }
  65. if (this._shouldBeVisible) {
  66. this._reveal();
  67. }
  68. else {
  69. this._hide(true);
  70. }
  71. }
  72. _reveal() {
  73. if (this._isVisible) {
  74. return;
  75. }
  76. this._isVisible = true;
  77. // The CSS animation doesn't play otherwise
  78. this._revealTimer.setIfNotSet(() => {
  79. var _a;
  80. (_a = this._domNode) === null || _a === void 0 ? void 0 : _a.setClassName(this._visibleClassName);
  81. }, 0);
  82. }
  83. _hide(withFadeAway) {
  84. var _a;
  85. this._revealTimer.cancel();
  86. if (!this._isVisible) {
  87. return;
  88. }
  89. this._isVisible = false;
  90. (_a = this._domNode) === null || _a === void 0 ? void 0 : _a.setClassName(this._invisibleClassName + (withFadeAway ? ' fade' : ''));
  91. }
  92. }