controls.tsx 13 KB

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