Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RegPreview] Various improvements on how files are saved #37628

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
code cleanup
  • Loading branch information
htcfreek committed Feb 25, 2025
commit 402064b1d3841434ea627cdc56939a23a288992f
Original file line number Diff line number Diff line change
@@ -110,8 +110,8 @@ private async void OpenButton_Click(object sender, RoutedEventArgs e)
{
case ContentDialogResult.Primary:
// Save, then continue the file open
bool success = SaveFile();
if (!success)
if (!AskFileName(false) ||
!SaveFile())
{
return;
}
@@ -167,23 +167,9 @@ private async void OpenButton_Click(object sender, RoutedEventArgs e)
/// </summary>
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(_appFileName))
if (!AskFileName(false))
{
// Save out a new REG file and then open it - we have to use the direct Win32 method because FileOpenPicker crashes when it's
// called while running as admin
IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(_mainWindow);
string filename = SaveFilePicker.ShowDialog(
windowHandle,
resourceLoader.GetString("SuggestFileName"),
resourceLoader.GetString("FilterRegistryName") + '\0' + "*.reg" + '\0' + resourceLoader.GetString("FilterAllFiles") + '\0' + "*.*" + '\0' + '\0',
resourceLoader.GetString("SaveDialogTitle"));

if (filename == string.Empty)
{
return;
}

_appFileName = filename;
return;
}

// save and update window title
@@ -196,21 +182,11 @@ private void SaveButton_Click(object sender, RoutedEventArgs e)
/// </summary>
private async void SaveAsButton_Click(object sender, RoutedEventArgs e)
{
// Save out a new REG file and then open it - we have to use the direct Win32 method because FileOpenPicker crashes when it's
// called while running as admin
IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(_mainWindow);
string filename = SaveFilePicker.ShowDialog(
windowHandle,
resourceLoader.GetString("SuggestFileName"),
resourceLoader.GetString("FilterRegistryName") + '\0' + "*.reg" + '\0' + resourceLoader.GetString("FilterAllFiles") + '\0' + "*.*" + '\0' + '\0',
resourceLoader.GetString("SaveDialogTitle"));

if (filename == string.Empty)
if (!AskFileName(true))
{
return;
}

_appFileName = filename;
_ = SaveFile();
UpdateToolBarAndUI(await OpenRegistryFile(_appFileName));
}
@@ -293,8 +269,8 @@ private async void WriteButton_Click(object sender, RoutedEventArgs e)
{
case ContentDialogResult.Primary:
// Save, then continue the file open
bool success = SaveFile();
if (!success)
if (!AskFileName(false) ||
!SaveFile())
{
return;
}
Original file line number Diff line number Diff line change
@@ -834,9 +834,9 @@ private async void HandleDirtyClosing(string title, string content, string prima
{
case ContentDialogResult.Primary:
// Save, then close
if (!DirtyCloseSaveFile())
if (!AskFileName(false) ||
!SaveFile())
{
// save cancelled
return;
}

@@ -855,31 +855,6 @@ private async void HandleDirtyClosing(string title, string content, string prima
Application.Current.Exit();
}

private bool DirtyCloseSaveFile()
{
if (string.IsNullOrEmpty(_appFileName))
{
// Save out a new REG file and then open it - we have to use the direct Win32 method because FileOpenPicker crashes when it's
// called while running as admin
IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(_mainWindow);
string filename = SaveFilePicker.ShowDialog(
windowHandle,
resourceLoader.GetString("SuggestFileName"),
resourceLoader.GetString("FilterRegistryName") + '\0' + "*.reg" + '\0' + resourceLoader.GetString("FilterAllFiles") + '\0' + "*.*" + '\0' + '\0',
resourceLoader.GetString("SaveDialogTitle"));

if (filename == string.Empty)
{
return false;
}

_appFileName = filename;
}

bool r = SaveFile();
return r;
}

/// <summary>
/// Method will open the Registry Editor or merge the current REG file into the Registry via the Editor
/// Process will prompt for elevation if it needs it.
@@ -956,6 +931,35 @@ public void UpdateUnsavedFileIndicator(bool show)
}
}

/// <summary>
/// Ask the user for the file path if it is unknown because of an unsaved file
/// </summary>
/// <param name="askAlways">Ask regardless of the known file name in case of save as action.</param>
/// <returns>Returns true if user selected a path, otherwise false</returns>
public bool AskFileName(bool askAlways)
{
if (string.IsNullOrEmpty(_appFileName) || askAlways )
{
// Save out a new REG file and then open it - we have to use the direct Win32 method because FileOpenPicker crashes when it's
// called while running as admin
IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(_mainWindow);
string filename = SaveFilePicker.ShowDialog(
windowHandle,
resourceLoader.GetString("SuggestFileName"),
resourceLoader.GetString("FilterRegistryName") + '\0' + "*.reg" + '\0' + resourceLoader.GetString("FilterAllFiles") + '\0' + "*.*" + '\0' + '\0',
resourceLoader.GetString("SaveDialogTitle"));

if (filename == string.Empty)
{
return false;
}

_appFileName = filename;
}

return true;
}

/// <summary>
/// Wrapper method that saves the current file in place, using the current text in editor.
/// </summary>