private MenuFlyoutItem CreateAutoGuessEncodingItem()
        {
            var autoGuessEncodingItem = new MenuFlyoutItem()
            {
                Text          = _resourceLoader.GetString("TextEditor_EncodingIndicator_FlyoutItem_AutoGuessEncoding"),
                FlowDirection = FlowDirection.LeftToRight,
            };

            autoGuessEncodingItem.Click += async(sender, args) =>
            {
                var selectedTextEditor = NotepadsCore.GetSelectedTextEditor();
                var file = selectedTextEditor?.EditingFile;
                if (file == null || selectedTextEditor.FileModificationState == FileModificationState.RenamedMovedOrDeleted)
                {
                    return;
                }

                Encoding encoding = null;

                try
                {
                    using (var inputStream = await file.OpenReadAsync())
                        using (var stream = inputStream.AsStreamForRead())
                        {
                            if (FileSystemUtility.TryGuessEncoding(stream, out var detectedEncoding))
                            {
                                encoding = detectedEncoding;
                            }
                        }
                }
                catch (Exception)
                {
                    encoding = null;
                }

                if (encoding == null)
                {
                    NotificationCenter.Instance.PostNotification(_resourceLoader.GetString("TextEditor_NotificationMsg_EncodingCannotBeDetermined"), 2500);
                    return;
                }

                try
                {
                    await selectedTextEditor.ReloadFromEditingFile(encoding);

                    NotificationCenter.Instance.PostNotification(_resourceLoader.GetString("TextEditor_NotificationMsg_FileReloaded"), 1500);
                }
                catch (Exception ex)
                {
                    var fileOpenErrorDialog = new FileOpenErrorDialog(selectedTextEditor.EditingFilePath, ex.Message);
                    await DialogManager.OpenDialogAsync(fileOpenErrorDialog, awaitPreviousDialog : false);

                    if (!fileOpenErrorDialog.IsAborted)
                    {
                        NotepadsCore.FocusOnSelectedTextEditor();
                    }
                }
            };
            return(autoGuessEncodingItem);
        }
Exemplo n.º 2
0
        public async Task <bool> OpenFile(StorageFile file)
        {
            try
            {
                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();
                return(true);
            }
            catch (Exception ex)
            {
                var fileOpenErrorDialog = NotepadsDialogFactory.GetFileOpenErrorDialog(file.Path, ex.Message);
                await DialogManager.OpenDialogAsync(fileOpenErrorDialog, awaitPreviousDialog : false);

                if (!fileOpenErrorDialog.IsAborted)
                {
                    NotepadsCore.FocusOnSelectedTextEditor();
                }
                return(false);
            }
        }
        private async void ReloadFileFromDisk(object sender, RoutedEventArgs e)
        {
            var selectedEditor = NotepadsCore.GetSelectedTextEditor();

            if (selectedEditor?.EditingFile != null &&
                selectedEditor.FileModificationState != FileModificationState.RenamedMovedOrDeleted)
            {
                try
                {
                    await selectedEditor.ReloadFromEditingFile();

                    NotificationCenter.Instance.PostNotification(_resourceLoader.GetString("TextEditor_NotificationMsg_FileReloaded"), 1500);
                }
                catch (Exception ex)
                {
                    var fileOpenErrorDialog = new FileOpenErrorDialog(selectedEditor.EditingFilePath, ex.Message);
                    await DialogManager.OpenDialogAsync(fileOpenErrorDialog, awaitPreviousDialog : false);

                    if (!fileOpenErrorDialog.IsAborted)
                    {
                        NotepadsCore.FocusOnSelectedTextEditor();
                    }
                }
            }
        }
Exemplo n.º 4
0
        private async Task OpenNewFiles()
        {
            IReadOnlyList <StorageFile> files;

            try
            {
                files = await FilePickerFactory.GetFileOpenPicker().PickMultipleFilesAsync();
            }
            catch (Exception ex)
            {
                var fileOpenErrorDialog = new FileOpenErrorDialog(filePath: null, ex.Message);
                await DialogManager.OpenDialogAsync(fileOpenErrorDialog, awaitPreviousDialog : false);

                if (!fileOpenErrorDialog.IsAborted)
                {
                    NotepadsCore.FocusOnSelectedTextEditor();
                }
                return;
            }

            if (files == null || files.Count == 0)
            {
                NotepadsCore.FocusOnSelectedTextEditor();
                return;
            }

            foreach (var file in files)
            {
                await OpenFile(file);
            }
        }
Exemplo n.º 5
0
        private void PathIndicatorClicked(ITextEditor selectedEditor)
        {
            NotepadsCore.FocusOnSelectedTextEditor();

            PathIndicatorFlyoutCopyFullPathFlyoutItem.Text         = _resourceLoader.GetString("Tab_ContextFlyout_CopyFullPathButtonDisplayText");
            PathIndicatorFlyoutOpenContainingFolderFlyoutItem.Text = _resourceLoader.GetString("Tab_ContextFlyout_OpenContainingFolderButtonDisplayText");

            if (App.IsGameBarWidget)
            {
                PathIndicatorFlyoutOpenContainingFolderFlyoutItem.Visibility = Visibility.Collapsed;
            }

            if (selectedEditor.FileModificationState == FileModificationState.Untouched)
            {
                if (selectedEditor.EditingFile != null)
                {
                    PathIndicator.ContextFlyout.ShowAt(FileModificationStateIndicator);
                }
            }
            else if (selectedEditor.FileModificationState == FileModificationState.Modified)
            {
                PathIndicator.ContextFlyout.ShowAt(FileModificationStateIndicator);
            }
            else if (selectedEditor.FileModificationState == FileModificationState.RenamedMovedOrDeleted)
            {
                NotificationCenter.Instance.PostNotification(_resourceLoader.GetString("TextEditor_FileRenamedMovedOrDeletedIndicator_ToolTip"), 2000);
            }
        }
        private void AddEncodingItem(Encoding encoding, MenuFlyoutSubItem reopenWithEncoding, MenuFlyoutSubItem saveWithEncoding)
        {
            const int EncodingMenuFlyoutItemHeight   = 30;
            const int EncodingMenuFlyoutItemFontSize = 14;

            var reopenWithEncodingItem =
                new MenuFlyoutItem()
            {
                Text          = EncodingUtility.GetEncodingName(encoding),
                FlowDirection = FlowDirection.LeftToRight,
                Height        = EncodingMenuFlyoutItemHeight,
                FontSize      = EncodingMenuFlyoutItemFontSize
            };

            reopenWithEncodingItem.Click += async(sender, args) =>
            {
                var selectedTextEditor = NotepadsCore.GetSelectedTextEditor();
                if (selectedTextEditor != null)
                {
                    try
                    {
                        await selectedTextEditor.ReloadFromEditingFile(encoding);

                        NotificationCenter.Instance.PostNotification(
                            _resourceLoader.GetString("TextEditor_NotificationMsg_FileReloaded"), 1500);
                    }
                    catch (Exception ex)
                    {
                        var fileOpenErrorDialog = NotepadsDialogFactory.GetFileOpenErrorDialog(selectedTextEditor.EditingFilePath, ex.Message);
                        await DialogManager.OpenDialogAsync(fileOpenErrorDialog, awaitPreviousDialog : false);

                        if (!fileOpenErrorDialog.IsAborted)
                        {
                            NotepadsCore.FocusOnSelectedTextEditor();
                        }
                    }
                }
            };
            reopenWithEncoding.Items?.Add(reopenWithEncodingItem);

            var saveWithEncodingItem =
                new MenuFlyoutItem()
            {
                Text          = EncodingUtility.GetEncodingName(encoding),
                FlowDirection = FlowDirection.LeftToRight,
                Height        = EncodingMenuFlyoutItemHeight,
                FontSize      = EncodingMenuFlyoutItemFontSize
            };

            saveWithEncodingItem.Click += (sender, args) =>
            {
                NotepadsCore.GetSelectedTextEditor()?.TryChangeEncoding(encoding);
            };
            saveWithEncoding.Items?.Add(saveWithEncodingItem);
        }
Exemplo n.º 7
0
        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);
            }
        }
Exemplo n.º 8
0
        private async Task <bool> Save(ITextEditor textEditor, bool saveAs, bool ignoreUnmodifiedDocument = false)
        {
            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);

                return(true);
            }
            catch (Exception ex)
            {
                var fileSaveErrorDialog =
                    ContentDialogFactory.GetFileSaveErrorDialog((file == null) ? string.Empty : file.Path, ex.Message);
                await ContentDialogMaker.CreateContentDialogAsync(fileSaveErrorDialog, awaitPreviousDialog : false);

                NotepadsCore.FocusOnSelectedTextEditor();
                return(false);
            }
        }
Exemplo n.º 9
0
        private async Task OpenNewFiles()
        {
            IReadOnlyList <StorageFile> files;

            try
            {
                var fileOpenPicker = FilePickerFactory.GetFileOpenPicker();
                foreach (var type in new List <string>()
                {
                    ".txt", ".md", ".markdown",
                    ".cfg", ".config", ".cnf", ".conf", ".ini", ".log",
                    ".json", ".yml", ".yaml", ".xml", ".xaml",
                    ".html", ".htm", ".asp", ".aspx", ".jsp", ".jspx", ".css", ".scss",
                    ".ps1", ".bat", ".cmd", ".vbs", ".sh", ".bashrc", ".rc", ".bash",
                    ".c", ".cmake", ".h", ".hpp", ".cpp", ".cc", ".cs", ".m", ".mm", ".php", ".py", ".rb", ".vb", ".java",
                    ".js", ".ts", ".lua",
                    ".csv",
                })
                {
                    fileOpenPicker.FileTypeFilter.Add(type);
                }

                files = await fileOpenPicker.PickMultipleFilesAsync();
            }
            catch (Exception ex)
            {
                var fileOpenErrorDialog = NotepadsDialogFactory.GetFileOpenErrorDialog(filePath: null, ex.Message);
                await DialogManager.OpenDialogAsync(fileOpenErrorDialog, awaitPreviousDialog : false);

                if (!fileOpenErrorDialog.IsAborted)
                {
                    NotepadsCore.FocusOnSelectedTextEditor();
                }
                return;
            }

            if (files == null || files.Count == 0)
            {
                NotepadsCore.FocusOnSelectedTextEditor();
                return;
            }

            foreach (var file in files)
            {
                await OpenFile(file);
            }
        }
Exemplo n.º 10
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);
            }
        }
Exemplo n.º 11
0
        private void PathIndicatorClicked(ITextEditor selectedEditor)
        {
            NotepadsCore.FocusOnSelectedTextEditor();

            if (selectedEditor.FileModificationState == FileModificationState.Untouched)
            {
                if (selectedEditor.EditingFile != null)
                {
                    FileModificationStateIndicator.ContextFlyout.ShowAt(FileModificationStateIndicator);
                }
            }
            else if (selectedEditor.FileModificationState == FileModificationState.Modified)
            {
                FileModificationStateIndicator.ContextFlyout.ShowAt(FileModificationStateIndicator);
            }
            else if (selectedEditor.FileModificationState == FileModificationState.RenamedMovedOrDeleted)
            {
                NotificationCenter.Instance.PostNotification(_resourceLoader.GetString("TextEditor_FileRenamedMovedOrDeletedIndicator_ToolTip"), 2000);
            }
        }
Exemplo n.º 12
0
        private async Task RenameFileAsync(ITextEditor textEditor)
        {
            if (textEditor == null)
            {
                return;
            }

            if (textEditor.FileModificationState == FileModificationState.RenamedMovedOrDeleted)
            {
                return;
            }

            if (textEditor.EditingFile != null && FileSystemUtility.IsFileReadOnly(textEditor.EditingFile))
            {
                return;
            }

            var fileRenameDialog = new FileRenameDialog(textEditor.EditingFileName ?? textEditor.FileNamePlaceholder,
                                                        fileExists: textEditor.EditingFile != null,
                                                        confirmedAction: async(newFilename) =>
            {
                try
                {
                    await textEditor.RenameAsync(newFilename);
                    NotepadsCore.FocusOnSelectedTextEditor();
                    NotificationCenter.Instance.PostNotification(_resourceLoader.GetString("TextEditor_NotificationMsg_FileRenamed"), 1500);
                }
                catch (Exception ex)
                {
                    var errorMessage = ex.Message?.TrimEnd('\r', '\n');
                    NotificationCenter.Instance.PostNotification(errorMessage, 3500);     // TODO: Use Content Dialog to display error message
                }
            });

            await DialogManager.OpenDialogAsync(fileRenameDialog, awaitPreviousDialog : false);
        }
Exemplo n.º 13
0
 private void StatusBarFlyout_OnClosing(FlyoutBase sender, FlyoutBaseClosingEventArgs args)
 {
     NotepadsCore.FocusOnSelectedTextEditor();
 }
Exemplo n.º 14
0
        private void StatusBarComponent_OnTapped(object sender, TappedRoutedEventArgs e)
        {
            var selectedEditor = NotepadsCore.GetSelectedTextEditor();
            if (selectedEditor == null) return;

            if (sender == FileModificationStateIndicator)
            {
                if (selectedEditor.FileModificationState == FileModificationState.Modified)
                {
                    FileModificationStateIndicator.ContextFlyout.ShowAt(FileModificationStateIndicator);
                }
                else if (selectedEditor.FileModificationState == FileModificationState.RenamedMovedOrDeleted)
                {
                    NotificationCenter.Instance.PostNotification(
                        _resourceLoader.GetString("TextEditor_FileRenamedMovedOrDeletedIndicator_ToolTip"), 2000);
                }
            }
            else if (sender == PathIndicator && !string.IsNullOrEmpty(PathIndicator.Text))
            {
                NotepadsCore.FocusOnSelectedTextEditor();

                if (selectedEditor.FileModificationState == FileModificationState.Untouched)
                {
                    if (selectedEditor.EditingFile != null)
                    {
                        FileModificationStateIndicator.ContextFlyout.ShowAt(FileModificationStateIndicator);
                    }
                }
                else if (selectedEditor.FileModificationState == FileModificationState.Modified)
                {
                    FileModificationStateIndicator.ContextFlyout.ShowAt(FileModificationStateIndicator);
                }
                else if (selectedEditor.FileModificationState == FileModificationState.RenamedMovedOrDeleted)
                {
                    NotificationCenter.Instance.PostNotification(
                        _resourceLoader.GetString("TextEditor_FileRenamedMovedOrDeletedIndicator_ToolTip"), 2000);
                }
            }
            else if (sender == ModificationIndicator)
            {
                PreviewTextChangesFlyoutItem.IsEnabled =
                    !selectedEditor.NoChangesSinceLastSaved(compareTextOnly: true) &&
                    selectedEditor.Mode != TextEditorMode.DiffPreview;
                ModificationIndicator?.ContextFlyout.ShowAt(ModificationIndicator);
            }
            else if (sender == LineColumnIndicator)
            {
                selectedEditor.ShowGoToControl();
            }
            else if (sender == FontZoomIndicator)
            {
                FontZoomIndicator?.ContextFlyout.ShowAt(FontZoomIndicator);
                FontZoomIndicatorFlyout.Opened += (sflyout, eflyout) => ToolTipService.SetToolTip(RestoreDefaultZoom, null);
            }
            else if (sender == LineEndingIndicator)
            {
                LineEndingIndicator?.ContextFlyout.ShowAt(LineEndingIndicator);
            }
            else if (sender == EncodingIndicator)
            {
                var reopenWithEncoding = EncodingSelectionFlyout?.Items?.FirstOrDefault(i => i.Name.Equals("ReopenWithEncoding"));
                if (reopenWithEncoding != null)
                {
                    reopenWithEncoding.IsEnabled = selectedEditor.EditingFile != null && selectedEditor.FileModificationState != FileModificationState.RenamedMovedOrDeleted;
                }
                EncodingIndicator?.ContextFlyout.ShowAt(EncodingIndicator);
            }
            else if (sender == ShadowWindowIndicator)
            {
                NotificationCenter.Instance.PostNotification(
                    _resourceLoader.GetString("App_ShadowWindowIndicator_Description"), 4000);
            }
        }
Exemplo n.º 15
0
        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)
                {
                    file = await OpenFileUsingFileSavePicker(textEditor);

                    if (file == null)
                    {
                        return(false);              // User cancelled
                    }
                }
                else
                {
                    file = textEditor.EditingFile;
                }

                bool promptSaveAs = false;
                try
                {
                    await SaveInternal(textEditor, file, rebuildOpenRecentItems);
                }
                catch (UnauthorizedAccessException) // Happens when the file we are saving is read-only
                {
                    promptSaveAs = true;
                }
                catch (FileNotFoundException) // Happens when the file not found or storage media is removed
                {
                    promptSaveAs = true;
                }

                if (promptSaveAs)
                {
                    file = await OpenFileUsingFileSavePicker(textEditor);

                    if (file == null)
                    {
                        return(false);              // User cancelled
                    }
                    await SaveInternal(textEditor, file, rebuildOpenRecentItems);

                    return(true);
                }

                return(true);
            }
            catch (Exception ex)
            {
                var fileSaveErrorDialog = new FileSaveErrorDialog((file == null) ? string.Empty : file.Path, ex.Message);
                await DialogManager.OpenDialogAsync(fileSaveErrorDialog, awaitPreviousDialog : false);

                if (!fileSaveErrorDialog.IsAborted)
                {
                    NotepadsCore.FocusOnSelectedTextEditor();
                }
                return(false);
            }
        }
        private void StatusBarComponent_OnTapped(object sender, TappedRoutedEventArgs e)
        {
            var selectedEditor = NotepadsCore.GetSelectedTextEditor();

            if (selectedEditor == null)
            {
                return;
            }

            if (sender == FileModificationStateIndicator)
            {
                if (selectedEditor.FileModificationState == FileModificationState.Modified)
                {
                    FileModificationStateIndicator.ContextFlyout.ShowAt(FileModificationStateIndicator);
                }
                else if (selectedEditor.FileModificationState == FileModificationState.RenamedMovedOrDeleted)
                {
                    NotificationCenter.Instance.PostNotification(
                        _resourceLoader.GetString("TextEditor_FileRenamedMovedOrDeletedIndicator_ToolTip"), 2000);
                }
            }
            else if (sender == PathIndicator && !string.IsNullOrEmpty(PathIndicator.Text))
            {
                NotepadsCore.FocusOnSelectedTextEditor();

                if (selectedEditor.FileModificationState == FileModificationState.Untouched)
                {
                    if (selectedEditor.EditingFile != null)
                    {
                        FileModificationStateIndicator.ContextFlyout.ShowAt(FileModificationStateIndicator);
                    }
                }
                else if (selectedEditor.FileModificationState == FileModificationState.Modified)
                {
                    FileModificationStateIndicator.ContextFlyout.ShowAt(FileModificationStateIndicator);
                }
                else if (selectedEditor.FileModificationState == FileModificationState.RenamedMovedOrDeleted)
                {
                    NotificationCenter.Instance.PostNotification(
                        _resourceLoader.GetString("TextEditor_FileRenamedMovedOrDeletedIndicator_ToolTip"), 2000);
                }
            }
            else if (sender == ModificationIndicator)
            {
                PreviewTextChangesFlyoutItem.IsEnabled =
                    !selectedEditor.NoChangesSinceLastSaved(compareTextOnly: true) &&
                    selectedEditor.Mode != TextEditorMode.DiffPreview;
                ModificationIndicator?.ContextFlyout.ShowAt(ModificationIndicator);
            }
            else if (sender == LineColumnIndicator)
            {
            }
            else if (sender == LineEndingIndicator)
            {
                LineEndingIndicator?.ContextFlyout.ShowAt(LineEndingIndicator);
            }
            else if (sender == EncodingIndicator)
            {
                EncodingIndicator?.ContextFlyout.ShowAt(EncodingIndicator);
            }
            else if (sender == ShadowWindowIndicator)
            {
                NotificationCenter.Instance.PostNotification(
                    _resourceLoader.GetString("App_ShadowWindowIndicator_Description"), 4000);
            }
        }