Example #1
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);
            }
        }
Example #2
0
        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 = NotepadsDialogFactory.GetFileOpenErrorDialog(selectedTextEditor.EditingFilePath, ex.Message);
                    await DialogManager.OpenDialogAsync(fileOpenErrorDialog, awaitPreviousDialog : false);

                    if (!fileOpenErrorDialog.IsAborted)
                    {
                        NotepadsCore.FocusOnSelectedTextEditor();
                    }
                }
            };
            return(autoGuessEncodingItem);
        }
Example #3
0
        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 = NotepadsDialogFactory.GetFileOpenErrorDialog(selectedEditor.EditingFilePath, ex.Message);
                    await DialogManager.OpenDialogAsync(fileOpenErrorDialog, awaitPreviousDialog : false);

                    if (!fileOpenErrorDialog.IsAborted)
                    {
                        NotepadsCore.FocusOnSelectedTextEditor();
                    }
                }
            }
        }
Example #4
0
        private void FontZoomIndicatorFlyoutSelection_OnClick(object sender, RoutedEventArgs e)
        {
            if (!(sender is AppBarButton button))
            {
                return;
            }

            var selectedTextEditor = NotepadsCore.GetSelectedTextEditor();

            if (selectedTextEditor == null)
            {
                return;
            }

            switch ((string)button.Name)
            {
            case "ZoomIn":
                selectedTextEditor.SetFontZoomFactor(FontZoomSlider.Value % 10 > 0
                        ? Math.Ceiling(FontZoomSlider.Value / 10) * 10
                        : FontZoomSlider.Value + 10);
                break;

            case "ZoomOut":
                selectedTextEditor.SetFontZoomFactor(FontZoomSlider.Value % 10 > 0
                        ? Math.Floor(FontZoomSlider.Value / 10) * 10
                        : FontZoomSlider.Value - 10);
                break;

            case "RestoreDefaultZoom":
                selectedTextEditor.SetFontZoomFactor(100);
                FontZoomIndicatorFlyout.Hide();
                break;
            }
        }
Example #5
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);
            }
        }
Example #6
0
        private async void ModificationFlyoutSelection_OnClick(object sender, RoutedEventArgs e)
        {
            if (!(sender is MenuFlyoutItem item))
            {
                return;
            }

            var selectedTextEditor = NotepadsCore.GetSelectedTextEditor();

            if (selectedTextEditor == null)
            {
                return;
            }

            switch ((string)item.Tag)
            {
            case "PreviewTextChanges":
                NotepadsCore.GetSelectedTextEditor().OpenSideBySideDiffViewer();
                break;

            case "RevertAllChanges":
                var fileName = selectedTextEditor.EditingFileName ?? selectedTextEditor.FileNamePlaceholder;
                var revertAllChangesConfirmationDialog = NotepadsDialogFactory.GetRevertAllChangesConfirmationDialog(
                    fileName,
                    confirmedAction: () =>
                {
                    selectedTextEditor.CloseSideBySideDiffViewer();
                    NotepadsCore.GetSelectedTextEditor().RevertAllChanges();
                });
                await DialogManager.OpenDialogAsync(revertAllChangesConfirmationDialog, awaitPreviousDialog : true);

                break;
            }
        }
Example #7
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);
            }
        }
Example #8
0
        private void CopyFullPath(object sender, RoutedEventArgs e)
        {
            var selectedEditor = NotepadsCore.GetSelectedTextEditor();

            if (selectedEditor?.EditingFile == null)
            {
                return;
            }

            try
            {
                DataPackage dataPackage = new DataPackage {
                    RequestedOperation = DataPackageOperation.Copy
                };
                dataPackage.SetText(selectedEditor.EditingFile.Path);
                Clipboard.SetContentWithOptions(dataPackage, new ClipboardContentOptions()
                {
                    IsAllowedInHistory = true, IsRoamable = true
                });
                Clipboard.Flush(); // This method allows the content to remain available after the application shuts down.
                NotificationCenter.Instance.PostNotification(_resourceLoader.GetString("TextEditor_NotificationMsg_FileNameOrPathCopied"), 1500);
            }
            catch (Exception ex)
            {
                LoggingService.LogError($"[{nameof(NotepadsMainPage)}] Failed to copy full path: {ex.Message}");
            }
        }
Example #9
0
        private async Task <StorageFile> OpenFileUsingFileSavePicker(ITextEditor textEditor)
        {
            NotepadsCore.SwitchTo(textEditor);
            StorageFile file = await FilePickerFactory.GetFileSavePicker(textEditor).PickSaveFileAsync();

            NotepadsCore.FocusOnTextEditor(textEditor);
            return(file);
        }
        private async void RenameFileAsync(object sender, RoutedEventArgs e)
        {
            var selectedEditor = NotepadsCore.GetSelectedTextEditor();

            if (selectedEditor?.EditingFile == null)
            {
                return;
            }
            await RenameFileAsync(selectedEditor);
        }
        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 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);
        }
Example #13
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();
            }
        }
        private void LineEndingSelection_OnClick(object sender, RoutedEventArgs e)
        {
            if (!(sender is MenuFlyoutItem item)) return;

            var lineEnding = LineEndingUtility.GetLineEndingByName((string) item.Tag);
            var textEditor = NotepadsCore.GetSelectedTextEditor();
            if (textEditor != null)
            {
                NotepadsCore.ChangeLineEnding(textEditor, lineEnding);
            }
        }
Example #15
0
        private void InitializeMainMenu()
        {
            MainMenuButton.Click += (sender, args) => FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);

            MenuCreateNewButton.Click       += (sender, args) => NotepadsCore.OpenNewTextEditor(_defaultNewFileName);
            MenuCreateNewWindowButton.Click += async(sender, args) => await OpenNewAppInstance();

            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);

            MenuSaveAllButton.Click += async(sender, args) => await SaveAll(NotepadsCore.GetAllTextEditors());

            MenuFindButton.Click           += (sender, args) => NotepadsCore.GetSelectedTextEditor()?.ShowFindAndReplaceControl(showReplaceBar: false);
            MenuReplaceButton.Click        += (sender, args) => NotepadsCore.GetSelectedTextEditor()?.ShowFindAndReplaceControl(showReplaceBar: true);
            MenuFullScreenButton.Click     += (sender, args) => EnterExitFullScreenMode();
            MenuCompactOverlayButton.Click += (sender, args) => EnterExitCompactOverlayMode();
            MenuPrintButton.Click          += async(sender, args) => await Print(NotepadsCore.GetSelectedTextEditor());

            MenuPrintAllButton.Click += async(sender, args) => await PrintAll(NotepadsCore.GetAllTextEditors());

            MenuSettingsButton.Click += (sender, args) => RootSplitView.IsPaneOpen = true;

            if (!App.IsFirstInstance)
            {
                MainMenuButton.Foreground    = new SolidColorBrush(ThemeSettingsService.AppAccentColor);
                MenuSettingsButton.IsEnabled = false;
            }

            if (App.IsGameBarWidget)
            {
                MenuFullScreenSeparator.Visibility = Visibility.Collapsed;
                MenuPrintSeparator.Visibility      = Visibility.Collapsed;
                MenuSettingsSeparator.Visibility   = Visibility.Collapsed;

                MenuCompactOverlayButton.Visibility = Visibility.Collapsed;
                MenuFullScreenButton.Visibility     = Visibility.Collapsed;
                MenuPrintButton.Visibility          = Visibility.Collapsed;
                MenuPrintAllButton.Visibility       = Visibility.Collapsed;
                MenuSettingsButton.Visibility       = Visibility.Collapsed;
            }

            if (!PrintManager.IsSupported())
            {
                MenuPrintButton.Visibility    = Visibility.Collapsed;
                MenuPrintAllButton.Visibility = Visibility.Collapsed;
                MenuPrintSeparator.Visibility = Visibility.Collapsed;
            }

            MainMenuButtonFlyout.Opening += MainMenuButtonFlyout_Opening;
        }
        private void FontZoomSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            if (!(sender is Slider)) return;

            var selectedTextEditor = NotepadsCore.GetSelectedTextEditor();
            if (selectedTextEditor == null) return;

            if (Math.Abs(e.NewValue - e.OldValue) > 0.1)
            {
                selectedTextEditor.SetFontZoomFactor(e.NewValue);
            }
        }
Example #17
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);
            }
        }
Example #18
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);
            }
        }
        private async void OpenContainingFolder(object sender, RoutedEventArgs e)
        {
            var selectedEditor = NotepadsCore.GetSelectedTextEditor();

            if (selectedEditor?.EditingFile == null)
            {
                return;
            }

            try
            {
                await Launcher.LaunchFolderPathAsync(Path.GetDirectoryName(selectedEditor?.EditingFile.Path));
            }
            catch (Exception ex)
            {
                LoggingService.LogError($"[{nameof(NotepadsMainPage)}] Failed to open containing folder: {ex.Message}");
            }
        }
Example #20
0
        private void MainMenuButtonFlyout_Opening(object sender, object e)
        {
            var selectedTextEditor = NotepadsCore.GetSelectedTextEditor();

            if (selectedTextEditor == null)
            {
                MenuSaveButton.IsEnabled     = false;
                MenuSaveAsButton.IsEnabled   = false;
                MenuFindButton.IsEnabled     = false;
                MenuReplaceButton.IsEnabled  = false;
                MenuPrintButton.IsEnabled    = false;
                MenuPrintAllButton.IsEnabled = false;
            }
            else if (selectedTextEditor.IsEditorEnabled() == false)
            {
                MenuSaveButton.IsEnabled    = selectedTextEditor.IsModified;
                MenuSaveAsButton.IsEnabled  = true;
                MenuFindButton.IsEnabled    = false;
                MenuReplaceButton.IsEnabled = false;
            }
            else
            {
                MenuSaveButton.IsEnabled    = selectedTextEditor.IsModified;
                MenuSaveAsButton.IsEnabled  = true;
                MenuFindButton.IsEnabled    = true;
                MenuReplaceButton.IsEnabled = true;

                if (PrintManager.IsSupported())
                {
                    MenuPrintButton.IsEnabled    = !string.IsNullOrEmpty(selectedTextEditor.GetText());
                    MenuPrintAllButton.IsEnabled = NotepadsCore.HaveNonemptyTextEditor();
                }
            }

            MenuFullScreenButton.Text = _resourceLoader.GetString(
                ApplicationView.GetForCurrentView().IsFullScreenMode
                    ? "App_ExitFullScreenMode_Text"
                    : "App_EnterFullScreenMode_Text");
            MenuCompactOverlayButton.Text = _resourceLoader.GetString(
                ApplicationView.GetForCurrentView().ViewMode == ApplicationViewMode.CompactOverlay
                    ? "App_ExitCompactOverlayMode_Text"
                    : "App_EnterCompactOverlayMode_Text");
            MenuSaveAllButton.IsEnabled = NotepadsCore.HaveUnsavedTextEditor();
        }
Example #21
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);
            }
        }
        private void StatusBarComponent_OnTapped(object sender, TappedRoutedEventArgs e)
        {
            var selectedEditor = NotepadsCore.GetSelectedTextEditor();

            if (selectedEditor == null)
            {
                return;
            }

            if (sender == FileModificationStateIndicator)
            {
                FileModificationStateIndicatorClicked(selectedEditor);
            }
            else if (sender == PathIndicator && !string.IsNullOrEmpty(PathIndicator.Text))
            {
                PathIndicatorClicked(selectedEditor);
            }
            else if (sender == ModificationIndicator)
            {
                ModificationIndicatorClicked(selectedEditor);
            }
            else if (sender == LineColumnIndicator)
            {
                LineColumnIndicatorClicked(selectedEditor);
            }
            else if (sender == FontZoomIndicator)
            {
                FontZoomIndicatorClicked(selectedEditor);
            }
            else if (sender == LineEndingIndicator)
            {
                LineEndingIndicatorClicked(selectedEditor);
            }
            else if (sender == EncodingIndicator)
            {
                EncodingIndicatorClicked(selectedEditor);
            }
            else if (sender == ShadowWindowIndicator)
            {
                ShadowWindowIndicatorClicked();
            }
        }
        public void ShowHideStatusBar(bool showStatusBar)
        {
            if (showStatusBar)
            {
                if (StatusBar == null)
                {
                    FindName("StatusBar");
                } // Lazy loading

                SetupStatusBar(NotepadsCore.GetSelectedTextEditor());
            }
            else
            {
                if (StatusBar != null)
                {
                    // If VS cannot find UnloadObject, ignore it. Reference: https://github.com/MicrosoftDocs/windows-uwp/issues/734
                    UnloadObject(StatusBar);
                }
            }
        }
Example #24
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);
            }
        }
Example #25
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);
        }
 private void StatusBarFlyout_OnClosing(FlyoutBase sender, FlyoutBaseClosingEventArgs args)
 {
     NotepadsCore.FocusOnSelectedTextEditor();
 }
        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);
            }
        }
        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);
            }
        }
Example #29
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);
            }
        }