Fixed text element changed event
This commit is contained in:
		
							parent
							
								
									24a7093674
								
							
						
					
					
						commit
						1393d703a1
					
				| @ -17,10 +17,10 @@ internal static class AppEvents | |||||||
|     #region Event: Text element was changed |     #region Event: Text element was changed | ||||||
| 
 | 
 | ||||||
|     // Text element changed event which can be subscribed: |     // 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: |     // 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 |     #endregion | ||||||
| } | } | ||||||
| @ -182,12 +182,19 @@ public partial class TextElements : UserControl | |||||||
| 
 | 
 | ||||||
|     private async void listTextElements_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) |     private async void listTextElements_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) | ||||||
|     { |     { | ||||||
|         // Load the text element: |         if (e.IsSelected) | ||||||
|         var selectedTextElementId = (int)e.Item.Tag; |         { | ||||||
|         this.currentTextElement = await TextElementProcessor.LoadTextElement(selectedTextElementId); |             // Get the text element's id: | ||||||
|  |             var selectedTextElementId = (int)e.Item.Tag; | ||||||
|              |              | ||||||
|         // Fire the event: |             // Load the text element: | ||||||
|         AppEvents.TextElementChanged(this.currentTextElement); |             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(); |     private async void textBoxFilter_KeyUp(object sender, KeyEventArgs e) => await this.LoadTextElements(); | ||||||
|  | |||||||
| @ -63,6 +63,18 @@ public sealed partial class Translation : UserControl | |||||||
|         this.isLoading = false; |         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) |     private async void textBox_TextChanged(object sender, EventArgs e) | ||||||
|     { |     { | ||||||
|         if(this.isLoading) |         if(this.isLoading) | ||||||
|  | |||||||
| @ -1,22 +1,58 @@ | |||||||
| using Processor; | using Processor; | ||||||
|  | using Timer = System.Timers.Timer; | ||||||
| 
 | 
 | ||||||
| namespace UI_WinForms.Components; | namespace UI_WinForms.Components; | ||||||
| 
 | 
 | ||||||
| public partial class Translations : UserControl | public partial class Translations : UserControl | ||||||
| { | { | ||||||
|     private readonly Dictionary<string, Translation> translationComponents = new(); |     private readonly Dictionary<string, Translation> translationComponents = new(); | ||||||
|  |     private readonly Timer clearTimer; | ||||||
| 
 | 
 | ||||||
|     public Translations() |     public Translations() | ||||||
|     { |     { | ||||||
|         this.InitializeComponent(); |         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(); |         this.Load += async (sender, args) => await this.SetupTranslations(); | ||||||
|         AppEvents.WhenTextElementChanged += async (sender, textElement) => |         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); |             var allTranslations = await TranslationProcessor.GetTranslations(textElement); | ||||||
|             foreach (var translation in allTranslations) |             foreach (var translation in allTranslations) | ||||||
|                 if (this.translationComponents.ContainsKey(translation.Culture)) |                 if (this.translationComponents.ContainsKey(translation.Culture)) | ||||||
|                     await this.translationComponents[translation.Culture].Configure(translation); |                     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() |     private async Task SetupTranslations() | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user