using System.Timers; using DataModel.Database; using Processor; using UI_WinForms.Resources; using Timer = System.Timers.Timer; namespace UI_WinForms.Components; public sealed partial class Translation : UserControl { private readonly string culture = "en-US"; private readonly Timer saveTimer; private readonly Timer translationTimer; private bool isLoading = false; private int currentTranslationId = -1; private bool isDeepLSourceCulture = false; private Translation? mainCultureTranslation = null; public Translation() { this.InitializeComponent(); this.Dock = DockStyle.Top; } public Translation(AppSettings.CultureInfo cultureInfo) { this.InitializeComponent(); this.culture = cultureInfo.Code; this.labelHead.Text = $"Culture: {cultureInfo.Code}"; this.Dock = DockStyle.Top; this.saveTimer = new Timer { Enabled = false, // disable timer for now, Interval = 1_000, // 1 second interval, AutoReset = false, // runs only once }; this.translationTimer = new Timer { Enabled = false, // disable timer for now, Interval = 1_600, // 1.6 second interval, AutoReset = false, // runs only once }; this.saveTimer.Elapsed += this.SaveChanges; this.translationTimer.Elapsed += this.TriggerTranslateNow; this.Load += async (sender, args) => await this.LateSetup(cultureInfo); } private async Task LateSetup(AppSettings.CultureInfo cultureInfo) { this.isDeepLSourceCulture = await AppSettings.GetDeepLSourceCultureIndex() == cultureInfo.Index; this.buttonDeepL.Visible = await AppSettings.GetDeepLMode() != SettingDeepLMode.DISABLED; this.buttonDeepL.Image = this.isDeepLSourceCulture ? Icons.icons8_trigger_1__svg : Icons.deepl_logo_icon_170284; if (this.isDeepLSourceCulture) { this.labelHead.Text = $"Culture: {cultureInfo.Code} (DeepL source culture)"; this.toolTip.SetToolTip(this.buttonDeepL, "Replaces all other translations by DeepL translations using this culture as source culture.\nWarning: already translated texts will be replaced as well."); if(this.Parent.Parent is Translations translations) translations.RegisterMainCultureTranslationComponent(this); } } public async Task Configure(DataModel.Database.Translation translation) { this.isLoading = true; this.currentTranslationId = translation.Id; this.textBox.Text = translation.Text; this.textBox.Multiline = translation.TextElement.IsMultiLine; this.Height = translation.TextElement.IsMultiLine ? 280 : 106; this.buttonDeepL.Visible = await AppSettings.GetDeepLMode() != SettingDeepLMode.DISABLED; 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) return; if(this.saveTimer.Enabled) this.saveTimer.Stop(); this.saveTimer.Start(); if(this.ParentForm is UI_WinForms.Main main) main.AddDeferredSaveOperation(this.currentTranslationId, () => this.SaveChanges(null, null)); if (this.isDeepLSourceCulture && await AppSettings.GetDeepLAction() == SettingDeepLAction.AUTOMATIC_ALL) { if (this.translationTimer.Enabled) this.translationTimer.Stop(); this.translationTimer.Start(); } } private async void SaveChanges(object? sender, ElapsedEventArgs e) { if (this.currentTranslationId > -1) await TranslationProcessor.SaveChangedTranslation(this.currentTranslationId, this.textBox.Text); if(this.ParentForm is UI_WinForms.Main main) main.RemoveDeferredSaveOperation(this.currentTranslationId); } private async void TriggerTranslateNow(object? sender, ElapsedEventArgs e) { if (this.currentTranslationId < 0 || this.isDeepLSourceCulture == false) return; if(this.Parent.Parent is Translations translations) await translations.MainCultureWasChanged(); } internal void SetMainCultureTranslation(Translation translation) => this.mainCultureTranslation = translation; internal string GetCurrentText() => this.textBox.Text; internal async Task Translate() { if (this.mainCultureTranslation is null) return; var text = await Processor.DeepL.Translate(this.mainCultureTranslation.GetCurrentText(), this.culture); if (this.textBox.InvokeRequired) this.textBox.Invoke(() => this.textBox.Text = text); else this.textBox.Text = text; } private async void buttonDeepL_Click(object sender, EventArgs e) { if (this.isDeepLSourceCulture) this.TriggerTranslateNow(null, null); else await this.Translate(); } }