예제 #1
0
        private void OnTextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
        {
            if (!args.IsContentChanging)
            {
                return;
            }

            var newFilename          = sender.Text;
            var isFilenameValid      = FileSystemUtility.IsFilenameValid(newFilename, out var error);
            var nameChanged          = string.Compare(_originalFilename, newFilename, StringComparison.OrdinalIgnoreCase) != 0;
            var isExtensionSupported = false;
            var fileExtension        = FileTypeUtility.GetFileExtension(newFilename);

            if (!_fileExists) // User can rename whatever they want for new file
            {
                isExtensionSupported = true;
            }
            else if (FileExtensionProvider.IsFileExtensionSupported(fileExtension))
            {
                // User can only rename an existing file if extension is supported by the app
                // This is a Windows 10 UWP limitation
                isExtensionSupported = true;
            }

            if (!isFilenameValid)
            {
                _errorMessageTextBlock.Foreground = new SolidColorBrush(Colors.Red);
                _errorMessageTextBlock.Text       = ResourceLoader.GetString($"InvalidFilenameError_{error}");
                _errorMessageTextBlock.Visibility = Visibility.Visible;
            }
            else if (!isExtensionSupported)
            {
                _errorMessageTextBlock.Foreground = new SolidColorBrush(Colors.OrangeRed);
                _errorMessageTextBlock.Text       = string.IsNullOrEmpty(fileExtension)
                    ? string.Format(ResourceLoader.GetString("FileRenameError_EmptyFileExtension"))
                    : string.Format(ResourceLoader.GetString("FileRenameError_UnsupportedFileExtension"), fileExtension);
                _errorMessageTextBlock.Visibility = Visibility.Visible;
            }
            else
            {
                _errorMessageTextBlock.Visibility = Visibility.Collapsed;
            }

            IsPrimaryButtonEnabled = isFilenameValid && nameChanged && isExtensionSupported;
        }
예제 #2
0
        private void TextEditorContextFlyout_Opening(object sender, object e)
        {
            if (_textEditor.TextEditorCore.Document.Selection.Type == SelectionType.InsertionPoint ||
                _textEditor.TextEditorCore.Document.Selection.Type == SelectionType.None)
            {
                PrepareForInsertionMode();
            }
            else
            {
                PrepareForSelectionMode();
            }

            Undo.IsEnabled = _textEditor.TextEditorCore.Document.CanUndo();
            Redo.IsEnabled = _textEditor.TextEditorCore.Document.CanRedo();

            PreviewToggle.Visibility = FileTypeUtility.IsPreviewSupported(_textEditor.FileType) ? Visibility.Visible : Visibility.Collapsed;
            WordWrap.Icon.Visibility = (_textEditor.TextEditorCore.TextWrapping == TextWrapping.Wrap) ? Visibility.Visible : Visibility.Collapsed;
        }
예제 #3
0
        public static FileSavePicker GetFileSavePicker(ITextEditor textEditor)
        {
            FileSavePicker savePicker = new FileSavePicker
            {
                SuggestedStartLocation = PickerLocationId.DocumentsLibrary
            };

            var fileName  = textEditor.EditingFileName ?? textEditor.FileNamePlaceholder;
            var extension = FileTypeUtility.GetFileExtension(fileName).ToLower();

            if (FileExtensionProvider.TextDocumentFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
            {
                savePicker.FileTypeChoices.Add("Text Documents", FileExtensionProvider.TextDocumentFileExtensions.ToList());
                savePicker.FileTypeChoices.Add("All Supported Files", AllSupportedExtensions);
                savePicker.FileTypeChoices.Add("Unknown", new List <string>()
                {
                    "."
                });
            }
            else if (AllSupportedExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
            {
                savePicker.FileTypeChoices.Add("All Supported Files", AllSupportedExtensions);
                savePicker.FileTypeChoices.Add("Text Documents", FileExtensionProvider.TextDocumentFileExtensions.ToList());
                savePicker.FileTypeChoices.Add("Unknown", new List <string>()
                {
                    "."
                });
            }
            else
            {
                savePicker.FileTypeChoices.Add("Unknown", new List <string>()
                {
                    "."
                });
                savePicker.FileTypeChoices.Add("Text Documents", FileExtensionProvider.TextDocumentFileExtensions.ToList());
                savePicker.FileTypeChoices.Add("All Supported Files", AllSupportedExtensions);
            }

            savePicker.SuggestedFileName = fileName;
            return(savePicker);
        }
예제 #4
0
        private bool TryRename()
        {
            var newFileName = _fileNameTextBox.Text;

            if (string.Compare(_originalFilename, newFileName, StringComparison.OrdinalIgnoreCase) == 0)
            {
                return(false);
            }

            if (!FileSystemUtility.IsFilenameValid(newFileName, out var error))
            {
                return(false);
            }

            if (_fileExists && !FileExtensionProvider.IsFileExtensionSupported(FileTypeUtility.GetFileExtension(newFileName)))
            {
                return(false);
            }

            _confirmedAction(newFileName.Trim());
            return(true);
        }
예제 #5
0
 // Here we track the file extension opened by user but not supported by Notepads.
 // This information will be used to on-board new extension support for future release.
 // Because UWP does not allow user to associate arbitrary file extension with the app.
 // File name will not and should not be tracked.
 private void TrackFileExtensionIfNotSupported(StorageFile file)
 {
     try
     {
         var extension = FileTypeUtility.GetFileExtension(file.Name).ToLower();
         if (!FileExtensionProvider.AllSupportedFileExtensions.Contains(extension))
         {
             if (string.IsNullOrEmpty(extension))
             {
                 extension = "<NoExtension>";
             }
             Analytics.TrackEvent("UnsupportedFileExtension", new Dictionary <string, string>()
             {
                 { "Extension", extension },
             });
         }
     }
     catch (Exception)
     {
         // ignore
     }
 }