Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Windows.Input;
- using SharpPad.Commands;
- using SharpPad.WPF.Notepad.Backend;
- namespace SharpPad.WPF.Notepad {
- public class DocumentViewModel : BaseViewModel {
- private string filePath;
- private int caretIndex;
- private int lineIndex;
- public string FilePath {
- get => this.filePath;
- set => this.RaisePropertyChanged(ref this.filePath, value);
- }
- public int CaretIndex {
- get => this.caretIndex;
- private set => this.RaisePropertyChanged(ref this.caretIndex, value);
- }
- public int LineIndex {
- get => this.lineIndex;
- private set => this.RaisePropertyChanged(ref this.lineIndex, value);
- }
- /// <summary>
- /// Gets or sets the document model used as a backend for this document. May be
- /// null, in which case, most document functionality won't work
- /// </summary>
- public ITextDocument DocumentModel { get; set; }
- /// <summary>
- /// Gets the editor model which is used to assist in usage of the document
- /// </summary>
- public ITextEditor Editor { get; private set; }
- public ICommand CloseCommand { get; }
- public DocumentViewModel() {
- this.CloseCommand = new RelayCommand(() => {
- // maybe reference the NotepadviewModel in the document, and remove `this` from the document list?
- // this.Editor?.CloseEditor(this.DocumentModel);
- });
- }
- public void OnDisconnectEditor() {
- if (this.Editor == null) {
- return;
- }
- this.Editor.CaretChanged -= this.OnEditorCaretChanged;
- this.Editor.TextBufferModified -= this.OnTextBufferChanged;
- this.Editor = null;
- }
- public void OnConnectEditor(ITextEditor editor) {
- if (this.Editor == editor)
- return;
- if (this.Editor != null)
- throw new InvalidOperationException("Another editor already connected");
- this.Editor = editor;
- editor.CaretChanged += this.OnEditorCaretChanged;
- editor.TextBufferModified += this.OnTextBufferChanged;
- if (this.DocumentModel == null) {
- this.DocumentModel = editor.NewDocument();
- }
- }
- private void OnEditorCaretChanged(ITextEditor editor) {
- this.RaisePropertyChanged(nameof(this.CaretIndex));
- this.RaisePropertyChanged(nameof(this.LineIndex));
- }
- private void OnTextBufferChanged(ITextEditor editor) {
- this.RaisePropertyChanged(nameof(this.CaretIndex));
- this.RaisePropertyChanged(nameof(this.LineIndex));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement