2022-09-17 19:48:13 +00:00
|
|
|
|
using System.Timers;
|
2022-09-20 18:18:26 +00:00
|
|
|
|
using DataModel.Database;
|
2022-09-17 19:48:13 +00:00
|
|
|
|
using Processor;
|
|
|
|
|
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 int currentTranslationId = -1;
|
2022-09-20 17:31:09 +00:00
|
|
|
|
private bool isLoading = false;
|
2022-09-17 19:48:13 +00:00
|
|
|
|
|
|
|
|
|
public Translation()
|
|
|
|
|
{
|
|
|
|
|
this.InitializeComponent();
|
|
|
|
|
this.Dock = DockStyle.Top;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Translation(string cultureCode)
|
|
|
|
|
{
|
|
|
|
|
this.InitializeComponent();
|
|
|
|
|
this.culture = cultureCode;
|
|
|
|
|
this.labelHead.Text = $"Culture: {cultureCode}";
|
|
|
|
|
this.Dock = DockStyle.Top;
|
|
|
|
|
this.saveTimer = new Timer
|
|
|
|
|
{
|
|
|
|
|
Enabled = false, // disable timer for now,
|
2022-09-20 17:44:15 +00:00
|
|
|
|
Interval = 1_000, // 1 second interval
|
2022-09-17 19:48:13 +00:00
|
|
|
|
AutoReset = false, // runs only once
|
|
|
|
|
};
|
|
|
|
|
this.saveTimer.Elapsed += this.SaveChanges;
|
2022-09-20 18:18:26 +00:00
|
|
|
|
this.Load += async (sender, args) => await this.LateSetup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task LateSetup()
|
|
|
|
|
{
|
|
|
|
|
this.buttonDeepL.Visible = await AppSettings.GetDeepLMode() != SettingDeepLMode.DISABLED;
|
2022-09-17 19:48:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void SaveChanges(object? sender, ElapsedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (this.currentTranslationId > -1)
|
|
|
|
|
await TranslationProcessor.SaveChangedTranslation(this.currentTranslationId, this.textBox.Text);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 18:18:26 +00:00
|
|
|
|
public async Task Configure(DataModel.Database.Translation translation)
|
2022-09-17 19:48:13 +00:00
|
|
|
|
{
|
2022-09-20 17:31:09 +00:00
|
|
|
|
this.isLoading = true;
|
|
|
|
|
|
2022-09-17 19:48:13 +00:00
|
|
|
|
this.currentTranslationId = translation.Id;
|
|
|
|
|
this.textBox.Text = translation.Text;
|
2022-09-20 17:42:04 +00:00
|
|
|
|
this.textBox.Multiline = translation.TextElement.IsMultiLine;
|
|
|
|
|
this.Height = translation.TextElement.IsMultiLine ? 280 : 106;
|
2022-09-20 18:18:26 +00:00
|
|
|
|
this.buttonDeepL.Visible = await AppSettings.GetDeepLMode() != SettingDeepLMode.DISABLED;
|
2022-09-20 17:42:04 +00:00
|
|
|
|
|
2022-09-20 17:31:09 +00:00
|
|
|
|
this.isLoading = false;
|
2022-09-17 19:48:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void textBox_TextChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
2022-09-20 17:31:09 +00:00
|
|
|
|
if(this.isLoading)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-09-17 19:48:13 +00:00
|
|
|
|
if(this.saveTimer.Enabled)
|
|
|
|
|
this.saveTimer.Stop();
|
|
|
|
|
|
|
|
|
|
this.saveTimer.Start();
|
|
|
|
|
}
|
|
|
|
|
}
|