Skip to content

Commit

Permalink
Linebreaks are correctly displayed in code field (#1112)
Browse files Browse the repository at this point in the history
  • Loading branch information
DocMoebiuz authored Feb 3, 2023
1 parent 33e299a commit 59229bc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
16 changes: 16 additions & 0 deletions Base/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
15 changes: 8 additions & 7 deletions UI/Panels/Config/HubHopPresetPanel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MobiFlight.HubHop;
using MobiFlight.Base;
using MobiFlight.HubHop;
using MobiFlight.InputConfig;
using MobiFlight.OutputConfig;
using MobiFlight.UI.Forms;
Expand Down Expand Up @@ -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;
Expand All @@ -253,7 +254,7 @@ internal InputConfig.MSFS2020CustomInputAction ToMsfsConfig()
new InputConfig.MSFS2020CustomInputAction()
{
PresetId = selectedPreset?.id,
Command = SimVarNameTextBox.Text
Command = SimVarNameTextBox.Text.ToLF()
};
return result;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -377,7 +378,7 @@ internal void syncFromConfig(MSFS2020EventIdInputAction inputAction)
}

// Restore the code
SimVarNameTextBox.Text = OriginalCode;
SimVarNameTextBox.Text = OriginalCode.ToCRLF();
TryToSelectOriginalPresetFromCode(OriginalCode);
}

Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 59229bc

Please sign in to comment.