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 bool isLoading = false; private int currentTranslationId = -1; private bool isDeepLSourceCulture = false; 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.saveTimer.Elapsed += this.SaveChanges; 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."); } } private async void SaveChanges(object? sender, ElapsedEventArgs e) { if (this.currentTranslationId > -1) await TranslationProcessor.SaveChangedTranslation(this.currentTranslationId, this.textBox.Text); } 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; } private async void textBox_TextChanged(object sender, EventArgs e) { if(this.isLoading) return; if(this.saveTimer.Enabled) this.saveTimer.Stop(); this.saveTimer.Start(); } }