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 ;
2022-09-20 18:38:12 +00:00
using UI_WinForms.Resources ;
2022-09-17 19:48:13 +00:00
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 ;
2022-09-20 17:31:09 +00:00
private bool isLoading = false ;
2022-09-20 18:38:12 +00:00
private int currentTranslationId = - 1 ;
private bool isDeepLSourceCulture = false ;
2022-09-17 19:48:13 +00:00
public Translation ( )
{
this . InitializeComponent ( ) ;
this . Dock = DockStyle . Top ;
}
2022-09-20 18:38:12 +00:00
public Translation ( AppSettings . CultureInfo cultureInfo )
2022-09-17 19:48:13 +00:00
{
this . InitializeComponent ( ) ;
2022-09-20 18:38:12 +00:00
this . culture = cultureInfo . Code ;
this . labelHead . Text = $"Culture: {cultureInfo.Code}" ;
2022-09-17 19:48:13 +00:00
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:38:12 +00:00
this . Load + = async ( sender , args ) = > await this . LateSetup ( cultureInfo ) ;
2022-09-20 18:18:26 +00:00
}
2022-09-20 18:38:12 +00:00
private async Task LateSetup ( AppSettings . CultureInfo cultureInfo )
2022-09-20 18:18:26 +00:00
{
2022-09-20 18:38:12 +00:00
this . isDeepLSourceCulture = await AppSettings . GetDeepLSourceCultureIndex ( ) = = cultureInfo . Index ;
2022-09-20 18:18:26 +00:00
this . buttonDeepL . Visible = await AppSettings . GetDeepLMode ( ) ! = SettingDeepLMode . DISABLED ;
2022-09-20 18:38:12 +00:00
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." ) ;
}
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 ( ) ;
}
}