From 03d50c847b3390e2a225e13f14ef92b6e8ee817b Mon Sep 17 00:00:00 2001 From: chylex <contact@chylex.com> Date: Mon, 12 Feb 2018 10:40:00 +0100 Subject: [PATCH] Add 'Configure' button to plugins with a configure() method & close dialog afterwards --- Core/Other/FormPlugins.cs | 2 +- Core/TweetDeckBrowser.cs | 5 ++++ Plugins/Controls/PluginControl.Designer.cs | 24 +++++++++---------- Plugins/Controls/PluginControl.cs | 12 ++++------ Plugins/Events/PluginChangedStateEventArgs.cs | 10 +++----- Plugins/Events/PluginEventArgs.cs | 11 +++++++++ Plugins/PluginBridge.cs | 11 ++++++++- Plugins/PluginManager.cs | 20 ++++++++++++++++ Resources/Scripts/plugins.browser.js | 11 +++++++++ TweetDuck.csproj | 1 + 10 files changed, 79 insertions(+), 28 deletions(-) create mode 100644 Plugins/Events/PluginEventArgs.cs diff --git a/Core/Other/FormPlugins.cs b/Core/Other/FormPlugins.cs index 6a6e5043..9ac67e12 100644 --- a/Core/Other/FormPlugins.cs +++ b/Core/Other/FormPlugins.cs @@ -76,7 +76,7 @@ private void flowLayoutPlugins_Resize(object sender, EventArgs e){ } private void btnOpenFolder_Click(object sender, EventArgs e){ - using(Process.Start("explorer.exe", "\""+pluginManager.PathCustomPlugins+"\"")){} + using(Process.Start("explorer.exe", '"'+pluginManager.PathCustomPlugins+'"')){} } private void btnReload_Click(object sender, EventArgs e){ diff --git a/Core/TweetDeckBrowser.cs b/Core/TweetDeckBrowser.cs index 9684ff54..57c98f97 100644 --- a/Core/TweetDeckBrowser.cs +++ b/Core/TweetDeckBrowser.cs @@ -71,6 +71,7 @@ public TweetDeckBrowser(FormBrowser owner, PluginManager plugins, TweetDeckBridg this.plugins = plugins; this.plugins.PluginChangedState += plugins_PluginChangedState; + this.plugins.PluginConfigureTriggered += plugins_PluginConfigureTriggered; Program.UserConfig.MuteToggled += UserConfig_MuteToggled; Program.UserConfig.ZoomLevelChanged += UserConfig_ZoomLevelChanged; @@ -164,6 +165,10 @@ private void plugins_PluginChangedState(object sender, PluginChangedStateEventAr 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/Controls/PluginControl.Designer.cs b/Plugins/Controls/PluginControl.Designer.cs index decdd47a..7ddfb43f 100644 --- a/Plugins/Controls/PluginControl.Designer.cs +++ b/Plugins/Controls/PluginControl.Designer.cs @@ -31,7 +31,7 @@ private void InitializeComponent() { this.flowLayoutInfo = new System.Windows.Forms.FlowLayoutPanel(); this.labelWebsite = new System.Windows.Forms.Label(); this.labelVersion = new System.Windows.Forms.Label(); - this.btnOpenConfig = new System.Windows.Forms.Button(); + this.btnConfigure = new System.Windows.Forms.Button(); this.labelType = new TweetDuck.Core.Controls.LabelVertical(); this.panelDescription.SuspendLayout(); this.flowLayoutInfo.SuspendLayout(); @@ -135,16 +135,16 @@ private void InitializeComponent() { this.labelVersion.TextAlign = System.Drawing.ContentAlignment.TopRight; this.labelVersion.UseMnemonic = false; // - // btnOpenConfig + // btnConfigure // - this.btnOpenConfig.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.btnOpenConfig.Location = new System.Drawing.Point(382, 80); - this.btnOpenConfig.Name = "btnOpenConfig"; - this.btnOpenConfig.Size = new System.Drawing.Size(68, 23); - this.btnOpenConfig.TabIndex = 4; - this.btnOpenConfig.Text = "Configure"; - this.btnOpenConfig.UseVisualStyleBackColor = true; - this.btnOpenConfig.Click += new System.EventHandler(this.btnOpenConfig_Click); + this.btnConfigure.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.btnConfigure.Location = new System.Drawing.Point(382, 80); + this.btnConfigure.Name = "btnConfigure"; + this.btnConfigure.Size = new System.Drawing.Size(68, 23); + this.btnConfigure.TabIndex = 4; + this.btnConfigure.Text = "Configure"; + this.btnConfigure.UseVisualStyleBackColor = true; + this.btnConfigure.Click += new System.EventHandler(this.btnConfigure_Click); // // labelType // @@ -163,7 +163,7 @@ private void InitializeComponent() { this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.labelType); - this.Controls.Add(this.btnOpenConfig); + this.Controls.Add(this.btnConfigure); this.Controls.Add(this.flowLayoutInfo); this.Controls.Add(this.panelDescription); this.Controls.Add(this.labelName); @@ -194,7 +194,7 @@ private void InitializeComponent() { private System.Windows.Forms.FlowLayoutPanel flowLayoutInfo; private System.Windows.Forms.Label labelWebsite; private System.Windows.Forms.Label labelVersion; - private System.Windows.Forms.Button btnOpenConfig; + private System.Windows.Forms.Button btnConfigure; private Core.Controls.LabelVertical labelType; } } diff --git a/Plugins/Controls/PluginControl.cs b/Plugins/Controls/PluginControl.cs index ba1818b1..b1c4ca80 100644 --- a/Plugins/Controls/PluginControl.cs +++ b/Plugins/Controls/PluginControl.cs @@ -1,7 +1,5 @@ using System; -using System.Diagnostics; using System.Drawing; -using System.IO; using System.Windows.Forms; using TweetDuck.Core.Controls; using TweetDuck.Core.Utils; @@ -57,8 +55,9 @@ private void labelWebsite_Click(object sender, EventArgs e){ } } - private void btnOpenConfig_Click(object sender, EventArgs e){ - using(Process.Start("explorer.exe", "/select,\""+plugin.ConfigPath.Replace('/', '\\')+"\"")){} + private void btnConfigure_Click(object sender, EventArgs e){ + pluginManager.ConfigurePlugin(plugin); + ParentForm?.Close(); } private void btnToggleState_Click(object sender, EventArgs e){ @@ -87,14 +86,13 @@ private void UpdatePluginState(){ labelName.ForeColor = textColor; labelDescription.ForeColor = textColor; btnToggleState.Text = isEnabled ? "Disable" : "Enable"; - btnOpenConfig.Visible = plugin.HasConfig; - btnOpenConfig.Enabled = btnOpenConfig.Visible && File.Exists(plugin.ConfigPath); + btnConfigure.Visible = isEnabled && pluginManager.IsPluginConfigurable(plugin); } else{ labelName.ForeColor = Color.DarkRed; labelDescription.ForeColor = Color.DarkRed; btnToggleState.Visible = false; - btnOpenConfig.Visible = false; + btnConfigure.Visible = false; } } } diff --git a/Plugins/Events/PluginChangedStateEventArgs.cs b/Plugins/Events/PluginChangedStateEventArgs.cs index 5d643c23..7e024559 100644 --- a/Plugins/Events/PluginChangedStateEventArgs.cs +++ b/Plugins/Events/PluginChangedStateEventArgs.cs @@ -1,12 +1,8 @@ -using System; - -namespace TweetDuck.Plugins.Events{ - sealed class PluginChangedStateEventArgs : EventArgs{ - public Plugin Plugin { get; } +namespace TweetDuck.Plugins.Events{ + sealed class PluginChangedStateEventArgs : PluginEventArgs{ public bool IsEnabled { get; } - public PluginChangedStateEventArgs(Plugin plugin, bool isEnabled){ - this.Plugin = plugin; + public PluginChangedStateEventArgs(Plugin plugin, bool isEnabled) : base(plugin){ this.IsEnabled = isEnabled; } } diff --git a/Plugins/Events/PluginEventArgs.cs b/Plugins/Events/PluginEventArgs.cs new file mode 100644 index 00000000..a41c3051 --- /dev/null +++ b/Plugins/Events/PluginEventArgs.cs @@ -0,0 +1,11 @@ +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 8d6eaf93..7436cf6e 100644 --- a/Plugins/PluginBridge.cs +++ b/Plugins/PluginBridge.cs @@ -15,9 +15,10 @@ private static string SanitizeCacheKey(string key){ private readonly PluginManager manager; private readonly TwoKeyDictionary<int, string, string> fileCache = new TwoKeyDictionary<int, string, string>(4, 2); - private readonly TwoKeyDictionary<int, string, InjectedHTML> notificationInjections = new TwoKeyDictionary<int,string,InjectedHTML>(4, 1); + private readonly TwoKeyDictionary<int, string, InjectedHTML> notificationInjections = new TwoKeyDictionary<int, string, InjectedHTML>(4, 1); public IEnumerable<InjectedHTML> NotificationInjections => notificationInjections.InnerValues; + public HashSet<Plugin> WithConfigureFunction { get; } = new HashSet<Plugin>(); public PluginBridge(PluginManager manager){ this.manager = manager; @@ -114,5 +115,13 @@ public void InjectIntoNotificationsBefore(int token, string key, string search, public void InjectIntoNotificationsAfter(int token, string key, string search, string html){ notificationInjections[token, key] = new InjectedHTML(InjectedHTML.Position.After, search, html); } + + public void SetConfigurable(int token){ + Plugin plugin = manager.GetPluginFromToken(token); + + if (plugin != null){ + WithConfigureFunction.Add(plugin); + } + } } } diff --git a/Plugins/PluginManager.cs b/Plugins/PluginManager.cs index 463eefb4..978fb046 100644 --- a/Plugins/PluginManager.cs +++ b/Plugins/PluginManager.cs @@ -1,6 +1,7 @@ using CefSharp; using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using TweetDuck.Plugins.Enums; @@ -25,6 +26,7 @@ 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 string rootPath; private readonly string configPath; @@ -57,6 +59,24 @@ public bool HasAnyPlugin(PluginEnvironment environment){ return plugins.Any(plugin => plugin.Environments.HasFlag(environment)); } + public bool IsPluginConfigurable(Plugin plugin){ + return plugin.HasConfig || Bridge.WithConfigureFunction.Contains(plugin); + } + + public void ConfigurePlugin(Plugin plugin){ + if (Bridge.WithConfigureFunction.Contains(plugin)){ + PluginConfigureTriggered?.Invoke(this, new PluginEventArgs(plugin)); + } + else if (plugin.HasConfig){ + if (File.Exists(plugin.ConfigPath)){ + using(Process.Start("explorer.exe", "/select,\""+plugin.ConfigPath.Replace('/', '\\')+"\"")){} + } + else{ + using(Process.Start("explorer.exe", '"'+plugin.GetPluginFolder(PluginFolder.Data).Replace('/', '\\')+'"')){} + } + } + } + public int GetTokenFromPlugin(Plugin plugin){ foreach(KeyValuePair<int, Plugin> kvp in tokens){ if (kvp.Value.Equals(plugin)){ diff --git a/Resources/Scripts/plugins.browser.js b/Resources/Scripts/plugins.browser.js index 56aa7690..a164278a 100644 --- a/Resources/Scripts/plugins.browser.js +++ b/Resources/Scripts/plugins.browser.js @@ -35,6 +35,10 @@ install(plugin){ this.installed.push(plugin); + if (typeof plugin.obj.configure === "function"){ + $TDP.setConfigurable(plugin.obj.$token); + } + if (!this.isDisabled(plugin)){ plugin.obj.enabled(); this.runWhenReady(plugin); @@ -93,6 +97,13 @@ window.TD_PLUGINS.setState(window.TD_PLUGINS.findObject(identifier), enable); }; + // + // Block: Setup a function to trigger plugin configuration. + // + window.TDPF_configurePlugin = function(identifier){ + window.TD_PLUGINS.findObject(identifier).obj.configure(); + }; + // // Block: Setup a function to reload the page. // diff --git a/TweetDuck.csproj b/TweetDuck.csproj index 1e838708..86169d2c 100644 --- a/TweetDuck.csproj +++ b/TweetDuck.csproj @@ -274,6 +274,7 @@ <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" />