diff --git a/I18N Commander/UI WinForms/Components/SectionTree.cs b/I18N Commander/UI WinForms/Components/SectionTree.cs index c67f531..d0a8e99 100644 --- a/I18N Commander/UI WinForms/Components/SectionTree.cs +++ b/I18N Commander/UI WinForms/Components/SectionTree.cs @@ -3,12 +3,14 @@ using Processor; using Processor.Generators; using UI_WinForms.Dialogs; using UI_WinForms.Resources; +using Timer = System.Timers.Timer; namespace UI_WinForms.Components; public partial class SectionTree : UserControl { - private static readonly Dictionary> NODE_PROGRESS_HANDLERS = new(); + private static readonly Dictionary?> NODE_PROGRESS_HANDLERS = new(); + private readonly Timer generatorTimer; public SectionTree() { @@ -27,9 +29,23 @@ public partial class SectionTree : UserControl // Set the image list to the tree view: this.treeView.ImageList = imgList; + // The generator timer: + this.generatorTimer = new Timer + { + Enabled = false, // disable timer for now, + Interval = 6_000, // 6 second interval, + AutoReset = false, // runs only once + }; + + this.generatorTimer.Elapsed += async (sender, args) => await this.PerformGenerators(); + // Subscribe to the load event: this.Load += this.LoadNodes; this.Load += (sender, args) => this.SetupGeneratorButton(); + + // Subscribe to all event for triggering the generators: + AppEvents.WhenTranslationChanged += (sender, translation) => this.GeneratorEvent(); + AppEvents.WhenSettingsChanged += (sender, args) => this.GeneratorEvent(); } private async void SetupGeneratorButton() @@ -294,8 +310,26 @@ public partial class SectionTree : UserControl selectedNode.Name = alteredSection.Result.DataKey; // [sic] name is the key } - private async void buttonGenerate_Click(object sender, EventArgs e) + private async void buttonGenerate_Click(object sender, EventArgs e) => await this.PerformGenerators(); + + private async void GeneratorEvent() { + if(await AppSettings.GetGeneratorMode() == SettingGeneratorMode.MANUAL) + return; + + if (this.generatorTimer.Enabled) + this.generatorTimer.Stop(); + this.generatorTimer.Start(); + } + + private async Task PerformGenerators() + { + if (this.buttonGenerate.InvokeRequired) + { + await this.buttonGenerate.Invoke(this.PerformGenerators); + return; + } + this.buttonGenerate.Enabled = false; await Generator.TriggerAllAsync(); this.buttonGenerate.Enabled = true;