1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-06 23:34:05 +02:00

Add 'Copy image' to context menu

Closes 
This commit is contained in:
chylex 2020-04-25 02:43:12 +02:00
parent 5888d540a6
commit e90f6ebc63
2 changed files with 47 additions and 18 deletions

View File

@ -28,10 +28,11 @@ abstract class ContextMenuBase : IContextMenuHandler{
private const CefMenuCommand MenuViewImage = (CefMenuCommand)26503;
private const CefMenuCommand MenuOpenMediaUrl = (CefMenuCommand)26504;
private const CefMenuCommand MenuCopyMediaUrl = (CefMenuCommand)26505;
private const CefMenuCommand MenuSaveMedia = (CefMenuCommand)26506;
private const CefMenuCommand MenuSaveTweetImages = (CefMenuCommand)26507;
private const CefMenuCommand MenuSearchInBrowser = (CefMenuCommand)26508;
private const CefMenuCommand MenuReadApplyROT13 = (CefMenuCommand)26509;
private const CefMenuCommand MenuCopyImage = (CefMenuCommand)26506;
private const CefMenuCommand MenuSaveMedia = (CefMenuCommand)26507;
private const CefMenuCommand MenuSaveTweetImages = (CefMenuCommand)26508;
private const CefMenuCommand MenuSearchInBrowser = (CefMenuCommand)26509;
private const CefMenuCommand MenuReadApplyROT13 = (CefMenuCommand)26510;
private const CefMenuCommand MenuOpenDevTools = (CefMenuCommand)26599;
protected ContextInfo.ContextData Context { get; private set; }
@ -85,6 +86,7 @@ public virtual void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser bro
model.AddItem(MenuViewImage, "View image in photo viewer");
model.AddItem(MenuOpenMediaUrl, TextOpen("image"));
model.AddItem(MenuCopyMediaUrl, TextCopy("image"));
model.AddItem(MenuCopyImage, "Copy image");
model.AddItem(MenuSaveMedia, TextSave("image"));
if (Context.Chirp.Images.Length > 1){
@ -124,6 +126,16 @@ public virtual bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser br
SetClipboardText(control, TwitterUrls.GetMediaLink(Context.MediaUrl, ImageQuality));
break;
case MenuCopyImage: {
string url = Context.MediaUrl;
control.InvokeAsyncSafe(() => {
TwitterUtils.CopyImage(url, ImageQuality);
});
break;
}
case MenuViewImage: {
string url = Context.MediaUrl;

View File

@ -24,8 +24,23 @@ static class TwitterUtils{
"tweetdeck", "TweetDeck", "tweetduck", "TweetDuck", "TD"
};
private static void DownloadTempImage(string url, ImageQuality quality, Action<string> process){
string file = Path.Combine(BrowserCache.CacheFolder, TwitterUrls.GetImageFileName(url) ?? Path.GetRandomFileName());
if (FileUtils.FileExistsAndNotEmpty(file)){
process(file);
}
else{
DownloadFileAuth(TwitterUrls.GetMediaLink(url, quality), file, () => {
process(file);
}, ex => {
FormMessage.Error("Image Download", "An error occurred while downloading the image: " + ex.Message, FormMessage.OK);
});
}
}
public static void ViewImage(string url, ImageQuality quality){
static void ViewImageInternal(string path){
DownloadTempImage(url, quality, path => {
string ext = Path.GetExtension(path);
if (ImageUrl.ValidExtensions.Contains(ext)){
@ -34,20 +49,22 @@ static void ViewImageInternal(string path){
else{
FormMessage.Error("Image Download", "Invalid file extension " + ext, FormMessage.OK);
}
}
});
}
string file = Path.Combine(BrowserCache.CacheFolder, TwitterUrls.GetImageFileName(url) ?? Path.GetRandomFileName());
if (FileUtils.FileExistsAndNotEmpty(file)){
ViewImageInternal(file);
}
else{
DownloadFileAuth(TwitterUrls.GetMediaLink(url, quality), file, () => {
ViewImageInternal(file);
}, ex => {
FormMessage.Error("Image Download", "An error occurred while downloading the image: " + ex.Message, FormMessage.OK);
});
}
public static void CopyImage(string url, ImageQuality quality){
DownloadTempImage(url, quality, path => {
Image image;
try{
image = Image.FromFile(path);
}catch(Exception ex){
FormMessage.Error("Copy Image", "An error occurred while copying the image: " + ex.Message, FormMessage.OK);
return;
}
ClipboardManager.SetImage(image);
});
}
public static void DownloadImage(string url, string username, ImageQuality quality){