diff --git a/Core/FormBrowser.cs b/Core/FormBrowser.cs index 18d7a80c..169be2d8 100644 --- a/Core/FormBrowser.cs +++ b/Core/FormBrowser.cs @@ -57,17 +57,17 @@ public FormBrowser(UpdaterSettings updaterSettings){ InitializeComponent(); Text = Program.BrandName; + + this.browser = new TweetDeckBrowser(this, new TweetDeckBridge.Browser(this, notification)); + this.contextMenu = ContextMenuBrowser.CreateMenu(this); - this.plugins = new PluginManager(Program.PluginPath, Program.PluginConfigFilePath); + this.plugins = new PluginManager(browser, Program.PluginPath, Program.PluginConfigFilePath); this.plugins.Reloaded += plugins_Reloaded; this.plugins.Executed += plugins_Executed; this.plugins.Reload(); 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 diff --git a/Core/TweetDeckBrowser.cs b/Core/TweetDeckBrowser.cs index 04abfb51..2bd63a58 100644 --- a/Core/TweetDeckBrowser.cs +++ b/Core/TweetDeckBrowser.cs @@ -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; @@ -67,10 +62,6 @@ public TweetDeckBrowser(FormBrowser owner, PluginManager plugins, TweetDeckBridg this.browser.SetupResourceHandler(TwitterUtils.LoadingSpinner); 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; @@ -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(); } diff --git a/Plugins/Events/PluginChangedStateEventArgs.cs b/Plugins/Events/PluginChangedStateEventArgs.cs index 7e024559..5d643c23 100644 --- a/Plugins/Events/PluginChangedStateEventArgs.cs +++ b/Plugins/Events/PluginChangedStateEventArgs.cs @@ -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; } } diff --git a/Plugins/Events/PluginEventArgs.cs b/Plugins/Events/PluginEventArgs.cs deleted file mode 100644 index a41c3051..00000000 --- a/Plugins/Events/PluginEventArgs.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace TweetDuck.Plugins.Events{ - class PluginEventArgs : EventArgs{ - public Plugin Plugin { get; } - - public PluginEventArgs(Plugin plugin){ - this.Plugin = plugin; - } - } -} diff --git a/Plugins/PluginBridge.cs b/Plugins/PluginBridge.cs index 7436cf6e..8006c434 100644 --- a/Plugins/PluginBridge.cs +++ b/Plugins/PluginBridge.cs @@ -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); diff --git a/Plugins/PluginConfig.cs b/Plugins/PluginConfig.cs index a35b2730..65981cde 100644 --- a/Plugins/PluginConfig.cs +++ b/Plugins/PluginConfig.cs @@ -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)); } } diff --git a/Plugins/PluginManager.cs b/Plugins/PluginManager.cs index 978fb046..5ea1609e 100644 --- a/Plugins/PluginManager.cs +++ b/Plugins/PluginManager.cs @@ -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)){ diff --git a/TweetDuck.csproj b/TweetDuck.csproj index 3b7a1e04..0731df46 100644 --- a/TweetDuck.csproj +++ b/TweetDuck.csproj @@ -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" />