Skip to content

Commit

Permalink
fixed UI sync issue with SentenceSilence
Browse files Browse the repository at this point in the history
  • Loading branch information
jame25 authored Nov 6, 2024
1 parent 8f1efa3 commit 1faba2b
Showing 1 changed file with 74 additions and 7 deletions.
81 changes: 74 additions & 7 deletions Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private void InitializeComponent()
sentenceSilenceNumeric = new NumericUpDown();
sentenceSilenceNumeric.Location = new System.Drawing.Point(10, 173);
sentenceSilenceNumeric.Width = 60;
sentenceSilenceNumeric.DecimalPlaces = 2;
sentenceSilenceNumeric.DecimalPlaces = 1;
sentenceSilenceNumeric.Increment = 0.1m;
sentenceSilenceNumeric.Minimum = 0.0m;
sentenceSilenceNumeric.Maximum = 2.0m;
Expand Down Expand Up @@ -473,6 +473,67 @@ private int GetSelectedSpeakerId()
return 0;
}

private string GetConfigPath()
{
return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "settings.conf");
}

private Dictionary<string, string> ReadCurrentSettings()
{
var settings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
string configPath = GetConfigPath();
if (File.Exists(configPath))
{
try
{
foreach (var line in File.ReadAllLines(configPath))
{
var parts = line.Split(new[] { '=' }, 2);
if (parts.Length == 2)
{
string key = parts[0].Trim();
string value = parts[1].Trim();
if (!settings.ContainsKey(key))
{
settings.Add(key, value);
}
else
{
Log($"[ReadCurrentSettings] Duplicate key '{key}' found in settings.conf. Ignoring duplicate.");
}
}
else
{
Log($"[ReadCurrentSettings] Invalid line format: '{line}'. Skipping.");
}
}
}
catch (Exception ex)
{
Log($"[ReadCurrentSettings] Exception while reading settings: {ex.Message}");
}
}
else
{
Log("[ReadCurrentSettings] settings.conf not found. Returning empty settings.");
}
return settings;
}

private void InitializeSentenceSilence()
{
var settings = ReadCurrentSettings();
if (settings.TryGetValue("SentenceSilence", out string silenceValue))
{
if (float.TryParse(silenceValue, System.Globalization.NumberStyles.Float,
System.Globalization.CultureInfo.InvariantCulture, out float value))
{
sentenceSilenceNumeric.Value = (decimal)value;
Log($"[InitializeSentenceSilence] Set value to: {value}");
}
}
}

private void SentenceSilenceNumeric_ValueChanged(object sender, EventArgs e)
{
float newSilence = (float)sentenceSilenceNumeric.Value;
Expand Down Expand Up @@ -697,6 +758,8 @@ private void LoadSettingsIntoUI()
StringComparer.OrdinalIgnoreCase
);

InitializeSentenceSilence();

if (settings.TryGetValue("Logging", out string logging))
{
loggingCheckBox.Checked = bool.Parse(logging);
Expand Down Expand Up @@ -1036,11 +1099,6 @@ private int GetSpeedIndex(string value)
return 0; // Default to first index if parsing fails
}

private string GetConfigPath()
{
return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "settings.conf");
}

private void SaveSettings(
uint monitoringModifiers,
uint monitoringVk,
Expand Down Expand Up @@ -1074,7 +1132,7 @@ private void SaveSettings(
UpdateOrAddSetting(lines, "SpeedIncreaseKey", $"0x{speedIncreaseVk:X2}");
UpdateOrAddSetting(lines, "SpeedDecreaseModifier", $"0x{speedDecreaseModifiers:X2}");
UpdateOrAddSetting(lines, "SpeedDecreaseKey", $"0x{speedDecreaseVk:X2}");
UpdateOrAddSetting(lines, "SentenceSilence", sentenceSilence.ToString("F2", System.Globalization.CultureInfo.InvariantCulture));
UpdateOrAddSetting(lines, "SentenceSilence", sentenceSilence.ToString("F1", System.Globalization.CultureInfo.InvariantCulture));

File.WriteAllLines(configPath, lines);
Log($"Settings saved successfully.");
Expand Down Expand Up @@ -1316,6 +1374,15 @@ public void UpdateVoiceModels(List<string> newModels)
UpdateVoiceModelComboBox();
}

public void UpdateSentenceSilence(float value)
{
if (sentenceSilenceNumeric != null)
{
sentenceSilenceNumeric.Value = (decimal)value;
Log($"[UpdateSentenceSilence] Updated sentence silence value to: {value}");
}
}

private void UpdateVoiceModelComboBox()
{
Log($"[UpdateVoiceModelComboBox] Updating combobox with {voiceModels.Count} models");
Expand Down

0 comments on commit 1faba2b

Please sign in to comment.