Streamline output path textbox handling

Addresses issue of cursor moving during typing
Addresses issue of extraneous spaces appearing
This commit is contained in:
Matt Nadareski
2021-04-12 13:25:12 -07:00
parent 7e73f69433
commit 4b7d4e05e7
2 changed files with 50 additions and 14 deletions

View File

@@ -246,7 +246,6 @@ namespace MPF.Data
// Cache if we had a directory separator or not
bool endedWithDirectorySeparator = directory.EndsWith(Path.DirectorySeparatorChar.ToString())
|| directory.EndsWith(Path.AltDirectorySeparatorChar.ToString());
bool endedWithSpace = directory.EndsWith(" ");
// Combine the path to make things separate easier
string combinedPath = Path.Combine(directory, filename);
@@ -271,10 +270,6 @@ namespace MPF.Data
if (replacePeriods)
filename = Path.GetFileNameWithoutExtension(filename).Replace('.', '_') + "." + Path.GetExtension(filename).TrimStart('.');
// If we had a space at the end before, add it again
if (endedWithSpace)
directory += " ";
// If we had a directory separator at the end before, add it again
if (endedWithDirectorySeparator)
directory += Path.DirectorySeparatorChar;

View File

@@ -208,10 +208,9 @@ namespace MPF.Windows
{
SystemTypeComboBox.SelectionChanged += SystemTypeComboBoxSelectionChanged;
MediaTypeComboBox.SelectionChanged += MediaTypeComboBoxSelectionChanged;
OutputFilenameTextBox.TextChanged += OutputFilenameTextBoxTextChanged;
OutputDirectoryTextBox.TextChanged += OutputDirectoryTextBoxTextChanged;
DriveLetterComboBox.SelectionChanged += DriveLetterComboBoxSelectionChanged;
DriveSpeedComboBox.SelectionChanged += DriveSpeedComboBoxSelectionChanged;
AddPathEventHandlers();
}
/// <summary>
@@ -221,10 +220,27 @@ namespace MPF.Windows
{
SystemTypeComboBox.SelectionChanged -= SystemTypeComboBoxSelectionChanged;
MediaTypeComboBox.SelectionChanged -= MediaTypeComboBoxSelectionChanged;
OutputFilenameTextBox.TextChanged -= OutputFilenameTextBoxTextChanged;
OutputDirectoryTextBox.TextChanged -= OutputDirectoryTextBoxTextChanged;
DriveLetterComboBox.SelectionChanged -= DriveLetterComboBoxSelectionChanged;
DriveSpeedComboBox.SelectionChanged -= DriveSpeedComboBoxSelectionChanged;
RemovePathEventHandlers();
}
/// <summary>
/// Add path textbox event handlers
/// </summary>
private void AddPathEventHandlers()
{
OutputFilenameTextBox.TextChanged += OutputFilenameTextBoxTextChanged;
OutputDirectoryTextBox.TextChanged += OutputDirectoryTextBoxTextChanged;
}
/// <summary>
/// Remove path textbox event handlers
/// </summary>
private void RemovePathEventHandlers()
{
OutputFilenameTextBox.TextChanged -= OutputFilenameTextBoxTextChanged;
OutputDirectoryTextBox.TextChanged -= OutputDirectoryTextBoxTextChanged;
}
/// <summary>
@@ -352,14 +368,17 @@ namespace MPF.Windows
ParametersTextBox.Text);
// Disable automatic reprocessing of the textboxes until we're done
OutputDirectoryTextBox.TextChanged -= OutputDirectoryTextBoxTextChanged;
OutputFilenameTextBox.TextChanged -= OutputFilenameTextBoxTextChanged;
RemovePathEventHandlers();
OutputDirectoryTextBox.Text = env.OutputDirectory;
OutputFilenameTextBox.Text = env.OutputFilename;
OutputDirectoryTextBox.SelectionStart = OutputDirectoryTextBox.Text.Length;
OutputDirectoryTextBox.SelectionLength = 0;
OutputDirectoryTextBox.TextChanged += OutputDirectoryTextBoxTextChanged;
OutputFilenameTextBox.TextChanged += OutputFilenameTextBoxTextChanged;
OutputFilenameTextBox.Text = env.OutputFilename;
OutputFilenameTextBox.SelectionStart = OutputFilenameTextBox.Text.Length;
OutputFilenameTextBox.SelectionLength = 0;
AddPathEventHandlers();
return env;
}
@@ -426,6 +445,9 @@ namespace MPF.Windows
// Get the extension for the file for the next two statements
string extension = Env.Parameters?.GetDefaultExtension(mediaType);
// Disable automatic reprocessing of the textboxes until we're done
RemovePathEventHandlers();
// Set the output filename, if we changed drives or it's not already
if (driveChanged || string.IsNullOrEmpty(OutputFilenameTextBox.Text))
OutputFilenameTextBox.Text = (drive?.VolumeLabel ?? systemType.LongName()) + (extension ?? ".bin");
@@ -437,6 +459,14 @@ namespace MPF.Windows
// Set the output directory, if we changed drives or it's not already
if (driveChanged || string.IsNullOrEmpty(OutputDirectoryTextBox.Text))
OutputDirectoryTextBox.Text = Path.Combine(Options.DefaultOutputPath, Path.GetFileNameWithoutExtension(OutputFilenameTextBox.Text) ?? string.Empty);
OutputDirectoryTextBox.SelectionStart = OutputDirectoryTextBox.Text.Length;
OutputDirectoryTextBox.SelectionLength = 0;
OutputFilenameTextBox.SelectionStart = OutputFilenameTextBox.Text.Length;
OutputFilenameTextBox.SelectionLength = 0;
AddPathEventHandlers();
}
/// <summary>
@@ -476,6 +506,9 @@ namespace MPF.Windows
else
Env.Parameters.Speed = DriveSpeedComboBox.SelectedValue as int?;
// Disable automatic reprocessing of the textboxes until we're done
RemovePathEventHandlers();
string trimmedPath = Env.Parameters.OutputPath?.Trim('"') ?? string.Empty;
string outputDirectory = Path.GetDirectoryName(trimmedPath);
string outputFilename = Path.GetFileName(trimmedPath);
@@ -489,6 +522,14 @@ namespace MPF.Windows
else
outputFilename = OutputFilenameTextBox.Text;
OutputDirectoryTextBox.SelectionStart = OutputDirectoryTextBox.Text.Length;
OutputDirectoryTextBox.SelectionLength = 0;
OutputFilenameTextBox.SelectionStart = OutputFilenameTextBox.Text.Length;
OutputFilenameTextBox.SelectionLength = 0;
AddPathEventHandlers();
MediaType? mediaType = Env.Parameters.GetMediaType();
int mediaTypeIndex = MediaTypes.FindIndex(m => m == mediaType);
if (mediaTypeIndex > -1)