diff --git a/I18N Commander/UI WinForms/AppEvents.cs b/I18N Commander/UI WinForms/AppEvents.cs index 5b99d62..5cd9ce9 100644 --- a/I18N Commander/UI WinForms/AppEvents.cs +++ b/I18N Commander/UI WinForms/AppEvents.cs @@ -17,10 +17,10 @@ internal static class AppEvents #region Event: Text element was changed // Text element changed event which can be subscribed: - internal static event EventHandler WhenTextElementChanged; + internal static event EventHandler WhenTextElementChanged; // Method to raise the text element changed event: - internal static void TextElementChanged(TextElement textElement) => WhenTextElementChanged?.Invoke(null, textElement); + internal static void TextElementChanged(TextElement? textElement) => WhenTextElementChanged?.Invoke(null, textElement); #endregion } \ No newline at end of file diff --git a/I18N Commander/UI WinForms/Components/TextElements.cs b/I18N Commander/UI WinForms/Components/TextElements.cs index 3a00f42..ea31b88 100644 --- a/I18N Commander/UI WinForms/Components/TextElements.cs +++ b/I18N Commander/UI WinForms/Components/TextElements.cs @@ -182,12 +182,19 @@ public partial class TextElements : UserControl private async void listTextElements_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) { - // Load the text element: - var selectedTextElementId = (int)e.Item.Tag; - this.currentTextElement = await TextElementProcessor.LoadTextElement(selectedTextElementId); - - // Fire the event: - AppEvents.TextElementChanged(this.currentTextElement); + if (e.IsSelected) + { + // Get the text element's id: + var selectedTextElementId = (int)e.Item.Tag; + + // Load the text element: + this.currentTextElement = await TextElementProcessor.LoadTextElement(selectedTextElementId); + + // Fire the event: + AppEvents.TextElementChanged(this.currentTextElement); + } + else + AppEvents.TextElementChanged(null); } private async void textBoxFilter_KeyUp(object sender, KeyEventArgs e) => await this.LoadTextElements(); diff --git a/I18N Commander/UI WinForms/Components/Translation.cs b/I18N Commander/UI WinForms/Components/Translation.cs index 28fefe1..48d01cd 100644 --- a/I18N Commander/UI WinForms/Components/Translation.cs +++ b/I18N Commander/UI WinForms/Components/Translation.cs @@ -63,6 +63,18 @@ public sealed partial class Translation : UserControl this.isLoading = false; } + public void Clear() + { + this.isLoading = true; + + this.currentTranslationId = -1; + this.textBox.Text = string.Empty; + this.textBox.Multiline = true; + this.Height = 280; + + this.isLoading = false; + } + private async void textBox_TextChanged(object sender, EventArgs e) { if(this.isLoading) diff --git a/I18N Commander/UI WinForms/Components/Translations.cs b/I18N Commander/UI WinForms/Components/Translations.cs index 40b2a59..21538e5 100644 --- a/I18N Commander/UI WinForms/Components/Translations.cs +++ b/I18N Commander/UI WinForms/Components/Translations.cs @@ -1,22 +1,58 @@ using Processor; +using Timer = System.Timers.Timer; namespace UI_WinForms.Components; public partial class Translations : UserControl { private readonly Dictionary translationComponents = new(); + private readonly Timer clearTimer; public Translations() { this.InitializeComponent(); + this.Enabled = false; + this.clearTimer = new Timer + { + Enabled = false, + Interval = 600, // ms + AutoReset = false, + }; + this.clearTimer.Elapsed += (sender, args) => this.ClearTranslations(); this.Load += async (sender, args) => await this.SetupTranslations(); AppEvents.WhenTextElementChanged += async (sender, textElement) => { + if(textElement is null) + { + this.clearTimer.Start(); + return; + } + + this.clearTimer.Stop(); + if (!this.Enabled) + this.Enabled = true; + var allTranslations = await TranslationProcessor.GetTranslations(textElement); foreach (var translation in allTranslations) if (this.translationComponents.ContainsKey(translation.Culture)) await this.translationComponents[translation.Culture].Configure(translation); }; + + AppEvents.WhenSectionChanged += (sender, section) => this.ClearTranslations(); + } + + private void ClearTranslations() + { + // Perform UI changes on the UI thread: + if (this.InvokeRequired) + { + this.Invoke(this.ClearTranslations); + return; + } + + this.Enabled = false; + foreach (var (_, translation) in this.translationComponents) + translation.Clear(); } private async Task SetupTranslations()