left-panel.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 { IconName } from './controls/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. switch (tab) {
  41. case 'data': this.plugin.state.setKind('data'); break;
  42. case 'settings': this.plugin.state.setKind('behavior'); break;
  43. }
  44. this.setState({ tab }, () => this.plugin.behaviors.layout.leftPanelTabName.next(tab));
  45. if (this.plugin.layout.state.regionState.left !== 'full') {
  46. PluginCommands.Layout.Update(this.plugin, { state: { regionState: { ...this.plugin.layout.state.regionState, left: 'full' } } });
  47. }
  48. }
  49. tabs: { [K in LeftPanelTabName]: JSX.Element } = {
  50. 'none': <></>,
  51. 'root': <>
  52. <SectionHeader icon='home' title='Home' />
  53. <StateObjectActions state={this.plugin.state.data} nodeRef={StateTransform.RootRef} hideHeader={true} initiallyCollapsed={true} alwaysExpandFirst={true} />
  54. {this.plugin.spec.components?.remoteState !== 'none' && <RemoteStateSnapshots listOnly /> }
  55. </>,
  56. 'data': <>
  57. <SectionHeader icon='flow-tree' title={<><RemoveAllButton /> State Tree</>} />
  58. <StateTree state={this.plugin.state.data} />
  59. </>,
  60. 'states': <StateSnapshots />,
  61. 'settings': <>
  62. <SectionHeader icon='settings' title='Plugin Settings' />
  63. <FullSettings />
  64. </>,
  65. 'help': <>
  66. <SectionHeader icon='help-circle' title='Help' />
  67. <HelpContent />
  68. </>
  69. }
  70. render() {
  71. const tab = this.state.tab;
  72. // TODO: show "changed dot" next to the 'data' tab icon indicating the state has changed.
  73. return <div className='msp-left-panel-controls'>
  74. <div className='msp-left-panel-controls-buttons'>
  75. <IconButton icon='home' toggleState={tab === 'root'} onClick={() => this.set('root')} title='Home' />
  76. {/* <IconButton icon='flow-tree' toggleState={tab === 'data'} onClick={() => this.set('data')} title='State Tree' /> */}
  77. <DataIcon set={this.set} />
  78. {this.plugin.spec.components?.remoteState !== 'none' && <IconButton icon='floppy' toggleState={tab === 'states'} onClick={() => this.set('states')} title='Plugin State' />}
  79. <IconButton icon='help-circle' toggleState={tab === 'help'} onClick={() => this.set('help')} title='Help' />
  80. <div className='msp-left-panel-controls-buttons-bottom'>
  81. <IconButton icon='settings' toggleState={tab === 'settings'} onClick={() => this.set('settings')} title='Settings' />
  82. </div>
  83. </div>
  84. <div className='msp-scrollable-container'>
  85. {this.tabs[tab]}
  86. </div>
  87. </div>;
  88. }
  89. }
  90. class DataIcon extends PluginUIComponent<{ set: (tab: LeftPanelTabName) => void }, { changed: boolean }> {
  91. state = { changed: false };
  92. get tab() {
  93. return this.plugin.behaviors.layout.leftPanelTabName.value
  94. }
  95. componentDidMount() {
  96. this.subscribe(this.plugin.behaviors.layout.leftPanelTabName, tab => {
  97. if (this.tab === 'data') this.setState({ changed: false });
  98. else this.forceUpdate();
  99. });
  100. this.subscribe(this.plugin.state.data.events.changed, state => {
  101. if (this.tab !== 'data') this.setState({ changed: true });
  102. });
  103. }
  104. render() {
  105. return <IconButton
  106. icon='flow-tree' toggleState={this.tab === 'data'} onClick={() => this.props.set('data')} title='State Tree'
  107. style={{ position: 'relative' }} extraContent={this.state.changed ? <div className='msp-left-panel-controls-button-data-dirty' /> : void 0} />;
  108. }
  109. }
  110. class FullSettings extends PluginUIComponent {
  111. private setSettings = (p: { param: PD.Base<any>, name: string, value: any }) => {
  112. PluginCommands.Canvas3D.SetSettings(this.plugin, { settings: { [p.name]: p.value } });
  113. }
  114. componentDidMount() {
  115. this.subscribe(this.plugin.events.canvas3d.settingsUpdated, () => this.forceUpdate());
  116. this.subscribe(this.plugin.layout.events.updated, () => this.forceUpdate());
  117. }
  118. icon(name: IconName, onClick: (e: React.MouseEvent<HTMLButtonElement>) => void, title: string, isOn = true) {
  119. return <IconButton icon={name} toggleState={isOn} onClick={onClick} title={title} />;
  120. }
  121. render() {
  122. return <>
  123. {this.plugin.canvas3d && <>
  124. <SectionHeader title='Viewport' />
  125. <ParameterControls params={Canvas3DParams} values={this.plugin.canvas3d.props} onChange={this.setSettings} />
  126. </>}
  127. <SectionHeader title='Behavior' />
  128. <StateTree state={this.plugin.state.behaviors} />
  129. </>
  130. }
  131. }
  132. export class RemoveAllButton extends PluginUIComponent<{ }> {
  133. componentDidMount() {
  134. this.subscribe(this.plugin.events.state.cell.created, e => {
  135. if (e.cell.transform.parent === StateTransform.RootRef) this.forceUpdate();
  136. });
  137. this.subscribe(this.plugin.events.state.cell.removed, e => {
  138. if (e.parent === StateTransform.RootRef) this.forceUpdate();
  139. });
  140. }
  141. remove = (e: React.MouseEvent<HTMLElement>) => {
  142. e.preventDefault();
  143. PluginCommands.State.RemoveObject(this.plugin, { state: this.plugin.state.data, ref: StateTransform.RootRef });
  144. }
  145. render() {
  146. const count = this.plugin.state.data.tree.children.get(StateTransform.RootRef).size;
  147. if (count < 2) return null;
  148. return <IconButton icon='remove' onClick={this.remove} title={'Remove All'} style={{ display: 'inline-block' }} />;
  149. }
  150. }