left-panel.tsx 7.4 KB

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