plugin.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * Copyright (c) 2018-2019 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 { List } from 'immutable';
  8. import { PluginState } from '../../mol-plugin/state';
  9. import { formatTime } from '../../mol-util';
  10. import { LogEntry } from '../../mol-util/log-entry';
  11. import * as React from 'react';
  12. import { PluginContext } from '../context';
  13. import { PluginReactContext, PluginUIComponent } from './base';
  14. import { LociLabels, TrajectoryViewportControls, StateSnapshotViewportControls, AnimationViewportControls, StructureToolsWrapper } from './controls';
  15. import { StateSnapshots } from './state';
  16. import { StateObjectActions } from './state/actions';
  17. import { StateTree } from './state/tree';
  18. import { BackgroundTaskProgress } from './task';
  19. import { Viewport, ViewportControls } from './viewport';
  20. import { StateTransform } from '../../mol-state';
  21. import { UpdateTransformControl } from './state/update-transform';
  22. import { SequenceView } from './sequence';
  23. import { Toasts } from './toast';
  24. import { ImageControls } from './image';
  25. export class Plugin extends React.Component<{ plugin: PluginContext }, {}> {
  26. region(kind: 'left' | 'right' | 'bottom' | 'main', element: JSX.Element) {
  27. return <div className={`msp-layout-region msp-layout-${kind}`}>
  28. <div className='msp-layout-static'>
  29. {element}
  30. </div>
  31. </div>
  32. }
  33. render() {
  34. return <PluginReactContext.Provider value={this.props.plugin}>
  35. <Layout />
  36. </PluginReactContext.Provider>;
  37. }
  38. }
  39. export class PluginContextContainer extends React.Component<{ plugin: PluginContext }> {
  40. render() {
  41. return <PluginReactContext.Provider value={this.props.plugin}>
  42. <div className='msp-plugin'>
  43. {this.props.children}
  44. </div>
  45. </PluginReactContext.Provider>;
  46. }
  47. }
  48. type RegionKind = 'top' | 'left' | 'right' | 'bottom' | 'main'
  49. class Layout extends PluginUIComponent {
  50. componentDidMount() {
  51. this.subscribe(this.plugin.layout.events.updated, () => this.forceUpdate());
  52. }
  53. region(kind: RegionKind, Element?: React.ComponentClass) {
  54. return <div className={`msp-layout-region msp-layout-${kind}`}>
  55. <div className='msp-layout-static'>
  56. {Element ? <Element /> : null}
  57. </div>
  58. </div>;
  59. }
  60. get layoutVisibilityClassName() {
  61. const layout = this.plugin.layout.state;
  62. const controls = (this.plugin.spec.layout && this.plugin.spec.layout.controls) || { };
  63. const classList: string[] = []
  64. if (controls.top === 'none' || !layout.showControls) {
  65. classList.push('msp-layout-hide-top')
  66. }
  67. if (controls.left === 'none' || !layout.showControls) {
  68. classList.push('msp-layout-hide-left')
  69. }
  70. if (controls.right === 'none' || !layout.showControls) {
  71. classList.push('msp-layout-hide-right')
  72. }
  73. if (controls.bottom === 'none' || !layout.showControls) {
  74. classList.push('msp-layout-hide-bottom')
  75. }
  76. return classList.join(' ')
  77. }
  78. get layoutClassName() {
  79. const layout = this.plugin.layout.state;
  80. const classList: string[] = ['msp-plugin-content']
  81. if (layout.isExpanded) {
  82. classList.push('msp-layout-expanded')
  83. } else {
  84. classList.push('msp-layout-standard', `msp-layout-standard-${layout.controlsDisplay}`)
  85. }
  86. return classList.join(' ')
  87. }
  88. render() {
  89. const layout = this.plugin.layout.state;
  90. const controls = (this.plugin.spec.layout && this.plugin.spec.layout.controls) || { };
  91. const viewport = (this.plugin.spec.layout && this.plugin.spec.layout.viewport) || ViewportWrapper;
  92. return <div className='msp-plugin'>
  93. <div className={this.layoutClassName}>
  94. <div className={this.layoutVisibilityClassName}>
  95. {this.region('main', viewport)}
  96. {layout.showControls && controls.top !== 'none' && this.region('top', controls.top || SequenceView)}
  97. {layout.showControls && controls.left !== 'none' && this.region('left', controls.left || State)}
  98. {layout.showControls && controls.right !== 'none' && this.region('right', controls.right || ControlsWrapper)}
  99. {layout.showControls && controls.bottom !== 'none' && this.region('bottom', controls.bottom || Log)}
  100. </div>
  101. </div>
  102. </div>;
  103. }
  104. }
  105. export class ControlsWrapper extends PluginUIComponent {
  106. render() {
  107. return <div className='msp-scrollable-container msp-right-controls'>
  108. <CurrentObject />
  109. {/* <AnimationControlsWrapper /> */}
  110. {/* <CameraSnapshots /> */}
  111. <StructureToolsWrapper />
  112. <ImageControls />
  113. <StateSnapshots />
  114. </div>;
  115. }
  116. }
  117. export class ViewportWrapper extends PluginUIComponent {
  118. render() {
  119. return <>
  120. <Viewport />
  121. <div className='msp-viewport-top-left-controls'>
  122. <AnimationViewportControls />
  123. <TrajectoryViewportControls />
  124. <StateSnapshotViewportControls />
  125. </div>
  126. <ViewportControls />
  127. <div style={{ position: 'absolute', left: '10px', bottom: '10px' }}>
  128. <BackgroundTaskProgress />
  129. </div>
  130. <div className='msp-highlight-toast-wrapper'>
  131. <LociLabels />
  132. <Toasts />
  133. </div>
  134. </>;
  135. }
  136. }
  137. export class State extends PluginUIComponent {
  138. componentDidMount() {
  139. this.subscribe(this.plugin.state.behavior.kind, () => this.forceUpdate());
  140. }
  141. set(kind: PluginState.Kind) {
  142. // TODO: do command for this?
  143. this.plugin.state.setKind(kind);
  144. }
  145. render() {
  146. const kind = this.plugin.state.behavior.kind.value;
  147. return <div className='msp-scrollable-container'>
  148. <div className='msp-btn-row-group msp-data-beh'>
  149. <button className='msp-btn msp-btn-block msp-form-control' onClick={() => this.set('data')} style={{ fontWeight: kind === 'data' ? 'bold' : 'normal' }}>
  150. <span className='msp-icon msp-icon-database' /> Data
  151. </button>
  152. <button className='msp-btn msp-btn-block msp-form-control' onClick={() => this.set('behavior')} style={{ fontWeight: kind === 'behavior' ? 'bold' : 'normal' }}>
  153. <span className='msp-icon msp-icon-tools' /> Behavior
  154. </button>
  155. </div>
  156. <StateTree state={kind === 'data' ? this.plugin.state.dataState : this.plugin.state.behaviorState} />
  157. </div>
  158. }
  159. }
  160. export class Log extends PluginUIComponent<{}, { entries: List<LogEntry> }> {
  161. private wrapper = React.createRef<HTMLDivElement>();
  162. componentDidMount() {
  163. this.subscribe(this.plugin.events.log, () => this.setState({ entries: this.plugin.log.entries }));
  164. }
  165. componentDidUpdate() {
  166. this.scrollToBottom();
  167. }
  168. state = { entries: this.plugin.log.entries };
  169. private scrollToBottom() {
  170. const log = this.wrapper.current;
  171. if (log) log.scrollTop = log.scrollHeight - log.clientHeight - 1;
  172. }
  173. render() {
  174. // TODO: ability to show full log
  175. // showing more entries dramatically slows animations.
  176. const maxEntries = 10;
  177. const xs = this.state.entries, l = xs.size;
  178. const entries: JSX.Element[] = [];
  179. for (let i = Math.max(0, l - maxEntries), o = 0; i < l; i++) {
  180. const e = xs.get(i);
  181. entries.push(<li key={o++}>
  182. <div className={'msp-log-entry-badge msp-log-entry-' + e!.type} />
  183. <div className='msp-log-timestamp'>{formatTime(e!.timestamp)}</div>
  184. <div className='msp-log-entry'>{e!.message}</div>
  185. </li>);
  186. }
  187. return <div ref={this.wrapper} className='msp-log' style={{ position: 'absolute', top: '0', right: '0', bottom: '0', left: '0', overflowY: 'auto' }}>
  188. <ul className='msp-list-unstyled'>{entries}</ul>
  189. </div>;
  190. }
  191. }
  192. export class CurrentObject extends PluginUIComponent {
  193. get current() {
  194. return this.plugin.state.behavior.currentObject.value;
  195. }
  196. componentDidMount() {
  197. this.subscribe(this.plugin.state.behavior.currentObject, o => {
  198. this.forceUpdate();
  199. });
  200. this.subscribe(this.plugin.events.state.object.updated, ({ ref, state }) => {
  201. const current = this.current;
  202. if (current.ref !== ref || current.state !== state) return;
  203. this.forceUpdate();
  204. });
  205. }
  206. render() {
  207. const current = this.current;
  208. const ref = current.ref;
  209. const cell = current.state.cells.get(ref)!;
  210. const transform = cell.transform;
  211. let showActions = true;
  212. if (ref === StateTransform.RootRef) {
  213. const children = current.state.tree.children.get(ref);
  214. showActions = children.size !== 0;
  215. }
  216. if (!showActions) return null;
  217. return <>
  218. {(cell.status === 'ok' || cell.status === 'error') && <UpdateTransformControl state={current.state} transform={transform} /> }
  219. {cell.status === 'ok' && <StateObjectActions state={current.state} nodeRef={ref} initiallyColapsed />}
  220. </>;
  221. }
  222. }