I18NCommander/I18N Commander/UI WinForms/Components/Setting.cs

117 lines
4.9 KiB
C#
Raw Normal View History

2022-07-26 17:08:47 +00:00
using DataModel.Database;
using Processor;
using UI_WinForms.Resources;
namespace UI_WinForms.Components;
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();
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:
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-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
}
);
return new Setting(settingData);
2022-07-26 17:08:47 +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;
}
);
return new Setting(settingData);
2022-07-30 13:49:19 +00:00
}
public static IEnumerable<Task<Setting>> GetAllSettings()
{
2022-07-30 13:49:19 +00:00
yield return ShowDeepLAPIKeySettingAsync();
yield return ShowDeepLModeSettingAsync();
}
2022-07-26 17:08:47 +00:00
}