84 lines
2.3 KiB
C#
84 lines
2.3 KiB
C#
using Qrakhen.Qamp.Editor.Commands;
|
|
using Qrakhen.Qamp.Editor.Primitives;
|
|
using Qrakhen.Qamp.Memory;
|
|
using System.Collections.ObjectModel;
|
|
using System.Windows.Input;
|
|
|
|
namespace Qrakhen.Qamp.Editor.ViewModel;
|
|
|
|
public class EditorFrameViewModel : ObservableObject
|
|
{
|
|
private CursorPosition _cursorPosition;
|
|
public CursorPosition CursorPosition {
|
|
get => _cursorPosition;
|
|
set => SetProperty(ref _cursorPosition, value);
|
|
}
|
|
|
|
public int CursorLine {
|
|
get => _cursorPosition.Line;
|
|
set => SetProperty(ref _cursorPosition.Line, value);
|
|
}
|
|
|
|
public int CursorColumn {
|
|
get => _cursorPosition.Column;
|
|
set => SetProperty(ref _cursorPosition.Column, value);
|
|
}
|
|
|
|
public LineBuffer CurrentLineBuffer => Lines[CursorLine];
|
|
|
|
public ICommand MoveCaretCommand { get; }
|
|
|
|
public ObservableCollection<LineBuffer> Lines { get; private set; } = new() {
|
|
new LineBuffer("test"),
|
|
new LineBuffer(" is very good;"),
|
|
new LineBuffer(" jaja();"),
|
|
new LineBuffer("}")
|
|
};
|
|
|
|
public EditorFrameViewModel()
|
|
{
|
|
MoveCaretCommand = new RelayCommand(ExecuteMoveCaret, CanMoveCaret);
|
|
}
|
|
|
|
public void SetCursor(int line, int column)
|
|
{
|
|
line = line < 0 ? 0 : line >= Lines.Count ? Lines.Count - 1 : line;
|
|
LineBuffer buffer = Lines[line];
|
|
column = column < 0 ? 0 : column > buffer.Tail ? buffer.Tail : column;
|
|
CursorPosition = new(line, column);
|
|
}
|
|
|
|
public void MoveCursor(int lines, int columns)
|
|
{
|
|
SetCursor(CursorLine + lines, CursorColumn + columns);
|
|
OnPropertyChanged(nameof(CurrentLineBuffer));
|
|
}
|
|
|
|
public bool CanMoveCaret(object? relativeUpdate)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public void ExecuteMoveCaret(object? relativeUpdate)
|
|
{
|
|
if (relativeUpdate is CursorPosition pos)
|
|
MoveCursor(pos.Line, pos.Column);
|
|
}
|
|
|
|
public void Delete(int count = 1, bool follow = true)
|
|
{
|
|
if (follow)
|
|
MoveCursor(0, -count);
|
|
CurrentLineBuffer.Delete(CursorColumn, count);
|
|
OnPropertyChanged(nameof(Lines));
|
|
}
|
|
|
|
public void Insert(string data, bool follow = true)
|
|
{
|
|
CurrentLineBuffer.Insert(CursorColumn, data);
|
|
if (follow)
|
|
MoveCursor(0, data.Length);
|
|
OnPropertyChanged(nameof(Lines));
|
|
}
|
|
}
|