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

87 lines
2.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;
2022-07-26 17:40:46 +00:00
public 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-26 17:40:46 +00:00
this.labelIcon.Image = settingMetaData.Icon;
this.labelSettingName.Text = settingMetaData.SettingName();
this.labelExplanation.Text = settingMetaData.SettingExplanation();
2022-07-26 17:08:47 +00:00
2022-07-26 17:40:46 +00:00
var dataControl = settingMetaData.SetupDataControl();
2022-07-26 17:08:47 +00:00
this.tableLayout.Controls.Add(dataControl, 2, 0);
}
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:40:46 +00:00
public static async Task<Setting> ShowDeepLSettingAsync()
2022-07-26 17:08:47 +00:00
{
var currentSetting = await AppSettings.GetDeepLMode();
2022-07-26 17:40:46 +00:00
var settingData = new Setting.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
{
SettingDeepL.DISABLED => 0,
SettingDeepL.USE_FREE_ACCOUNT => 1,
SettingDeepL.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:
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:
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
{
Dock = DockStyle.Top,
};
}
public static IEnumerable<Task<Setting>> GetAllSettings()
{
yield return ShowDeepLSettingAsync();
}
2022-07-26 17:08:47 +00:00
}