55using DeepLClient . Managers ;
66using Serilog ;
77using Syncfusion . Windows . Forms ;
8- using Syncfusion . Windows . Forms . Tools . Win32API ;
98using System . Diagnostics ;
9+ using System . Diagnostics . CodeAnalysis ;
1010
1111namespace DeepLClient . Controls
1212{
13+ [ SuppressMessage ( "ReSharper" , "EmptyGeneralCatchClause" ) ]
1314 public partial class TextPage : UserControl
1415 {
1516 public TextPage ( )
@@ -34,7 +35,7 @@ private void BindComboBoxTheme()
3435 private async void TextPage_Load ( object sender , EventArgs e )
3536 {
3637 // set cost info
37- LblCost . Text = SubscriptionManager . UsingFreeSubscription ( ) ? "FREE" : "€ 0,00" ;
38+ LblCost . Text = SubscriptionManager . BaseCostNotation ( ) ;
3839
3940 // activate source textbox
4041 ActiveControl = TbSource ;
@@ -84,7 +85,7 @@ private async void ExecuteTranslation()
8485 if ( string . IsNullOrWhiteSpace ( sourceText ) ) return ;
8586
8687 // do we have enough chars left?
87- if ( await SubscriptionManager . CharactersWillExceedLimit ( sourceText . Length ) )
88+ if ( await SubscriptionManager . CharactersWillExceedLimitAsync ( sourceText . Length ) )
8889 {
8990 using var limit = new LimitExceeded ( sourceText . Length ) ;
9091 var ignoreLimit = limit . ShowDialog ( ) ;
@@ -152,23 +153,18 @@ private async void ExecuteTranslation()
152153 TbTranslated . Text = translatedText . Text ;
153154
154155 // store the selected languages
155- StoreSelectedLanguages ( ) ;
156+ SettingsManager . StoreSelectedLanguages ( CbSourceLanguage , CbTargetLanguage ) ;
156157
157158 // copy it to the clipbaord if configured
158- if ( Variables . AppSettings . CopyTranslationToClipboard && ! string . IsNullOrWhiteSpace ( translatedText . Text ) )
159- {
160- Clipboard . SetText ( translatedText . Text , TextDataFormat . UnicodeText ) ;
161- LblClipboardCopied . Visible = true ;
162- _ = Task . Run ( HideClipboardCopied ) ;
163- }
159+ CopyToClipboard ( ) ;
164160
165161 // is auto detect enabled?
166162 if ( sourceLanguage != null ) return ;
167163
168164 // yep, set the detected source language
169165 LblDetectedInfo . Visible = true ;
170166 LblDetected . Visible = true ;
171- LblDetected . Text = Variables . SourceLanguages . First ( x => x . Value == translatedText . DetectedSourceLanguageCode ) . Key ;
167+ LblDetected . Text = DeepLManager . GetSourceLanguageByLanguageCode ( translatedText . DetectedSourceLanguageCode ) ;
172168 }
173169 catch ( ConnectionException ex )
174170 {
@@ -194,67 +190,15 @@ private async void ExecuteTranslation()
194190 }
195191 }
196192
197- /// <summary>
198- /// Gets and stores the selected languages for both source and target.
199- /// </summary>
200- private void StoreSelectedLanguages ( )
201- {
202- // get source language
203- string sourceLanguage = null ;
204- if ( CbSourceLanguage . SelectedItem != null )
205- {
206- var item = ( KeyValuePair < string , string > ) CbSourceLanguage . SelectedItem ;
207- sourceLanguage = item . Value ;
208- }
209-
210- if ( sourceLanguage != null )
211- {
212- // store last used
213- Variables . AppSettings . LastSourceLanguage = sourceLanguage ;
214- }
215-
216- // get target language
217- string targetLanguage = null ;
218- if ( CbTargetLanguage . SelectedItem != null )
219- {
220- var item = ( KeyValuePair < string , string > ) CbTargetLanguage . SelectedItem ;
221- targetLanguage = item . Value ;
222- }
223-
224- if ( targetLanguage != null )
225- {
226- // store last used
227- Variables . AppSettings . LastTargetLanguage = targetLanguage ;
228- }
229-
230- // store them
231- SettingsManager . Store ( ) ;
232-
233- // done
234- }
235-
236193 /// <summary>
237194 /// Shows or hides the formality dropbox, depending on whether the selected language supports it.
238195 /// </summary>
239196 /// <param name="sender"></param>
240197 /// <param name="e"></param>
241198 private void CbTargetLanguage_SelectedValueChanged ( object sender , EventArgs e )
242199 {
243- string targetLanguage = null ;
244- if ( CbTargetLanguage . SelectedItem != null )
245- {
246- var item = ( KeyValuePair < string , string > ) CbTargetLanguage . SelectedItem ;
247- targetLanguage = item . Value ;
248- }
249-
250- if ( targetLanguage == null )
251- {
252- // notify
253- return ;
254- }
255-
256200 // check formality supported
257- var supported = Variables . FormalitySupportedLanguages . Contains ( targetLanguage ) ;
201+ var supported = DeepLManager . TargetLanguageSupportsFormality ( CbTargetLanguage ) ;
258202 CbTargetFormality . Visible = supported ;
259203 LblTargetFormality . Visible = supported ;
260204 LblFormalityInfo . Visible = supported ;
@@ -268,16 +212,9 @@ private void CbTargetLanguage_SelectedValueChanged(object sender, EventArgs e)
268212 private void TbSource_TextChanged ( object sender , EventArgs e )
269213 {
270214 LblCharacters . Text = TbSource . Text . Length . ToString ( ) ;
271- LblCost . Text = SubscriptionManager . UsingFreeSubscription ( ) ? "FREE" : SubscriptionManager . CalculateCost ( TbSource . Text . Length , false ) ;
215+ LblCost . Text = SubscriptionManager . CalculateCost ( TbSource . Text . Length , false ) ;
272216 }
273-
274- /// <summary>
275- /// Hide the auto detected language info when a new source language is selected.
276- /// </summary>
277- /// <param name="sender"></param>
278- /// <param name="e"></param>
279- private void CbSourceLanguage_SelectedValueChanged ( object sender , EventArgs e ) => ResetDetectedSourceLanguage ( ) ;
280-
217+
281218 /// <summary>
282219 /// Shows the dragdrop effect when hovering a file or text.
283220 /// </summary>
@@ -299,67 +236,98 @@ private void TextPage_DragEnter(object sender, DragEventArgs e)
299236 /// <param name="e"></param>
300237 private void TextPage_DragDrop ( object sender , DragEventArgs e )
301238 {
302- if ( e . Data == null ) return ;
303- var text = string . Empty ;
304-
305- if ( e . Data . GetDataPresent ( DataFormats . Text ) )
306- {
307- // simple text
308- text = ( string ) e . Data ? . GetData ( DataFormats . Text ) ;
309- }
310- else if ( e . Data . GetDataPresent ( DataFormats . FileDrop ) )
239+ try
311240 {
312- // file was dropped, fetch the relevant info
313- var files = ( string [ ] ) e . Data ? . GetData ( DataFormats . FileDrop ) ;
314- if ( files == null ) return ;
315- if ( ! files . Any ( ) ) return ;
316- var file = files . First ( ) ;
317-
318- // is it supported?
319- if ( ! DocumentManager . FileIsSupported ( file , true ) )
320- {
321- MessageBoxAdv . Show ( this , "Only .txt documents are supported here.\r \n \r \n For other formats, use the 'documents' tab." , Variables . MessageBoxTitle , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
322- return ;
323- }
241+ if ( e . Data == null ) return ;
242+ var text = string . Empty ;
324243
325- // and does it still exist?
326- if ( ! File . Exists ( file ) )
244+ if ( e . Data . GetDataPresent ( DataFormats . Text ) )
327245 {
328- MessageBoxAdv . Show ( this , "The selected document doesn't exist (anymore)." , Variables . MessageBoxTitle , MessageBoxButtons . OK , MessageBoxIcon . Exclamation ) ;
329- return ;
246+ // simple text, use as-is
247+ text = ( string ) e . Data ? . GetData ( DataFormats . Text ) ;
330248 }
249+ else if ( e . Data . GetDataPresent ( DataFormats . FileDrop ) )
250+ {
251+ // file was dropped, fetch the relevant info
252+ var files = ( string [ ] ) e . Data ? . GetData ( DataFormats . FileDrop ) ;
253+ if ( files == null ) return ;
254+ if ( ! files . Any ( ) ) return ;
255+ var file = files . First ( ) ;
256+
257+ // is it supported?
258+ if ( ! DocumentManager . FileIsSupported ( file , true ) )
259+ {
260+ MessageBoxAdv . Show ( this , "Only .txt documents are supported here.\r \n \r \n For other formats, use the 'documents' tab." , Variables . MessageBoxTitle , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
261+ return ;
262+ }
331263
332- // yep, use its content
333- text = File . ReadAllText ( file ) ;
334- }
264+ // and does it still exist?
265+ if ( ! File . Exists ( file ) )
266+ {
267+ MessageBoxAdv . Show ( this , "The selected document doesn't exist (anymore)." , Variables . MessageBoxTitle , MessageBoxButtons . OK , MessageBoxIcon . Exclamation ) ;
268+ return ;
269+ }
270+
271+ // yep, use its content
272+ text = File . ReadAllText ( file ) ;
273+ }
335274
336- // check for empty
337- if ( string . IsNullOrWhiteSpace ( text ) ) return ;
275+ // check for empty
276+ if ( string . IsNullOrWhiteSpace ( text ) ) return ;
338277
339- // set the text
340- TbSource . Text = text . Trim ( ) ;
278+ // set the text
279+ TbSource . Text = text . Trim ( ) ;
341280
342- // enable auto detect
343- CbSourceLanguage . SelectedItem = Variables . SourceLanguages . GetEntry ( "AUTO DETECT" ) ;
281+ // enable auto detect
282+ CbSourceLanguage . SelectedItem = Variables . SourceLanguages . GetEntry ( "AUTO DETECT" ) ;
344283
345- // select the translation button
346- ActiveControl = BtnTranslate ;
284+ // select the translation button
285+ ActiveControl = BtnTranslate ;
286+ }
287+ catch ( Exception ex )
288+ {
289+ Log . Fatal ( ex , "[TEXT] Error while processing dropped content: {err}" , ex . Message ) ;
290+ }
347291 }
348292
349293 /// <summary>
350- /// Copy the translated text (if any) to the clipboard.
294+ /// Copies the translated text text to clipboard
351295 /// </summary>
352- /// <param name="sender"></param>
353- /// <param name="e"></param>
354- private void BtnCopyClipboard_Click ( object sender , EventArgs e )
296+ /// <param name="force"></param>
297+ private void CopyToClipboard ( bool force = false )
355298 {
356- if ( string . IsNullOrEmpty ( TbTranslated . Text ) ) return ;
299+ try
300+ {
301+ // copy the text
302+ var cleanText = TbTranslated . Text . Trim ( ) ;
303+
304+ // copy it to the clipboard if not empty and configured as such
305+ if ( string . IsNullOrWhiteSpace ( cleanText ) || ( ! force && ! Variables . AppSettings . CopyTranslationToClipboard ) ) return ;
306+
307+ // prepare sta thread
308+ var clipboardThread = new Thread ( new ThreadStart ( delegate
309+ {
310+ Clipboard . SetDataObject ( cleanText , true , 10 , 100 ) ;
311+ } ) ) ;
357312
358- Clipboard . SetText ( TbTranslated . Text , TextDataFormat . UnicodeText ) ;
359- LblClipboardCopied . Visible = true ;
360- _ = Task . Run ( HideClipboardCopied ) ;
361- }
313+ // set it
314+ clipboardThread . SetApartmentState ( ApartmentState . STA ) ;
362315
316+ // go
317+ clipboardThread . Start ( ) ;
318+
319+ // notify the user
320+ LblClipboardCopied . Visible = true ;
321+
322+ // hide the notification after a bit
323+ _ = Task . Run ( HideClipboardCopied ) ;
324+ }
325+ catch ( Exception ex )
326+ {
327+ Log . Fatal ( ex , "[TEXT] Error while copying to clipboard: {err}" , ex . Message ) ;
328+ }
329+ }
330+
363331 /// <summary>
364332 /// Resets and hides the detected source language info
365333 /// </summary>
@@ -385,13 +353,13 @@ private async void HideClipboardCopied()
385353 try
386354 {
387355 await Task . Delay ( TimeSpan . FromSeconds ( 3 ) ) ;
356+
388357 if ( IsDisposed ) return ;
358+ if ( ! IsHandleCreated ) return ;
359+
389360 LblClipboardCopied . Invoke ( delegate { LblClipboardCopied . Visible = false ; } ) ;
390361 }
391- catch
392- {
393- // best effort
394- }
362+ catch { }
395363 }
396364
397365 /// <summary>
@@ -401,6 +369,7 @@ private async void HideClipboardCopied()
401369 private void LockInterface ( bool @lock = true )
402370 {
403371 if ( IsDisposed ) return ;
372+ if ( ! IsHandleCreated ) return ;
404373
405374 Invoke ( new MethodInvoker ( delegate
406375 {
@@ -436,11 +405,9 @@ private void TbSource_KeyDown(object sender, KeyEventArgs e)
436405 ExecuteTranslation ( ) ;
437406 }
438407
439- private void BtnTranslate_Click ( object sender , EventArgs e ) => ExecuteTranslation ( ) ;
440-
441408 private void BtnClean_Click ( object sender , EventArgs e )
442409 {
443- // clear the text
410+ // clear the text (will also clear costs)
444411 TbSource . Text = string . Empty ;
445412 TbTranslated . Text = string . Empty ;
446413
@@ -488,8 +455,7 @@ private void BtnSave_Click(object sender, EventArgs e)
488455 var q = MessageBoxAdv . Show ( this , "Page succesfully saved as a text file!\r \n \n Click 'ok' to open its folder." , Variables . MessageBoxTitle , MessageBoxButtons . OKCancel , MessageBoxIcon . Information ) ;
489456 if ( q != DialogResult . OK ) return ;
490457
491- var argument = "/select, \" " + dialog . FileName + "\" " ;
492- Process . Start ( "explorer.exe" , argument ) ;
458+ HelperFunctions . OpenFileInExplorer ( dialog . FileName ) ;
493459 }
494460 catch ( Exception ex )
495461 {
@@ -533,5 +499,11 @@ private void BtnPrint_Click(object sender, EventArgs e)
533499 LockInterface ( false ) ;
534500 }
535501 }
502+
503+ private void BtnCopyClipboard_Click ( object sender , EventArgs e ) => CopyToClipboard ( true ) ;
504+
505+ private void BtnTranslate_Click ( object sender , EventArgs e ) => ExecuteTranslation ( ) ;
506+
507+ private void CbSourceLanguage_SelectedValueChanged ( object sender , EventArgs e ) => ResetDetectedSourceLanguage ( ) ;
536508 }
537509}
0 commit comments