-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Basic copy plot to clipboard #2640
Conversation
// --- Start Positron --- | ||
async writeImage(data: string): Promise<void> { | ||
const blob = new Blob([data], { type: 'image/png' }); | ||
navigator.clipboard.write([ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where I think that permission will be required. I haven't been able to test the web version so this will likely fail silently due to permissions.
@@ -4,7 +4,7 @@ | |||
*--------------------------------------------------------------------------------------------*/ | |||
|
|||
import { exec } from 'child_process'; | |||
import { app, BrowserWindow, clipboard, Display, Menu, MessageBoxOptions, MessageBoxReturnValue, OpenDevToolsOptions, OpenDialogOptions, OpenDialogReturnValue, powerMonitor, SaveDialogOptions, SaveDialogReturnValue, screen, shell } from 'electron'; | |||
import { app, BrowserWindow, clipboard, Display, Menu, MessageBoxOptions, MessageBoxReturnValue, nativeImage, OpenDevToolsOptions, OpenDialogOptions, OpenDialogReturnValue, powerMonitor, SaveDialogOptions, SaveDialogReturnValue, screen, shell } from 'electron'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also needs a Positron code fence. You should add the nativeImage
import in a separate import
statement to avoid merge conflicts; instructions here: https://connect.posit.it/positron-wiki/overlay-strategy.html#a-note-on-imports
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
if (plot.lastRender?.uri) { | ||
this._clipboardService.writeImage(plot.lastRender.uri); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should show a little toast notification, or gleam the icon or something, so there's visual feedback that the copy happened.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea. It now returns whether the copy occurred and will toast a notification.
Implement write image in clipboard service
Only show copy action for supported plots
221e55f
to
1032a79
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, up to you whether you want to tighten up the error handling before merge
if (copyResult) { | ||
positronPlotsContext.notificationService.info(localize('positronPlotsServiceCopyToClipboard', 'Plot copied to clipboard')); | ||
} else { | ||
positronPlotsContext.notificationService.error(localize('positronPlotsServiceCopyToClipboardError', 'Failed to copy plot to clipboard')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell this can only happen if the plot is unsupported for copying (i.e. a webview plot) so maybe we could make the error message more specific?
Also might be worth adding a try/catch -- if an exception occurs while trying to write the clipboard data, currently nothing will happen (no toast); in this case we should show a toast with the exception text.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'm not sure why I did it that way. Definitely fixing this!
Intent
Address #421
Approach
Adds a Plots pane action button to copy the current plot to the clipboard. This currently writes the same image as shown without any ability to set any options (for dynamic plots).
The clipboard service needed to add support for Electron's
clipboard.writeImage
API. It can conveniently handle a data URI that we already have from rendering the plot for the pane.QA Notes
This is basic support for copying to the clipboard for desktop. It's unknown if this works in the web. The web API to access the clipboard does need permission so this may fail until obtaining permission is implemented.