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

Fix resource handlers reuse & broken notification sound

This commit is contained in:
chylex 2020-06-08 23:28:42 +02:00
parent 1c16187346
commit 16ced3d827
7 changed files with 35 additions and 21 deletions

View File

@ -4,19 +4,19 @@
namespace TweetDuck.Browser.Data{
sealed class ResourceHandlers{
private readonly ConcurrentDictionary<string, IResourceHandler> handlers = new ConcurrentDictionary<string, IResourceHandler>(StringComparer.OrdinalIgnoreCase);
private readonly ConcurrentDictionary<string, Func<IResourceHandler>> handlers = new ConcurrentDictionary<string, Func<IResourceHandler>>(StringComparer.OrdinalIgnoreCase);
public bool HasHandler(IRequest request){
return handlers.ContainsKey(request.Url);
}
public IResourceHandler GetHandler(IRequest request){
return handlers.TryGetValue(request.Url, out var handler) ? handler : null;
return handlers.TryGetValue(request.Url, out var factory) ? factory() : null;
}
public bool Register(string url, IResourceHandler handler){
public bool Register(string url, Func<IResourceHandler> factory){
if (Uri.TryCreate(url, UriKind.Absolute, out Uri uri)){
handlers.AddOrUpdate(uri.AbsoluteUri, handler, (key, prev) => handler);
handlers.AddOrUpdate(uri.AbsoluteUri, factory, (key, prev) => factory);
return true;
}
@ -24,15 +24,27 @@ public bool Register(string url, IResourceHandler handler){
}
public bool Register(ResourceLink link){
return Register(link.Url, link.Handler);
return Register(link.Url, link.Factory);
}
public bool Unregister(string url){
return handlers.TryRemove(url, out IResourceHandler _);
return handlers.TryRemove(url, out _);
}
public bool Unregister(ResourceLink link){
return Unregister(link.Url);
}
public static Func<IResourceHandler> ForString(string str){
return () => ResourceHandler.FromString(str);
}
public static Func<IResourceHandler> ForString(string str, string mimeType){
return () => ResourceHandler.FromString(str, mimeType: mimeType);
}
public static Func<IResourceHandler> ForBytes(byte[] bytes, string mimeType){
return () => ResourceHandler.FromByteArray(bytes, mimeType);
}
}
}

View File

@ -1,13 +1,14 @@
using CefSharp;
using System;
using CefSharp;
namespace TweetDuck.Browser.Data{
sealed class ResourceLink{
public string Url { get; }
public IResourceHandler Handler { get; }
public Func<IResourceHandler> Factory { get; }
public ResourceLink(string url, IResourceHandler handler){
public ResourceLink(string url, Func<IResourceHandler> factory){
this.Url = url;
this.Handler = handler;
this.Factory = factory;
}
}
}

View File

@ -1,6 +1,5 @@
using System.Drawing;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using TweetDuck.Browser.Data;
using TweetDuck.Browser.Handling;
@ -14,7 +13,7 @@
namespace TweetDuck.Browser.Notification{
abstract partial class FormNotificationBase : Form, AnalyticsFile.IProvider{
public static readonly ResourceLink AppLogo = new ResourceLink("https://ton.twimg.com/tduck/avatar", ResourceHandler.FromByteArray(Properties.Resources.avatar, "image/png"));
public static readonly ResourceLink AppLogo = new ResourceLink("https://ton.twimg.com/tduck/avatar", ResourceHandlers.ForBytes(Properties.Resources.avatar, "image/png"));
protected const string BlankURL = TwitterUrls.TweetDeck + "/?blank";
@ -135,8 +134,8 @@ protected FormNotificationBase(FormBrowser owner, bool enableContextMenu){
var resourceRequestHandler = new ResourceRequestHandlerBase();
var resourceHandlers = resourceRequestHandler.ResourceHandlers;
resourceHandlers.Register(BlankURL, ResourceHandler.FromString(string.Empty));
resourceHandlers.Register(TwitterUrls.TweetDeck, this.resourceHandler);
resourceHandlers.Register(BlankURL, ResourceHandlers.ForString(string.Empty));
resourceHandlers.Register(TwitterUrls.TweetDeck, () => this.resourceHandler);
resourceHandlers.Register(AppLogo);
this.browser = new ChromiumWebBrowser(BlankURL){

View File

@ -1,7 +1,9 @@
using System.Drawing;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using CefSharp;
using TweetDuck.Browser.Data;
using TweetDuck.Controls;
using TweetDuck.Dialogs;
using TweetDuck.Dialogs.Settings;
@ -11,7 +13,7 @@ namespace TweetDuck.Browser.Notification{
static class SoundNotification{
public const string SupportedFormats = "*.wav;*.ogg;*.mp3;*.flac;*.opus;*.weba;*.webm";
public static IResourceHandler CreateFileHandler(string path){
public static Func<IResourceHandler> CreateFileHandler(string path){
string mimeType = Path.GetExtension(path) switch{
".weba" => "audio/webm",
".webm" => "audio/webm",
@ -24,7 +26,7 @@ public static IResourceHandler CreateFileHandler(string path){
};
try{
return ResourceHandler.FromFilePath(path, mimeType);
return ResourceHandlers.ForBytes(File.ReadAllBytes(path), mimeType);
}catch{
FormBrowser browser = FormManager.TryFind<FormBrowser>();

View File

@ -137,7 +137,7 @@ private void browser_FrameLoadStart(object sender, FrameLoadStartEventArgs e){
if (TwitterUrls.IsTwitter(url)){
string css = Program.Resources.Load("styles/twitter.css");
resourceHandlers.Register(TwitterStyleUrl, ResourceHandler.FromString(css, mimeType: "text/css"));
resourceHandlers.Register(TwitterStyleUrl, ResourceHandlers.ForString(css, "text/css"));
CefScriptExecutor.RunFile(frame, "twitter.js");
}
@ -192,7 +192,7 @@ private void browser_LoadError(object sender, LoadErrorEventArgs e){
string errorName = Enum.GetName(typeof(CefErrorCode), e.ErrorCode);
string errorTitle = StringUtils.ConvertPascalCaseToScreamingSnakeCase(errorName ?? string.Empty);
resourceHandlers.Register(ErrorUrl, ResourceHandler.FromString(errorPage.Replace("{err}", errorTitle)));
resourceHandlers.Register(ErrorUrl, ResourceHandlers.ForString(errorPage.Replace("{err}", errorTitle)));
browser.Load(ErrorUrl);
}
}

View File

@ -17,7 +17,7 @@ sealed partial class FormGuide : Form, FormManager.IAppDialog{
private const string GuideUrl = "https://tweetduck.chylex.com/guide/v2/";
private const string GuidePathRegex = @"^guide(?:/v\d+)?(?:/(#.*))?";
private static readonly ResourceLink DummyPage = new ResourceLink("http://td/dummy", ResourceHandler.FromString(""));
private static readonly ResourceLink DummyPage = new ResourceLink("http://td/dummy", ResourceHandlers.ForString(string.Empty));
public static bool CheckGuideUrl(string url, out string hash){
if (!url.Contains("//tweetduck.chylex.com/guide")){

View File

@ -19,7 +19,7 @@ static class TwitterUtils{
public static readonly Color BackgroundColor = Color.FromArgb(28, 99, 153);
public const string BackgroundColorOverride = "setTimeout(function f(){let h=document.head;if(!h){setTimeout(f,5);return;}let e=document.createElement('style');e.innerHTML='body,body::before{background:#1c6399!important;margin:0}';h.appendChild(e);},1)";
public static readonly ResourceLink LoadingSpinner = new ResourceLink("https://ton.twimg.com/tduck/spinner", ResourceHandler.FromByteArray(Properties.Resources.spinner, "image/apng"));
public static readonly ResourceLink LoadingSpinner = new ResourceLink("https://ton.twimg.com/tduck/spinner", ResourceHandlers.ForBytes(Properties.Resources.spinner, "image/apng"));
public static readonly string[] DictionaryWords = {
"tweetdeck", "TweetDeck", "tweetduck", "TweetDuck", "TD"