Exemple #1
0
        private void OpenNewTextEditor(string text,
                                       StorageFile file,
                                       long dateModifiedFileTime,
                                       Encoding encoding,
                                       LineEnding lineEnding)
        {
            //LoggingService.LogInfo("Opening a text editor.");
            var textEditor = new TextEditor
            {
                ExtensionProvider = _extensionProvider
            };

            textEditor.Init(new TextFile(text, encoding, lineEnding, dateModifiedFileTime), file);
            textEditor.Loaded           += TextEditor_Loaded;
            textEditor.Unloaded         += TextEditor_Unloaded;
            textEditor.SelectionChanged += TextEditor_SelectionChanged;
            textEditor.KeyDown          += TextEditorKeyDown;
            textEditor.EditorModificationStateChanged += TextEditor_OnEditorModificationStateChanged;
            textEditor.ModeChanged += TextEditor_ModeChanged;
            textEditor.FileModificationStateChanged += (sender, args) => { TextEditorFileModificationStateChanged?.Invoke(this, sender as TextEditor); };
            textEditor.LineEndingChanged            += (sender, args) => { TextEditorLineEndingChanged?.Invoke(this, sender as TextEditor); };
            textEditor.EncodingChanged += (sender, args) => { TextEditorEncodingChanged?.Invoke(this, sender as TextEditor); };

            var newItem = new SetsViewItem
            {
                Header  = file == null ? DefaultNewFileName : file.Name,
                Content = textEditor,
                SelectionIndicatorForeground = Application.Current.Resources["SystemControlForegroundAccentBrush"] as SolidColorBrush,
                Icon = new SymbolIcon(Symbol.Save)
                {
                    Foreground = Application.Current.Resources["SystemControlForegroundAccentBrush"] as SolidColorBrush,
                }
            };

            if (newItem.Content == null || newItem.Content is Page)
            {
                throw new Exception("Content should not be null and type should not be Page (SetsView does not work well with Page controls)");
            }

            newItem.Icon.Visibility = Visibility.Collapsed;
            newItem.ContextFlyout   = new TabContextFlyout(this, textEditor);

            // Notepads should replace current "Untitled.txt" with open file if it is empty and it is the only tab that has been created.
            if (GetNumberOfOpenedTextEditors() == 1 && file != null)
            {
                var selectedEditor = GetAllTextEditors().First();
                if (selectedEditor.EditingFile == null && !selectedEditor.IsModified)
                {
                    Sets.Items?.Clear();
                }
            }

            Sets.Items?.Add(newItem);

            if (GetNumberOfOpenedTextEditors() > 1)
            {
                Sets.SelectedItem = newItem;
                Sets.ScrollToLastSet();
            }
        }
Exemple #2
0
        public ITextEditor CreateTextEditor(
            Guid id,
            TextFile textFile,
            StorageFile editingFile,
            string fileNamePlaceholder,
            bool isModified = false)
        {
            TextEditor textEditor = new TextEditor
            {
                Id = id,
                ExtensionProvider   = _extensionProvider,
                FileNamePlaceholder = fileNamePlaceholder
            };

            textEditor.Init(textFile, editingFile, isModified: isModified);
            textEditor.Loaded                       += TextEditor_Loaded;
            textEditor.Unloaded                     += TextEditor_Unloaded;
            textEditor.SelectionChanged             += TextEditor_SelectionChanged;
            textEditor.KeyDown                      += TextEditorKeyDown;
            textEditor.ModificationStateChanged     += TextEditor_OnEditorModificationStateChanged;
            textEditor.ModeChanged                  += TextEditor_ModeChanged;
            textEditor.FileModificationStateChanged += (sender, args) =>
            {
                TextEditorFileModificationStateChanged?.Invoke(this, sender as ITextEditor);
            };
            textEditor.LineEndingChanged += (sender, args) =>
            {
                TextEditorLineEndingChanged?.Invoke(this, sender as ITextEditor);
            };
            textEditor.EncodingChanged += (sender, args) => { TextEditorEncodingChanged?.Invoke(this, sender as ITextEditor); };

            return(textEditor);
        }
Exemple #3
0
 private void TextEditor_OnFileModificationStateChanged(object sender, EventArgs e)
 {
     if (!(sender is ITextEditor textEditor))
     {
         return;
     }
     TextEditorFileModificationStateChanged?.Invoke(this, textEditor);
 }
Exemple #4
0
        public ITextEditor OpenNewTextEditor(
            Guid id,
            string text,
            StorageFile file,
            long dateModifiedFileTime,
            Encoding encoding,
            LineEnding lineEnding,
            bool isModified,
            int atIndex = -1)
        {
            TextEditor textEditor = new TextEditor
            {
                Id = id,
                ExtensionProvider = _extensionProvider
            };

            textEditor.Init(new TextFile(text, encoding, lineEnding, dateModifiedFileTime), file, isModified: isModified);
            textEditor.Loaded                       += TextEditor_Loaded;
            textEditor.Unloaded                     += TextEditor_Unloaded;
            textEditor.SelectionChanged             += TextEditor_SelectionChanged;
            textEditor.KeyDown                      += TextEditorKeyDown;
            textEditor.ModificationStateChanged     += TextEditor_OnEditorModificationStateChanged;
            textEditor.ModeChanged                  += TextEditor_ModeChanged;
            textEditor.FileModificationStateChanged += (sender, args) => { TextEditorFileModificationStateChanged?.Invoke(this, sender as ITextEditor); };
            textEditor.LineEndingChanged            += (sender, args) => { TextEditorLineEndingChanged?.Invoke(this, sender as ITextEditor); };
            textEditor.EncodingChanged              += (sender, args) => { TextEditorEncodingChanged?.Invoke(this, sender as ITextEditor); };

            var textEditorSetsViewItem = new SetsViewItem
            {
                Header  = textEditor.EditingFileName ?? DefaultNewFileName,
                Content = textEditor,
                SelectionIndicatorForeground = Application.Current.Resources["SystemControlForegroundAccentBrush"] as SolidColorBrush,
                Icon = new SymbolIcon(Symbol.Save)
                {
                    Foreground = Application.Current.Resources["SystemControlForegroundAccentBrush"] as SolidColorBrush,
                }
            };

            if (textEditorSetsViewItem.Content == null || textEditorSetsViewItem.Content is Page)
            {
                throw new Exception("Content should not be null and type should not be Page (SetsView does not work well with Page controls)");
            }

            textEditorSetsViewItem.Icon.Visibility = isModified ? Visibility.Visible : Visibility.Collapsed;
            textEditorSetsViewItem.ContextFlyout   = new TabContextFlyout(this, textEditor);

            // Notepads should replace current "Untitled.txt" with open file if it is empty and it is the only tab that has been created.
            // If index != -1, it means set was created after a drag and drop, we should skip this logic
            if (GetNumberOfOpenedTextEditors() == 1 && file != null && atIndex == -1)
            {
                var selectedEditor = GetAllTextEditors().First();
                if (selectedEditor.EditingFile == null && !selectedEditor.IsModified)
                {
                    Sets.Items?.Clear();
                }
            }

            if (atIndex == -1)
            {
                Sets.Items?.Add(textEditorSetsViewItem);
            }
            else
            {
                Sets.Items?.Insert(atIndex, textEditorSetsViewItem);
            }

            if (GetNumberOfOpenedTextEditors() > 1)
            {
                Sets.SelectedItem = textEditorSetsViewItem;
                Sets.ScrollToLastSet();
            }

            return(textEditor);
        }