left-panel.tsx 8.0 KB

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