1 /* 2 * CopyPlotToClipboardDesktopMetafileDialog.java 3 * 4 * Copyright (C) 2021 by RStudio, PBC 5 * 6 * Unless you have received this program directly from RStudio pursuant 7 * to the terms of a commercial license agreement with RStudio, then 8 * this program is licensed to you under the terms of version 3 of the 9 * GNU Affero General Public License. This program is distributed WITHOUT 10 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, 11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the 12 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. 13 * 14 */ 15 package org.rstudio.studio.client.workbench.exportplot.clipboard; 16 17 18 import org.rstudio.core.client.widget.Operation; 19 import org.rstudio.core.client.widget.OperationWithInput; 20 import org.rstudio.studio.client.common.Timers; 21 import org.rstudio.studio.client.workbench.exportplot.ExportPlotPreviewer; 22 import org.rstudio.studio.client.workbench.exportplot.ExportPlotResources; 23 import org.rstudio.studio.client.workbench.exportplot.ExportPlotSizeEditor; 24 import org.rstudio.studio.client.workbench.exportplot.model.ExportPlotOptions; 25 26 import com.google.gwt.safehtml.shared.SafeHtmlUtils; 27 import com.google.gwt.user.client.Command; 28 import com.google.gwt.user.client.ui.Label; 29 import com.google.gwt.user.client.ui.RadioButton; 30 31 public class CopyPlotToClipboardDesktopMetafileDialog extends CopyPlotToClipboardDesktopDialog 32 { 33 CopyPlotToClipboardDesktopMetafileDialog( ExportPlotPreviewer previewer, ExportPlotClipboard clipboard, ExportPlotOptions options, OperationWithInput<ExportPlotOptions> onClose)34 public CopyPlotToClipboardDesktopMetafileDialog( 35 ExportPlotPreviewer previewer, 36 ExportPlotClipboard clipboard, 37 ExportPlotOptions options, 38 OperationWithInput<ExportPlotOptions> onClose) 39 { 40 super(previewer, clipboard, options, onClose); 41 42 43 ExportPlotResources.Styles styles = ExportPlotResources.INSTANCE.styles(); 44 45 Label label = new Label(); 46 label.setStylePrimaryName(styles.copyFormatLabel()); 47 label.setText("Copy as:"); 48 addLeftWidget(label); 49 50 copyAsBitmapRadioButton_ = new RadioButton( 51 "Format", 52 SafeHtmlUtils.fromString("Bitmap")); 53 copyAsBitmapRadioButton_.setStylePrimaryName(styles.copyFormatBitmap()); 54 addLeftWidget(copyAsBitmapRadioButton_); 55 56 copyAsMetafileRadioButton_ = new RadioButton( 57 "Format", 58 SafeHtmlUtils.fromString("Metafile")); 59 copyAsMetafileRadioButton_.setStylePrimaryName(styles.copyFormatMetafile()); 60 addLeftWidget(copyAsMetafileRadioButton_); 61 62 if (options.getCopyAsMetafile()) 63 copyAsMetafileRadioButton_.setValue(true); 64 else 65 copyAsBitmapRadioButton_.setValue(true); 66 } 67 68 69 @Override performCopy(Operation onCompleted)70 protected void performCopy(Operation onCompleted) 71 { 72 if (getCopyAsMetafile()) 73 copyAsMetafile(onCompleted); 74 else 75 copyAsBitmap(onCompleted); 76 } 77 78 79 @Override getCurrentOptions(ExportPlotOptions previous)80 protected ExportPlotOptions getCurrentOptions(ExportPlotOptions previous) 81 { 82 ExportPlotSizeEditor sizeEditor = getSizeEditor(); 83 return ExportPlotOptions.create(sizeEditor.getImageWidth(), 84 sizeEditor.getImageHeight(), 85 sizeEditor.getKeepRatio(), 86 previous.getFormat(), 87 previous.getViewAfterSave(), 88 getCopyAsMetafile()); 89 } 90 91 getCopyAsMetafile()92 private boolean getCopyAsMetafile() 93 { 94 return copyAsMetafileRadioButton_.getValue(); 95 } 96 copyAsMetafile(final Operation onCompleted)97 private void copyAsMetafile(final Operation onCompleted) 98 { 99 ExportPlotSizeEditor sizeEditor = getSizeEditor(); 100 sizeEditor.setGripperVisible(false); 101 102 // NOTE: we use a timer here just to be absolutely sure the 103 // browser has re-rendered and hidden the gripper before attempting 104 // to get a screenshot. note that the usual tools, e.g. scheduleDeferred(), 105 // don't work as expected here 106 Timers.singleShot(200, () -> doCopyAsMetafile(onCompleted)); 107 } 108 doCopyAsMetafile(final Operation onCompleted)109 private void doCopyAsMetafile(final Operation onCompleted) 110 { 111 ExportPlotSizeEditor sizeEditor = getSizeEditor(); 112 clipboard_.copyPlotToClipboardMetafile( 113 sizeEditor.getImageWidth(), 114 sizeEditor.getImageHeight(), 115 new Command() 116 { 117 @Override 118 public void execute() 119 { 120 sizeEditor.setGripperVisible(true); 121 onCompleted.execute(); 122 } 123 }); 124 } 125 126 private RadioButton copyAsBitmapRadioButton_; 127 private RadioButton copyAsMetafileRadioButton_; 128 } 129