using DataModel.Database; using Processor; using UI_WinForms.Resources; namespace UI_WinForms.Components; public partial class Setting : UserControl { public Setting() { this.InitializeComponent(); } private Setting(SettingUIData settingMetaData) { this.InitializeComponent(); this.labelIcon.Image = settingMetaData.Icon; this.labelSettingName.Text = settingMetaData.SettingName(); this.labelExplanation.Text = settingMetaData.SettingExplanation(); 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); // Calculate the needed height of the explanation label & centering of the data control when the parent window is resized: 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; // 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); }; } private readonly record struct SettingUIData( Bitmap Icon, Func SettingName, Func SettingExplanation, Func SetupDataControl ); private static async Task ShowDeepLSettingAsync() { var currentSetting = await AppSettings.GetDeepLMode(); var settingData = new SettingUIData( Icon: Icons.deepl_logo_icon_170284, 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 { SettingDeepL.DISABLED => 0, SettingDeepL.USE_FREE_ACCOUNT => 1, SettingDeepL.USE_PRO_ACCOUNT => 2, _ => 0, }; // Setup the change event handler: dropdown.SelectedValueChanged += async (sender, args) => await AppSettings.SetDeepLMode(dropdown.SelectedIndex switch { 0 => SettingDeepL.DISABLED, 1 => SettingDeepL.USE_FREE_ACCOUNT, 2 => SettingDeepL.USE_PRO_ACCOUNT, _ => SettingDeepL.DISABLED, }); // Apply the desired layout: dropdown.Dock = DockStyle.Fill; dropdown.DropDownStyle = ComboBoxStyle.DropDownList; return dropdown; } ); return new Setting(settingData) { Dock = DockStyle.Top, }; } public static IEnumerable> GetAllSettings() { yield return ShowDeepLSettingAsync(); } }