From 59229bcb13323dc835839b5e455923e5c2e4603e Mon Sep 17 00:00:00 2001 From: Sebastian M Date: Fri, 3 Feb 2023 16:40:03 +0100 Subject: [PATCH] Linebreaks are correctly displayed in code field (#1112) --- Base/Extensions.cs | 16 ++++++++++++++++ UI/Panels/Config/HubHopPresetPanel.cs | 15 ++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Base/Extensions.cs b/Base/Extensions.cs index 7414a4661..5035fc365 100644 --- a/Base/Extensions.cs +++ b/Base/Extensions.cs @@ -13,5 +13,21 @@ public static string Truncate(this string value, int maxLength) if (string.IsNullOrEmpty(value)) return value; return value.Length <= maxLength ? value : value.Substring(0, maxLength); } + + public static string ToCRLF(this string value) + { + if (string.IsNullOrEmpty(value)) return value; + // handle mixed line endings correctly (avoid expansion of \n in \r\n -> \r\n\n) + // first, convert all line-endings to only LF + // then, convert all line-endings to CRLF + return value.Replace("\r\n", "\n").Replace("\n", "\r\n"); + } + + public static string ToLF(this string value) + { + if (string.IsNullOrEmpty(value)) return value; + // mixed lines are treated correctly + return value.Replace("\r\n", "\n"); + } } } diff --git a/UI/Panels/Config/HubHopPresetPanel.cs b/UI/Panels/Config/HubHopPresetPanel.cs index dcafe33b7..c43b3bea7 100644 --- a/UI/Panels/Config/HubHopPresetPanel.cs +++ b/UI/Panels/Config/HubHopPresetPanel.cs @@ -1,4 +1,5 @@ -using MobiFlight.HubHop; +using MobiFlight.Base; +using MobiFlight.HubHop; using MobiFlight.InputConfig; using MobiFlight.OutputConfig; using MobiFlight.UI.Forms; @@ -230,7 +231,7 @@ internal void syncToConfig(OutputConfigItem config) Msfs2020HubhopPreset selectedPreset = (PresetComboBox.Items[PresetComboBox.SelectedIndex] as Msfs2020HubhopPreset); config.SimConnectValue.UUID = selectedPreset?.id; - config.SimConnectValue.Value = SimVarNameTextBox.Text; + config.SimConnectValue.Value = SimVarNameTextBox.Text.ToLF(); } else if (FlightSimType == FlightSimType.XPLANE) { config.XplaneDataRef.Path = SimVarNameTextBox.Text; @@ -253,7 +254,7 @@ internal InputConfig.MSFS2020CustomInputAction ToMsfsConfig() new InputConfig.MSFS2020CustomInputAction() { PresetId = selectedPreset?.id, - Command = SimVarNameTextBox.Text + Command = SimVarNameTextBox.Text.ToLF() }; return result; } @@ -301,7 +302,7 @@ internal void syncFromConfigMSFS(OutputConfigItem config) // Restore the code if (config.SimConnectValue.Value != "") { SimVarNameTextBox.TextChanged -= SimVarNameTextBox_TextChanged; - SimVarNameTextBox.Text = config.SimConnectValue.Value; + SimVarNameTextBox.Text = config.SimConnectValue.Value.ToCRLF(); SimVarNameTextBox.TextChanged += SimVarNameTextBox_TextChanged; } @@ -334,7 +335,7 @@ internal void syncFromConfig(InputConfig.MSFS2020CustomInputAction inputAction) if (inputAction == null || inputAction.Command == "") return; // Restore the code - SimVarNameTextBox.Text = inputAction.Command; + SimVarNameTextBox.Text = inputAction.Command.ToCRLF(); if (inputAction.PresetId != null) { @@ -377,7 +378,7 @@ internal void syncFromConfig(MSFS2020EventIdInputAction inputAction) } // Restore the code - SimVarNameTextBox.Text = OriginalCode; + SimVarNameTextBox.Text = OriginalCode.ToCRLF(); TryToSelectOriginalPresetFromCode(OriginalCode); } @@ -435,7 +436,7 @@ private void PresetComboBox_SelectedIndexChanged(object sender, EventArgs e) Msfs2020HubhopPreset selectedPreset = FilteredPresetList.Items.Find(x => x.id == selectedItem.id); if (selectedPreset == null) return; DescriptionLabel.Text = selectedPreset?.description; - SimVarNameTextBox.Text = selectedPreset?.code; + SimVarNameTextBox.Text = selectedPreset?.code?.ToCRLF(); if (FlightSimType==FlightSimType.XPLANE) {