left-panel.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 { Canvas3DParams } from '../mol-canvas3d/canvas3d';
  8. import { PluginCommands } from '../mol-plugin/commands';
  9. import { LeftPanelTabName } from '../mol-plugin/layout';
  10. import { StateTransform } from '../mol-state';
  11. import { ParamDefinition as PD } from '../mol-util/param-definition';
  12. import { PluginUIComponent } from './base';
  13. import { IconButton, SectionHeader } from './controls/common';
  14. import { ParameterControls } from './controls/parameters';
  15. import { StateObjectActions } from './state/actions';
  16. import { RemoteStateSnapshots, StateSnapshots } from './state/snapshots';
  17. import { StateTree } from './state/tree';
  18. import { HelpContent } from './viewport/help';
  19. import { HomeOutlinedSvg, AccountTreeOutlinedSvg, TuneSvg, HelpOutlineSvg, SaveOutlinedSvg, DeleteOutlinedSvg } 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. 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={HomeOutlinedSvg} 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={AccountTreeOutlinedSvg} title={<><RemoveAllButton /> State Tree</>} />
  54. <StateTree state={this.plugin.state.data} />
  55. </>,
  56. 'states': <StateSnapshots />,
  57. 'settings': <>
  58. <SectionHeader icon={TuneSvg} title='Plugin Settings' />
  59. <FullSettings />
  60. </>,
  61. 'help': <>
  62. <SectionHeader icon={HelpOutlineSvg} title='Help' />
  63. <HelpContent />
  64. </>
  65. }
  66. render() {
  67. const tab = this.state.tab;
  68. return <div className='msp-left-panel-controls'>
  69. <div className='msp-left-panel-controls-buttons'>
  70. <IconButton svg={HomeOutlinedSvg} toggleState={tab === 'root'} transparent onClick={() => this.set('root')} title='Home' />
  71. <DataIcon set={this.set} />
  72. <IconButton svg={SaveOutlinedSvg} toggleState={tab === 'states'} transparent onClick={() => this.set('states')} title='Plugin State' />
  73. <IconButton svg={HelpOutlineSvg} toggleState={tab === 'help'} transparent onClick={() => this.set('help')} title='Help' />
  74. <div className='msp-left-panel-controls-buttons-bottom'>
  75. <IconButton svg={TuneSvg} toggleState={tab === 'settings'} transparent onClick={() => this.set('settings')} title='Settings' />
  76. </div>
  77. </div>
  78. <div className='msp-scrollable-container'>
  79. {this.tabs[tab]}
  80. </div>
  81. </div>;
  82. }
  83. }
  84. class DataIcon extends PluginUIComponent<{ set: (tab: LeftPanelTabName) => void }, { changed: boolean }> {
  85. state = { changed: false };
  86. get tab() {
  87. return this.plugin.behaviors.layout.leftPanelTabName.value;
  88. }
  89. componentDidMount() {
  90. this.subscribe(this.plugin.behaviors.layout.leftPanelTabName, tab => {
  91. if (this.tab === 'data') this.setState({ changed: false });
  92. else this.forceUpdate();
  93. });
  94. this.subscribe(this.plugin.state.data.events.changed, state => {
  95. if (this.tab !== 'data') this.setState({ changed: true });
  96. });
  97. }
  98. render() {
  99. return <IconButton
  100. svg={AccountTreeOutlinedSvg} toggleState={this.tab === 'data'} transparent onClick={() => this.props.set('data')} title='State Tree'
  101. style={{ position: 'relative' }} extraContent={this.state.changed ? <div className='msp-left-panel-controls-button-data-dirty' /> : void 0} />;
  102. }
  103. }
  104. class FullSettings extends PluginUIComponent {
  105. private setSettings = (p: { param: PD.Base<any>, name: string, value: any }) => {
  106. PluginCommands.Canvas3D.SetSettings(this.plugin, { settings: { [p.name]: p.value } });
  107. }
  108. componentDidMount() {
  109. this.subscribe(this.plugin.events.canvas3d.settingsUpdated, () => this.forceUpdate());
  110. this.subscribe(this.plugin.layout.events.updated, () => this.forceUpdate());
  111. this.subscribe(this.plugin.canvas3d!.camera.stateChanged, state => {
  112. if (state.radiusMax !== undefined || state.radius !== undefined) {
  113. this.forceUpdate();
  114. }
  115. });
  116. }
  117. render() {
  118. return <>
  119. {this.plugin.canvas3d && <>
  120. <SectionHeader title='Viewport' />
  121. <ParameterControls params={Canvas3DParams} values={this.plugin.canvas3d.props} onChange={this.setSettings} />
  122. </>}
  123. <SectionHeader title='Behavior' />
  124. <StateTree state={this.plugin.state.behaviors} />
  125. </>;
  126. }
  127. }
  128. class RemoveAllButton extends PluginUIComponent<{ }> {
  129. componentDidMount() {
  130. this.subscribe(this.plugin.state.events.cell.created, e => {
  131. if (e.cell.transform.parent === StateTransform.RootRef) this.forceUpdate();
  132. });
  133. this.subscribe(this.plugin.state.events.cell.removed, e => {
  134. if (e.parent === StateTransform.RootRef) this.forceUpdate();
  135. });
  136. }
  137. remove = (e: React.MouseEvent<HTMLElement>) => {
  138. e.preventDefault();
  139. PluginCommands.State.RemoveObject(this.plugin, { state: this.plugin.state.data, ref: StateTransform.RootRef });
  140. }
  141. render() {
  142. const count = this.plugin.state.data.tree.children.get(StateTransform.RootRef).size;
  143. if (count === 0) return null;
  144. return <IconButton svg={DeleteOutlinedSvg} onClick={this.remove} title={'Remove All'} style={{ display: 'inline-block' }} small className='msp-no-hover-outline' transparent />;
  145. }
  146. }