ui.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 { CollapsableControls, CollapsableState } from '../../mol-plugin-ui/base';
  7. import { Button } from '../../mol-plugin-ui/controls/common';
  8. import { GetAppSvg, CubeSendSvg } from '../../mol-plugin-ui/controls/icons';
  9. import { download } from '../../mol-util/download';
  10. import { GeometryControls } from './controls';
  11. interface State {
  12. busy?: boolean
  13. }
  14. export class GeometryExporterUI extends CollapsableControls<{}, State> {
  15. private _controls: GeometryControls | undefined;
  16. get controls() {
  17. return this._controls || (this._controls = new GeometryControls(this.plugin));
  18. }
  19. protected defaultState(): State & CollapsableState {
  20. return {
  21. header: 'Export Geometries',
  22. isCollapsed: true,
  23. brand: { accent: 'cyan', svg: CubeSendSvg }
  24. };
  25. }
  26. protected renderControls(): JSX.Element {
  27. return <>
  28. <Button icon={GetAppSvg}
  29. onClick={this.saveObj} style={{ marginTop: 1 }}
  30. disabled={this.state.busy || !this.plugin.canvas3d?.reprCount.value}>
  31. Save OBJ + MTL
  32. </Button>
  33. </>;
  34. }
  35. componentDidMount() {
  36. this.subscribe(this.plugin.canvas3d!.reprCount, () => {
  37. if (!this.state.isCollapsed) this.forceUpdate();
  38. });
  39. }
  40. componentWillUnmount() {
  41. this._controls?.dispose();
  42. this._controls = void 0;
  43. }
  44. saveObj = async () => {
  45. try {
  46. this.setState({ busy: true });
  47. const data = await this.controls.exportObj();
  48. this.setState({ busy: false });
  49. download(new Blob([data.zipData]), data.filename);
  50. } catch {
  51. this.setState({ busy: false });
  52. }
  53. }
  54. }