616c1769ef110e3b4f7b80e0b84941fe10b4185b1a70ce48ae3d47fd13e4d5fbcc221583f4e039273414ef656472d9623bf2dfa9a862fd65a781355a9e456c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 * as dom from '../../dom.js';
  6. import { StandardKeyboardEvent } from '../../keyboardEvent.js';
  7. import { DomScrollableElement } from '../scrollbar/scrollableElement.js';
  8. import { Disposable } from '../../../common/lifecycle.js';
  9. import './hover.css';
  10. const $ = dom.$;
  11. export class HoverWidget extends Disposable {
  12. constructor() {
  13. super();
  14. this.containerDomNode = document.createElement('div');
  15. this.containerDomNode.className = 'monaco-hover';
  16. this.containerDomNode.tabIndex = 0;
  17. this.containerDomNode.setAttribute('role', 'tooltip');
  18. this.contentsDomNode = document.createElement('div');
  19. this.contentsDomNode.className = 'monaco-hover-content';
  20. this.scrollbar = this._register(new DomScrollableElement(this.contentsDomNode, {
  21. consumeMouseWheelIfScrollbarIsNeeded: true
  22. }));
  23. this.containerDomNode.appendChild(this.scrollbar.getDomNode());
  24. }
  25. onContentsChanged() {
  26. this.scrollbar.scanDomNode();
  27. }
  28. }
  29. export class HoverAction extends Disposable {
  30. constructor(parent, actionOptions, keybindingLabel) {
  31. super();
  32. this.actionContainer = dom.append(parent, $('div.action-container'));
  33. this.actionContainer.setAttribute('tabindex', '0');
  34. this.action = dom.append(this.actionContainer, $('a.action'));
  35. this.action.setAttribute('role', 'button');
  36. if (actionOptions.iconClass) {
  37. dom.append(this.action, $(`span.icon.${actionOptions.iconClass}`));
  38. }
  39. const label = dom.append(this.action, $('span'));
  40. label.textContent = keybindingLabel ? `${actionOptions.label} (${keybindingLabel})` : actionOptions.label;
  41. this._register(dom.addDisposableListener(this.actionContainer, dom.EventType.CLICK, e => {
  42. e.stopPropagation();
  43. e.preventDefault();
  44. actionOptions.run(this.actionContainer);
  45. }));
  46. this._register(dom.addDisposableListener(this.actionContainer, dom.EventType.KEY_UP, e => {
  47. const event = new StandardKeyboardEvent(e);
  48. if (event.equals(3 /* KeyCode.Enter */)) {
  49. e.stopPropagation();
  50. e.preventDefault();
  51. actionOptions.run(this.actionContainer);
  52. }
  53. }));
  54. this.setEnabled(true);
  55. }
  56. static render(parent, actionOptions, keybindingLabel) {
  57. return new HoverAction(parent, actionOptions, keybindingLabel);
  58. }
  59. setEnabled(enabled) {
  60. if (enabled) {
  61. this.actionContainer.classList.remove('disabled');
  62. this.actionContainer.removeAttribute('aria-disabled');
  63. }
  64. else {
  65. this.actionContainer.classList.add('disabled');
  66. this.actionContainer.setAttribute('aria-disabled', 'true');
  67. }
  68. }
  69. }