1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-04-20 03:15:47 +02:00

Move screenshot height calculation to the screenshot window

This commit is contained in:
chylex 2018-04-03 18:26:33 +02:00
parent ebe3868720
commit 3f0b161cd0
9 changed files with 68 additions and 48 deletions

View File

@ -1,19 +0,0 @@
using System;
using System.Windows.Forms;
using TweetDuck.Core.Controls;
namespace TweetDuck.Core.Bridge{
sealed class CallbackBridge{
private readonly Control owner;
private readonly Action safeCallback;
public CallbackBridge(Control owner, Action safeCallback){
this.owner = owner;
this.safeCallback = safeCallback;
}
public void Trigger(){
owner.InvokeSafe(safeCallback);
}
}
}

View File

@ -117,8 +117,8 @@ public void OnTweetSound(){
});
}
public void ScreenshotTweet(string html, int width, int height){
form.InvokeAsyncSafe(() => form.OnTweetScreenshotReady(html, width, height));
public void ScreenshotTweet(string html, int width){
form.InvokeAsyncSafe(() => form.OnTweetScreenshotReady(html, width));
}
public void PlayVideo(string url, string username){

View File

@ -455,12 +455,12 @@ public void ShowTweetDetail(string columnId, string chirpId, string fallbackUrl)
AnalyticsFile.TweetDetails.Trigger();
}
public void OnTweetScreenshotReady(string html, int width, int height){
public void OnTweetScreenshotReady(string html, int width){
if (notificationScreenshotManager == null){
notificationScreenshotManager = new TweetScreenshotManager(this, plugins);
}
notificationScreenshotManager.Trigger(html, width, height);
notificationScreenshotManager.Trigger(html, width);
AnalyticsFile.TweetScreenshots.Trigger();
}

View File

@ -3,7 +3,7 @@
using System.Drawing.Imaging;
using System.Windows.Forms;
using CefSharp;
using TweetDuck.Core.Bridge;
using TweetDuck.Core.Controls;
using TweetDuck.Core.Other;
using TweetDuck.Core.Utils;
using TweetDuck.Data;
@ -15,19 +15,26 @@ sealed class FormNotificationScreenshotable : FormNotificationBase{
protected override bool CanDragWindow => false;
private readonly PluginManager plugins;
private readonly int width;
public FormNotificationScreenshotable(Action callback, FormBrowser owner, PluginManager pluginManager) : base(owner, false){
public FormNotificationScreenshotable(Action callback, FormBrowser owner, PluginManager pluginManager, string html, int width) : base(owner, false){
this.plugins = pluginManager;
this.width = width;
browser.RegisterAsyncJsObject("$TD_NotificationScreenshot", new CallbackBridge(this, callback));
browser.RegisterAsyncJsObject("$TD_NotificationScreenshot", new ScreenshotBridge(this, SetScreenshotHeight, callback));
browser.LoadingStateChanged += (sender, args) => {
if (!args.IsLoading){
using(IFrame frame = args.Browser.MainFrame){
ScriptLoader.ExecuteScript(frame, "window.setTimeout($TD_NotificationScreenshot.trigger, document.getElementsByTagName('iframe').length ? 267 : 67)", "gen:screenshot");
if (!ScriptLoader.ExecuteFile(frame, "screenshot.js")){
this.InvokeAsyncSafe(callback);
}
}
}
};
LoadTweet(new TweetNotification(string.Empty, string.Empty, string.Empty, html, 0, string.Empty, string.Empty));
SetScreenshotHeight(0);
}
protected override string GetTweetHTML(TweetNotification tweet){
@ -40,12 +47,16 @@ protected override string GetTweetHTML(TweetNotification tweet){
return html;
}
public void LoadNotificationForScreenshot(TweetNotification tweet, int width, int height){
LoadTweet(tweet);
SetNotificationSize(width, height);
private void SetScreenshotHeight(int height){
SetNotificationSize(width, height); // TODO test how it works on high DPI, probably not great?
}
public void TakeScreenshot(){
if (ClientSize.Height == 0){
FormMessage.Error("Screenshot Failed", "Could not detect screenshot size.", FormMessage.OK);
return;
}
IntPtr context = NativeMethods.GetDC(this.Handle);
if (context == IntPtr.Zero){

View File

@ -0,0 +1,26 @@
using System;
using System.Windows.Forms;
using TweetDuck.Core.Controls;
namespace TweetDuck.Core.Notification.Screenshot{
sealed class ScreenshotBridge{
private readonly Control owner;
private readonly Action<int> safeSetHeight;
private readonly Action safeTriggerScreenshot;
public ScreenshotBridge(Control owner, Action<int> safeSetHeight, Action safeTriggerScreenshot){
this.owner = owner;
this.safeSetHeight = safeSetHeight;
this.safeTriggerScreenshot = safeTriggerScreenshot;
}
public void SetHeight(int tweetHeight){
owner.InvokeSafe(() => safeSetHeight(tweetHeight));
}
public void TriggerScreenshot(){
owner.InvokeSafe(safeTriggerScreenshot);
}
}
}

View File

@ -37,13 +37,12 @@ private void disposer_Tick(object sender, EventArgs e){
screenshot = null;
}
public void Trigger(string html, int width, int height){
public void Trigger(string html, int width){
if (screenshot != null){
return;
}
screenshot = new FormNotificationScreenshotable(Callback, owner, plugins);
screenshot.LoadNotificationForScreenshot(new TweetNotification(string.Empty, string.Empty, string.Empty, html, 0, string.Empty, string.Empty), width, height);
screenshot = new FormNotificationScreenshotable(Callback, owner, plugins, html, width);
screenshot.Show();
timeout.Start();

View File

@ -705,20 +705,7 @@
html.addClass("td-notification-padded");
}
let testTweet = html.clone().css({
position: "absolute",
left: "-999px",
width: columnWidth+"px"
}).appendTo(document.body);
let testTweetAvatar = testTweet.find(".tweet-avatar").first();
let avatarBottom = testTweetAvatar.length === 1 ? testTweetAvatar.offset().top+testTweetAvatar.height() : 0;
let realHeight = Math.floor(Math.max(testTweet.height(), avatarBottom+9));
testTweet.remove();
html.find(".js-stream-item-content").first().css("height", "100vh");
$TD.screenshotTweet(html[0].outerHTML, columnWidth, realHeight);
$TD.screenshotTweet(html[0].outerHTML, columnWidth);
};
})();

View File

@ -0,0 +1,16 @@
(function($TD){
document.getElementsByClassName("column")[0].style.height = "100%";
let ele = document.getElementsByTagName("article")[0];
ele.style.width = window.outerWidth+"px";
ele.style.position = "absolute";
let contentHeight = ele.offsetHeight;
ele.style.position = "static";
let avatar = ele.querySelector(".tweet-avatar");
let avatarBottom = avatar ? avatar.getBoundingClientRect().bottom : 0;
$TD.setHeight(Math.floor(Math.max(contentHeight, avatarBottom+9)));
setTimeout($TD.triggerScreenshot, document.getElementsByTagName("iframe").length ? 267 : 67);
})($TD_NotificationScreenshot);

View File

@ -245,7 +245,7 @@
<Compile Include="Core\Other\Settings\TabSettingsNotifications.Designer.cs">
<DependentUpon>TabSettingsNotifications.cs</DependentUpon>
</Compile>
<Compile Include="Core\Bridge\CallbackBridge.cs" />
<Compile Include="Core\Notification\Screenshot\ScreenshotBridge.cs" />
<Compile Include="Data\CommandLineArgs.cs" />
<Compile Include="Core\Notification\Screenshot\FormNotificationScreenshotable.cs">
<SubType>Form</SubType>