From 4a32cd9339ffafd64a6c750c76f37327394cbe99 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Mon, 3 Oct 2022 19:10:39 +0200 Subject: [PATCH] Implemented .NET generator settings --- .../DataModel/Database/SettingNames.cs | 2 + I18N Commander/Processor/AppSettings.cs | 148 ++++++++++++++++++ .../UI WinForms/Components/Setting.cs | 89 +++++++++++ .../UI WinForms/Resources/Icons.Designer.cs | 10 ++ .../UI WinForms/Resources/Icons.resx | 3 + .../Resources/icons8-folder-tree-512.png | Bin 0 -> 2439 bytes 6 files changed, 252 insertions(+) create mode 100644 I18N Commander/UI WinForms/Resources/icons8-folder-tree-512.png diff --git a/I18N Commander/DataModel/Database/SettingNames.cs b/I18N Commander/DataModel/Database/SettingNames.cs index 734eaed..6ed3ee9 100644 --- a/I18N Commander/DataModel/Database/SettingNames.cs +++ b/I18N Commander/DataModel/Database/SettingNames.cs @@ -8,4 +8,6 @@ public static class SettingNames public static readonly string DEEPL_SOURCE_CULTURE = "DeepL Source Culture"; public static readonly string DEEPL_MODE = "DeepL Mode"; public static readonly string GENERATOR_MODE = "Generator Mode"; + public static readonly string GENERATOR_DOTNET_ENABLED = "Generator .NET Enabled"; + public static readonly string GENERATOR_DOTNET_DESTINATION_PATH = "Generator .NET Destination Path"; } \ No newline at end of file diff --git a/I18N Commander/Processor/AppSettings.cs b/I18N Commander/Processor/AppSettings.cs index 8526d7a..3efcaf5 100644 --- a/I18N Commander/Processor/AppSettings.cs +++ b/I18N Commander/Processor/AppSettings.cs @@ -537,5 +537,153 @@ public static class AppSettings #endregion + #region .NET Generator Enabled/Disabled + + private static bool CACHE_GENERATOR_DOTNET_ENABLED = true; + private static bool CACHE_GENERATOR_DOTNET_ENABLED_IS_LOADED = false; + + public static async Task GetGeneratorDotnetEnabled() + { + // When possible, use the cache: + if (CACHE_GENERATOR_DOTNET_ENABLED_IS_LOADED) + return CACHE_GENERATOR_DOTNET_ENABLED; + + var generatorDotnetEnabled = false; + try + { + // 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.GENERATOR_DOTNET_ENABLED) is { } existingSetting) + { + generatorDotnetEnabled = existingSetting.BoolValue; + return generatorDotnetEnabled; + } + + // Does not exist, so create it: + var setting = new Setting + { + Code = SettingNames.GENERATOR_DOTNET_ENABLED, + BoolValue = generatorDotnetEnabled, + }; + + await db.Settings.AddAsync(setting); + await db.SaveChangesAsync(); + return generatorDotnetEnabled; + } + finally + { + CACHE_GENERATOR_DOTNET_ENABLED_IS_LOADED = true; + CACHE_GENERATOR_DOTNET_ENABLED = generatorDotnetEnabled; + } + } + + public static async Task SetGeneratorDotnetEnabled(bool enabled) + { + // Update the cache: + CACHE_GENERATOR_DOTNET_ENABLED = enabled; + CACHE_GENERATOR_DOTNET_ENABLED_IS_LOADED = true; + + // 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.GENERATOR_DOTNET_ENABLED) is { } existingSetting) + { + existingSetting.BoolValue = enabled; + await db.SaveChangesAsync(); + } + + // Does not exist, so create it: + else + { + var setting = new Setting + { + Code = SettingNames.GENERATOR_DOTNET_ENABLED, + BoolValue = enabled, + }; + + await db.Settings.AddAsync(setting); + await db.SaveChangesAsync(); + } + } + + #endregion + + #region .NET Generator Destination Path + + private static string CACHE_GENERATOR_DOTNET_DESTINATION_PATH = string.Empty; + private static bool CACHE_GENERATOR_DOTNET_DESTINATION_PATH_IS_LOADED = false; + + public static async Task GetGeneratorDotnetDestinationPath() + { + // When possible, use the cache: + if (CACHE_GENERATOR_DOTNET_DESTINATION_PATH_IS_LOADED) + return CACHE_GENERATOR_DOTNET_DESTINATION_PATH; + + var generatorDotnetDestinationPath = string.Empty; + try + { + // 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.GENERATOR_DOTNET_DESTINATION_PATH) is { } existingSetting) + { + generatorDotnetDestinationPath = existingSetting.TextValue; + return generatorDotnetDestinationPath; + } + + // Does not exist, so create it: + var setting = new Setting + { + Code = SettingNames.GENERATOR_DOTNET_DESTINATION_PATH, + TextValue = generatorDotnetDestinationPath, + }; + + await db.Settings.AddAsync(setting); + await db.SaveChangesAsync(); + return generatorDotnetDestinationPath; + } + finally + { + CACHE_GENERATOR_DOTNET_DESTINATION_PATH_IS_LOADED = true; + CACHE_GENERATOR_DOTNET_DESTINATION_PATH = generatorDotnetDestinationPath; + } + } + + public static async Task SetGeneratorDotnetDestinationPath(string path) + { + // Update the cache: + CACHE_GENERATOR_DOTNET_DESTINATION_PATH = path; + CACHE_GENERATOR_DOTNET_DESTINATION_PATH_IS_LOADED = true; + + // 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.GENERATOR_DOTNET_DESTINATION_PATH) is { } existingSetting) + { + existingSetting.TextValue = path; + await db.SaveChangesAsync(); + } + + // Does not exist, so create it: + else + { + var setting = new Setting + { + Code = SettingNames.GENERATOR_DOTNET_DESTINATION_PATH, + TextValue = path, + }; + + await db.Settings.AddAsync(setting); + await db.SaveChangesAsync(); + } + } + + #endregion + #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 c354430..af0abba 100644 --- a/I18N Commander/UI WinForms/Components/Setting.cs +++ b/I18N Commander/UI WinForms/Components/Setting.cs @@ -434,8 +434,97 @@ public sealed partial class Setting : UserControl return new Setting(settingData); } + private static async Task ShowGeneratorDotnetEnabledSettingAsync() + { + var currentSetting = await AppSettings.GetGeneratorDotnetEnabled(); + + var settingData = new SettingUIData( + Icon: Icons.icons8_code_512, + SettingName: () => "Generator: .NET", + ChangeNeedsRestart: false, + SettingExplanation: () => "When enabled, .NET translation files are generated. Requires a .NET 6 or newer project.", + SettingExplanationLink: () => (string.Empty, string.Empty), + SetupDataControl: (changeTrigger) => + { + // Set up an checkbox: + var checkbox = new CheckBox(); + checkbox.Checked = currentSetting; + checkbox.CheckedChanged += (sender, args) => changeTrigger(); + checkbox.CheckedChanged += async (sender, args) => await AppSettings.SetGeneratorDotnetEnabled(checkbox.Checked); + checkbox.Text = "Enable .NET Generator"; + + // Apply the desired layout: + checkbox.Dock = DockStyle.Fill; + return checkbox; + } + ); + + return new Setting(settingData); + } + + private static async Task ShowGeneratorDotnetDestinationPathSettingAsync() + { + var currentSetting = await AppSettings.GetGeneratorDotnetDestinationPath(); + + var settingData = new SettingUIData( + Icon: Icons.icons8_code_512, + SettingName: () => "Generator: .NET Destination Path", + ChangeNeedsRestart: false, + SettingExplanation: () => "The destination path for the .NET translation files. You might use environment variables like %USERPROFILE%.", + SettingExplanationLink: () => (string.Empty, string.Empty), + SetupDataControl: (changeTrigger) => + { + // Set up a horizontal layout: + var layout = new TableLayoutPanel(); + layout.ColumnCount = 2; + layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F)); + layout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 66F)); + layout.RowCount = 1; + layout.RowStyles.Add(new RowStyle(SizeType.Percent, 100F)); + layout.Dock = DockStyle.Fill; + + // Set up a textbox: + var textbox = new TextBox(); + textbox.Text = currentSetting; + textbox.TextChanged += (sender, args) => changeTrigger(); + textbox.TextChanged += async (sender, args) => await AppSettings.SetGeneratorDotnetDestinationPath(textbox.Text); + textbox.Dock = DockStyle.Fill; + textbox.Margin = new Padding(0, 13, 0, 13); + layout.Controls.Add(textbox, 0, 0); + + // Set up a button: + var button = new Button(); + button.Text = string.Empty; + button.Image = Icons.icons8_folder_tree_512; + button.FlatStyle = FlatStyle.Flat; + button.FlatAppearance.BorderSize = 0; + button.BackColor = Color.Empty; + button.UseVisualStyleBackColor = true; + button.Size = new Size(60, 60); + button.Click += (sender, args) => + { + var dialog = new FolderBrowserDialog(); + dialog.SelectedPath = textbox.Text; + dialog.InitialDirectory = textbox.Text; + dialog.Description = "Select the destination path for the .NET translation files."; + dialog.ShowNewFolderButton = true; + if (dialog.ShowDialog() == DialogResult.OK) + textbox.Text = dialog.SelectedPath; + }; + button.Dock = DockStyle.Fill; + layout.Controls.Add(button, 1, 0); + + return layout; + } + ); + + return new Setting(settingData); + } + public static IEnumerable> GetAllSettings() { + yield return ShowGeneratorDotnetDestinationPathSettingAsync(); + yield return ShowGeneratorDotnetEnabledSettingAsync(); yield return ShowGeneratorModeSettingAsync(); yield return ShowDeepLSourceCultureSettingAsync(); foreach (var setting in ShowCultureSettingsAsync()) diff --git a/I18N Commander/UI WinForms/Resources/Icons.Designer.cs b/I18N Commander/UI WinForms/Resources/Icons.Designer.cs index 95a9b6e..7a66bfe 100644 --- a/I18N Commander/UI WinForms/Resources/Icons.Designer.cs +++ b/I18N Commander/UI WinForms/Resources/Icons.Designer.cs @@ -190,6 +190,16 @@ namespace UI_WinForms.Resources { } } + /// + /// Looks up a localized resource of type System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap icons8_folder_tree_512 { + get { + object obj = ResourceManager.GetObject("icons8_folder_tree_512", 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 15f9723..538f121 100644 --- a/I18N Commander/UI WinForms/Resources/Icons.resx +++ b/I18N Commander/UI WinForms/Resources/Icons.resx @@ -157,6 +157,9 @@ icons8-document-512.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + icons8-folder-tree-512.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + icons8-increase-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-folder-tree-512.png b/I18N Commander/UI WinForms/Resources/icons8-folder-tree-512.png new file mode 100644 index 0000000000000000000000000000000000000000..60db4b817ec593685ed118896223caba42aa5748 GIT binary patch literal 2439 zcmaJ@dt4J&77ieSns{Lb<-1~j!JLjHr?wRb0 zjS08Kx?(XHj4daE6^DM~4Ud%t`bl%`*I+R7>Y?}qB!L@E6TmV8UkImx1hq_o;us7) zM6KWpGC%~E28tm$19+nS2Eai=2EYs8lDG;cD1jpKl;HZjn0P^6hJY#rLe^sGY8oma z0}(z>EtASsG&KV->eA5PhHWB%Gg6QY29Usw#W7(ehzlSD5JPBIer9KDJJ?-sZ=6~OeB->CBeBe5n))o=ljQL5oI1Z6Q%Ei;tizG70TXUlN|=7w;QH^QWvP zk!d87QPxOBFM^Pd@c)u*=w%!orE|Ho2uOv%%3R~tbc3%4GpAS6ODSaRY#h#5Um9Ov z$TtH}C}Cli02CTMqjJ-z3Kk(bd?gqvMoY^8LPa77txPU1*3h#!UxF`*FkRPKiyR4v zs=eb>#F?;&hKu%lTy)|ak<*QYKio{zZh8;B@#yFv&dv^WF*|)gIXZHb=xmA9?})-+ zOo|}18gEnqgYl`zs?bK6g)Z0;vWUH_V%}FREmjU+7O+YVl(UQ0h2kr%IyCpMGp`P` zMeRvl-DBa%a{I7k%KYEhuA&1zMG0kR4waXx2n!u9T_*?sXDVCVQ)?a-bo9vcb@oU6 zsCeh6UoT#~Z}SaePfP!i&oAse)O7lwj+8YrT4&+cH(qmQ`18r`B%a`$sV)B7fwIQI z;`(B7m#R@0{O!_6@~Kq?-goPD<|R*}8A(6$sunp-Q4(z|U#4bawGIL9U-FkX94#3f zY`fXe{w&{PboqFKXsy@DP3<Rh%@h{WzVqyLx$3+w%8zw62$IDjs)h!SnLQ zwg1wY6$jpLYUaZpd4uhOLCZRf_?1^z_5<`SVXcTrRADeSiwut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