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

Refactor PluginManager to use ITweetDeckBrowser & do some cleanup

This commit is contained in:
chylex 2018-02-12 15:26:21 +01:00
parent 41acd8c15b
commit ec94ea3273
8 changed files with 38 additions and 56 deletions

View File

@ -58,7 +58,10 @@ public FormBrowser(UpdaterSettings updaterSettings){
Text = Program.BrandName;
this.plugins = new PluginManager(Program.PluginPath, Program.PluginConfigFilePath);
this.browser = new TweetDeckBrowser(this, new TweetDeckBridge.Browser(this, notification));
this.contextMenu = ContextMenuBrowser.CreateMenu(this);
this.plugins = new PluginManager(browser, Program.PluginPath, Program.PluginConfigFilePath);
this.plugins.Reloaded += plugins_Reloaded;
this.plugins.Executed += plugins_Executed;
this.plugins.Reload();
@ -66,9 +69,6 @@ public FormBrowser(UpdaterSettings updaterSettings){
this.notification = new FormNotificationTweet(this, plugins);
this.notification.Show();
this.browser = new TweetDeckBrowser(this, plugins, new TweetDeckBridge.Browser(this, notification));
this.contextMenu = ContextMenuBrowser.CreateMenu(this);
Controls.Add(new MenuStrip{ Visible = false }); // fixes Alt freezing the program in Win 10 Anniversary Update
Disposed += (sender, args) => {

View File

@ -9,9 +9,6 @@
using TweetDuck.Core.Handling.General;
using TweetDuck.Core.Notification;
using TweetDuck.Core.Utils;
using TweetDuck.Plugins;
using TweetDuck.Plugins.Enums;
using TweetDuck.Plugins.Events;
using TweetDuck.Resources;
namespace TweetDuck.Core{
@ -32,11 +29,10 @@ public bool IsTweetDeckWebsite{
}
private readonly ChromiumWebBrowser browser;
private readonly PluginManager plugins;
private string prevSoundNotificationPath = null;
public TweetDeckBrowser(FormBrowser owner, PluginManager plugins, TweetDeckBridge bridge){
public TweetDeckBrowser(FormBrowser owner, TweetDeckBridge bridge){
this.browser = new ChromiumWebBrowser(TwitterUtils.TweetDeckURL){
DialogHandler = new FileDialogHandler(),
DragHandler = new DragHandlerBrowser(),
@ -57,7 +53,6 @@ public TweetDeckBrowser(FormBrowser owner, PluginManager plugins, TweetDeckBridg
this.browser.LoadError += browser_LoadError;
this.browser.RegisterAsyncJsObject("$TD", bridge);
this.browser.RegisterAsyncJsObject("$TDP", plugins.Bridge);
this.browser.BrowserSettings.BackgroundColor = (uint)TwitterUtils.BackgroundColor.ToArgb();
this.browser.Dock = DockStyle.None;
@ -68,10 +63,6 @@ public TweetDeckBrowser(FormBrowser owner, PluginManager plugins, TweetDeckBridg
owner.Controls.Add(browser);
this.plugins = plugins;
this.plugins.PluginChangedState += plugins_PluginChangedState;
this.plugins.PluginConfigureTriggered += plugins_PluginConfigureTriggered;
Program.UserConfig.MuteToggled += UserConfig_MuteToggled;
Program.UserConfig.ZoomLevelChanged += UserConfig_ZoomLevelChanged;
Program.UserConfig.SoundNotificationChanged += UserConfig_SoundNotificationInfoChanged;
@ -92,8 +83,6 @@ public void Focus(){
}
public void Dispose(){
plugins.PluginChangedState -= plugins_PluginChangedState;
Program.UserConfig.MuteToggled -= UserConfig_MuteToggled;
Program.UserConfig.ZoomLevelChanged -= UserConfig_ZoomLevelChanged;
Program.UserConfig.SoundNotificationChanged -= UserConfig_SoundNotificationInfoChanged;
@ -145,21 +134,22 @@ private void browser_FrameLoadStart(object sender, FrameLoadStartEventArgs e){
}
private void browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e){
if (e.Frame.IsMain && TwitterUtils.IsTweetDeckWebsite(e.Frame)){
e.Frame.ExecuteJavaScriptAsync(TwitterUtils.BackgroundColorOverride);
IFrame frame = e.Frame;
if (frame.IsMain && TwitterUtils.IsTweetDeckWebsite(frame)){
frame.ExecuteJavaScriptAsync(TwitterUtils.BackgroundColorOverride);
UpdateProperties();
TweetDeckBridge.RestoreSessionData(e.Frame);
ScriptLoader.ExecuteFile(e.Frame, "code.js");
TweetDeckBridge.RestoreSessionData(frame);
ScriptLoader.ExecuteFile(frame, "code.js");
InjectBrowserCSS();
ReinjectCustomCSS(Program.UserConfig.CustomBrowserCSS);
UserConfig_SoundNotificationInfoChanged(null, EventArgs.Empty);
plugins.ExecutePlugins(e.Frame, PluginEnvironment.Browser);
TweetDeckBridge.ResetStaticProperties();
if (Program.UserConfig.FirstRun){
ScriptLoader.ExecuteFile(e.Frame, "introduction.js");
ScriptLoader.ExecuteFile(frame, "introduction.js");
}
}
}
@ -178,14 +168,6 @@ private void browser_LoadError(object sender, LoadErrorEventArgs e){
}
}
private void plugins_PluginChangedState(object sender, PluginChangedStateEventArgs e){
browser.ExecuteScriptAsync("TDPF_setPluginState", e.Plugin, e.IsEnabled);
}
private void plugins_PluginConfigureTriggered(object sender, PluginEventArgs e){
browser.ExecuteScriptAsync("TDPF_configurePlugin", e.Plugin);
}
private void UserConfig_MuteToggled(object sender, EventArgs e){
UpdateProperties();
}

View File

@ -1,8 +1,12 @@
namespace TweetDuck.Plugins.Events{
sealed class PluginChangedStateEventArgs : PluginEventArgs{
using System;
namespace TweetDuck.Plugins.Events{
sealed class PluginChangedStateEventArgs : EventArgs{
public Plugin Plugin { get; }
public bool IsEnabled { get; }
public PluginChangedStateEventArgs(Plugin plugin, bool isEnabled) : base(plugin){
public PluginChangedStateEventArgs(Plugin plugin, bool isEnabled){
this.Plugin = plugin;
this.IsEnabled = isEnabled;
}
}

View File

@ -1,11 +0,0 @@
using System;
namespace TweetDuck.Plugins.Events{
class PluginEventArgs : EventArgs{
public Plugin Plugin { get; }
public PluginEventArgs(Plugin plugin){
this.Plugin = plugin;
}
}
}

View File

@ -23,7 +23,7 @@ private static string SanitizeCacheKey(string key){
public PluginBridge(PluginManager manager){
this.manager = manager;
this.manager.Reloaded += manager_Reloaded;
this.manager.PluginChangedState += manager_PluginChangedState;
this.manager.Config.PluginChangedState += Config_PluginChangedState;
}
// Event handlers
@ -32,7 +32,7 @@ private void manager_Reloaded(object sender, PluginErrorEventArgs e){
fileCache.Clear();
}
private void manager_PluginChangedState(object sender, PluginChangedStateEventArgs e){
private void Config_PluginChangedState(object sender, PluginChangedStateEventArgs e){
if (!e.IsEnabled){
int token = manager.GetTokenFromPlugin(e.Plugin);

View File

@ -6,7 +6,7 @@
namespace TweetDuck.Plugins{
sealed class PluginConfig{
public event EventHandler<PluginChangedStateEventArgs> InternalPluginChangedState; // should only be accessed from PluginManager
public event EventHandler<PluginChangedStateEventArgs> PluginChangedState;
public IEnumerable<string> DisabledPlugins => disabled;
public bool AnyDisabled => disabled.Count > 0;
@ -20,7 +20,7 @@ sealed class PluginConfig{
public void SetEnabled(Plugin plugin, bool enabled){
if ((enabled && disabled.Remove(plugin.Identifier)) || (!enabled && disabled.Add(plugin.Identifier))){
InternalPluginChangedState?.Invoke(this, new PluginChangedStateEventArgs(plugin, enabled));
PluginChangedState?.Invoke(this, new PluginChangedStateEventArgs(plugin, enabled));
}
}

View File

@ -4,6 +4,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using TweetDuck.Core;
using TweetDuck.Plugins.Enums;
using TweetDuck.Plugins.Events;
using TweetDuck.Resources;
@ -25,9 +26,8 @@ sealed class PluginManager{
public event EventHandler<PluginErrorEventArgs> Reloaded;
public event EventHandler<PluginErrorEventArgs> Executed;
public event EventHandler<PluginChangedStateEventArgs> PluginChangedState;
public event EventHandler<PluginEventArgs> PluginConfigureTriggered;
private readonly ITweetDeckBrowser browser;
private readonly string rootPath;
private readonly string configPath;
@ -35,22 +35,30 @@ sealed class PluginManager{
private readonly Dictionary<int, Plugin> tokens = new Dictionary<int, Plugin>();
private readonly Random rand = new Random();
public PluginManager(string rootPath, string configPath){
public PluginManager(ITweetDeckBrowser browser, string rootPath, string configPath){
this.browser = browser;
this.rootPath = rootPath;
this.configPath = configPath;
this.Config = new PluginConfig();
this.Bridge = new PluginBridge(this);
this.browser.OnFrameLoaded(OnFrameLoaded);
this.browser.RegisterBridge("$TDP", Bridge);
Config.Load(configPath);
Config.InternalPluginChangedState += Config_InternalPluginChangedState;
Config.PluginChangedState += Config_PluginChangedState;
}
private void Config_InternalPluginChangedState(object sender, PluginChangedStateEventArgs e){
PluginChangedState?.Invoke(this, e);
private void Config_PluginChangedState(object sender, PluginChangedStateEventArgs e){
browser.ExecuteFunction("TDPF_setPluginState", e.Plugin, e.IsEnabled);
Config.Save(configPath);
}
private void OnFrameLoaded(IFrame frame){
ExecutePlugins(frame, PluginEnvironment.Browser);
}
public bool IsPluginInstalled(string identifier){
return plugins.Any(plugin => plugin.Identifier.Equals(identifier));
}
@ -65,7 +73,7 @@ public bool IsPluginConfigurable(Plugin plugin){
public void ConfigurePlugin(Plugin plugin){
if (Bridge.WithConfigureFunction.Contains(plugin)){
PluginConfigureTriggered?.Invoke(this, new PluginEventArgs(plugin));
browser.ExecuteFunction("TDPF_configurePlugin", plugin);
}
else if (plugin.HasConfig){
if (File.Exists(plugin.ConfigPath)){

View File

@ -275,7 +275,6 @@
<SubType>Component</SubType>
</Compile>
<Compile Include="Plugins\Enums\PluginFolder.cs" />
<Compile Include="Plugins\Events\PluginEventArgs.cs" />
<Compile Include="Plugins\Plugin.cs" />
<Compile Include="Plugins\Events\PluginChangedStateEventArgs.cs" />
<Compile Include="Plugins\PluginBridge.cs" />