Implemented automatic generators

This commit is contained in:
Thorsten Sommer 2022-10-30 16:25:36 +01:00
parent 274d3641eb
commit c48466d91e
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -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<TreeNode, EventHandler<DataModel.Database.Translation?>> NODE_PROGRESS_HANDLERS = new();
private static readonly Dictionary<TreeNode, EventHandler<DataModel.Database.Translation?>?> 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;