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

79 lines
2.7 KiB
C#

using DataModel.Database;
using Processor;
using UI_WinForms.Resources;
namespace UI_WinForms.Components;
public partial class Setting<TSetting> : UserControl
{
private readonly SettingUIData<TSetting> settingData;
public Setting()
{
this.InitializeComponent();
}
private Setting(SettingUIData<TSetting> settingData)
{
this.InitializeComponent();
this.settingData = settingData;
this.labelIcon.Image = settingData.Icon;
this.labelSettingName.Text = settingData.SettingName();
this.labelExplanation.Text = settingData.SettingExplanation();
var dataControl = this.settingData.SetupDataControl();
this.tableLayout.Controls.Add(dataControl, 2, 0);
}
private void buttonSave_Click(object sender, EventArgs e)
{
}
private readonly record struct SettingUIData<TInnerSetting>(
Bitmap Icon,
Func<string> SettingName,
Func<string> SettingExplanation,
Func<Control> SetupDataControl,
Func<TInnerSetting> GetCurrentSetting,
Func<TInnerSetting, Task> SaveSetting
);
public static async Task<Setting<SettingDeepL>> ShowDeepLSettingAsync()
{
var currentSetting = await AppSettings.GetDeepLMode();
var settingData = new Setting<SettingDeepL>.SettingUIData<SettingDeepL>(
Icon: Icons.icons8_add_folder_512,
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,
};
// Apply the desired design:
dropdown.Dock = DockStyle.Fill;
dropdown.DropDownStyle = ComboBoxStyle.DropDownList;
return dropdown;
},
GetCurrentSetting: () => currentSetting,
SaveSetting: async setting => await AppSettings.SetDeepLMode(setting));
return new Setting<SettingDeepL>(settingData)
{
Dock = DockStyle.Top,
};
}
}