toolbar.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. IIterator, find, map
  5. } from '@phosphor/algorithm';
  6. import {
  7. Message
  8. } from '@phosphor/messaging';
  9. import {
  10. AttachedProperty
  11. } from '@phosphor/properties';
  12. import {
  13. PanelLayout, Widget
  14. } from '@phosphor/widgets';
  15. import {
  16. IClientSession
  17. } from '.';
  18. /**
  19. * The class name added to toolbars.
  20. */
  21. const TOOLBAR_CLASS = 'jp-Toolbar';
  22. /**
  23. * The class name added to toolbar items.
  24. */
  25. const TOOLBAR_ITEM_CLASS = 'jp-Toolbar-item';
  26. /**
  27. * The class name added to toolbar buttons.
  28. */
  29. const TOOLBAR_BUTTON_CLASS = 'jp-Toolbar-button';
  30. /**
  31. * The class name added to a pressed button.
  32. */
  33. const TOOLBAR_PRESSED_CLASS = 'jp-mod-pressed';
  34. /**
  35. * The class name added to toolbar interrupt button.
  36. */
  37. const TOOLBAR_INTERRUPT_CLASS = 'jp-StopIcon';
  38. /**
  39. * The class name added to toolbar restart button.
  40. */
  41. const TOOLBAR_RESTART_CLASS = 'jp-RefreshIcon';
  42. /**
  43. * The class name added to toolbar kernel name text.
  44. */
  45. const TOOLBAR_KERNEL_CLASS = 'jp-Toolbar-kernelName';
  46. /**
  47. * The class name added to toolbar kernel indicator icon.
  48. */
  49. const TOOLBAR_INDICATOR_CLASS = 'jp-Toolbar-kernelIndicator';
  50. /**
  51. * The class name added to a busy kernel indicator.
  52. */
  53. const TOOLBAR_BUSY_CLASS = 'jp-mod-busy';
  54. /**
  55. * A class which provides a toolbar widget.
  56. */
  57. export
  58. class Toolbar<T extends Widget> extends Widget {
  59. /**
  60. * Construct a new toolbar widget.
  61. */
  62. constructor() {
  63. super();
  64. this.addClass(TOOLBAR_CLASS);
  65. this.layout = new PanelLayout();
  66. }
  67. /**
  68. * Get an iterator over the ordered toolbar item names.
  69. *
  70. * @returns An iterator over the toolbar item names.
  71. */
  72. names(): IIterator<string> {
  73. let layout = this.layout as PanelLayout;
  74. return map(layout.widgets, widget => {
  75. return Private.nameProperty.get(widget);
  76. });
  77. }
  78. /**
  79. * Add an item to the end of the toolbar.
  80. *
  81. * @param name - The name of the widget to add to the toolbar.
  82. *
  83. * @param widget - The widget to add to the toolbar.
  84. *
  85. * @param index - The optional name of the item to insert after.
  86. *
  87. * @returns Whether the item was added to toolbar. Returns false if
  88. * an item of the same name is already in the toolbar.
  89. */
  90. addItem(name: string, widget: T): boolean {
  91. let layout = this.layout as PanelLayout;
  92. return this.insertItem(layout.widgets.length, name, widget);
  93. }
  94. /**
  95. * Insert an item into the toolbar at the specified index.
  96. *
  97. * @param index - The index at which to insert the item.
  98. *
  99. * @param name - The name of the item.
  100. *
  101. * @param widget - The widget to add.
  102. *
  103. * @returns Whether the item was added to the toolbar. Returns false if
  104. * an item of the same name is already in the toolbar.
  105. *
  106. * #### Notes
  107. * The index will be clamped to the bounds of the items.
  108. */
  109. insertItem(index: number, name: string, widget: T): boolean {
  110. let existing = find(this.names(), value => value === name);
  111. if (existing) {
  112. return false;
  113. }
  114. widget.addClass(TOOLBAR_ITEM_CLASS);
  115. let layout = this.layout as PanelLayout;
  116. layout.insertWidget(index, widget);
  117. Private.nameProperty.set(widget, name);
  118. return true;
  119. }
  120. /**
  121. * Remove an item in the toolbar by value.
  122. *
  123. * @param name - The name of the widget to remove from the toolbar.
  124. */
  125. removeItem(widget: T): void {
  126. let layout = this.layout as PanelLayout;
  127. layout.removeWidget(widget);
  128. }
  129. /**
  130. * Handle the DOM events for the widget.
  131. *
  132. * @param event - The DOM event sent to the widget.
  133. *
  134. * #### Notes
  135. * This method implements the DOM `EventListener` interface and is
  136. * called in response to events on the dock panel's node. It should
  137. * not be called directly by user code.
  138. */
  139. handleEvent(event: Event): void {
  140. switch (event.type) {
  141. case 'click':
  142. if (!this.node.contains(document.activeElement)) {
  143. this.parent.activate();
  144. }
  145. break;
  146. default:
  147. break;
  148. }
  149. }
  150. /**
  151. * Handle `after-attach` messages for the widget.
  152. */
  153. protected onAfterAttach(msg: Message): void {
  154. this.node.addEventListener('click', this);
  155. }
  156. /**
  157. * Handle `before-detach` messages for the widget.
  158. */
  159. protected onBeforeDetach(msg: Message): void {
  160. this.node.removeEventListener('click', this);
  161. }
  162. }
  163. /**
  164. * The namespace for Toolbar class statics.
  165. */
  166. export
  167. namespace Toolbar {
  168. /**
  169. * Create an interrupt toolbar item.
  170. */
  171. export
  172. function createInterruptButton(session: IClientSession): ToolbarButton {
  173. return new ToolbarButton({
  174. className: TOOLBAR_INTERRUPT_CLASS,
  175. onClick: () => {
  176. if (session.kernel) {
  177. session.kernel.interrupt();
  178. }
  179. },
  180. tooltip: 'Interrupt the kernel'
  181. });
  182. }
  183. /**
  184. * Create a restart toolbar item.
  185. */
  186. export
  187. function createRestartButton(session: IClientSession): ToolbarButton {
  188. return new ToolbarButton({
  189. className: TOOLBAR_RESTART_CLASS,
  190. onClick: () => {
  191. session.restart();
  192. },
  193. tooltip: 'Restart the kernel'
  194. });
  195. }
  196. /**
  197. * Create a kernel name indicator item.
  198. *
  199. * #### Notes
  200. * It will display the `'display_name`' of the current kernel,
  201. * or `'No Kernel!'` if there is no kernel.
  202. * It can handle a change in context or kernel.
  203. */
  204. export
  205. function createKernelNameItem(session: IClientSession): Widget {
  206. return new Private.KernelName(session);
  207. }
  208. /**
  209. * Create a kernel status indicator item.
  210. *
  211. * #### Notes
  212. * It show display a busy status if the kernel status is
  213. * not idle.
  214. * It will show the current status in the node title.
  215. * It can handle a change to the context or the kernel.
  216. */
  217. export
  218. function createKernelStatusItem(session: IClientSession): Widget {
  219. return new Private.KernelIndicator(session);
  220. }
  221. }
  222. /**
  223. * A widget which acts as a button in a toolbar.
  224. */
  225. export
  226. class ToolbarButton extends Widget {
  227. /**
  228. * Construct a new toolbar button.
  229. */
  230. constructor(options: ToolbarButton.IOptions = {}) {
  231. super({ node: document.createElement('span') });
  232. options = options || {};
  233. this.addClass(TOOLBAR_BUTTON_CLASS);
  234. this._onClick = options.onClick;
  235. if (options.className) {
  236. this.addClass(options.className);
  237. }
  238. this.node.title = options.tooltip || '';
  239. }
  240. /**
  241. * Dispose of the resources held by the widget.
  242. */
  243. dispose(): void {
  244. this._onClick = null;
  245. super.dispose();
  246. }
  247. /**
  248. * Handle the DOM events for the widget.
  249. *
  250. * @param event - The DOM event sent to the widget.
  251. *
  252. * #### Notes
  253. * This method implements the DOM `EventListener` interface and is
  254. * called in response to events on the dock panel's node. It should
  255. * not be called directly by user code.
  256. */
  257. handleEvent(event: Event): void {
  258. switch (event.type) {
  259. case 'click':
  260. if (this._onClick) {
  261. this._onClick();
  262. }
  263. break;
  264. case 'mousedown':
  265. this.addClass(TOOLBAR_PRESSED_CLASS);
  266. break;
  267. case 'mouseup':
  268. case 'mouseout':
  269. this.removeClass(TOOLBAR_PRESSED_CLASS);
  270. break;
  271. default:
  272. break;
  273. }
  274. }
  275. /**
  276. * Handle `after-attach` messages for the widget.
  277. */
  278. protected onAfterAttach(msg: Message): void {
  279. this.node.addEventListener('click', this);
  280. this.node.addEventListener('mousedown', this);
  281. this.node.addEventListener('mouseup', this);
  282. this.node.addEventListener('mouseout', this);
  283. }
  284. /**
  285. * Handle `before-detach` messages for the widget.
  286. */
  287. protected onBeforeDetach(msg: Message): void {
  288. this.node.removeEventListener('click', this);
  289. this.node.removeEventListener('mousedown', this);
  290. this.node.removeEventListener('mouseup', this);
  291. this.node.removeEventListener('mouseout', this);
  292. }
  293. private _onClick: () => void;
  294. }
  295. /**
  296. * A namespace for `ToolbarButton` statics.
  297. */
  298. export
  299. namespace ToolbarButton {
  300. /**
  301. * The options used to construct a toolbar button.
  302. */
  303. export
  304. interface IOptions {
  305. /**
  306. * The callback for a click event.
  307. */
  308. onClick?: () => void;
  309. /**
  310. * The class name added to the button.
  311. */
  312. className?: string;
  313. /**
  314. * The tooltip added to the button node.
  315. */
  316. tooltip?: string;
  317. }
  318. }
  319. /**
  320. * A namespace for private data.
  321. */
  322. namespace Private {
  323. /**
  324. * An attached property for the name of a toolbar item.
  325. */
  326. export
  327. const nameProperty = new AttachedProperty<Widget, string>({
  328. name: 'name',
  329. create: () => ''
  330. });
  331. /**
  332. * A kernel name widget.
  333. */
  334. export
  335. class KernelName extends Widget {
  336. /**
  337. * Construct a new kernel name widget.
  338. */
  339. constructor(session: IClientSession) {
  340. super();
  341. this.addClass(TOOLBAR_KERNEL_CLASS);
  342. this._onKernelChanged(session);
  343. session.kernelChanged.connect(this._onKernelChanged, this);
  344. }
  345. /**
  346. * Update the text of the kernel name item.
  347. */
  348. _onKernelChanged(session: IClientSession): void {
  349. this.node.textContent = session.kernelDisplayName;
  350. }
  351. }
  352. /**
  353. * A toolbar item that displays kernel status.
  354. */
  355. export
  356. class KernelIndicator extends Widget {
  357. /**
  358. * Construct a new kernel status widget.
  359. */
  360. constructor(session: IClientSession) {
  361. super();
  362. this.addClass(TOOLBAR_INDICATOR_CLASS);
  363. this._onStatusChanged(session);
  364. session.statusChanged.connect(this._onStatusChanged, this);
  365. }
  366. /**
  367. * Handle a status on a kernel.
  368. */
  369. private _onStatusChanged(session: IClientSession) {
  370. if (this.isDisposed) {
  371. return;
  372. }
  373. let status = session.status;
  374. this.toggleClass(TOOLBAR_BUSY_CLASS, status !== 'idle');
  375. let title = 'Kernel ' + status[0].toUpperCase() + status.slice(1);
  376. this.node.title = title;
  377. }
  378. }
  379. }