1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-29 02:34:06 +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 MenuViewImage = (CefMenuCommand)26503;
private const CefMenuCommand MenuOpenMediaUrl = (CefMenuCommand)26504; private const CefMenuCommand MenuOpenMediaUrl = (CefMenuCommand)26504;
private const CefMenuCommand MenuCopyMediaUrl = (CefMenuCommand)26505; private const CefMenuCommand MenuCopyMediaUrl = (CefMenuCommand)26505;
private const CefMenuCommand MenuSaveMedia = (CefMenuCommand)26506; private const CefMenuCommand MenuCopyImage = (CefMenuCommand)26506;
private const CefMenuCommand MenuSaveTweetImages = (CefMenuCommand)26507; private const CefMenuCommand MenuSaveMedia = (CefMenuCommand)26507;
private const CefMenuCommand MenuSearchInBrowser = (CefMenuCommand)26508; private const CefMenuCommand MenuSaveTweetImages = (CefMenuCommand)26508;
private const CefMenuCommand MenuReadApplyROT13 = (CefMenuCommand)26509; private const CefMenuCommand MenuSearchInBrowser = (CefMenuCommand)26509;
private const CefMenuCommand MenuReadApplyROT13 = (CefMenuCommand)26510;
private const CefMenuCommand MenuOpenDevTools = (CefMenuCommand)26599; private const CefMenuCommand MenuOpenDevTools = (CefMenuCommand)26599;
protected ContextInfo.ContextData Context { get; private set; } 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(MenuViewImage, "View image in photo viewer");
model.AddItem(MenuOpenMediaUrl, TextOpen("image")); model.AddItem(MenuOpenMediaUrl, TextOpen("image"));
model.AddItem(MenuCopyMediaUrl, TextCopy("image")); model.AddItem(MenuCopyMediaUrl, TextCopy("image"));
model.AddItem(MenuCopyImage, "Copy image");
model.AddItem(MenuSaveMedia, TextSave("image")); model.AddItem(MenuSaveMedia, TextSave("image"));
if (Context.Chirp.Images.Length > 1){ 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)); SetClipboardText(control, TwitterUrls.GetMediaLink(Context.MediaUrl, ImageQuality));
break; break;
case MenuCopyImage: {
string url = Context.MediaUrl;
control.InvokeAsyncSafe(() => {
TwitterUtils.CopyImage(url, ImageQuality);
});
break;
}
case MenuViewImage: { case MenuViewImage: {
string url = Context.MediaUrl; string url = Context.MediaUrl;

View File

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