ui.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * Copyright (c) 2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Sukolsak Sakshuwong <sukolsak@stanford.edu>
  5. */
  6. import { merge } from 'rxjs';
  7. import { CollapsableControls, CollapsableState } from '../../mol-plugin-ui/base';
  8. import { Button } from '../../mol-plugin-ui/controls/common';
  9. import { GetAppSvg, CubeSendSvg } from '../../mol-plugin-ui/controls/icons';
  10. import { ParameterControls } from '../../mol-plugin-ui/controls/parameters';
  11. import { download } from '../../mol-util/download';
  12. import { GeometryParams, GeometryControls } from './controls';
  13. interface State {
  14. busy?: boolean
  15. }
  16. export class GeometryExporterUI extends CollapsableControls<{}, State> {
  17. private _controls: GeometryControls | undefined;
  18. get controls() {
  19. return this._controls || (this._controls = new GeometryControls(this.plugin));
  20. }
  21. protected defaultState(): State & CollapsableState {
  22. return {
  23. header: 'Export Geometry',
  24. isCollapsed: true,
  25. brand: { accent: 'cyan', svg: CubeSendSvg }
  26. };
  27. }
  28. protected renderControls(): JSX.Element {
  29. const ctrl = this.controls;
  30. return <>
  31. <ParameterControls
  32. params={GeometryParams}
  33. values={ctrl.behaviors.params.value}
  34. onChangeValues={xs => ctrl.behaviors.params.next(xs)}
  35. isDisabled={this.state.busy}
  36. />
  37. <Button icon={GetAppSvg}
  38. onClick={this.save} style={{ marginTop: 1 }}
  39. disabled={this.state.busy || !this.plugin.canvas3d?.reprCount.value}>
  40. Save
  41. </Button>
  42. </>;
  43. }
  44. componentDidMount() {
  45. const merged = merge(
  46. this.controls.behaviors.params,
  47. this.plugin.canvas3d!.reprCount
  48. );
  49. this.subscribe(merged, () => {
  50. if (!this.state.isCollapsed) this.forceUpdate();
  51. });
  52. }
  53. componentWillUnmount() {
  54. this._controls?.dispose();
  55. this._controls = void 0;
  56. }
  57. save = async () => {
  58. try {
  59. this.setState({ busy: true });
  60. const data = await this.controls.exportGeometry();
  61. this.setState({ busy: false });
  62. download(data.blob, data.filename);
  63. } catch {
  64. this.setState({ busy: false });
  65. }
  66. }
  67. }