From 6c7b5dbadeb3e22181827c75bb4b08aea5ec700e Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Mon, 26 Sep 2022 22:07:01 +0200 Subject: [PATCH] Added translation progress to sections --- .../UI WinForms/Components/SectionTree.cs | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/I18N Commander/UI WinForms/Components/SectionTree.cs b/I18N Commander/UI WinForms/Components/SectionTree.cs index f7b3db6..d402363 100644 --- a/I18N Commander/UI WinForms/Components/SectionTree.cs +++ b/I18N Commander/UI WinForms/Components/SectionTree.cs @@ -1,4 +1,5 @@ -using Processor; +using DataModel.Database; +using Processor; using UI_WinForms.Dialogs; using UI_WinForms.Resources; @@ -6,6 +7,8 @@ namespace UI_WinForms.Components; public partial class SectionTree : UserControl { + private static readonly Dictionary> NODE_PROGRESS_HANDLERS = new(); + public SectionTree() { this.InitializeComponent(); @@ -46,17 +49,23 @@ public partial class SectionTree : UserControl { foreach (var section in await SectionProcessor.LoadLayer(i)) { + var translationProgress = await TranslationProcessor.CalculateTranslationProgress(section); + // Create the tree node: var node = new TreeNode { Name = section.DataKey, // [sic] name is the key - Text = section.Name, + Text = $"{section.Name} [{translationProgress.Result}%]", StateImageIndex = 1, }; // Cache the node: treeNodes.Add(section.DataKey, node); + // Wire each node to the translation change event to update the translation progress: + NODE_PROGRESS_HANDLERS.Add(node, async (_, translation) => await this.UpdateTranslationProgress(node, section, translation)); + AppEvents.WhenTranslationChanged += NODE_PROGRESS_HANDLERS[node]; + // Is this a root node? if (section.Depth is 0) @@ -107,6 +116,18 @@ public partial class SectionTree : UserControl this.treeView.ExpandAll(); } + private async Task UpdateTranslationProgress(TreeNode node, Section section, DataModel.Database.Translation? translation) + { + if (translation is null || translation.TextElement.Section.DataKey == section.DataKey) + { + var currentTranslationProgress = await TranslationProcessor.CalculateTranslationProgress(section); + if(this.treeView.InvokeRequired) + this.treeView.Invoke(() => node.Text = $"{section.Name} [{currentTranslationProgress.Result}%]"); + else + node.Text = $"{section.Name} [{currentTranslationProgress.Result}%]"; + } + } + private async void buttonAdd_Click(object sender, EventArgs e) { if(this.DesignMode) @@ -135,13 +156,19 @@ public partial class SectionTree : UserControl if(!addedSection.Successful) return; + var translationProgress = await TranslationProcessor.CalculateTranslationProgress(addedSection.Result!); + // Add the new section to the tree control: var node = new TreeNode { Name = addedSection.Result!.DataKey, // [sic] name is the key - Text = addedSection.Result.Name, + Text = $"{addedSection.Result.Name} [{translationProgress.Result}%]", StateImageIndex = 1, }; + + // Wire each node to the translation change event to update the translation progress: + NODE_PROGRESS_HANDLERS.Add(node, async (_, translation) => await this.UpdateTranslationProgress(node, addedSection.Result, translation)); + AppEvents.WhenTranslationChanged += NODE_PROGRESS_HANDLERS[node]; if(!addRootNode && selectedNode is not null) selectedNode.Nodes.Add(node); @@ -171,14 +198,18 @@ public partial class SectionTree : UserControl // Ask the user, if he really wants to remove the section: if(MessageBox.Show(numberChildren > 0 ? $"Are you sure, you want to remove the section '{selectedNode.Text}', its {numberChildren} children and so on?" : $"Are you sure, you want to remove the section '{selectedNode.Text}'?", "Remove section", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No) return; - + // Remove the section from the database: // (notice, that the node's name is its key) await SectionProcessor.RemoveSection(selectedNode.Name); // Remove all nodes from the tree control: this.treeView.Nodes.Clear(); + foreach (var (treeNode, handler) in NODE_PROGRESS_HANDLERS) + AppEvents.WhenTranslationChanged -= handler; + NODE_PROGRESS_HANDLERS.Clear(); + // Reload the tree: this.LoadNodes(this, EventArgs.Empty); @@ -187,6 +218,8 @@ public partial class SectionTree : UserControl // Fire the click event: this.treeView_NodeMouseClick(this, new TreeNodeMouseClickEventArgs(this.treeView.SelectedNode!, MouseButtons.Left, 1, 0, 0)); + + AppEvents.TranslationChanged(null); } private async void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)