controls.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * Copyright (c) 2018-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import * as React from 'react';
  8. import { UpdateTrajectory } from '../mol-plugin-state/actions/structure';
  9. import { LociLabel } from '../mol-plugin-state/manager/loci-label';
  10. import { PluginStateObject } from '../mol-plugin-state/objects';
  11. import { StateTransforms } from '../mol-plugin-state/transforms';
  12. import { ModelFromTrajectory } from '../mol-plugin-state/transforms/model';
  13. import { PluginCommands } from '../mol-plugin/commands';
  14. import { StateTransformer } from '../mol-state';
  15. import { PluginUIComponent } from './base';
  16. import { IconButton } from './controls/common';
  17. import { Icon, NavigateBeforeSvg, NavigateNextSvg, SkipPreviousSvg, StopSvg, PlayArrowSvg, SubscriptionsOutlinedSvg, BuildSvg } from './controls/icons';
  18. import { AnimationControls } from './state/animation';
  19. import { StructureComponentControls } from './structure/components';
  20. import { StructureMeasurementsControls } from './structure/measurements';
  21. import { StructureSelectionActionsControls } from './structure/selection';
  22. import { StructureSourceControls } from './structure/source';
  23. import { VolumeStreamingControls, VolumeSourceControls } from './structure/volume';
  24. import { PluginConfig } from '../mol-plugin/config';
  25. import { StructureSuperpositionControls } from './structure/superposition';
  26. import { StructureQuickStylesControls } from './structure/quick-styles';
  27. export class TrajectoryViewportControls extends PluginUIComponent<{}, { show: boolean, label: string }> {
  28. state = { show: false, label: '' };
  29. private update = () => {
  30. const state = this.plugin.state.data;
  31. const models = state.selectQ(q => q.ofTransformer(StateTransforms.Model.ModelFromTrajectory));
  32. if (models.length === 0) {
  33. this.setState({ show: false });
  34. return;
  35. }
  36. let label = '', count = 0;
  37. const parents = new Set<string>();
  38. for (const m of models) {
  39. if (!m.sourceRef) continue;
  40. const parent = state.cells.get(m.sourceRef)!.obj as PluginStateObject.Molecule.Trajectory;
  41. if (!parent) continue;
  42. if (parent.data.frameCount > 1) {
  43. if (parents.has(m.sourceRef)) {
  44. // do not show the controls if there are 2 models of the same trajectory present
  45. this.setState({ show: false });
  46. return;
  47. }
  48. parents.add(m.sourceRef);
  49. count++;
  50. if (!label) {
  51. const idx = (m.transform.params! as StateTransformer.Params<ModelFromTrajectory>).modelIndex;
  52. label = `Model ${idx + 1} / ${parent.data.frameCount}`;
  53. }
  54. }
  55. }
  56. if (count > 1) label = '';
  57. this.setState({ show: count > 0, label });
  58. };
  59. componentDidMount() {
  60. this.subscribe(this.plugin.state.data.events.changed, this.update);
  61. this.subscribe(this.plugin.behaviors.state.isAnimating, this.update);
  62. }
  63. reset = () => PluginCommands.State.ApplyAction(this.plugin, {
  64. state: this.plugin.state.data,
  65. action: UpdateTrajectory.create({ action: 'reset' })
  66. });
  67. prev = () => PluginCommands.State.ApplyAction(this.plugin, {
  68. state: this.plugin.state.data,
  69. action: UpdateTrajectory.create({ action: 'advance', by: -1 })
  70. });
  71. next = () => PluginCommands.State.ApplyAction(this.plugin, {
  72. state: this.plugin.state.data,
  73. action: UpdateTrajectory.create({ action: 'advance', by: 1 })
  74. });
  75. render() {
  76. const isAnimating = this.plugin.behaviors.state.isAnimating.value;
  77. if (!this.state.show || (isAnimating && !this.state.label) || !this.plugin.config.get(PluginConfig.Viewport.ShowTrajectoryControls)) return null;
  78. return <div className='msp-traj-controls'>
  79. {!isAnimating && <IconButton svg={SkipPreviousSvg} title='First Model' onClick={this.reset} disabled={isAnimating} />}
  80. {!isAnimating && <IconButton svg={NavigateBeforeSvg} title='Previous Model' onClick={this.prev} disabled={isAnimating} />}
  81. {!isAnimating && <IconButton svg={NavigateNextSvg} title='Next Model' onClick={this.next} disabled={isAnimating} />}
  82. {!!this.state.label && <span>{this.state.label}</span> }
  83. </div>;
  84. }
  85. }
  86. export class StateSnapshotViewportControls extends PluginUIComponent<{}, { isBusy: boolean, show: boolean }> {
  87. state = { isBusy: false, show: true };
  88. componentDidMount() {
  89. // TODO: this needs to be diabled when the state is updating!
  90. this.subscribe(this.plugin.managers.snapshot.events.changed, () => this.forceUpdate());
  91. this.subscribe(this.plugin.behaviors.state.isBusy, isBusy => this.setState({ isBusy }));
  92. this.subscribe(this.plugin.behaviors.state.isAnimating, isBusy => this.setState({ isBusy }));
  93. window.addEventListener('keyup', this.keyUp, false);
  94. }
  95. componentWillUnmount() {
  96. super.componentWillUnmount();
  97. window.removeEventListener('keyup', this.keyUp, false);
  98. }
  99. keyUp = (e: KeyboardEvent) => {
  100. if (!e.ctrlKey || this.state.isBusy || e.target !== document.body) return;
  101. const snapshots = this.plugin.managers.snapshot;
  102. if (e.keyCode === 37 || e.key === 'ArrowLeft') {
  103. if (snapshots.state.isPlaying) snapshots.stop();
  104. this.prev();
  105. } else if (e.keyCode === 38 || e.key === 'ArrowUp') {
  106. if (snapshots.state.isPlaying) snapshots.stop();
  107. if (snapshots.state.entries.size === 0) return;
  108. const e = snapshots.state.entries.get(0)!;
  109. this.update(e.snapshot.id);
  110. } else if (e.keyCode === 39 || e.key === 'ArrowRight') {
  111. if (snapshots.state.isPlaying) snapshots.stop();
  112. this.next();
  113. } else if (e.keyCode === 40 || e.key === 'ArrowDown') {
  114. if (snapshots.state.isPlaying) snapshots.stop();
  115. if (snapshots.state.entries.size === 0) return;
  116. const e = snapshots.state.entries.get(snapshots.state.entries.size - 1)!;
  117. this.update(e.snapshot.id);
  118. }
  119. };
  120. async update(id: string) {
  121. this.setState({ isBusy: true });
  122. await PluginCommands.State.Snapshots.Apply(this.plugin, { id });
  123. this.setState({ isBusy: false });
  124. }
  125. change = (e: React.ChangeEvent<HTMLSelectElement>) => {
  126. if (e.target.value === 'none') return;
  127. this.update(e.target.value);
  128. };
  129. prev = () => {
  130. const s = this.plugin.managers.snapshot;
  131. const id = s.getNextId(s.state.current, -1);
  132. if (id) this.update(id);
  133. };
  134. next = () => {
  135. const s = this.plugin.managers.snapshot;
  136. const id = s.getNextId(s.state.current, 1);
  137. if (id) this.update(id);
  138. };
  139. togglePlay = () => {
  140. this.plugin.managers.snapshot.togglePlay();
  141. };
  142. render() {
  143. const snapshots = this.plugin.managers.snapshot;
  144. const count = snapshots.state.entries.size;
  145. if (count < 2 || !this.state.show) {
  146. return null;
  147. }
  148. const current = snapshots.state.current;
  149. const isPlaying = snapshots.state.isPlaying;
  150. return <div className='msp-state-snapshot-viewport-controls'>
  151. <select className='msp-form-control' value={current || 'none'} onChange={this.change} disabled={this.state.isBusy || isPlaying}>
  152. {!current && <option key='none' value='none'></option>}
  153. {snapshots.state.entries.valueSeq().map((e, i) => <option key={e!.snapshot.id} value={e!.snapshot.id}>{`[${i! + 1}/${count}]`} {e!.name || new Date(e!.timestamp).toLocaleString()}</option>)}
  154. </select>
  155. <IconButton svg={isPlaying ? StopSvg : PlayArrowSvg} title={isPlaying ? 'Pause' : 'Cycle States'} onClick={this.togglePlay}
  156. disabled={isPlaying ? false : this.state.isBusy} />
  157. {!isPlaying && <>
  158. <IconButton svg={NavigateBeforeSvg} title='Previous State' onClick={this.prev} disabled={this.state.isBusy || isPlaying} />
  159. <IconButton svg={NavigateNextSvg} title='Next State' onClick={this.next} disabled={this.state.isBusy || isPlaying} />
  160. </>}
  161. </div>;
  162. }
  163. }
  164. export class AnimationViewportControls extends PluginUIComponent<{}, { isEmpty: boolean, isExpanded: boolean, isBusy: boolean, isAnimating: boolean, isPlaying: boolean }> {
  165. state = { isEmpty: true, isExpanded: false, isBusy: false, isAnimating: false, isPlaying: false };
  166. componentDidMount() {
  167. this.subscribe(this.plugin.managers.snapshot.events.changed, () => {
  168. if (this.plugin.managers.snapshot.state.isPlaying) this.setState({ isPlaying: true, isExpanded: false });
  169. else this.setState({ isPlaying: false });
  170. });
  171. this.subscribe(this.plugin.behaviors.state.isBusy, isBusy => {
  172. if (isBusy) this.setState({ isBusy: true, isExpanded: false, isEmpty: this.plugin.state.data.tree.transforms.size < 2 });
  173. else this.setState({ isBusy: false, isEmpty: this.plugin.state.data.tree.transforms.size < 2 });
  174. });
  175. this.subscribe(this.plugin.behaviors.state.isAnimating, isAnimating => {
  176. if (isAnimating) this.setState({ isAnimating: true, isExpanded: false });
  177. else this.setState({ isAnimating: false });
  178. });
  179. }
  180. toggleExpanded = () => this.setState({ isExpanded: !this.state.isExpanded });
  181. stop = () => {
  182. this.plugin.managers.animation.stop();
  183. this.plugin.managers.snapshot.stop();
  184. };
  185. render() {
  186. const isPlaying = this.plugin.managers.snapshot.state.isPlaying;
  187. if (isPlaying || this.state.isEmpty || this.plugin.managers.animation.isEmpty || !this.plugin.config.get(PluginConfig.Viewport.ShowAnimation)) return null;
  188. const isAnimating = this.state.isAnimating;
  189. return <div className='msp-animation-viewport-controls'>
  190. <div>
  191. <div className='msp-semi-transparent-background' />
  192. <IconButton svg={isAnimating || isPlaying ? StopSvg : SubscriptionsOutlinedSvg} transparent title={isAnimating ? 'Stop' : 'Select Animation'}
  193. onClick={isAnimating || isPlaying ? this.stop : this.toggleExpanded} toggleState={this.state.isExpanded}
  194. disabled={isAnimating || isPlaying ? false : this.state.isBusy || this.state.isPlaying || this.state.isEmpty} />
  195. </div>
  196. {(this.state.isExpanded && !this.state.isBusy) && <div className='msp-animation-viewport-controls-select'>
  197. <AnimationControls onStart={this.toggleExpanded} />
  198. </div>}
  199. </div>;
  200. }
  201. }
  202. export class SelectionViewportControls extends PluginUIComponent {
  203. componentDidMount() {
  204. this.subscribe(this.plugin.behaviors.interaction.selectionMode, () => this.forceUpdate());
  205. }
  206. onMouseMove = (e: React.MouseEvent) => {
  207. // ignore mouse moves when no button is held
  208. if (e.buttons === 0) e.stopPropagation();
  209. };
  210. render() {
  211. if (!this.plugin.selectionMode) return null;
  212. return <div className='msp-selection-viewport-controls' onMouseMove={this.onMouseMove}>
  213. <StructureSelectionActionsControls />
  214. </div>;
  215. }
  216. }
  217. export class LociLabels extends PluginUIComponent<{}, { labels: ReadonlyArray<LociLabel> }> {
  218. state = { labels: [] };
  219. componentDidMount() {
  220. this.subscribe(this.plugin.behaviors.labels.highlight, e => this.setState({ labels: e.labels }));
  221. }
  222. render() {
  223. if (this.state.labels.length === 0) {
  224. return null;
  225. }
  226. return <div className='msp-highlight-info'>
  227. {this.state.labels.map((e, i) => <div key={'' + i} dangerouslySetInnerHTML={{ __html: e }} />)}
  228. </div>;
  229. }
  230. }
  231. export class CustomStructureControls extends PluginUIComponent<{ initiallyCollapsed?: boolean }> {
  232. componentDidMount() {
  233. this.subscribe(this.plugin.state.behaviors.events.changed, () => this.forceUpdate());
  234. }
  235. render() {
  236. const controls: JSX.Element[] = [];
  237. this.plugin.customStructureControls.forEach((Controls, key) => {
  238. controls.push(<Controls initiallyCollapsed={this.props.initiallyCollapsed} key={key} />);
  239. });
  240. return controls.length > 0 ? <>{controls}</> : null;
  241. }
  242. }
  243. export class DefaultStructureTools extends PluginUIComponent {
  244. render() {
  245. return <>
  246. <div className='msp-section-header'><Icon svg={BuildSvg} />Structure Tools</div>
  247. <StructureSourceControls />
  248. <StructureMeasurementsControls />
  249. <StructureSuperpositionControls />
  250. <StructureQuickStylesControls />
  251. <StructureComponentControls />
  252. {this.plugin.config.get(PluginConfig.VolumeStreaming.Enabled) && <VolumeStreamingControls />}
  253. <VolumeSourceControls />
  254. <CustomStructureControls />
  255. </>;
  256. }
  257. }