From c211c9ef6209dee5fb5363818fdba8961809c40e Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 30 Jul 2022 16:38:38 +0200 Subject: [PATCH] Added DeepL action setting --- .../DataModel/Database/SettingDeepLAction.cs | 7 +++ .../DataModel/Database/SettingNames.cs | 1 + I18N Commander/Processor/AppSettings.cs | 55 ++++++++++++++++++ .../UI WinForms/Components/Setting.cs | 42 +++++++++++++ .../UI WinForms/Resources/Icons.Designer.cs | 10 ++++ .../UI WinForms/Resources/Icons.resx | 3 + .../Resources/icons8-play-512 (2).png | Bin 0 -> 2051 bytes 7 files changed, 118 insertions(+) create mode 100644 I18N Commander/DataModel/Database/SettingDeepLAction.cs create mode 100644 I18N Commander/UI WinForms/Resources/icons8-play-512 (2).png diff --git a/I18N Commander/DataModel/Database/SettingDeepLAction.cs b/I18N Commander/DataModel/Database/SettingDeepLAction.cs new file mode 100644 index 0000000..50312d7 --- /dev/null +++ b/I18N Commander/DataModel/Database/SettingDeepLAction.cs @@ -0,0 +1,7 @@ +namespace DataModel.Database; + +public enum SettingDeepLAction +{ + MANUAL, + AUTOMATIC_ALL, +} \ No newline at end of file diff --git a/I18N Commander/DataModel/Database/SettingNames.cs b/I18N Commander/DataModel/Database/SettingNames.cs index 7a41b64..d507d63 100644 --- a/I18N Commander/DataModel/Database/SettingNames.cs +++ b/I18N Commander/DataModel/Database/SettingNames.cs @@ -2,6 +2,7 @@ public static class SettingNames { + public static readonly string DEEPL_ACTION = "DeepL Action"; public static readonly string DEEPL_API_KEY = "DeepL API Key"; public static readonly string DEEPL_MODE = "DeepL Mode"; } \ No newline at end of file diff --git a/I18N Commander/Processor/AppSettings.cs b/I18N Commander/Processor/AppSettings.cs index 63f28f5..1d97a66 100644 --- a/I18N Commander/Processor/AppSettings.cs +++ b/I18N Commander/Processor/AppSettings.cs @@ -113,4 +113,59 @@ public static class AppSettings } #endregion + + #region DeepL Action + + public static async Task SetDeepLAction(SettingDeepLAction action) + { + // Convert the enum to its int value: + var intValue = (int)action; + + // Get the database: + await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); + + // Check, if the setting is already set: + if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_ACTION) is {} existingSetting) + { + existingSetting.IntegerValue = intValue; + await db.SaveChangesAsync(); + } + + // Does not exist, so create it: + else + { + var setting = new Setting + { + Code = SettingNames.DEEPL_ACTION, + IntegerValue = intValue, + }; + + await db.Settings.AddAsync(setting); + await db.SaveChangesAsync(); + } + } + + public static async Task GetDeepLAction() + { + // Get the database: + await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); + + // Check, if the setting is already set: + if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_ACTION) is { } existingSetting) + return (SettingDeepLAction) existingSetting.IntegerValue; + + // Does not exist, so create it: + var setting = new Setting + { + Code = SettingNames.DEEPL_ACTION, + IntegerValue = (int)SettingDeepLAction.MANUAL, + }; + + await db.Settings.AddAsync(setting); + await db.SaveChangesAsync(); + + return (SettingDeepLAction) setting.IntegerValue; + } + + #endregion } \ No newline at end of file diff --git a/I18N Commander/UI WinForms/Components/Setting.cs b/I18N Commander/UI WinForms/Components/Setting.cs index e7a6966..6bff82f 100644 --- a/I18N Commander/UI WinForms/Components/Setting.cs +++ b/I18N Commander/UI WinForms/Components/Setting.cs @@ -108,9 +108,51 @@ public sealed partial class Setting : UserControl return new Setting(settingData); } + + private static async Task ShowDeepLActionSettingAsync() + { + var currentSetting = await AppSettings.GetDeepLAction(); + var settingData = new SettingUIData( + Icon: Icons.icons8_play_512__2_, + SettingName: () => "DeepL Operation", + SettingExplanation: () => "Should the missing translations be automatically completed by DeepL? This can lead to higher costs. By default, DeepL is only applied manually.", + SetupDataControl: () => + { + // We set up a combo box with the available actions: + var dropdown = new ComboBox(); + dropdown.Items.Add("Manual"); + dropdown.Items.Add("Automatic"); + dropdown.SelectedIndex = currentSetting switch + { + SettingDeepLAction.MANUAL => 0, + SettingDeepLAction.AUTOMATIC_ALL => 1, + + _ => 0, + }; + + // Setup the change event handler: + dropdown.SelectedValueChanged += async (sender, args) => await AppSettings.SetDeepLAction(dropdown.SelectedIndex switch + { + 0 => SettingDeepLAction.MANUAL, + 1 => SettingDeepLAction.AUTOMATIC_ALL, + + _ => SettingDeepLAction.MANUAL, + }); + + // Apply the desired layout: + dropdown.Dock = DockStyle.Fill; + dropdown.DropDownStyle = ComboBoxStyle.DropDownList; + + return dropdown; + } + ); + + return new Setting(settingData); + } public static IEnumerable> GetAllSettings() { + yield return ShowDeepLActionSettingAsync(); yield return ShowDeepLAPIKeySettingAsync(); yield return ShowDeepLModeSettingAsync(); } diff --git a/I18N Commander/UI WinForms/Resources/Icons.Designer.cs b/I18N Commander/UI WinForms/Resources/Icons.Designer.cs index bb5ab78..f0d5fcd 100644 --- a/I18N Commander/UI WinForms/Resources/Icons.Designer.cs +++ b/I18N Commander/UI WinForms/Resources/Icons.Designer.cs @@ -200,6 +200,16 @@ namespace UI_WinForms.Resources { } } + /// + /// Looks up a localized resource of type System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap icons8_play_512__2_ { + get { + object obj = ResourceManager.GetObject("icons8_play_512__2_", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + /// /// Looks up a localized resource of type System.Drawing.Bitmap. /// diff --git a/I18N Commander/UI WinForms/Resources/Icons.resx b/I18N Commander/UI WinForms/Resources/Icons.resx index e4db609..a18a78c 100644 --- a/I18N Commander/UI WinForms/Resources/Icons.resx +++ b/I18N Commander/UI WinForms/Resources/Icons.resx @@ -160,6 +160,9 @@ icons8-open-file-under-cursor-512.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + icons8-play-512 (2).png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + icons8-remove-tag-512.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a diff --git a/I18N Commander/UI WinForms/Resources/icons8-play-512 (2).png b/I18N Commander/UI WinForms/Resources/icons8-play-512 (2).png new file mode 100644 index 0000000000000000000000000000000000000000..03d329fdcc8fe1392c7035d4e444c1755a523767 GIT binary patch literal 2051 zcmah~2~ZPf6b==tATZ!Ty-JsGR%LT=nV6Uo!Vw@029$sd8j>YhA=$XukOVt%{)7p~a(G`X@n9(Ka)iHUfS=XWQB0?mHW=b#xES6%a2&_}608S#Y zI69SBl8z8iDk4LbZ1PA;8yP~SY;r|1PXuyU@Cc)9mK+=X-pw6Y{D3< z*kn0DsF)OrR;z`zelUj1C^QCxL7~zqbh}KxC9B!M3jWP zITNP5F!>6x42edRGD6NK`v<%OB%soF_f0|>8dD|KC{f^=K4l`B!r0^>I)yd`CB7Fj zrJ5Ow>W~Z(4^<#aHBg<5VhT+B02qR0V0av8YlKa<_?oN((~Jo)DS-UUVN5~%kdRm$ zOG7gNrWS^CA%18`AdL|i=u3w!GN5QoV2h^dd13;v=v2^~)F59!YCJ8FNe^QBQI}Ea zOe)o^Y-WSIAQcnh|KpoF8e#zpfq)r-stF9&o3|`&9*1nQtSrk6)ZE_?WUew(EHP!B zO;+KUR3ky8X6Hc8f~v7JLMz6RU>PVdn;e{$hJyO&p;%MrLNu60g)P;cw9v{Cp!VUj zQ6?gzm~Pzb&{&jd!L!VSH?AnKv+RMZ4`v8ua)^M%(4^4X=?)ezCmXTsOHTO0GvLOwg8y{V_%xAC467Z?j}ELY==`YW zW|My3E8CTo{)U*ic!RO}iG17fu`R`yO0K6gto=(po^(-|oX{@xW}W;lDMgp8!#!)u z&w2JA|E4PHg7*bi0qm14d00>99IRMAdhBwN$2{xwhPq|XizRI$KDtn>XFQH>KK!!3 zE4#@3P^E3u@SKq^ygL?HB}Y@aPMlZvmxsHG%Tf|od$x_N%q_3DyOe%noM7`VdYgf$J=s@b7!>TnsCy_&m4X`yLk9uY%jH>Y{o^OPxBsGU2E9$vVWC* zYf1;6HB}V>IV?AI~;= z4i9t7Dzxwxs}zImj-WEX5xg#JRS}755^45a(_=*{uUG&|l+Nb`$BPOcU9aD0j9t=x zyLRoF*qJ%AZ0@@TIWOsOtsvr8l;&7hHa{mRVc2jAUiziWw|g9$dHoN6{iH76X8sAs zeaj<93U=@A`#n3oo5SJaG2AugPk@nojqi3e{6^>BHHy+7y{=|19X)sO#)YsQgTftX z^@g74CPUqhzGvHxxg9k+^`ss+IatVhdhIrxBJu7Zn%b{a<6jLrF6&yds$({*OL;wz z~ycc+tI}# z>u>0JPvL=5{>z`7dM-Kj9HHdrw<^UcxJDb>Ca^PeGYzsP|sf`KIyDonJZB zKC#E{|B=J?c+u@K<~G*S_&mi6ZR~&G%TjT3VM{iQ_PJIM^OGX|MeZ9-?{0o*46lZ} GA@5&^UiNDM literal 0 HcmV?d00001