private async Task <bool> Save(ITextEditor textEditor, bool saveAs, bool ignoreUnmodifiedDocument = false, bool rebuildOpenRecentItems = true) { if (textEditor == null) { return(false); } if (ignoreUnmodifiedDocument && !textEditor.IsModified) { return(true); } StorageFile file = null; try { if (textEditor.EditingFile == null || saveAs || FileSystemUtility.IsFileReadOnly(textEditor.EditingFile) || !await FileSystemUtility.FileIsWritable(textEditor.EditingFile)) { NotepadsCore.SwitchTo(textEditor); file = await FilePickerFactory.GetFileSavePicker(textEditor, saveAs).PickSaveFileAsync(); NotepadsCore.FocusOnTextEditor(textEditor); if (file == null) { return(false); // User cancelled } } else { file = textEditor.EditingFile; } await NotepadsCore.SaveContentToFileAndUpdateEditorState(textEditor, file); var success = MRUService.TryAdd(file); // Remember recently used files if (success && rebuildOpenRecentItems) { await BuildOpenRecentButtonSubItems(); } return(true); } catch (Exception ex) { var fileSaveErrorDialog = NotepadsDialogFactory.GetFileSaveErrorDialog((file == null) ? string.Empty : file.Path, ex.Message); await DialogManager.OpenDialogAsync(fileSaveErrorDialog, awaitPreviousDialog : false); if (!fileSaveErrorDialog.IsAborted) { NotepadsCore.FocusOnSelectedTextEditor(); } return(false); } }
private async Task SaveInternal(ITextEditor textEditor, StorageFile file, bool rebuildOpenRecentItems) { await NotepadsCore.SaveContentToFileAndUpdateEditorState(textEditor, file); var success = MRUService.TryAdd(file); // Remember recently used files if (success && rebuildOpenRecentItems) { await BuildOpenRecentButtonSubItems(); } }
public async Task <bool> OpenFile(StorageFile file, bool rebuildOpenRecentItems = true) { try { if (file == null) { return(false); } var openedEditor = NotepadsCore.GetTextEditor(file); if (openedEditor != null) { NotepadsCore.SwitchTo(openedEditor); NotificationCenter.Instance.PostNotification( _resourceLoader.GetString("TextEditor_NotificationMsg_FileAlreadyOpened"), 2500); return(false); } var editor = await NotepadsCore.CreateTextEditor(Guid.NewGuid(), file); NotepadsCore.OpenTextEditor(editor); NotepadsCore.FocusOnSelectedTextEditor(); var success = MRUService.TryAdd(file); // Remember recently used files if (success && rebuildOpenRecentItems) { await BuildOpenRecentButtonSubItems(); } TrackFileExtensionIfNotSupported(file); return(true); } catch (Exception ex) { var fileOpenErrorDialog = new FileOpenErrorDialog(file.Path, ex.Message); await DialogManager.OpenDialogAsync(fileOpenErrorDialog, awaitPreviousDialog : false); if (!fileOpenErrorDialog.IsAborted) { NotepadsCore.FocusOnSelectedTextEditor(); } return(false); } }
private async Task BuildOpenRecentButtonSubItems() { var openRecentSubItem = new MenuFlyoutSubItem { Text = _resourceLoader.GetString("MainMenu_Button_Open_Recent/Text"), Icon = new FontIcon { Glyph = "\xE81C" }, Name = "MenuOpenRecentlyUsedFileButton", }; var MRUFileList = new HashSet <string>(); foreach (var item in await MRUService.Get(top: 10)) { if (item is StorageFile file) { if (MRUFileList.Contains(file.Path)) { // MRU might contains files with same path (User opens a recently used file after renaming it) // So we need to do the decouple here continue; } var newItem = new MenuFlyoutItem() { Text = file.Path }; ToolTipService.SetToolTip(newItem, file.Path); newItem.Click += async(sender, args) => { await OpenFile(file); }; openRecentSubItem.Items?.Add(newItem); MRUFileList.Add(file.Path); } } var oldOpenRecentSubItem = MainMenuButtonFlyout.Items?.FirstOrDefault(i => i.Name == openRecentSubItem.Name); if (oldOpenRecentSubItem != null) { MainMenuButtonFlyout.Items.Remove(oldOpenRecentSubItem); } openRecentSubItem.IsEnabled = false; if (openRecentSubItem.Items?.Count > 0) { openRecentSubItem.Items?.Add(new MenuFlyoutSeparator()); var clearRecentlyOpenedSubItem = new MenuFlyoutItem() { Text = _resourceLoader.GetString("MainMenu_Button_Open_Recent_ClearRecentlyOpenedSubItem_Text") }; clearRecentlyOpenedSubItem.Click += async(sender, args) => { MRUService.ClearAll(); await BuildOpenRecentButtonSubItems(); }; openRecentSubItem.Items?.Add(clearRecentlyOpenedSubItem); openRecentSubItem.IsEnabled = true; } if (MainMenuButtonFlyout.Items != null) { var indexToInsert = MainMenuButtonFlyout.Items.IndexOf(MenuOpenFileButton) + 1; MainMenuButtonFlyout.Items.Insert(indexToInsert, openRecentSubItem); } }