Advertisement
bobmarley12345

mvvmnotepad

Dec 19th, 2023
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Windows.Input;
  4. using SharpPad.Commands;
  5. using SharpPad.WPF.Notepad.Backend;
  6.  
  7. namespace SharpPad.WPF.Notepad {
  8.     public class DocumentViewModel : BaseViewModel {
  9.         private string filePath;
  10.         private int caretIndex;
  11.         private int lineIndex;
  12.  
  13.         public string FilePath {
  14.             get => this.filePath;
  15.             set => this.RaisePropertyChanged(ref this.filePath, value);
  16.         }
  17.  
  18.         public int CaretIndex {
  19.             get => this.caretIndex;
  20.             private set => this.RaisePropertyChanged(ref this.caretIndex, value);
  21.         }
  22.  
  23.         public int LineIndex {
  24.             get => this.lineIndex;
  25.             private set => this.RaisePropertyChanged(ref this.lineIndex, value);
  26.         }
  27.  
  28.         /// <summary>
  29.         /// Gets or sets the document model used as a backend for this document. May be
  30.         /// null, in which case, most document functionality won't work
  31.         /// </summary>
  32.         public ITextDocument DocumentModel { get; set; }
  33.  
  34.         /// <summary>
  35.         /// Gets the editor model which is used to assist in usage of the document
  36.         /// </summary>
  37.         public ITextEditor Editor { get; private set; }
  38.  
  39.         public ICommand CloseCommand { get; }
  40.  
  41.         public DocumentViewModel() {
  42.             this.CloseCommand = new RelayCommand(() => {
  43.                 // maybe reference the NotepadviewModel in the document, and remove `this` from the document list?
  44.                 // this.Editor?.CloseEditor(this.DocumentModel);
  45.             });
  46.         }
  47.  
  48.         public void OnDisconnectEditor() {
  49.             if (this.Editor == null) {
  50.                 return;
  51.             }
  52.  
  53.             this.Editor.CaretChanged -= this.OnEditorCaretChanged;
  54.             this.Editor.TextBufferModified -= this.OnTextBufferChanged;
  55.             this.Editor = null;
  56.         }
  57.  
  58.         public void OnConnectEditor(ITextEditor editor) {
  59.             if (this.Editor == editor)
  60.                 return;
  61.             if (this.Editor != null)
  62.                 throw new InvalidOperationException("Another editor already connected");
  63.  
  64.             this.Editor = editor;
  65.             editor.CaretChanged += this.OnEditorCaretChanged;
  66.             editor.TextBufferModified += this.OnTextBufferChanged;
  67.             if (this.DocumentModel == null) {
  68.                 this.DocumentModel = editor.NewDocument();
  69.             }
  70.         }
  71.  
  72.         private void OnEditorCaretChanged(ITextEditor editor) {
  73.             this.RaisePropertyChanged(nameof(this.CaretIndex));
  74.             this.RaisePropertyChanged(nameof(this.LineIndex));
  75.         }
  76.  
  77.         private void OnTextBufferChanged(ITextEditor editor) {
  78.             this.RaisePropertyChanged(nameof(this.CaretIndex));
  79.             this.RaisePropertyChanged(nameof(this.LineIndex));
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement