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:
parent
41acd8c15b
commit
ec94ea3273
@ -57,17 +57,17 @@ public FormBrowser(UpdaterSettings updaterSettings){
|
|||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
Text = Program.BrandName;
|
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.Reloaded += plugins_Reloaded;
|
||||||
this.plugins.Executed += plugins_Executed;
|
this.plugins.Executed += plugins_Executed;
|
||||||
this.plugins.Reload();
|
this.plugins.Reload();
|
||||||
|
|
||||||
this.notification = new FormNotificationTweet(this, plugins);
|
this.notification = new FormNotificationTweet(this, plugins);
|
||||||
this.notification.Show();
|
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
|
Controls.Add(new MenuStrip{ Visible = false }); // fixes Alt freezing the program in Win 10 Anniversary Update
|
||||||
|
|
||||||
|
@ -9,9 +9,6 @@
|
|||||||
using TweetDuck.Core.Handling.General;
|
using TweetDuck.Core.Handling.General;
|
||||||
using TweetDuck.Core.Notification;
|
using TweetDuck.Core.Notification;
|
||||||
using TweetDuck.Core.Utils;
|
using TweetDuck.Core.Utils;
|
||||||
using TweetDuck.Plugins;
|
|
||||||
using TweetDuck.Plugins.Enums;
|
|
||||||
using TweetDuck.Plugins.Events;
|
|
||||||
using TweetDuck.Resources;
|
using TweetDuck.Resources;
|
||||||
|
|
||||||
namespace TweetDuck.Core{
|
namespace TweetDuck.Core{
|
||||||
@ -32,11 +29,10 @@ public bool IsTweetDeckWebsite{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private readonly ChromiumWebBrowser browser;
|
private readonly ChromiumWebBrowser browser;
|
||||||
private readonly PluginManager plugins;
|
|
||||||
|
|
||||||
private string prevSoundNotificationPath = null;
|
private string prevSoundNotificationPath = null;
|
||||||
|
|
||||||
public TweetDeckBrowser(FormBrowser owner, PluginManager plugins, TweetDeckBridge bridge){
|
public TweetDeckBrowser(FormBrowser owner, TweetDeckBridge bridge){
|
||||||
this.browser = new ChromiumWebBrowser(TwitterUtils.TweetDeckURL){
|
this.browser = new ChromiumWebBrowser(TwitterUtils.TweetDeckURL){
|
||||||
DialogHandler = new FileDialogHandler(),
|
DialogHandler = new FileDialogHandler(),
|
||||||
DragHandler = new DragHandlerBrowser(),
|
DragHandler = new DragHandlerBrowser(),
|
||||||
@ -57,7 +53,6 @@ public TweetDeckBrowser(FormBrowser owner, PluginManager plugins, TweetDeckBridg
|
|||||||
this.browser.LoadError += browser_LoadError;
|
this.browser.LoadError += browser_LoadError;
|
||||||
|
|
||||||
this.browser.RegisterAsyncJsObject("$TD", bridge);
|
this.browser.RegisterAsyncJsObject("$TD", bridge);
|
||||||
this.browser.RegisterAsyncJsObject("$TDP", plugins.Bridge);
|
|
||||||
|
|
||||||
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;
|
||||||
@ -67,10 +62,6 @@ public TweetDeckBrowser(FormBrowser owner, PluginManager plugins, TweetDeckBridg
|
|||||||
this.browser.SetupResourceHandler(TwitterUtils.LoadingSpinner);
|
this.browser.SetupResourceHandler(TwitterUtils.LoadingSpinner);
|
||||||
|
|
||||||
owner.Controls.Add(browser);
|
owner.Controls.Add(browser);
|
||||||
|
|
||||||
this.plugins = plugins;
|
|
||||||
this.plugins.PluginChangedState += plugins_PluginChangedState;
|
|
||||||
this.plugins.PluginConfigureTriggered += plugins_PluginConfigureTriggered;
|
|
||||||
|
|
||||||
Program.UserConfig.MuteToggled += UserConfig_MuteToggled;
|
Program.UserConfig.MuteToggled += UserConfig_MuteToggled;
|
||||||
Program.UserConfig.ZoomLevelChanged += UserConfig_ZoomLevelChanged;
|
Program.UserConfig.ZoomLevelChanged += UserConfig_ZoomLevelChanged;
|
||||||
@ -92,8 +83,6 @@ public void Focus(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose(){
|
public void Dispose(){
|
||||||
plugins.PluginChangedState -= plugins_PluginChangedState;
|
|
||||||
|
|
||||||
Program.UserConfig.MuteToggled -= UserConfig_MuteToggled;
|
Program.UserConfig.MuteToggled -= UserConfig_MuteToggled;
|
||||||
Program.UserConfig.ZoomLevelChanged -= UserConfig_ZoomLevelChanged;
|
Program.UserConfig.ZoomLevelChanged -= UserConfig_ZoomLevelChanged;
|
||||||
Program.UserConfig.SoundNotificationChanged -= UserConfig_SoundNotificationInfoChanged;
|
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){
|
private void browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e){
|
||||||
if (e.Frame.IsMain && TwitterUtils.IsTweetDeckWebsite(e.Frame)){
|
IFrame frame = e.Frame;
|
||||||
e.Frame.ExecuteJavaScriptAsync(TwitterUtils.BackgroundColorOverride);
|
|
||||||
|
if (frame.IsMain && TwitterUtils.IsTweetDeckWebsite(frame)){
|
||||||
|
frame.ExecuteJavaScriptAsync(TwitterUtils.BackgroundColorOverride);
|
||||||
|
|
||||||
UpdateProperties();
|
UpdateProperties();
|
||||||
TweetDeckBridge.RestoreSessionData(e.Frame);
|
TweetDeckBridge.RestoreSessionData(frame);
|
||||||
ScriptLoader.ExecuteFile(e.Frame, "code.js");
|
ScriptLoader.ExecuteFile(frame, "code.js");
|
||||||
InjectBrowserCSS();
|
InjectBrowserCSS();
|
||||||
ReinjectCustomCSS(Program.UserConfig.CustomBrowserCSS);
|
ReinjectCustomCSS(Program.UserConfig.CustomBrowserCSS);
|
||||||
UserConfig_SoundNotificationInfoChanged(null, EventArgs.Empty);
|
UserConfig_SoundNotificationInfoChanged(null, EventArgs.Empty);
|
||||||
plugins.ExecutePlugins(e.Frame, PluginEnvironment.Browser);
|
|
||||||
|
|
||||||
TweetDeckBridge.ResetStaticProperties();
|
TweetDeckBridge.ResetStaticProperties();
|
||||||
|
|
||||||
if (Program.UserConfig.FirstRun){
|
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){
|
private void UserConfig_MuteToggled(object sender, EventArgs e){
|
||||||
UpdateProperties();
|
UpdateProperties();
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
namespace TweetDuck.Plugins.Events{
|
using System;
|
||||||
sealed class PluginChangedStateEventArgs : PluginEventArgs{
|
|
||||||
|
namespace TweetDuck.Plugins.Events{
|
||||||
|
sealed class PluginChangedStateEventArgs : EventArgs{
|
||||||
|
public Plugin Plugin { get; }
|
||||||
public bool IsEnabled { 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;
|
this.IsEnabled = isEnabled;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace TweetDuck.Plugins.Events{
|
|
||||||
class PluginEventArgs : EventArgs{
|
|
||||||
public Plugin Plugin { get; }
|
|
||||||
|
|
||||||
public PluginEventArgs(Plugin plugin){
|
|
||||||
this.Plugin = plugin;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -23,7 +23,7 @@ private static string SanitizeCacheKey(string key){
|
|||||||
public PluginBridge(PluginManager manager){
|
public PluginBridge(PluginManager manager){
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
this.manager.Reloaded += manager_Reloaded;
|
this.manager.Reloaded += manager_Reloaded;
|
||||||
this.manager.PluginChangedState += manager_PluginChangedState;
|
this.manager.Config.PluginChangedState += Config_PluginChangedState;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Event handlers
|
// Event handlers
|
||||||
@ -32,7 +32,7 @@ private void manager_Reloaded(object sender, PluginErrorEventArgs e){
|
|||||||
fileCache.Clear();
|
fileCache.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void manager_PluginChangedState(object sender, PluginChangedStateEventArgs e){
|
private void Config_PluginChangedState(object sender, PluginChangedStateEventArgs e){
|
||||||
if (!e.IsEnabled){
|
if (!e.IsEnabled){
|
||||||
int token = manager.GetTokenFromPlugin(e.Plugin);
|
int token = manager.GetTokenFromPlugin(e.Plugin);
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
namespace TweetDuck.Plugins{
|
namespace TweetDuck.Plugins{
|
||||||
sealed class PluginConfig{
|
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 IEnumerable<string> DisabledPlugins => disabled;
|
||||||
public bool AnyDisabled => disabled.Count > 0;
|
public bool AnyDisabled => disabled.Count > 0;
|
||||||
@ -20,7 +20,7 @@ sealed class PluginConfig{
|
|||||||
|
|
||||||
public void SetEnabled(Plugin plugin, bool enabled){
|
public void SetEnabled(Plugin plugin, bool enabled){
|
||||||
if ((enabled && disabled.Remove(plugin.Identifier)) || (!enabled && disabled.Add(plugin.Identifier))){
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using TweetDuck.Core;
|
||||||
using TweetDuck.Plugins.Enums;
|
using TweetDuck.Plugins.Enums;
|
||||||
using TweetDuck.Plugins.Events;
|
using TweetDuck.Plugins.Events;
|
||||||
using TweetDuck.Resources;
|
using TweetDuck.Resources;
|
||||||
@ -25,9 +26,8 @@ sealed class PluginManager{
|
|||||||
|
|
||||||
public event EventHandler<PluginErrorEventArgs> Reloaded;
|
public event EventHandler<PluginErrorEventArgs> Reloaded;
|
||||||
public event EventHandler<PluginErrorEventArgs> Executed;
|
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 rootPath;
|
||||||
private readonly string configPath;
|
private readonly string configPath;
|
||||||
|
|
||||||
@ -35,22 +35,30 @@ sealed class PluginManager{
|
|||||||
private readonly Dictionary<int, Plugin> tokens = new Dictionary<int, Plugin>();
|
private readonly Dictionary<int, Plugin> tokens = new Dictionary<int, Plugin>();
|
||||||
private readonly Random rand = new Random();
|
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.rootPath = rootPath;
|
||||||
this.configPath = configPath;
|
this.configPath = configPath;
|
||||||
|
|
||||||
this.Config = new PluginConfig();
|
this.Config = new PluginConfig();
|
||||||
this.Bridge = new PluginBridge(this);
|
this.Bridge = new PluginBridge(this);
|
||||||
|
|
||||||
|
this.browser.OnFrameLoaded(OnFrameLoaded);
|
||||||
|
this.browser.RegisterBridge("$TDP", Bridge);
|
||||||
|
|
||||||
Config.Load(configPath);
|
Config.Load(configPath);
|
||||||
Config.InternalPluginChangedState += Config_InternalPluginChangedState;
|
Config.PluginChangedState += Config_PluginChangedState;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Config_InternalPluginChangedState(object sender, PluginChangedStateEventArgs e){
|
private void Config_PluginChangedState(object sender, PluginChangedStateEventArgs e){
|
||||||
PluginChangedState?.Invoke(this, e);
|
browser.ExecuteFunction("TDPF_setPluginState", e.Plugin, e.IsEnabled);
|
||||||
Config.Save(configPath);
|
Config.Save(configPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnFrameLoaded(IFrame frame){
|
||||||
|
ExecutePlugins(frame, PluginEnvironment.Browser);
|
||||||
|
}
|
||||||
|
|
||||||
public bool IsPluginInstalled(string identifier){
|
public bool IsPluginInstalled(string identifier){
|
||||||
return plugins.Any(plugin => plugin.Identifier.Equals(identifier));
|
return plugins.Any(plugin => plugin.Identifier.Equals(identifier));
|
||||||
}
|
}
|
||||||
@ -65,7 +73,7 @@ public bool IsPluginConfigurable(Plugin plugin){
|
|||||||
|
|
||||||
public void ConfigurePlugin(Plugin plugin){
|
public void ConfigurePlugin(Plugin plugin){
|
||||||
if (Bridge.WithConfigureFunction.Contains(plugin)){
|
if (Bridge.WithConfigureFunction.Contains(plugin)){
|
||||||
PluginConfigureTriggered?.Invoke(this, new PluginEventArgs(plugin));
|
browser.ExecuteFunction("TDPF_configurePlugin", plugin);
|
||||||
}
|
}
|
||||||
else if (plugin.HasConfig){
|
else if (plugin.HasConfig){
|
||||||
if (File.Exists(plugin.ConfigPath)){
|
if (File.Exists(plugin.ConfigPath)){
|
||||||
|
@ -275,7 +275,6 @@
|
|||||||
<SubType>Component</SubType>
|
<SubType>Component</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="Plugins\Enums\PluginFolder.cs" />
|
<Compile Include="Plugins\Enums\PluginFolder.cs" />
|
||||||
<Compile Include="Plugins\Events\PluginEventArgs.cs" />
|
|
||||||
<Compile Include="Plugins\Plugin.cs" />
|
<Compile Include="Plugins\Plugin.cs" />
|
||||||
<Compile Include="Plugins\Events\PluginChangedStateEventArgs.cs" />
|
<Compile Include="Plugins\Events\PluginChangedStateEventArgs.cs" />
|
||||||
<Compile Include="Plugins\PluginBridge.cs" />
|
<Compile Include="Plugins\PluginBridge.cs" />
|
||||||
|
Loading…
Reference in New Issue
Block a user