Fixed text element changed event

This commit is contained in:
Thorsten Sommer 2022-09-21 18:21:52 +02:00
parent 24a7093674
commit 1393d703a1
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
4 changed files with 63 additions and 8 deletions

View File

@ -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<TextElement> WhenTextElementChanged;
internal static event EventHandler<TextElement?> 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
}

View File

@ -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();

View File

@ -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)

View File

@ -1,22 +1,58 @@
using Processor;
using Timer = System.Timers.Timer;
namespace UI_WinForms.Components;
public partial class Translations : UserControl
{
private readonly Dictionary<string, Translation> 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()