mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-03 14:34:08 +02:00
Create and use a custom resource handler factory
This commit is contained in:
parent
6f414d312c
commit
cd7aeaeed2
43
Core/Handling/ResourceHandlerFactory.cs
Normal file
43
Core/Handling/ResourceHandlerFactory.cs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using CefSharp;
|
||||||
|
using TweetDuck.Data;
|
||||||
|
|
||||||
|
namespace TweetDuck.Core.Handling{
|
||||||
|
sealed class ResourceHandlerFactory : IResourceHandlerFactory{
|
||||||
|
public bool HasHandlers => !handlers.IsEmpty;
|
||||||
|
|
||||||
|
private readonly ConcurrentDictionary<string, IResourceHandler> handlers = new ConcurrentDictionary<string, IResourceHandler>(StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
public IResourceHandler GetResourceHandler(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request){
|
||||||
|
try{
|
||||||
|
return handlers.TryGetValue(request.Url, out IResourceHandler handler) ? handler : null;
|
||||||
|
}finally{
|
||||||
|
request.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// registration
|
||||||
|
|
||||||
|
public bool RegisterHandler(string url, IResourceHandler handler){
|
||||||
|
if (Uri.TryCreate(url, UriKind.Absolute, out Uri uri)){
|
||||||
|
handlers.AddOrUpdate(uri.AbsoluteUri, handler, (key, prev) => handler);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool RegisterHandler(ResourceLink link){
|
||||||
|
return RegisterHandler(link.Url, link.Handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool UnregisterHandler(string url){
|
||||||
|
return handlers.TryRemove(url, out IResourceHandler _);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool UnregisterHandler(ResourceLink link){
|
||||||
|
return UnregisterHandler(link.Url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -121,18 +121,20 @@ protected FormNotificationBase(FormBrowser owner, bool enableContextMenu){
|
|||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
this.owner.FormClosed += owner_FormClosed;
|
this.owner.FormClosed += owner_FormClosed;
|
||||||
|
|
||||||
|
ResourceHandlerFactory resourceHandlerFactory = new ResourceHandlerFactory();
|
||||||
|
resourceHandlerFactory.RegisterHandler(TwitterUtils.TweetDeckURL, this.resourceHandler);
|
||||||
|
resourceHandlerFactory.RegisterHandler(TweetNotification.AppLogo);
|
||||||
|
|
||||||
this.browser = new ChromiumWebBrowser("about:blank"){
|
this.browser = new ChromiumWebBrowser("about:blank"){
|
||||||
MenuHandler = new ContextMenuNotification(this, enableContextMenu),
|
MenuHandler = new ContextMenuNotification(this, enableContextMenu),
|
||||||
JsDialogHandler = new JavaScriptDialogHandler(),
|
JsDialogHandler = new JavaScriptDialogHandler(),
|
||||||
LifeSpanHandler = new LifeSpanHandler(),
|
LifeSpanHandler = new LifeSpanHandler(),
|
||||||
RequestHandler = new RequestHandlerBase(false)
|
RequestHandler = new RequestHandlerBase(false),
|
||||||
|
ResourceHandlerFactory = resourceHandlerFactory
|
||||||
};
|
};
|
||||||
|
|
||||||
this.browser.Dock = DockStyle.None;
|
this.browser.Dock = DockStyle.None;
|
||||||
this.browser.ClientSize = ClientSize;
|
this.browser.ClientSize = ClientSize;
|
||||||
|
|
||||||
this.browser.SetupResourceHandler(TwitterUtils.TweetDeckURL, this.resourceHandler);
|
|
||||||
this.browser.SetupResourceHandler(TweetNotification.AppLogo);
|
|
||||||
this.browser.SetupZoomEvents();
|
this.browser.SetupZoomEvents();
|
||||||
|
|
||||||
Controls.Add(browser);
|
Controls.Add(browser);
|
||||||
|
@ -63,12 +63,16 @@ private FormGuide(string url, FormBrowser owner){
|
|||||||
Text = Program.BrandName+" Guide";
|
Text = Program.BrandName+" Guide";
|
||||||
Size = new Size(owner.Size.Width*3/4, owner.Size.Height*3/4);
|
Size = new Size(owner.Size.Width*3/4, owner.Size.Height*3/4);
|
||||||
VisibleChanged += (sender, args) => this.MoveToCenter(owner);
|
VisibleChanged += (sender, args) => this.MoveToCenter(owner);
|
||||||
|
|
||||||
|
ResourceHandlerFactory resourceHandlerFactory = new ResourceHandlerFactory();
|
||||||
|
resourceHandlerFactory.RegisterHandler(DummyPage);
|
||||||
|
|
||||||
this.browser = new ChromiumWebBrowser(url){
|
this.browser = new ChromiumWebBrowser(url){
|
||||||
MenuHandler = new ContextMenuGuide(owner),
|
MenuHandler = new ContextMenuGuide(owner),
|
||||||
JsDialogHandler = new JavaScriptDialogHandler(),
|
JsDialogHandler = new JavaScriptDialogHandler(),
|
||||||
LifeSpanHandler = new LifeSpanHandler(),
|
LifeSpanHandler = new LifeSpanHandler(),
|
||||||
RequestHandler = new RequestHandlerBase(true)
|
RequestHandler = new RequestHandlerBase(true),
|
||||||
|
ResourceHandlerFactory = resourceHandlerFactory
|
||||||
};
|
};
|
||||||
|
|
||||||
browser.LoadingStateChanged += browser_LoadingStateChanged;
|
browser.LoadingStateChanged += browser_LoadingStateChanged;
|
||||||
@ -77,8 +81,6 @@ private FormGuide(string url, FormBrowser owner){
|
|||||||
browser.BrowserSettings.BackgroundColor = (uint)BackColor.ToArgb();
|
browser.BrowserSettings.BackgroundColor = (uint)BackColor.ToArgb();
|
||||||
browser.Dock = DockStyle.None;
|
browser.Dock = DockStyle.None;
|
||||||
browser.Location = ControlExtensions.InvisibleLocation;
|
browser.Location = ControlExtensions.InvisibleLocation;
|
||||||
|
|
||||||
browser.SetupResourceHandler(DummyPage);
|
|
||||||
browser.SetupZoomEvents();
|
browser.SetupZoomEvents();
|
||||||
|
|
||||||
Controls.Add(browser);
|
Controls.Add(browser);
|
||||||
|
@ -39,10 +39,14 @@ public bool IsTweetDeckWebsite{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private readonly ChromiumWebBrowser browser;
|
private readonly ChromiumWebBrowser browser;
|
||||||
|
private readonly ResourceHandlerFactory resourceHandlerFactory = new ResourceHandlerFactory();
|
||||||
|
|
||||||
private string prevSoundNotificationPath = null;
|
private string prevSoundNotificationPath = null;
|
||||||
|
|
||||||
public TweetDeckBrowser(FormBrowser owner, PluginManager plugins, TweetDeckBridge tdBridge, UpdateBridge updateBridge){
|
public TweetDeckBrowser(FormBrowser owner, PluginManager plugins, TweetDeckBridge tdBridge, UpdateBridge updateBridge){
|
||||||
|
resourceHandlerFactory.RegisterHandler(TweetNotification.AppLogo);
|
||||||
|
resourceHandlerFactory.RegisterHandler(TwitterUtils.LoadingSpinner);
|
||||||
|
|
||||||
RequestHandlerBrowser requestHandler = new RequestHandlerBrowser();
|
RequestHandlerBrowser requestHandler = new RequestHandlerBrowser();
|
||||||
|
|
||||||
this.browser = new ChromiumWebBrowser(TwitterUtils.TweetDeckURL){
|
this.browser = new ChromiumWebBrowser(TwitterUtils.TweetDeckURL){
|
||||||
@ -52,7 +56,8 @@ public TweetDeckBrowser(FormBrowser owner, PluginManager plugins, TweetDeckBridg
|
|||||||
JsDialogHandler = new JavaScriptDialogHandler(),
|
JsDialogHandler = new JavaScriptDialogHandler(),
|
||||||
KeyboardHandler = new KeyboardHandlerBrowser(owner),
|
KeyboardHandler = new KeyboardHandlerBrowser(owner),
|
||||||
LifeSpanHandler = new LifeSpanHandler(),
|
LifeSpanHandler = new LifeSpanHandler(),
|
||||||
RequestHandler = requestHandler
|
RequestHandler = requestHandler,
|
||||||
|
ResourceHandlerFactory = resourceHandlerFactory
|
||||||
};
|
};
|
||||||
|
|
||||||
this.browser.LoadingStateChanged += browser_LoadingStateChanged;
|
this.browser.LoadingStateChanged += browser_LoadingStateChanged;
|
||||||
@ -66,11 +71,8 @@ public TweetDeckBrowser(FormBrowser owner, PluginManager plugins, TweetDeckBridg
|
|||||||
this.browser.BrowserSettings.BackgroundColor = (uint)TwitterUtils.BackgroundColor.ToArgb();
|
this.browser.BrowserSettings.BackgroundColor = (uint)TwitterUtils.BackgroundColor.ToArgb();
|
||||||
this.browser.Dock = DockStyle.None;
|
this.browser.Dock = DockStyle.None;
|
||||||
this.browser.Location = ControlExtensions.InvisibleLocation;
|
this.browser.Location = ControlExtensions.InvisibleLocation;
|
||||||
|
|
||||||
this.browser.SetupResourceHandler(TweetNotification.AppLogo);
|
|
||||||
this.browser.SetupResourceHandler(TwitterUtils.LoadingSpinner);
|
|
||||||
this.browser.SetupZoomEvents();
|
this.browser.SetupZoomEvents();
|
||||||
|
|
||||||
owner.Controls.Add(browser);
|
owner.Controls.Add(browser);
|
||||||
plugins.Register(browser, PluginEnvironment.Browser, owner, true);
|
plugins.Register(browser, PluginEnvironment.Browser, owner, true);
|
||||||
|
|
||||||
@ -174,10 +176,16 @@ private void Config_SoundNotificationInfoChanged(object sender, EventArgs e){
|
|||||||
|
|
||||||
bool hasCustomSound = Config.IsCustomSoundNotificationSet;
|
bool hasCustomSound = Config.IsCustomSoundNotificationSet;
|
||||||
string newNotificationPath = Config.NotificationSoundPath;
|
string newNotificationPath = Config.NotificationSoundPath;
|
||||||
|
|
||||||
if (prevSoundNotificationPath != newNotificationPath){
|
if (prevSoundNotificationPath != newNotificationPath){
|
||||||
browser.SetupResourceHandler(soundUrl, hasCustomSound ? SoundNotification.CreateFileHandler(newNotificationPath) : null);
|
|
||||||
prevSoundNotificationPath = newNotificationPath;
|
prevSoundNotificationPath = newNotificationPath;
|
||||||
|
|
||||||
|
if (hasCustomSound){
|
||||||
|
resourceHandlerFactory.RegisterHandler(soundUrl, SoundNotification.CreateFileHandler(newNotificationPath));
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
resourceHandlerFactory.UnregisterHandler(soundUrl);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
browser.ExecuteScriptAsync("TDGF_setSoundNotificationData", hasCustomSound, Config.NotificationSoundVolume);
|
browser.ExecuteScriptAsync("TDGF_setSoundNotificationData", hasCustomSound, Config.NotificationSoundVolume);
|
||||||
|
@ -60,21 +60,6 @@ public static ChromiumWebBrowser AsControl(this IWebBrowser browserControl){
|
|||||||
return (ChromiumWebBrowser)browserControl;
|
return (ChromiumWebBrowser)browserControl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetupResourceHandler(this ChromiumWebBrowser browser, string url, IResourceHandler handler){
|
|
||||||
DefaultResourceHandlerFactory factory = (DefaultResourceHandlerFactory)browser.ResourceHandlerFactory;
|
|
||||||
|
|
||||||
if (handler == null){
|
|
||||||
factory.UnregisterHandler(url);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
factory.RegisterHandler(url, handler);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void SetupResourceHandler(this ChromiumWebBrowser browser, ResourceLink resource){
|
|
||||||
browser.SetupResourceHandler(resource.Url, resource.Handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void SetupZoomEvents(this ChromiumWebBrowser browser){
|
public static void SetupZoomEvents(this ChromiumWebBrowser browser){
|
||||||
void UpdateZoomLevel(object sender, EventArgs args){
|
void UpdateZoomLevel(object sender, EventArgs args){
|
||||||
SetZoomLevel(browser.GetBrowser(), Config.ZoomLevel);
|
SetZoomLevel(browser.GetBrowser(), Config.ZoomLevel);
|
||||||
|
@ -95,6 +95,7 @@
|
|||||||
<Compile Include="Core\Handling\KeyboardHandlerNotification.cs" />
|
<Compile Include="Core\Handling\KeyboardHandlerNotification.cs" />
|
||||||
<Compile Include="Core\Handling\RequestHandlerBase.cs" />
|
<Compile Include="Core\Handling\RequestHandlerBase.cs" />
|
||||||
<Compile Include="Core\Handling\RequestHandlerBrowser.cs" />
|
<Compile Include="Core\Handling\RequestHandlerBrowser.cs" />
|
||||||
|
<Compile Include="Core\Handling\ResourceHandlerFactory.cs" />
|
||||||
<Compile Include="Core\Handling\ResourceHandlerNotification.cs" />
|
<Compile Include="Core\Handling\ResourceHandlerNotification.cs" />
|
||||||
<Compile Include="Core\Management\ContextInfo.cs" />
|
<Compile Include="Core\Management\ContextInfo.cs" />
|
||||||
<Compile Include="Core\Notification\Example\FormNotificationExample.cs">
|
<Compile Include="Core\Notification\Example\FormNotificationExample.cs">
|
||||||
|
Loading…
Reference in New Issue
Block a user