Added the generator mode setting

This commit is contained in:
Thorsten Sommer 2022-10-03 18:13:07 +02:00
parent 433a4e33a1
commit d242d02ece
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
7 changed files with 146 additions and 1 deletions

View File

@ -0,0 +1,7 @@
namespace DataModel.Database;
public enum SettingGeneratorMode
{
AUTOMATIC,
MANUAL,
}

View File

@ -2,9 +2,10 @@
public static class SettingNames
{
public static readonly string DEEPL_SOURCE_CULTURE = "DeepL Source Culture";
public static readonly string CULTURE = "Culture";
public static readonly string DEEPL_ACTION = "DeepL Action";
public static readonly string DEEPL_API_KEY = "DeepL API Key";
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";
}

View File

@ -460,4 +460,82 @@ public static class AppSettings
#endregion
#endregion
#region Generator Settings
#region Generator Mode
private static SettingGeneratorMode CACHE_GENERATOR_MODE = SettingGeneratorMode.MANUAL;
private static bool CACHE_GENERATOR_MODE_IS_LOADED = false;
public static async Task<SettingGeneratorMode> GetGeneratorMode()
{
// When possible, use the cache:
if (CACHE_GENERATOR_MODE_IS_LOADED)
return CACHE_GENERATOR_MODE;
var generatorMode = SettingGeneratorMode.MANUAL;
try
{
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Check, if the setting is already set:
if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.GENERATOR_MODE) is { } existingSetting)
{
generatorMode = (SettingGeneratorMode) existingSetting.IntegerValue;
return generatorMode;
}
// Does not exist, so create it:
var setting = new Setting
{
Code = SettingNames.GENERATOR_MODE,
IntegerValue = (int) generatorMode,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
return generatorMode;
}
finally
{
CACHE_GENERATOR_MODE_IS_LOADED = true;
CACHE_GENERATOR_MODE = generatorMode;
}
}
public static async Task SetGeneratorMode(SettingGeneratorMode mode)
{
// Update the cache:
CACHE_GENERATOR_MODE = mode;
CACHE_GENERATOR_MODE_IS_LOADED = true;
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Check, if the setting is already set:
if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.GENERATOR_MODE) is { } existingSetting)
{
existingSetting.IntegerValue = (int) mode;
await db.SaveChangesAsync();
}
// Does not exist, so create it:
else
{
var setting = new Setting
{
Code = SettingNames.GENERATOR_MODE,
IntegerValue = (int) mode,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
}
}
#endregion
#endregion
}

View File

@ -388,9 +388,55 @@ public sealed partial class Setting : UserControl
cultureIndices.Remove(innerLoopIndex);
}
}
private static async Task<Setting> ShowGeneratorModeSettingAsync()
{
var currentSetting = await AppSettings.GetGeneratorMode();
var settingData = new SettingUIData(
Icon: Icons.icons8_code_512,
SettingName: () => "Generator Mode",
ChangeNeedsRestart: false,
SettingExplanation: () => "The generator mode determines how the translation files are generated.",
SettingExplanationLink: () => (string.Empty, string.Empty),
SetupDataControl: (changeTrigger) =>
{
// We set up a combo box with the available actions:
var dropdown = new ComboBox();
dropdown.Items.Add("Automatic generation");
dropdown.Items.Add("Manual generation");
dropdown.SelectedIndex = currentSetting switch
{
SettingGeneratorMode.AUTOMATIC => 0,
SettingGeneratorMode.MANUAL => 1,
_ => 0,
};
// Setup the change event handler:
dropdown.SelectedValueChanged += (sender, args) => changeTrigger();
dropdown.SelectedValueChanged += async (sender, args) => await AppSettings.SetGeneratorMode(dropdown.SelectedIndex switch
{
0 => SettingGeneratorMode.AUTOMATIC,
1 => SettingGeneratorMode.MANUAL,
_ => SettingGeneratorMode.AUTOMATIC,
});
// Apply the desired layout:
dropdown.Dock = DockStyle.Fill;
dropdown.DropDownStyle = ComboBoxStyle.DropDownList;
return dropdown;
}
);
return new Setting(settingData);
}
public static IEnumerable<Task<Setting>> GetAllSettings()
{
yield return ShowGeneratorModeSettingAsync();
yield return ShowDeepLSourceCultureSettingAsync();
foreach (var setting in ShowCultureSettingsAsync())
{

View File

@ -140,6 +140,16 @@ namespace UI_WinForms.Resources {
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap icons8_code_512 {
get {
object obj = ResourceManager.GetObject("icons8_code_512", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>

View File

@ -142,6 +142,9 @@
<data name="icons8_chat_bubble_512" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>icons8-chat-bubble-512.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="icons8_code_512" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>icons8-code-512.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="icons8_collectibles_512" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>icons8-collectibles-512.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB