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);
            }
        }
Esempio n. 2
0
        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();
            }
        }
Esempio n. 3
0
        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);
            }
        }