Implemented the save function

This commit is contained in:
Thorsten Sommer 2022-07-26 19:40:46 +02:00
parent 62b8bb24d0
commit 83cc7644d1
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 37 additions and 48 deletions

View File

@ -1,6 +1,6 @@
namespace UI_WinForms.Components namespace UI_WinForms.Components
{ {
partial class Setting<TSetting> partial class Setting
{ {
/// <summary> /// <summary>
/// Required designer variable. /// Required designer variable.
@ -29,7 +29,6 @@
private void InitializeComponent() private void InitializeComponent()
{ {
this.tableLayout = new System.Windows.Forms.TableLayoutPanel(); this.tableLayout = new System.Windows.Forms.TableLayoutPanel();
this.buttonSave = new System.Windows.Forms.Button();
this.labelExplanation = new System.Windows.Forms.Label(); this.labelExplanation = new System.Windows.Forms.Label();
this.labelSettingName = new System.Windows.Forms.Label(); this.labelSettingName = new System.Windows.Forms.Label();
this.labelIcon = new System.Windows.Forms.Label(); this.labelIcon = new System.Windows.Forms.Label();
@ -38,13 +37,12 @@
// //
// tableLayout // tableLayout
// //
this.tableLayout.ColumnCount = 5; this.tableLayout.ColumnCount = 4;
this.tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 66F)); this.tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 66F));
this.tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 250F)); this.tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 250F));
this.tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 220F)); this.tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 220F));
this.tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); this.tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 120F)); this.tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.tableLayout.Controls.Add(this.buttonSave, 4, 0);
this.tableLayout.Controls.Add(this.labelExplanation, 3, 0); this.tableLayout.Controls.Add(this.labelExplanation, 3, 0);
this.tableLayout.Controls.Add(this.labelSettingName, 1, 0); this.tableLayout.Controls.Add(this.labelSettingName, 1, 0);
this.tableLayout.Controls.Add(this.labelIcon, 0, 0); this.tableLayout.Controls.Add(this.labelIcon, 0, 0);
@ -57,24 +55,13 @@
this.tableLayout.Size = new System.Drawing.Size(1264, 70); this.tableLayout.Size = new System.Drawing.Size(1264, 70);
this.tableLayout.TabIndex = 0; this.tableLayout.TabIndex = 0;
// //
// buttonSave
//
this.buttonSave.Dock = System.Windows.Forms.DockStyle.Fill;
this.buttonSave.Location = new System.Drawing.Point(1147, 3);
this.buttonSave.Name = "buttonSave";
this.buttonSave.Size = new System.Drawing.Size(114, 64);
this.buttonSave.TabIndex = 0;
this.buttonSave.Text = "Save";
this.buttonSave.UseVisualStyleBackColor = true;
this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
//
// labelExplanation // labelExplanation
// //
this.labelExplanation.AutoSize = true; this.labelExplanation.AutoSize = true;
this.labelExplanation.Dock = System.Windows.Forms.DockStyle.Fill; this.labelExplanation.Dock = System.Windows.Forms.DockStyle.Fill;
this.labelExplanation.Location = new System.Drawing.Point(539, 0); this.labelExplanation.Location = new System.Drawing.Point(539, 0);
this.labelExplanation.Name = "labelExplanation"; this.labelExplanation.Name = "labelExplanation";
this.labelExplanation.Size = new System.Drawing.Size(602, 70); this.labelExplanation.Size = new System.Drawing.Size(722, 70);
this.labelExplanation.TabIndex = 1; this.labelExplanation.TabIndex = 1;
this.labelExplanation.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; this.labelExplanation.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
// //
@ -115,7 +102,6 @@
#endregion #endregion
private TableLayoutPanel tableLayout; private TableLayoutPanel tableLayout;
private Button buttonSave;
private Label labelExplanation; private Label labelExplanation;
private Label labelSettingName; private Label labelSettingName;
private Label labelIcon; private Label labelIcon;

View File

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

View File

@ -7,7 +7,7 @@ public partial class Settings : UserControl
this.InitializeComponent(); this.InitializeComponent();
this.Load += async (sender, args) => this.Load += async (sender, args) =>
{ {
var deepL = await Setting<bool>.ShowDeepLSettingAsync(); var deepL = await Setting.ShowDeepLSettingAsync();
this.panelSettings.Controls.Add(deepL); this.panelSettings.Controls.Add(deepL);
}; };
} }