mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-05 02:34:07 +02:00
Use custom ResourceHandler for notifications and tweak notification code
This commit is contained in:
parent
26d5a8ce08
commit
81bf93e5ab
66
Core/Handling/ResourceHandlerNotification.cs
Normal file
66
Core/Handling/ResourceHandlerNotification.cs
Normal file
@ -0,0 +1,66 @@
|
||||
using CefSharp;
|
||||
using System.Collections.Specialized;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace TweetDck.Core.Handling{
|
||||
class ResourceHandlerNotification : IResourceHandler{
|
||||
private readonly NameValueCollection headers = new NameValueCollection(0);
|
||||
private MemoryStream dataIn;
|
||||
|
||||
public void SetHTML(string html){
|
||||
if (dataIn != null){
|
||||
dataIn.Dispose();
|
||||
}
|
||||
|
||||
dataIn = ResourceHandler.GetMemoryStream(html, Encoding.UTF8);
|
||||
}
|
||||
|
||||
public void Dispose(){
|
||||
if (dataIn != null){
|
||||
dataIn.Dispose();
|
||||
dataIn = null;
|
||||
}
|
||||
}
|
||||
|
||||
bool IResourceHandler.ProcessRequest(IRequest request, ICallback callback){
|
||||
callback.Continue();
|
||||
return true;
|
||||
}
|
||||
|
||||
void IResourceHandler.GetResponseHeaders(IResponse response, out long responseLength, out string redirectUrl){
|
||||
redirectUrl = null;
|
||||
|
||||
response.MimeType = "text/html";
|
||||
response.StatusCode = 200;
|
||||
response.StatusText = "OK";
|
||||
response.ResponseHeaders = headers;
|
||||
responseLength = dataIn.Length;
|
||||
}
|
||||
|
||||
bool IResourceHandler.ReadResponse(Stream dataOut, out int bytesRead, ICallback callback){
|
||||
callback.Dispose();
|
||||
|
||||
try{
|
||||
int length = (int)dataIn.Length;
|
||||
|
||||
dataIn.CopyTo(dataOut, length);
|
||||
bytesRead = length;
|
||||
return true;
|
||||
}catch{ // catch IOException, possibly NullReferenceException if dataIn is null
|
||||
bytesRead = 0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool IResourceHandler.CanGetCookie(Cookie cookie){
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IResourceHandler.CanSetCookie(Cookie cookie){
|
||||
return true;
|
||||
}
|
||||
|
||||
void IResourceHandler.Cancel(){}
|
||||
}
|
||||
}
|
@ -71,6 +71,8 @@ public bool IsNotificationVisible{
|
||||
protected readonly Form owner;
|
||||
protected readonly ChromiumWebBrowser browser;
|
||||
|
||||
private readonly ResourceHandlerNotification resourceHandler = new ResourceHandlerNotification();
|
||||
|
||||
private string currentColumn;
|
||||
private int pauseCounter;
|
||||
|
||||
@ -106,12 +108,14 @@ public FormNotificationBase(Form owner, bool enableContextMenu){
|
||||
|
||||
this.browser.Dock = DockStyle.None;
|
||||
this.browser.ClientSize = ClientSize;
|
||||
this.browser.IsBrowserInitializedChanged += Browser_IsBrowserInitializedChanged;
|
||||
|
||||
#if DEBUG
|
||||
this.browser.ConsoleMessage += BrowserUtils.HandleConsoleMessage;
|
||||
#endif
|
||||
|
||||
this.browser.IsBrowserInitializedChanged += Browser_IsBrowserInitializedChanged;
|
||||
DefaultResourceHandlerFactory handlerFactory = (DefaultResourceHandlerFactory)browser.ResourceHandlerFactory;
|
||||
handlerFactory.RegisterHandler("https://tweetdeck.twitter.com", this.resourceHandler);
|
||||
|
||||
Controls.Add(browser);
|
||||
|
||||
@ -148,7 +152,7 @@ private void Browser_IsBrowserInitializedChanged(object sender, IsBrowserInitial
|
||||
|
||||
public virtual void HideNotification(bool loadBlank){
|
||||
if (loadBlank){
|
||||
browser.LoadHtml("", "about:blank");
|
||||
browser.Load("about:blank");
|
||||
}
|
||||
|
||||
Location = ControlExtensions.InvisibleLocation;
|
||||
@ -178,7 +182,9 @@ protected virtual void LoadTweet(TweetNotification tweet){
|
||||
CurrentUrl = tweet.Url;
|
||||
CurrentQuotedTweetUrl = string.Empty; // load from JS
|
||||
currentColumn = tweet.Column;
|
||||
browser.LoadHtml(GetTweetHTML(tweet), "http://tweetdeck.twitter.com/?"+DateTime.Now.Ticks);
|
||||
|
||||
resourceHandler.SetHTML(GetTweetHTML(tweet));
|
||||
browser.Load("https://tweetdeck.twitter.com");
|
||||
}
|
||||
|
||||
protected virtual void SetNotificationSize(int width, int height){
|
||||
|
@ -108,7 +108,7 @@ private IntPtr MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam){
|
||||
|
||||
private void FormNotification_FormClosing(object sender, FormClosingEventArgs e){
|
||||
if (e.CloseReason == CloseReason.UserClosing){
|
||||
HideNotification(false);
|
||||
HideNotification(true);
|
||||
e.Cancel = true;
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
using CefSharp;
|
||||
using TweetDck.Core.Bridge;
|
||||
using TweetDck.Core.Controls;
|
||||
using TweetDck.Resources;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
@ -20,8 +18,12 @@ public FormNotificationScreenshotable(Action callback, Form owner) : base(owner,
|
||||
};
|
||||
}
|
||||
|
||||
protected override string GetTweetHTML(TweetNotification tweet){
|
||||
return tweet.GenerateHtml(enableCustomCSS: false);
|
||||
}
|
||||
|
||||
public void LoadNotificationForScreenshot(TweetNotification tweet, int width, int height){
|
||||
browser.LoadHtml(tweet.GenerateHtml(enableCustomCSS: false), "http://tweetdeck.twitter.com/?"+DateTime.Now.Ticks);
|
||||
LoadTweet(tweet);
|
||||
SetNotificationSize(width, height);
|
||||
}
|
||||
|
||||
|
@ -95,6 +95,7 @@
|
||||
<Compile Include="Core\FormBrowser.Designer.cs">
|
||||
<DependentUpon>FormBrowser.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Core\Handling\ResourceHandlerNotification.cs" />
|
||||
<Compile Include="Core\Notification\FormNotificationMain.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
|
Loading…
Reference in New Issue
Block a user