Skip to content

Commit

Permalink
Add FileNamePlaceholder
Browse files Browse the repository at this point in the history
Add FileNamePlaceholder
  • Loading branch information
0x7c13 committed Sep 7, 2019
1 parent bb50c99 commit e98df51
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/Notepads/App.xaml.cs
Expand Up @@ -169,6 +169,7 @@ private async System.Threading.Tasks.Task ActivateAsync(IActivatedEventArgs e)
};

LoggingService.LogInfo($"AppLaunchSettings: {string.Join(";", appLaunchSettings.Select(x => x.Key + "=" + x.Value).ToArray())}");
Analytics.TrackEvent("AppLaunch_Settings", appLaunchSettings);

await ActivationService.ActivateAsync(rootFrame, e);

Expand Down
2 changes: 2 additions & 0 deletions src/Notepads/Controls/TextEditor/ITextEditor.cs
Expand Up @@ -30,6 +30,8 @@ public interface ITextEditor

Encoding RequestedEncoding { get; }

string FileNamePlaceholder { get; set; }

string EditingFileName { get; }

string EditingFilePath { get; }
Expand Down
3 changes: 3 additions & 0 deletions src/Notepads/Controls/TextEditor/TextEditor.xaml.cs
Expand Up @@ -60,6 +60,8 @@ public sealed partial class TextEditor : UserControl, ITextEditor

public event EventHandler FileReloaded;

public string FileNamePlaceholder { get; set; } = string.Empty;

public FileType FileType { get; private set; }

public TextFile LastSavedSnapshot { get; private set; }
Expand Down Expand Up @@ -198,6 +200,7 @@ public TextEditorStateMetaData GetTextEditorStateMetaData()

var metaData = new TextEditorStateMetaData
{
FileNamePlaceholder = FileNamePlaceholder,
LastSavedEncoding = EncodingUtility.GetEncodingName(LastSavedSnapshot.Encoding),
LastSavedLineEnding = LineEndingUtility.GetLineEndingName(LastSavedSnapshot.LineEnding),
DateModifiedFileTime = LastSavedSnapshot.DateModifiedFileTime,
Expand Down
2 changes: 2 additions & 0 deletions src/Notepads/Controls/TextEditor/TextEditorStateMetaData.cs
Expand Up @@ -2,6 +2,8 @@
{
public class TextEditorStateMetaData
{
public string FileNamePlaceholder { get; set; }

public string LastSavedEncoding { get; set; }

public string LastSavedLineEnding { get; set; }
Expand Down
5 changes: 3 additions & 2 deletions src/Notepads/Core/INotepadsCore.cs
Expand Up @@ -46,9 +46,10 @@ public interface INotepadsCore
Guid id,
TextFile textFile,
StorageFile editingFile,
bool isModified);
string fileNamePlaceHolder,
bool isModified = false);

void OpenNewTextEditor();
void OpenNewTextEditor(string fileNamePlaceholder);

void OpenTextEditor(ITextEditor editor, int atIndex = -1);

Expand Down
27 changes: 15 additions & 12 deletions src/Notepads/Core/NotepadsCore.cs
Expand Up @@ -27,8 +27,6 @@ public class NotepadsCore : INotepadsCore
{
public SetsView Sets;

public readonly string DefaultNewFileName;

public event EventHandler<ITextEditor> TextEditorLoaded;

public event EventHandler<ITextEditor> TextEditorUnloaded;
Expand Down Expand Up @@ -70,7 +68,6 @@ public class NotepadsCore : INotepadsCore
private const string NotepadsTextEditorEditingFilePath = "NotepadsTextEditorEditingFilePath";

public NotepadsCore(SetsView sets,
string defaultNewFileName,
INotepadsExtensionProvider extensionProvider)
{
Sets = sets;
Expand All @@ -85,16 +82,19 @@ public class NotepadsCore : INotepadsCore
Sets.DragItemsCompleted += Sets_DragItemsCompleted;

_extensionProvider = extensionProvider;
DefaultNewFileName = defaultNewFileName;
ThemeSettingsService.OnAccentColorChanged += OnAppAccentColorChanged;
}

public void OpenNewTextEditor()
public void OpenNewTextEditor(string fileNamePlaceholder)
{
var textFile = new TextFile(string.Empty,
EditorSettingsService.EditorDefaultEncoding,
EditorSettingsService.EditorDefaultLineEnding);
var newEditor = CreateTextEditor(Guid.NewGuid(), textFile, null);
var newEditor = CreateTextEditor(
Guid.NewGuid(),
textFile,
null,
fileNamePlaceholder);
OpenTextEditor(newEditor);
}

Expand Down Expand Up @@ -160,18 +160,21 @@ public void OpenTextEditors(ITextEditor[] editors, Guid? selectedEditorId = null
bool ignoreFileSizeLimit = false)
{
var textFile = await FileSystemUtility.ReadFile(file, ignoreFileSizeLimit);
return CreateTextEditor(id, textFile, file);
return CreateTextEditor(id, textFile, file, file.Name);
}

public ITextEditor CreateTextEditor(Guid id,
public ITextEditor CreateTextEditor(
Guid id,
TextFile textFile,
StorageFile editingFile,
string fileNamePlaceholder,
bool isModified = false)
{
TextEditor textEditor = new TextEditor
{
Id = id,
ExtensionProvider = _extensionProvider
ExtensionProvider = _extensionProvider,
FileNamePlaceholder = fileNamePlaceholder
};

textEditor.Init(textFile, editingFile, isModified: isModified);
Expand Down Expand Up @@ -216,7 +219,7 @@ public int GetNumberOfOpenedTextEditors()

public bool TryGetSharingContent(ITextEditor textEditor, out string title, out string content)
{
title = textEditor.EditingFileName ?? DefaultNewFileName;
title = textEditor.EditingFileName ?? textEditor.FileNamePlaceholder;
content = textEditor.GetContentForSharing();
return !string.IsNullOrEmpty(content);
}
Expand Down Expand Up @@ -348,7 +351,7 @@ private SetsViewItem CreateTextEditorSetsViewItem(ITextEditor textEditor)
{
var textEditorSetsViewItem = new SetsViewItem
{
Header = textEditor.EditingFileName ?? DefaultNewFileName,
Header = textEditor.EditingFileName ?? textEditor.FileNamePlaceholder,
Content = textEditor,
SelectionIndicatorForeground =
Application.Current.Resources["SystemControlForegroundAccentBrush"] as SolidColorBrush,
Expand Down Expand Up @@ -696,7 +699,7 @@ private async void Sets_Drop(object sender, DragEventArgs args)
LineEndingUtility.GetLineEndingByName(metaData.LastSavedLineEnding),
metaData.DateModifiedFileTime);

var newEditor = CreateTextEditor(Guid.NewGuid(), textFile, editingFile, metaData.IsModified);
var newEditor = CreateTextEditor(Guid.NewGuid(), textFile, editingFile, metaData.FileNamePlaceholder, metaData.IsModified);
OpenTextEditor(newEditor, atIndex);
newEditor.ResetEditorState(metaData, pendingText);

Expand Down
13 changes: 12 additions & 1 deletion src/Notepads/Core/SessionManager.cs
Expand Up @@ -8,6 +8,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AppCenter.Analytics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Notepads.Controls.TextEditor;
Expand Down Expand Up @@ -82,6 +83,7 @@ public async Task<int> LoadLastSessionAsync()
catch (Exception ex)
{
LoggingService.LogError($"[SessionManager] Failed to load last session metadata: {ex.Message}");
Analytics.TrackEvent("SessionManager_FailedToLoadLastSession", new Dictionary<string, string>() {{ "Exception", ex.Message }});
await ClearSessionDataAsync();
return 0;
}
Expand All @@ -99,6 +101,7 @@ public async Task<int> LoadLastSessionAsync()
catch (Exception ex)
{
LoggingService.LogError($"[SessionManager] Failed to recover TextEditor: {ex.Message}");
Analytics.TrackEvent("SessionManager_FailedToRecoverTextEditor", new Dictionary<string, string>() {{ "Exception", ex.Message }});
continue;
}

Expand Down Expand Up @@ -192,6 +195,7 @@ public async Task SaveSessionAsync(Action actionAfterSaving = null)
catch (Exception ex)
{
LoggingService.LogError($"[SessionManager] Failed to save session metadata: {ex.Message}");
Analytics.TrackEvent("SessionManager_FailedToSaveSessionMetaData", new Dictionary<string, string>() {{ "Exception", ex.Message }});
actionAfterSaving?.Invoke();
_semaphoreSlim.Release();
return; // Failed to save the session - do not proceed to delete backup files
Expand Down Expand Up @@ -225,7 +229,11 @@ private async Task<TextEditorSessionDataV1> BuildTextEditorSessionData(ITextEdit
{
// Add the opened file to FutureAccessList so we can access it next launch
var futureAccessToken = ToToken(textEditor.Id);
await FileSystemUtility.TryAddOrReplaceTokenInFutureAccessList(futureAccessToken, textEditor.EditingFile);
if (!await FileSystemUtility.TryAddOrReplaceTokenInFutureAccessList(futureAccessToken, textEditor.EditingFile))
{
Analytics.TrackEvent("SessionManager_FailedToAddTokenInFutureAccessList",
new Dictionary<string, string>() {{ "ItemCount", FileSystemUtility.GetFutureAccessListItemCount().ToString() }});
}
textEditorData.EditingFileFutureAccessToken = futureAccessToken;
textEditorData.EditingFileName = textEditor.EditingFileName;
textEditorData.EditingFilePath = textEditor.EditingFilePath;
Expand Down Expand Up @@ -323,6 +331,7 @@ public async Task ClearSessionDataAsync()
catch (Exception ex)
{
LoggingService.LogError($"[SessionManager] Failed to delete session meta data: {ex.Message}");
Analytics.TrackEvent("SessionManager_FailedToDeleteSessionMetaData", new Dictionary<string, string>() {{ "Exception", ex.Message }});
}
}

Expand Down Expand Up @@ -388,6 +397,7 @@ private async Task<ITextEditor> RecoverTextEditorAsync(TextEditorSessionDataV1 e
editorSessionData.Id,
textFile,
editingFile,
editorSessionData.StateMetaData.FileNamePlaceholder,
editorSessionData.StateMetaData.IsModified);

if (pendingFile != null)
Expand Down Expand Up @@ -469,6 +479,7 @@ private void DeleteOrphanedTokensInFutureAccessList(NotepadsSessionDataV1 sessio
catch (Exception ex)
{
LoggingService.LogError($"[SessionManager] Failed to delete orphaned token in FutureAccessList: {ex.Message}");
Analytics.TrackEvent("SessionManager_FailedToDeleteOrphanedTokenInFutureAccessList", new Dictionary<string, string>() {{ "Exception", ex.Message }});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Notepads/NotepadsMainPage.IO.cs
Expand Up @@ -100,7 +100,7 @@ private async Task<bool> Save(ITextEditor textEditor, bool saveAs, bool ignoreUn
!await FileSystemUtility.FileIsWritable(textEditor.EditingFile))
{
NotepadsCore.SwitchTo(textEditor);
file = await FilePickerFactory.GetFileSavePicker(textEditor, _defaultNewFileName, saveAs)
file = await FilePickerFactory.GetFileSavePicker(textEditor, saveAs)
.PickSaveFileAsync();
NotepadsCore.FocusOnTextEditor(textEditor);
if (file == null)
Expand Down
4 changes: 2 additions & 2 deletions src/Notepads/NotepadsMainPage.StatusBar.cs
Expand Up @@ -72,7 +72,7 @@ private void UpdateFileModificationStateIndicator(ITextEditor textEditor)
private void UpdatePathIndicator(ITextEditor textEditor)
{
if (StatusBar == null) return;
PathIndicator.Text = textEditor.EditingFilePath ?? _defaultNewFileName;
PathIndicator.Text = textEditor.EditingFilePath ?? textEditor.FileNamePlaceholder;

if (textEditor.FileModificationState == FileModificationState.Untouched)
{
Expand Down Expand Up @@ -158,7 +158,7 @@ private async void ModificationFlyoutSelection_OnClick(object sender, RoutedEven
NotepadsCore.GetSelectedTextEditor().OpenSideBySideDiffViewer();
break;
case "RevertAllChanges":
var fileName = selectedTextEditor.EditingFileName ?? _defaultNewFileName;
var fileName = selectedTextEditor.EditingFileName ?? selectedTextEditor.FileNamePlaceholder;
var setCloseSaveReminderDialog = ContentDialogFactory.GetRevertAllChangesConfirmationDialog(
fileName, () =>
{
Expand Down
18 changes: 9 additions & 9 deletions src/Notepads/NotepadsMainPage.xaml.cs
Expand Up @@ -26,8 +26,6 @@

public sealed partial class NotepadsMainPage : Page, INotificationDelegate
{
private readonly string _defaultNewFileName;

private IReadOnlyList<IStorageItem> _appLaunchFiles;

private string _appLaunchCmdDir;
Expand All @@ -52,7 +50,7 @@ private INotepadsCore NotepadsCore
{
if (_notepadsCore == null)
{
_notepadsCore = new NotepadsCore(Sets, _resourceLoader.GetString("TextEditor_DefaultNewFileName"), new NotepadsExtensionProvider());
_notepadsCore = new NotepadsCore(Sets, new NotepadsExtensionProvider());
_notepadsCore.StorageItemsDropped += OnStorageItemsDropped;
_notepadsCore.TextEditorLoaded += OnTextEditorLoaded;
_notepadsCore.TextEditorUnloaded += OnTextEditorUnloaded;
Expand Down Expand Up @@ -98,6 +96,8 @@ private INotepadsCore NotepadsCore

private ISessionManager SessionManager => _sessionManager ?? (_sessionManager = SessionUtility.GetSessionManager(NotepadsCore));

private readonly string _defaultNewFileName;

public NotepadsMainPage()
{
InitializeComponent();
Expand Down Expand Up @@ -154,9 +154,9 @@ private void InitControls()
ToolTipService.SetToolTip(ExitCompactOverlayButton, _resourceLoader.GetString("App_ExitCompactOverlayMode_Text"));
RootSplitView.PaneOpening += delegate { SettingsFrame.Navigate(typeof(SettingsPage), null, new SuppressNavigationTransitionInfo()); };
RootSplitView.PaneClosed += delegate { NotepadsCore.FocusOnSelectedTextEditor(); };
NewSetButton.Click += delegate { NotepadsCore.OpenNewTextEditor(); };
NewSetButton.Click += delegate { NotepadsCore.OpenNewTextEditor(_defaultNewFileName); };
MainMenuButton.Click += (sender, args) => FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
MenuCreateNewButton.Click += (sender, args) => NotepadsCore.OpenNewTextEditor();
MenuCreateNewButton.Click += (sender, args) => NotepadsCore.OpenNewTextEditor(_defaultNewFileName);
MenuOpenFileButton.Click += async (sender, args) => await OpenNewFiles();
MenuSaveButton.Click += async (sender, args) => await Save(NotepadsCore.GetSelectedTextEditor(), saveAs: false);
MenuSaveAsButton.Click += async (sender, args) => await Save(NotepadsCore.GetSelectedTextEditor(), saveAs: true);
Expand Down Expand Up @@ -216,8 +216,8 @@ private KeyboardCommandHandler GetKeyboardCommandHandler()
new KeyboardShortcut<KeyRoutedEventArgs>(true, false, false, VirtualKey.W, (args) => NotepadsCore.CloseTextEditor(NotepadsCore.GetSelectedTextEditor())),
new KeyboardShortcut<KeyRoutedEventArgs>(true, false, false, VirtualKey.Tab, (args) => NotepadsCore.SwitchTo(true)),
new KeyboardShortcut<KeyRoutedEventArgs>(true, false, true, VirtualKey.Tab, (args) => NotepadsCore.SwitchTo(false)),
new KeyboardShortcut<KeyRoutedEventArgs>(true, false, false, VirtualKey.N, (args) => NotepadsCore.OpenNewTextEditor()),
new KeyboardShortcut<KeyRoutedEventArgs>(true, false, false, VirtualKey.T, (args) => NotepadsCore.OpenNewTextEditor()),
new KeyboardShortcut<KeyRoutedEventArgs>(true, false, false, VirtualKey.N, (args) => NotepadsCore.OpenNewTextEditor(_defaultNewFileName)),
new KeyboardShortcut<KeyRoutedEventArgs>(true, false, false, VirtualKey.T, (args) => NotepadsCore.OpenNewTextEditor(_defaultNewFileName)),
new KeyboardShortcut<KeyRoutedEventArgs>(true, false, false, VirtualKey.O, async (args) => await OpenNewFiles()),
new KeyboardShortcut<KeyRoutedEventArgs>(true, false, false, VirtualKey.S, async (args) => await Save(NotepadsCore.GetSelectedTextEditor(), saveAs: false, ignoreUnmodifiedDocument: true)),
new KeyboardShortcut<KeyRoutedEventArgs>(true, false, true, VirtualKey.S, async (args) => await Save(NotepadsCore.GetSelectedTextEditor(), saveAs: true)),
Expand Down Expand Up @@ -293,7 +293,7 @@ private async void Sets_Loaded(object sender, RoutedEventArgs e)
{
if (loadedCount == 0)
{
NotepadsCore.OpenNewTextEditor();
NotepadsCore.OpenNewTextEditor(_defaultNewFileName);
}

if (!App.IsFirstInstance)
Expand Down Expand Up @@ -460,7 +460,7 @@ private async void OnTextEditorUnloaded(object sender, ITextEditor textEditor)

private async void OnTextEditorClosingWithUnsavedContent(object sender, ITextEditor textEditor)
{
var file = textEditor.EditingFilePath ?? _defaultNewFileName;
var file = textEditor.EditingFilePath ?? textEditor.FileNamePlaceholder;

var setCloseSaveReminderDialog = ContentDialogFactory.GetSetCloseSaveReminderDialog(file, async () =>
{
Expand Down
4 changes: 2 additions & 2 deletions src/Notepads/Services/FilePickerFactory.cs
Expand Up @@ -16,7 +16,7 @@ public static FileOpenPicker GetFileOpenPicker()
return fileOpenPicker;
}

public static FileSavePicker GetFileSavePicker(ITextEditor textEditor, string defaultFileName, bool saveAs)
public static FileSavePicker GetFileSavePicker(ITextEditor textEditor, bool saveAs)
{
FileSavePicker savePicker = new FileSavePicker
{
Expand Down Expand Up @@ -44,7 +44,7 @@ public static FileSavePicker GetFileSavePicker(ITextEditor textEditor, string de
".js", ".ts", ".lua",
});
savePicker.FileTypeChoices.Add("Unknown", new List<string>() { "." });
savePicker.SuggestedFileName = textEditor.EditingFileName ?? defaultFileName;
savePicker.SuggestedFileName = textEditor.EditingFileName ?? textEditor.FileNamePlaceholder;
return savePicker;
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/Notepads/Utilities/FileSystemUtility.cs
Expand Up @@ -364,5 +364,17 @@ public static async Task<bool> TryAddOrReplaceTokenInFutureAccessList(string tok
}
return false;
}

public static int GetFutureAccessListItemCount()
{
try
{
return StorageApplicationPermissions.FutureAccessList.Entries.Count;
}
catch
{
return -1;
}
}
}
}

0 comments on commit e98df51

Please sign in to comment.