volume.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Copyright (c) 2020 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 { InitVolumeStreaming } from '../../mol-plugin/behavior/dynamic/volume-streaming/transformers';
  9. import { CollapsableControls, CollapsableState } from '../base';
  10. import { ApplyActionControl } from '../state/apply-action';
  11. import { UpdateTransformControl } from '../state/update-transform';
  12. import { BindingsHelp } from '../viewport/help';
  13. import { ExpandGroup } from '../controls/common';
  14. import { StructureHierarchyManager } from '../../mol-plugin-state/manager/structure/hierarchy';
  15. import { FocusLoci } from '../../mol-plugin/behavior/dynamic/representation';
  16. import { StateSelection, StateTransform } from '../../mol-state';
  17. import { VolumeStreaming } from '../../mol-plugin/behavior/dynamic/volume-streaming/behavior';
  18. interface VolumeStreamingControlState extends CollapsableState {
  19. isBusy: boolean
  20. }
  21. export class VolumeStreamingControls extends CollapsableControls<{}, VolumeStreamingControlState> {
  22. protected defaultState(): VolumeStreamingControlState {
  23. return {
  24. header: 'Volume Streaming',
  25. isCollapsed: false,
  26. isBusy: false,
  27. isHidden: true,
  28. brand: { name: 'Vol', accent: 'cyan' }
  29. };
  30. }
  31. componentDidMount() {
  32. // TODO: do not hide this but instead show some help text??
  33. this.subscribe(this.plugin.managers.structure.hierarchy.behaviors.selection, () => {
  34. this.setState({
  35. isHidden: !this.canEnable(),
  36. description: StructureHierarchyManager.getSelectedStructuresDescription(this.plugin)
  37. })
  38. });
  39. this.subscribe(this.plugin.events.state.cell.stateUpdated, e => {
  40. if (StateTransform.hasTag(e.cell.transform, VolumeStreaming.RootTag)) this.forceUpdate();
  41. });
  42. this.subscribe(this.plugin.behaviors.state.isBusy, v => {
  43. this.setState({ isBusy: v })
  44. });
  45. }
  46. get pivot() {
  47. return this.plugin.managers.structure.hierarchy.selection.structures[0];
  48. }
  49. canEnable() {
  50. const { selection } = this.plugin.managers.structure.hierarchy;
  51. if (selection.structures.length !== 1) return false;
  52. const pivot = this.pivot.cell;
  53. if (!pivot.obj) return false;
  54. return !!InitVolumeStreaming.definition.isApplicable?.(pivot.obj, pivot.transform, this.plugin);
  55. }
  56. renderEnable() {
  57. const pivot = this.pivot;
  58. if (!pivot.cell.parent) return null;
  59. const root = StateSelection.findTagInSubtree(pivot.cell.parent.tree, this.pivot.cell.transform.ref, VolumeStreaming.RootTag);
  60. const rootCell = root && pivot.cell.parent.cells.get(root);
  61. const simpleApply = rootCell && rootCell.status === 'error'
  62. ? { header: 'Error enabling', icon: 'alert' as const, title: rootCell.errorText }
  63. : { header: 'Enable', icon: 'check' as const, title: 'Enable' }
  64. return <ApplyActionControl state={pivot.cell.parent} action={InitVolumeStreaming} initiallyCollapsed={true} nodeRef={pivot.cell.transform.ref} simpleApply={simpleApply} />;
  65. }
  66. renderParams() {
  67. const pivot = this.pivot;
  68. if (!pivot.cell.parent) return null;
  69. const bindings = pivot.volumeStreaming?.cell.transform.params?.entry.params.view.name === 'selection-box' && this.plugin.state.behaviors.cells.get(FocusLoci.id)?.params?.values?.bindings;
  70. return <>
  71. <UpdateTransformControl state={pivot.cell.parent} transform={pivot.volumeStreaming!.cell.transform} customHeader='none' noMargin autoHideApply />
  72. {bindings && <ExpandGroup header='Controls Help'>
  73. <BindingsHelp bindings={bindings} />
  74. </ExpandGroup>}
  75. </>;
  76. }
  77. renderControls() {
  78. const pivot = this.pivot;
  79. if (!pivot) return null;
  80. if (!pivot.volumeStreaming) return this.renderEnable();
  81. return this.renderParams();
  82. }
  83. }