actions.js 5.2 KB

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