left-panel.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * Copyright (c) 2019-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 { throttleTime } from 'rxjs';
  9. import { Canvas3DParams } from '../mol-canvas3d/canvas3d';
  10. import { PluginCommands } from '../mol-plugin/commands';
  11. import { LeftPanelTabName } from '../mol-plugin/layout';
  12. import { StateTransform } from '../mol-state';
  13. import { ParamDefinition as PD } from '../mol-util/param-definition';
  14. import { PluginUIComponent } from './base';
  15. import { IconButton, SectionHeader } from './controls/common';
  16. import { AccountTreeOutlinedSvg, DeleteOutlinedSvg, HelpOutlineSvg, HomeOutlinedSvg, SaveOutlinedSvg, TuneSvg } from './controls/icons';
  17. import { ParameterControls } from './controls/parameters';
  18. import { StateObjectActions } from './state/actions';
  19. import { RemoteStateSnapshots, StateSnapshots } from './state/snapshots';
  20. import { StateTree } from './state/tree';
  21. import { HelpContent } from './viewport/help';
  22. export class CustomImportControls extends PluginUIComponent<{ initiallyCollapsed?: boolean }> {
  23. componentDidMount() {
  24. this.subscribe(this.plugin.state.behaviors.events.changed, () => this.forceUpdate());
  25. }
  26. render() {
  27. const controls: JSX.Element[] = [];
  28. this.plugin.customImportControls.forEach((Controls, key) => {
  29. controls.push(<Controls initiallyCollapsed={this.props.initiallyCollapsed} key={key} />);
  30. });
  31. return controls.length > 0 ? <>{controls}</> : null;
  32. }
  33. }
  34. export class LeftPanelControls extends PluginUIComponent<{}, { tab: LeftPanelTabName }> {
  35. state = { tab: this.plugin.behaviors.layout.leftPanelTabName.value };
  36. componentDidMount() {
  37. this.subscribe(this.plugin.behaviors.layout.leftPanelTabName, tab => {
  38. if (this.state.tab !== tab) this.setState({ tab });
  39. if (tab === 'none' && this.plugin.layout.state.regionState.left !== 'collapsed') {
  40. PluginCommands.Layout.Update(this.plugin, { state: { regionState: { ...this.plugin.layout.state.regionState, left: 'collapsed' } } });
  41. }
  42. });
  43. this.subscribe(this.plugin.state.data.events.changed, ({ state }) => {
  44. if (this.state.tab !== 'data') return;
  45. if (state.cells.size === 1) this.set('root');
  46. });
  47. }
  48. set = (tab: LeftPanelTabName) => {
  49. if (this.state.tab === tab) {
  50. this.setState({ tab: 'none' }, () => this.plugin.behaviors.layout.leftPanelTabName.next('none'));
  51. PluginCommands.Layout.Update(this.plugin, { state: { regionState: { ...this.plugin.layout.state.regionState, left: 'collapsed' } } });
  52. return;
  53. }
  54. this.setState({ tab }, () => this.plugin.behaviors.layout.leftPanelTabName.next(tab));
  55. if (this.plugin.layout.state.regionState.left !== 'full') {
  56. PluginCommands.Layout.Update(this.plugin, { state: { regionState: { ...this.plugin.layout.state.regionState, left: 'full' } } });
  57. }
  58. };
  59. tabs: { [K in LeftPanelTabName]: JSX.Element } = {
  60. 'none': <></>,
  61. 'root': <>
  62. <SectionHeader icon={HomeOutlinedSvg} title='Home' />
  63. <StateObjectActions state={this.plugin.state.data} nodeRef={StateTransform.RootRef} hideHeader={true} initiallyCollapsed={true} alwaysExpandFirst={true} />
  64. <CustomImportControls />
  65. {this.plugin.spec.components?.remoteState !== 'none' && <RemoteStateSnapshots listOnly /> }
  66. </>,
  67. 'data': <>
  68. <SectionHeader icon={AccountTreeOutlinedSvg} title={<><RemoveAllButton /> State Tree</>} />
  69. <StateTree state={this.plugin.state.data} />
  70. </>,
  71. 'states': <StateSnapshots />,
  72. 'settings': <>
  73. <SectionHeader icon={TuneSvg} title='Plugin Settings' />
  74. <FullSettings />
  75. </>,
  76. 'help': <>
  77. <SectionHeader icon={HelpOutlineSvg} title='Help' />
  78. <HelpContent />
  79. </>
  80. };
  81. render() {
  82. const tab = this.state.tab;
  83. return <div className='msp-left-panel-controls'>
  84. <div className='msp-left-panel-controls-buttons'>
  85. <IconButton svg={HomeOutlinedSvg} toggleState={tab === 'root'} transparent onClick={() => this.set('root')} title='Home' />
  86. <DataIcon set={this.set} />
  87. <IconButton svg={SaveOutlinedSvg} toggleState={tab === 'states'} transparent onClick={() => this.set('states')} title='Plugin State' />
  88. <IconButton svg={HelpOutlineSvg} toggleState={tab === 'help'} transparent onClick={() => this.set('help')} title='Help' />
  89. <div className='msp-left-panel-controls-buttons-bottom'>
  90. <IconButton svg={TuneSvg} toggleState={tab === 'settings'} transparent onClick={() => this.set('settings')} title='Settings' />
  91. </div>
  92. </div>
  93. <div className='msp-scrollable-container'>
  94. {this.tabs[tab]}
  95. </div>
  96. </div>;
  97. }
  98. }
  99. class DataIcon extends PluginUIComponent<{ set: (tab: LeftPanelTabName) => void }, { changed: boolean }> {
  100. state = { changed: false };
  101. get tab() {
  102. return this.plugin.behaviors.layout.leftPanelTabName.value;
  103. }
  104. componentDidMount() {
  105. this.subscribe(this.plugin.behaviors.layout.leftPanelTabName, tab => {
  106. if (this.tab === 'data') this.setState({ changed: false });
  107. else this.forceUpdate();
  108. });
  109. this.subscribe(this.plugin.state.data.events.changed, state => {
  110. if (this.tab !== 'data') this.setState({ changed: true });
  111. });
  112. }
  113. render() {
  114. return <IconButton
  115. svg={AccountTreeOutlinedSvg} toggleState={this.tab === 'data'} transparent onClick={() => this.props.set('data')} title='State Tree'
  116. style={{ position: 'relative' }} extraContent={this.state.changed ? <div className='msp-left-panel-controls-button-data-dirty' /> : void 0} />;
  117. }
  118. }
  119. class FullSettings extends PluginUIComponent {
  120. private setSettings = (p: { param: PD.Base<any>, name: string, value: any }) => {
  121. PluginCommands.Canvas3D.SetSettings(this.plugin, { settings: { [p.name]: p.value } });
  122. };
  123. componentDidMount() {
  124. this.subscribe(this.plugin.events.canvas3d.settingsUpdated, () => this.forceUpdate());
  125. this.subscribe(this.plugin.layout.events.updated, () => this.forceUpdate());
  126. if (this.plugin.canvas3d) {
  127. this.subscribe(this.plugin.canvas3d.camera.stateChanged.pipe(throttleTime(500, undefined, { leading: true, trailing: true })), state => {
  128. if (state.radiusMax !== undefined || state.radius !== undefined) {
  129. this.forceUpdate();
  130. }
  131. });
  132. }
  133. }
  134. render() {
  135. return <>
  136. {this.plugin.canvas3d && <>
  137. <SectionHeader title='Viewport' />
  138. <ParameterControls params={Canvas3DParams} values={this.plugin.canvas3d.props} onChange={this.setSettings} />
  139. </>}
  140. <SectionHeader title='Behavior' />
  141. <StateTree state={this.plugin.state.behaviors} />
  142. </>;
  143. }
  144. }
  145. class RemoveAllButton extends PluginUIComponent<{ }> {
  146. componentDidMount() {
  147. this.subscribe(this.plugin.state.events.cell.created, e => {
  148. if (e.cell.transform.parent === StateTransform.RootRef) this.forceUpdate();
  149. });
  150. this.subscribe(this.plugin.state.events.cell.removed, e => {
  151. if (e.parent === StateTransform.RootRef) this.forceUpdate();
  152. });
  153. }
  154. remove = (e: React.MouseEvent<HTMLElement>) => {
  155. e.preventDefault();
  156. PluginCommands.State.RemoveObject(this.plugin, { state: this.plugin.state.data, ref: StateTransform.RootRef });
  157. };
  158. render() {
  159. const count = this.plugin.state.data.tree.children.get(StateTransform.RootRef).size;
  160. if (count === 0) return null;
  161. return <IconButton svg={DeleteOutlinedSvg} onClick={this.remove} title={'Remove All'} style={{ display: 'inline-block' }} small className='msp-no-hover-outline' transparent />;
  162. }
  163. }