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

87 lines
2.9 KiB
C#

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);
}
private readonly record struct SettingUIData(
Bitmap Icon,
Func<string> SettingName,
Func<string> SettingExplanation,
Func<Control> SetupDataControl
);
public static async Task<Setting> ShowDeepLSettingAsync()
{
var currentSetting = await AppSettings.GetDeepLMode();
var settingData = new Setting.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) =>
{
var newSetting = dropdown.SelectedIndex switch
{
0 => SettingDeepL.DISABLED,
1 => SettingDeepL.USE_FREE_ACCOUNT,
2 => SettingDeepL.USE_PRO_ACCOUNT,
_ => SettingDeepL.DISABLED,
};
await AppSettings.SetDeepLMode(newSetting);
};
// Apply the desired layout:
dropdown.Dock = DockStyle.Fill;
dropdown.DropDownStyle = ComboBoxStyle.DropDownList;
return dropdown;
}
);
return new Setting(settingData)
{
Dock = DockStyle.Top,
};
}
public static IEnumerable<Task<Setting>> GetAllSettings()
{
yield return ShowDeepLSettingAsync();
}
}