controls.tsx 13 KB

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