Permalink
Browse files

Completely rewrite all image pasting code

  • Loading branch information...
chylex committed Apr 15, 2017
1 parent 35624bc commit 61b1155a038d7b4b7bd9e0bfa53af06ec6bfafbf
Showing with 13 additions and 144 deletions.
  1. +2 −31 Core/Bridge/TweetDeckBridge.cs
  2. +0 −11 Core/FormBrowser.cs
  3. +0 −28 Core/Handling/FileDialogHandler.cs
  4. +0 −9 Program.cs
  5. +11 −64 Resources/Scripts/code.js
  6. +0 −1 TweetDck.csproj
@@ -1,8 +1,4 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using System.Windows.Forms;
using TweetDck.Core.Controls;
using TweetDck.Core.Notification;
using TweetDck.Core.Utils;
@@ -12,10 +8,9 @@ sealed class TweetDeckBridge{
public static string LastRightClickedLink = string.Empty;
public static string LastHighlightedTweet = string.Empty;
public static string LastHighlightedQuotedTweet = string.Empty;
public static string ClipboardImagePath = string.Empty;
public static void ResetStaticProperties(){
LastRightClickedLink = LastHighlightedTweet = LastHighlightedQuotedTweet = ClipboardImagePath = string.Empty;
LastRightClickedLink = LastHighlightedTweet = LastHighlightedQuotedTweet = string.Empty;
}
private readonly FormBrowser form;
@@ -80,30 +75,6 @@ sealed class TweetDeckBridge{
notification.InvokeAsyncSafe(notification.FinishCurrentNotification);
}
public void TryPasteImage(){
form.InvokeSafe(() => {
if (Clipboard.ContainsImage()){
Image img = Clipboard.GetImage();
if (img == null)return;
try{
Directory.CreateDirectory(Program.TemporaryPath);
ClipboardImagePath = Path.Combine(Program.TemporaryPath, "TD-Img-"+DateTime.Now.Ticks+".png");
img.Save(ClipboardImagePath, ImageFormat.Png);
form.OnImagePasted();
}catch(Exception e){
Program.Reporter.HandleException("Clipboard Image Error", "Could not paste image from clipboard.", true, e);
}
}
});
}
public void ClickUploadImage(int offsetX, int offsetY){
form.InvokeAsyncSafe(() => form.TriggerImageUpload(offsetX, offsetY));
}
public void ScreenshotTweet(string html, int width, int height){
form.InvokeAsyncSafe(() => form.OnTweetScreenshotReady(html, width, height));
}
@@ -66,7 +66,6 @@ sealed partial class FormBrowser : Form{
this.browser = new ChromiumWebBrowser("https://tweetdeck.twitter.com/"){
MenuHandler = new ContextMenuBrowser(this),
DialogHandler = new FileDialogHandler(this),
JsDialogHandler = new JavaScriptDialogHandler(),
LifeSpanHandler = new LifeSpanHandler()
};
@@ -448,16 +447,6 @@ sealed partial class FormBrowser : Form{
}
}
public void OnImagePasted(){
browser.ExecuteScriptAsync("TDGF_tryPasteImage()");
}
public void TriggerImageUpload(int offsetX, int offsetY){
IBrowserHost host = browser.GetBrowser().GetHost();
host.SendMouseClickEvent(offsetX, offsetY, MouseButtonType.Left, false, 1, CefEventFlags.None);
host.SendMouseClickEvent(offsetX, offsetY, MouseButtonType.Left, true, 1, CefEventFlags.None);
}
public void TriggerTweetScreenshot(){
browser.ExecuteScriptAsync("TDGF_triggerScreenshot()");
}

This file was deleted.

Oops, something went wrong.
@@ -28,7 +28,6 @@ static class Program{
public static readonly string ProgramPath = AppDomain.CurrentDomain.BaseDirectory;
public static readonly string StoragePath = IsPortable ? Path.Combine(ProgramPath, "portable", "storage") : GetDataStoragePath();
public static readonly string TemporaryPath = IsPortable ? Path.Combine(ProgramPath, "portable", "tmp") : Path.Combine(Path.GetTempPath(), BrandName+'_'+Path.GetRandomFileName().Substring(0, 6));
public static readonly string PluginDataPath = Path.Combine(StoragePath, "TD_Plugins");
public static readonly string ConfigFilePath = Path.Combine(StoragePath, "TD_UserConfig.cfg");
@@ -260,14 +259,6 @@ static class Program{
UserConfig.Save();
try{
Directory.Delete(TemporaryPath, true);
}catch(DirectoryNotFoundException){
}catch(Exception e){
// welp, too bad
Debug.WriteLine(e.ToString());
}
Cef.Shutdown();
BrowserCache.Exit();
@@ -405,72 +405,19 @@
//
// Block: Paste images when tweeting.
//
(function(){
var lastPasteElement;
var prevScrollTop;
var getScroller = function(){
return $(".js-drawer").find(".js-compose-scroller").first().children().first();
};
var clickUpload = function(){
$(document).one("uiFilesAdded", function(){
getScroller().scrollTop(prevScrollTop);
$(".js-drawer").find(".js-compose-text").first()[0].focus();
});
var button = $(".js-add-image-button").first();
var scroller = getScroller();
prevScrollTop = scroller.scrollTop();
scroller.scrollTop(0);
scroller.scrollTop(button.offset().top); // scrolls the button into view
var buttonPos = button.children().first().offset(); // finds the camera icon offset
$TD.clickUploadImage(Math.floor(buttonPos.left), Math.floor(buttonPos.top));
};
app.delegate(".js-compose-text,.js-reply-tweetbox", "paste", function(){
lastPasteElement = $(this);
$TD.tryPasteImage();
});
window.TDGF_tryPasteImage = function(){
if (lastPasteElement){
var parent = lastPasteElement.parent();
if (parent.siblings(".js-add-image-button").length === 0){
var pop = parent.closest(".js-inline-reply,.rpl").find(".js-inline-compose-pop,.js-reply-popout");
if (pop.length === 0){
lastPasteElement = null;
return;
}
pop.click();
var drawer = $(".js-drawer");
var counter = 0;
var interval = setInterval(function(){
if (drawer.offset().left >= 195){
clickUpload();
clearInterval(interval);
}
else if (++counter >= 10){
clearInterval(interval);
}
}, 51);
}
else{
clickUpload();
onAppReady.push(function(){
var uploader = $._data(document, "events")["uiComposeAddImageClick"][0].handler.context;
app.delegate(".js-compose-text,.js-reply-tweetbox", "paste", function(e){
for(let item of e.originalEvent.clipboardData.items){
if (item.type.startsWith("image/")){
$(this).closest(".rpl").find(".js-reply-popout").click(); // popout direct messages
uploader.addFilesToUpload([ item.getAsFile() ]);
break;
}
lastPasteElement = null;
}
};
})();
});
});
//
// Block: Support for extra mouse buttons.
@@ -109,7 +109,6 @@
<DependentUpon>FormNotificationBase.cs</DependentUpon>
</Compile>
<Compile Include="Core\Handling\ContextMenuNotification.cs" />
<Compile Include="Core\Handling\FileDialogHandler.cs" />
<Compile Include="Core\Handling\JavaScriptDialogHandler.cs" />
<Compile Include="Core\Handling\LifeSpanHandler.cs" />
<Compile Include="Core\Notification\FormNotificationTweet.cs">

0 comments on commit 61b1155

Please sign in to comment.