actions.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  6. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  7. return new (P || (P = Promise))(function (resolve, reject) {
  8. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  9. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  10. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  11. step((generator = generator.apply(thisArg, _arguments || [])).next());
  12. });
  13. };
  14. import { Emitter } from './event.js';
  15. import { Disposable } from './lifecycle.js';
  16. import * as nls from '../../nls.js';
  17. export class Action extends Disposable {
  18. constructor(id, label = '', cssClass = '', enabled = true, actionCallback) {
  19. super();
  20. this._onDidChange = this._register(new Emitter());
  21. this.onDidChange = this._onDidChange.event;
  22. this._enabled = true;
  23. this._id = id;
  24. this._label = label;
  25. this._cssClass = cssClass;
  26. this._enabled = enabled;
  27. this._actionCallback = actionCallback;
  28. }
  29. get id() {
  30. return this._id;
  31. }
  32. get label() {
  33. return this._label;
  34. }
  35. set label(value) {
  36. this._setLabel(value);
  37. }
  38. _setLabel(value) {
  39. if (this._label !== value) {
  40. this._label = value;
  41. this._onDidChange.fire({ label: value });
  42. }
  43. }
  44. get tooltip() {
  45. return this._tooltip || '';
  46. }
  47. set tooltip(value) {
  48. this._setTooltip(value);
  49. }
  50. _setTooltip(value) {
  51. if (this._tooltip !== value) {
  52. this._tooltip = value;
  53. this._onDidChange.fire({ tooltip: value });
  54. }
  55. }
  56. get class() {
  57. return this._cssClass;
  58. }
  59. set class(value) {
  60. this._setClass(value);
  61. }
  62. _setClass(value) {
  63. if (this._cssClass !== value) {
  64. this._cssClass = value;
  65. this._onDidChange.fire({ class: value });
  66. }
  67. }
  68. get enabled() {
  69. return this._enabled;
  70. }
  71. set enabled(value) {
  72. this._setEnabled(value);
  73. }
  74. _setEnabled(value) {
  75. if (this._enabled !== value) {
  76. this._enabled = value;
  77. this._onDidChange.fire({ enabled: value });
  78. }
  79. }
  80. get checked() {
  81. return this._checked;
  82. }
  83. set checked(value) {
  84. this._setChecked(value);
  85. }
  86. _setChecked(value) {
  87. if (this._checked !== value) {
  88. this._checked = value;
  89. this._onDidChange.fire({ checked: value });
  90. }
  91. }
  92. run(event, data) {
  93. return __awaiter(this, void 0, void 0, function* () {
  94. if (this._actionCallback) {
  95. yield this._actionCallback(event);
  96. }
  97. });
  98. }
  99. }
  100. export class ActionRunner extends Disposable {
  101. constructor() {
  102. super(...arguments);
  103. this._onBeforeRun = this._register(new Emitter());
  104. this.onBeforeRun = this._onBeforeRun.event;
  105. this._onDidRun = this._register(new Emitter());
  106. this.onDidRun = this._onDidRun.event;
  107. }
  108. run(action, context) {
  109. return __awaiter(this, void 0, void 0, function* () {
  110. if (!action.enabled) {
  111. return;
  112. }
  113. this._onBeforeRun.fire({ action });
  114. let error = undefined;
  115. try {
  116. yield this.runAction(action, context);
  117. }
  118. catch (e) {
  119. error = e;
  120. }
  121. this._onDidRun.fire({ action, error });
  122. });
  123. }
  124. runAction(action, context) {
  125. return __awaiter(this, void 0, void 0, function* () {
  126. yield action.run(context);
  127. });
  128. }
  129. }
  130. export class Separator extends Action {
  131. constructor(label) {
  132. super(Separator.ID, label, label ? 'separator text' : 'separator');
  133. this.checked = false;
  134. this.enabled = false;
  135. }
  136. }
  137. Separator.ID = 'vs.actions.separator';
  138. export class SubmenuAction {
  139. constructor(id, label, actions, cssClass) {
  140. this.tooltip = '';
  141. this.enabled = true;
  142. this.checked = undefined;
  143. this.id = id;
  144. this.label = label;
  145. this.class = cssClass;
  146. this._actions = actions;
  147. }
  148. get actions() { return this._actions; }
  149. dispose() {
  150. // there is NOTHING to dispose and the SubmenuAction should
  151. // never have anything to dispose as it is a convenience type
  152. // to bridge into the rendering world.
  153. }
  154. run() {
  155. return __awaiter(this, void 0, void 0, function* () { });
  156. }
  157. }
  158. export class EmptySubmenuAction extends Action {
  159. constructor() {
  160. super(EmptySubmenuAction.ID, nls.localize('submenu.empty', '(empty)'), undefined, false);
  161. }
  162. }
  163. EmptySubmenuAction.ID = 'vs.actions.empty';