From d3dd29f4bf7d79dc9624a65d590bcdf899e33464 Mon Sep 17 00:00:00 2001 From: Andrei Tudor Date: Sun, 26 May 2024 02:05:06 +0300 Subject: [PATCH] Fixed Install/Remove plugin page in UI --- DiscordBotCore/Online/PluginManager.cs | 6 ++ .../Others/Actions/InternalActionsManager.cs | 1 + .../WindowsForms/PluginListWindow.Designer.cs | 19 +++++- DiscordBotUI/WindowsForms/PluginListWindow.cs | 60 +++++++++++++++---- 4 files changed, 72 insertions(+), 14 deletions(-) diff --git a/DiscordBotCore/Online/PluginManager.cs b/DiscordBotCore/Online/PluginManager.cs index 34fab64..2479f05 100644 --- a/DiscordBotCore/Online/PluginManager.cs +++ b/DiscordBotCore/Online/PluginManager.cs @@ -165,6 +165,12 @@ public class PluginManager await ServerCom.DownloadFileAsync(dependency.DownloadLink, dependency.DownloadLocation, progress); currentProgress += stepProgress; } + + PluginInfo pluginInfo = new PluginInfo(pluginData.Name, + pluginData.Version, + pluginData.Dependencies.Select(dep => dep.DownloadLocation).ToList()); + + await AppendPluginToDatabase(pluginInfo); } diff --git a/DiscordBotCore/Others/Actions/InternalActionsManager.cs b/DiscordBotCore/Others/Actions/InternalActionsManager.cs index 9282fb3..97420c5 100644 --- a/DiscordBotCore/Others/Actions/InternalActionsManager.cs +++ b/DiscordBotCore/Others/Actions/InternalActionsManager.cs @@ -23,6 +23,7 @@ public class InternalActionManager if (loadedActions == null) return; + foreach (var action in loadedActions) Actions.TryAdd(action.ActionName, action); diff --git a/DiscordBotUI/WindowsForms/PluginListWindow.Designer.cs b/DiscordBotUI/WindowsForms/PluginListWindow.Designer.cs index 10ed40d..164b37c 100644 --- a/DiscordBotUI/WindowsForms/PluginListWindow.Designer.cs +++ b/DiscordBotUI/WindowsForms/PluginListWindow.Designer.cs @@ -29,32 +29,45 @@ private void InitializeComponent() { dataGridView1 = new DataGridView(); + labelInstalling = new Label(); ((System.ComponentModel.ISupportInitialize)dataGridView1).BeginInit(); SuspendLayout(); // // dataGridView1 // dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView1.Dock = DockStyle.Fill; + dataGridView1.Dock = DockStyle.Top; dataGridView1.Location = new Point(0, 0); dataGridView1.Name = "dataGridView1"; - dataGridView1.Size = new Size(800, 450); + dataGridView1.Size = new Size(800, 438); dataGridView1.TabIndex = 0; // + // labelInstalling + // + labelInstalling.AutoSize = true; + labelInstalling.Location = new Point(0, 441); + labelInstalling.Name = "labelInstalling"; + labelInstalling.Size = new Size(67, 15); + labelInstalling.TabIndex = 1; + labelInstalling.Text = "Installing ..."; + // // PluginListWindow // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(800, 457); + Controls.Add(labelInstalling); Controls.Add(dataGridView1); Name = "PluginListWindow"; Text = "PluginListWindow"; ((System.ComponentModel.ISupportInitialize)dataGridView1).EndInit(); ResumeLayout(false); + PerformLayout(); } #endregion private DataGridView dataGridView1; + private Label labelInstalling; } } \ No newline at end of file diff --git a/DiscordBotUI/WindowsForms/PluginListWindow.cs b/DiscordBotUI/WindowsForms/PluginListWindow.cs index c2861a7..5bc8fbe 100644 --- a/DiscordBotUI/WindowsForms/PluginListWindow.cs +++ b/DiscordBotUI/WindowsForms/PluginListWindow.cs @@ -1,4 +1,5 @@ -using DiscordBotCore.Online; +using DiscordBotCore.Loaders; +using DiscordBotCore.Online; namespace DiscordBotUI_Windows.WindowsForms { @@ -13,6 +14,8 @@ namespace DiscordBotUI_Windows.WindowsForms private async Task PluginListWindowLoad() { + labelInstalling.Visible = false; + var listOfPlugins = await DiscordBotCore.Application.CurrentApplication.PluginManager.GetPluginsList(); dataGridView1.Rows.Clear(); dataGridView1.Columns.Clear(); @@ -30,21 +33,56 @@ namespace DiscordBotUI_Windows.WindowsForms bool isInstalled = await DiscordBotCore.Application.CurrentApplication.PluginManager.IsPluginInstalled(plugin.Name); string isInstalledMessage = isInstalled ? "Installed" : "Not Installed"; int rowIndex = dataGridView1.Rows.Add(plugin.Name, plugin.Description, plugin.HasDependencies, plugin.Version, isInstalledMessage); - dataGridView1.Rows[rowIndex].Cells["Install"] = (new DataGridViewButtonCell() + dataGridView1.Rows[rowIndex].Cells["Install"] = new DataGridViewButtonCell() { - Value = isInstalled ? "Installed" : "Install", + Value = isInstalled ? "Remove" : "Install", Style = { BackColor = isInstalled ? System.Drawing.Color.LightGray : default } - }); - if(isInstalled) - { - dataGridView1.Rows[rowIndex].Cells["Install"].Style.BackColor = System.Drawing.Color.LightGray; - - } + }; } - dataGridView1.Refresh(); - dataGridView1.Update(); dataGridView1.ReadOnly = true; + + dataGridView1.CellContentClick += async (sender, e) => + { + var senderGrid = (DataGridView)sender; + + if (e.ColumnIndex == 5 && e.RowIndex >= 0) + { + var pluginName = (string)senderGrid.Rows[e.RowIndex].Cells["Name"].Value; + var isInstalled = (string)senderGrid.Rows[e.RowIndex].Cells["Install"].Value == "Remove"; + + + if (isInstalled) + { + await DiscordBotCore.Application.CurrentApplication.PluginManager.MarkPluginToUninstall(pluginName); + dataGridView1.Rows[e.RowIndex].Cells["Install"] = new DataGridViewButtonCell() + { + Value = "Install", + Style = { BackColor = default } + }; + } + else + { + labelInstalling.Visible = true; + labelInstalling.Text = "Installing " + pluginName; + + var result = await DiscordBotCore.Application.CurrentApplication.PluginManager.GetPluginDataByName(pluginName); + + await DiscordBotCore.Application.CurrentApplication.PluginManager.InstallPlugin(result!, null); + + labelInstalling.Visible = false; + + dataGridView1.Rows[e.RowIndex].Cells["Install"] = new DataGridViewButtonCell() + { + Value = "Remove", + Style = { BackColor = System.Drawing.Color.LightGray } + }; + } + } + + dataGridView1.Refresh(); + dataGridView1.Update(); + }; } } }