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-21 17:57:59 +00:00
private readonly Timer translationTimer ;
2022-09-17 19:48:13 +00:00
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-21 20:52:17 +00:00
private bool isManualOnlyMode = false ;
2022-09-21 17:57:59 +00:00
private Translation ? mainCultureTranslation = null ;
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-21 17:57:59 +00:00
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,
2022-09-17 19:48:13 +00:00
AutoReset = false , // runs only once
} ;
this . saveTimer . Elapsed + = this . SaveChanges ;
2022-09-21 17:57:59 +00:00
this . translationTimer . Elapsed + = this . TriggerTranslateNow ;
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-21 18:09:40 +00:00
this . textBox . ReadOnly = ! ( this . isDeepLSourceCulture | | await AppSettings . GetDeepLAction ( ) = = SettingDeepLAction . MANUAL ) ;
2022-09-21 20:52:17 +00:00
this . checkBoxManual . Visible = ! this . isDeepLSourceCulture ;
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-21 17:57:59 +00:00
if ( this . Parent . Parent is Translations translations )
translations . RegisterMainCultureTranslationComponent ( this ) ;
2022-09-20 18:38:12 +00:00
}
2022-09-17 19:48:13 +00:00
}
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-21 20:52:17 +00:00
this . isManualOnlyMode = this . checkBoxManual . Checked = translation . TranslateManual ;
2022-09-17 19:48:13 +00:00
this . currentTranslationId = translation . Id ;
2022-09-21 20:52:17 +00:00
try
{
this . textBox . Text = translation . Text ;
this . textBox . Multiline = translation . TextElement . IsMultiLine ;
this . textBox . ReadOnly = ! ( this . isDeepLSourceCulture | | await AppSettings . GetDeepLAction ( ) = = SettingDeepLAction . MANUAL ) ;
if ( this . isManualOnlyMode )
this . textBox . ReadOnly = false ;
}
catch ( ObjectDisposedException )
{
}
2022-09-20 17:42:04 +00:00
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
}
2022-09-21 16:21:52 +00:00
public void Clear ( )
{
this . isLoading = true ;
this . currentTranslationId = - 1 ;
this . textBox . Text = string . Empty ;
this . textBox . Multiline = true ;
this . Height = 280 ;
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 ( ) ;
2022-09-21 15:33:03 +00:00
if ( this . ParentForm is UI_WinForms . Main main )
main . AddDeferredSaveOperation ( this . currentTranslationId , ( ) = > this . SaveChanges ( null , null ) ) ;
2022-09-21 17:57:59 +00:00
if ( this . isDeepLSourceCulture & & await AppSettings . GetDeepLAction ( ) = = SettingDeepLAction . AUTOMATIC_ALL )
{
if ( this . translationTimer . Enabled )
this . translationTimer . Stop ( ) ;
this . translationTimer . Start ( ) ;
}
2022-09-21 15:33:03 +00:00
}
private async void SaveChanges ( object? sender , ElapsedEventArgs e )
{
if ( this . currentTranslationId > - 1 )
2022-09-26 17:25:51 +00:00
{
var storedTranslation = await TranslationProcessor . SaveChangedTranslation ( this . currentTranslationId , this . textBox . Text , this . checkBoxManual . Checked ) ;
AppEvents . TranslationChanged ( storedTranslation . Result ) ;
}
2022-09-21 15:33:03 +00:00
if ( this . ParentForm is UI_WinForms . Main main )
main . RemoveDeferredSaveOperation ( this . currentTranslationId ) ;
2022-09-21 20:52:17 +00:00
this . isManualOnlyMode = this . checkBoxManual . Checked ;
2022-09-17 19:48:13 +00:00
}
2022-09-21 17:57:59 +00:00
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 ;
2022-09-21 20:52:17 +00:00
internal async Task Translate ( bool wasManualTriggered = false )
2022-09-21 17:57:59 +00:00
{
if ( this . mainCultureTranslation is null )
return ;
2022-09-21 20:52:17 +00:00
if ( this . isManualOnlyMode & & ! wasManualTriggered )
return ;
2022-09-21 17:57:59 +00:00
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
2022-09-21 20:52:17 +00:00
await this . Translate ( wasManualTriggered : true ) ;
}
private async void checkBoxManual_CheckedChanged ( object sender , EventArgs e )
{
if ( this . isLoading )
return ;
this . SaveChanges ( null , null ) ;
this . textBox . ReadOnly = ! ( this . isDeepLSourceCulture | | await AppSettings . GetDeepLAction ( ) = = SettingDeepLAction . MANUAL ) ;
if ( this . isManualOnlyMode )
this . textBox . ReadOnly = false ;
2022-09-21 17:57:59 +00:00
}
2022-09-17 19:48:13 +00:00
}