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
Next Next commit
unsaved indicator
  • Loading branch information
htcfreek committed Feb 25, 2025
commit 70f7c2eec91111cabf3ce1f22d0ff55bc129dc14
Original file line number Diff line number Diff line change
@@ -112,6 +112,7 @@ private async void OpenButton_Click(object sender, RoutedEventArgs e)
case ContentDialogResult.Secondary:
// Don't save and continue the file open!
saveButton.IsEnabled = false;
UpdateUnsavedFileIndicator(false);
break;
default:
// Don't open the new file!
@@ -143,6 +144,7 @@ private async void OpenButton_Click(object sender, RoutedEventArgs e)

// disable the Save button as it's a new file
saveButton.IsEnabled = false;
UpdateUnsavedFileIndicator(false);

// Restore the event handler as we're loaded
MonacoEditor.TextChanged += MonacoEditor_TextChanged;
@@ -193,6 +195,7 @@ private async void RefreshButton_Click(object sender, RoutedEventArgs e)
UpdateToolBarAndUI(await OpenRegistryFile(_appFileName), true, true);

saveButton.IsEnabled = false;
UpdateUnsavedFileIndicator(false);

// restore the TextChanged handler
MonacoEditor.TextChanged += MonacoEditor_TextChanged;
@@ -355,6 +358,7 @@ private void MonacoEditor_TextChanged(object sender, EventArgs e)
{
RefreshRegistryFile();
saveButton.IsEnabled = true;
UpdateUnsavedFileIndicator(true);
});
}
}
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.UI.Input;
@@ -22,6 +23,9 @@ namespace RegistryPreviewUILib
{
public sealed partial class RegistryPreviewMainPage : Page
{
private static readonly string _usavedFileIndicator = "* ";
private static readonly char[] _usavedFileIndicatorChars = [' ', '*'];

private static SemaphoreSlim _dialogSemaphore = new(1);
private string lastKeyPath;

@@ -834,6 +838,7 @@ private async void HandleDirtyClosing(string title, string content, string prima
break;
case ContentDialogResult.Secondary:
// Don't save, and then close!
UpdateUnsavedFileIndicator(false);
saveButton.IsEnabled = false;
break;
default:
@@ -902,6 +907,25 @@ public void ChangeCursor(UIElement uiElement, bool wait)
type.InvokeMember("ProtectedCursor", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.Instance, null, uiElement, new object[] { cursor }, CultureInfo.InvariantCulture);
}

public void UpdateUnsavedFileIndicator(bool show)
{
// get and cut current title
string currentTitel = Regex.Replace(_mainWindow.Title, APPNAME + @"$|\s-\s" + APPNAME + @"$", string.Empty);

// verify
bool titleContiansIndicator = currentTitel.StartsWith(_usavedFileIndicator, StringComparison.CurrentCultureIgnoreCase);

// update
if (!titleContiansIndicator && show)
{
_updateWindowTitleFunction(_usavedFileIndicator + currentTitel);
}
else if (titleContiansIndicator && !show)
{
_updateWindowTitleFunction(currentTitel.TrimStart(_usavedFileIndicatorChars));
}
}

/// <summary>
/// Wrapper method that saves the current file in place, using the current text in editor.
/// </summary>
@@ -930,6 +954,7 @@ private void SaveFile()
streamWriter.Close();

// only change when the save is successful
UpdateUnsavedFileIndicator(false);
saveButton.IsEnabled = false;
}
catch (UnauthorizedAccessException ex)