2022-07-26 17:08:47 +00:00
using DataModel.Database ;
using Processor ;
using UI_WinForms.Resources ;
namespace UI_WinForms.Components ;
2022-07-30 14:38:07 +00:00
public sealed partial class Setting : UserControl
2022-07-26 17:08:47 +00:00
{
public Setting ( )
{
this . InitializeComponent ( ) ;
}
2022-07-26 17:40:46 +00:00
private Setting ( SettingUIData settingMetaData )
2022-07-26 17:08:47 +00:00
{
this . InitializeComponent ( ) ;
2022-07-30 14:38:07 +00:00
this . Dock = DockStyle . Top ;
2022-07-26 17:40:46 +00:00
this . labelIcon . Image = settingMetaData . Icon ;
this . labelSettingName . Text = settingMetaData . SettingName ( ) ;
this . labelExplanation . Text = settingMetaData . SettingExplanation ( ) ;
2022-07-27 19:56:59 +00:00
var dataControl = settingMetaData . SetupDataControl ( ) ;
this . tableLayout . Controls . Add ( dataControl , 2 , 0 ) ;
// Ensure, that this data control is vertical centered by calculating the needed margin, considering the outer size of the table layout:
var margin = ( this . tableLayout . GetRowHeights ( ) . First ( ) - dataControl . Height ) / 2f ;
dataControl . Margin = new Padding ( 0 , ( int ) margin , 0 , ( int ) margin ) ;
2022-07-26 17:08:47 +00:00
2022-07-27 19:56:59 +00:00
// Calculate the needed height of the explanation label & centering of the data control when the parent window is resized:
2022-07-27 19:38:54 +00:00
this . tableLayout . Resize + = ( sender , args ) = >
{
// Adjust the height of the parent controls (table & user control):
this . tableLayout . Height = Math . Max ( ( int ) this . labelExplanation . CreateGraphics ( ) . MeasureString ( this . labelExplanation . Text , this . labelExplanation . Font , new SizeF ( this . labelExplanation . Width , 1000 ) ) . Height , 66 ) ;
this . Height = this . tableLayout . Height + this . tableLayout . Margin . Vertical ;
2022-07-27 19:56:59 +00:00
// Ensure, that this data control is vertical centered by calculating the needed margin, considering the outer size of the table layout:
var margin = ( this . tableLayout . GetRowHeights ( ) . First ( ) - dataControl . Height ) / 2f ;
dataControl . Margin = new Padding ( 0 , ( int ) margin , 0 , ( int ) margin ) ;
2022-07-27 19:38:54 +00:00
} ;
2022-07-26 17:08:47 +00:00
}
2022-07-26 17:40:46 +00:00
private readonly record struct SettingUIData (
2022-07-26 17:08:47 +00:00
Bitmap Icon ,
Func < string > SettingName ,
Func < string > SettingExplanation ,
2022-07-26 17:40:46 +00:00
Func < Control > SetupDataControl
2022-07-26 17:08:47 +00:00
) ;
2022-07-26 17:46:44 +00:00
2022-07-30 13:49:19 +00:00
private static async Task < Setting > ShowDeepLModeSettingAsync ( )
2022-07-26 17:08:47 +00:00
{
var currentSetting = await AppSettings . GetDeepLMode ( ) ;
2022-07-26 17:46:44 +00:00
var settingData = new SettingUIData (
2022-07-26 17:15:36 +00:00
Icon : Icons . deepl_logo_icon_170284 ,
2022-07-26 17:08:47 +00:00
SettingName : ( ) = > "DeepL Service" ,
SettingExplanation : ( ) = > "DeepL is a translation service that offers a wide range of translation services. This setting allows you to choose between the free and pro version of DeepL." ,
SetupDataControl : ( ) = >
{
var dropdown = new ComboBox ( ) ;
dropdown . Items . Add ( "Disabled" ) ;
dropdown . Items . Add ( "Free version" ) ;
dropdown . Items . Add ( "Pro version" ) ;
dropdown . SelectedIndex = currentSetting switch
{
2022-07-30 14:09:58 +00:00
SettingDeepLMode . DISABLED = > 0 ,
SettingDeepLMode . USE_FREE_ACCOUNT = > 1 ,
SettingDeepLMode . USE_PRO_ACCOUNT = > 2 ,
2022-07-26 17:40:46 +00:00
2022-07-26 17:08:47 +00:00
_ = > 0 ,
} ;
2022-07-26 17:40:46 +00:00
// Setup the change event handler:
2022-07-26 17:48:30 +00:00
dropdown . SelectedValueChanged + = async ( sender , args ) = > await AppSettings . SetDeepLMode ( dropdown . SelectedIndex switch
2022-07-26 17:40:46 +00:00
{
2022-07-30 14:09:58 +00:00
0 = > SettingDeepLMode . DISABLED ,
1 = > SettingDeepLMode . USE_FREE_ACCOUNT ,
2 = > SettingDeepLMode . USE_PRO_ACCOUNT ,
2022-07-26 17:40:46 +00:00
2022-07-30 14:09:58 +00:00
_ = > SettingDeepLMode . DISABLED ,
2022-07-26 17:48:30 +00:00
} ) ;
2022-07-26 17:40:46 +00:00
// Apply the desired layout:
2022-07-26 17:08:47 +00:00
dropdown . Dock = DockStyle . Fill ;
dropdown . DropDownStyle = ComboBoxStyle . DropDownList ;
return dropdown ;
2022-07-26 17:40:46 +00:00
}
) ;
2022-07-30 14:38:07 +00:00
return new Setting ( settingData ) ;
2022-07-26 17:08:47 +00:00
}
2022-07-26 17:45:18 +00:00
2022-07-30 13:49:19 +00:00
private static async Task < Setting > ShowDeepLAPIKeySettingAsync ( )
{
var currentSetting = await AppSettings . GetDeepLAPIKey ( ) ;
var settingData = new SettingUIData (
Icon : Icons . icons8_key_512 ,
SettingName : ( ) = > "DeepL API Key" ,
SettingExplanation : ( ) = > "The API key is required to use the DeepL translation service. You can find your API key on the DeepL website." ,
SetupDataControl : ( ) = >
{
var textbox = new TextBox ( ) ;
textbox . Text = currentSetting ;
textbox . TextChanged + = async ( sender , args ) = > await AppSettings . SetDeepLAPIKey ( textbox . Text ) ;
textbox . Dock = DockStyle . Fill ;
return textbox ;
}
) ;
2022-07-30 14:38:07 +00:00
return new Setting ( settingData ) ;
2022-07-30 13:49:19 +00:00
}
2022-07-30 14:38:38 +00:00
private static async Task < Setting > ShowDeepLActionSettingAsync ( )
{
var currentSetting = await AppSettings . GetDeepLAction ( ) ;
var settingData = new SettingUIData (
Icon : Icons . icons8_play_512__2_ ,
SettingName : ( ) = > "DeepL Operation" ,
SettingExplanation : ( ) = > "Should the missing translations be automatically completed by DeepL? This can lead to higher costs. By default, DeepL is only applied manually." ,
SetupDataControl : ( ) = >
{
// We set up a combo box with the available actions:
var dropdown = new ComboBox ( ) ;
dropdown . Items . Add ( "Manual" ) ;
dropdown . Items . Add ( "Automatic" ) ;
dropdown . SelectedIndex = currentSetting switch
{
SettingDeepLAction . MANUAL = > 0 ,
SettingDeepLAction . AUTOMATIC_ALL = > 1 ,
_ = > 0 ,
} ;
// Setup the change event handler:
dropdown . SelectedValueChanged + = async ( sender , args ) = > await AppSettings . SetDeepLAction ( dropdown . SelectedIndex switch
{
0 = > SettingDeepLAction . MANUAL ,
1 = > SettingDeepLAction . AUTOMATIC_ALL ,
_ = > SettingDeepLAction . MANUAL ,
} ) ;
// Apply the desired layout:
dropdown . Dock = DockStyle . Fill ;
dropdown . DropDownStyle = ComboBoxStyle . DropDownList ;
return dropdown ;
}
) ;
return new Setting ( settingData ) ;
}
2022-07-30 13:49:19 +00:00
2022-07-31 19:19:54 +00:00
private static IEnumerable < Task < Setting > > ShowCultureSettingsAsync ( )
{
var isFirstCulture = true ; // We need this flag to distinguish the first task from the others.
var numberOfCultures = int . MaxValue ; // There is always at least one culture, which is the default culture. We update this value later, out of the tasks.
while ( numberOfCultures > 0 )
{
var innerLoopIndex = numberOfCultures ; // needed to avoid closure issues.
yield return Task . Run ( async ( ) = >
{
var localCultureIndex = innerLoopIndex ;
// Get the total number of cultures. We cannot do this in the outer loop,
// because we cannot await there. The AppSettings is caching the answer, though.
var numberCultures = await AppSettings . GetNumberCultures ( ) ;
// Update the number of cultures in the outer loop for the first call:
if ( isFirstCulture )
{
localCultureIndex = numberCultures ;
numberOfCultures = numberCultures ;
isFirstCulture = false ;
}
// Get the current culture code:
var currentCultureCode = await AppSettings . GetCultureCode ( localCultureIndex ) ;
// Construct the setting:
return new Setting ( new ( )
{
Icon = Icons . icons8_chat_bubble_512 ,
SettingName = ( ) = > $"{localCultureIndex}. Culture" ,
SettingExplanation = ( ) = > "The culture according to RFC 4646: First comes the ISO 639-1 language code in lower case, followed by a hyphen, followed by the ISO 3166-1 alpha-2 country code in upper case. Example: en-US for English in the USA, de-DE for German in Germany." ,
SetupDataControl = ( ) = >
{
var textbox = new TextBox ( ) ;
textbox . Text = currentCultureCode ;
textbox . TextChanged + = async ( sender , args ) = >
{
await AppSettings . SetCultureCode ( localCultureIndex , textbox . Text ) ;
} ;
textbox . Dock = DockStyle . Fill ;
return textbox ;
}
} ) ;
} ) ;
numberOfCultures - - ;
}
}
2022-07-26 17:45:18 +00:00
public static IEnumerable < Task < Setting > > GetAllSettings ( )
{
2022-07-31 19:19:54 +00:00
foreach ( var setting in ShowCultureSettingsAsync ( ) )
{
yield return setting ;
}
2022-07-30 14:38:38 +00:00
yield return ShowDeepLActionSettingAsync ( ) ;
2022-07-30 13:49:19 +00:00
yield return ShowDeepLAPIKeySettingAsync ( ) ;
yield return ShowDeepLModeSettingAsync ( ) ;
2022-07-26 17:45:18 +00:00
}
2022-07-26 17:08:47 +00:00
}