Skip to content

Commit 5dea150

Browse files
committed
Merge commit 'b134b2b5c4564cdb92187d469d8f14324e56d1de'
2 parents c762cb3 + b134b2b commit 5dea150

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

Blog/RallreakechuFeakenalldea/RallreakechuFeakenalldea.Desktop/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"RallreakechuFeakenalldea.Desktop": {
44
"commandName": "Project",
5-
"commandLineArgs": "C:\\lindexi\\TestFile_GBK.txt"
5+
"commandLineArgs": "C:\\lindexi\\TestFile.txt"
66
}
77
}
88
}

Blog/RallreakechuFeakenalldea/SimpleWrite/ViewModels/StatusViewModel.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using SimpleWrite.Models;
22

33
using System;
4+
using Avalonia.Threading;
45

56
namespace SimpleWrite.ViewModels;
67

@@ -42,7 +43,12 @@ public string StatusText
4243
public void SetCurrentCaretInfoText(string currentCaretInfoText)
4344
{
4445
_currentCaretInfoText = currentCaretInfoText;
45-
OnPropertyChanged(nameof(StatusText));
46+
47+
// 放在后台执行,防止在主线程访问 Render 之后,执行强行渲染导致布局完成更新光标信息,从而触发 Avalonia 已知问题: 禁止在 Render 时执行 InvalidVisual 方法
48+
_ = Dispatcher.UIThread.InvokeAsync(() =>
49+
{
50+
OnPropertyChanged(nameof(StatusText));
51+
}, DispatcherPriority.Background);
4652
}
4753

4854
private string? _currentCaretInfoText;

Blog/RallreakechuFeakenalldea/SimpleWrite/Views/Components/MainEditorView.axaml.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ private void UpdateCurrentEditorMode(EditorModel editorModel)
7474
CurrentTextEditor = textEditor;
7575
}
7676

77-
78-
7977
private TextEditor _currentTextEditor = null!;
8078

8179
public TextEditor CurrentTextEditor

Blog/RallreakechuFeakenalldea/SimpleWrite/Views/Components/StatusBar.axaml.cs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,20 @@ private void UpdateTextEditor(TextEditor textEditor)
5454
{
5555
_currentTextEditor = textEditor;
5656

57+
_currentTextEditor.CurrentSelectionChanged -= CurrentTextEditor_CurrentSelectionChanged;
5758
_currentTextEditor.CurrentSelectionChanged += CurrentTextEditor_CurrentSelectionChanged;
59+
60+
_currentTextEditor.LayoutCompleted -=
61+
OnLayoutCompleted;
62+
_currentTextEditor.LayoutCompleted +=
63+
OnLayoutCompleted;
64+
5865
SetCurrentCaretInfoText(textEditor.CurrentSelection);
66+
67+
void OnLayoutCompleted(object? sender, LayoutCompletedEventArgs e)
68+
{
69+
SetCurrentCaretInfoText(textEditor.CurrentSelection);
70+
}
5971
}
6072

6173
private void CurrentTextEditor_CurrentSelectionChanged(object? sender, TextEditorValueChangeEventArgs<Selection> e)
@@ -66,25 +78,39 @@ private void CurrentTextEditor_CurrentSelectionChanged(object? sender, TextEdito
6678

6779
private void SetCurrentCaretInfoText(in Selection selection)
6880
{
81+
string caretInfo;
82+
6983
if (selection.IsEmpty)
7084
{
7185
CaretOffset caretOffset = selection.StartOffset;
7286
if (caretOffset.IsAtLineStart)
7387
{
74-
ViewModel.SetCurrentCaretInfoText($"光标位置: {caretOffset.Offset} 处于行首");
88+
caretInfo = $"光标位置: {caretOffset.Offset} 处于行首";
7589
}
7690
else
7791
{
78-
ViewModel.SetCurrentCaretInfoText($"光标位置: {caretOffset.Offset}");
92+
caretInfo = $"光标位置: {caretOffset.Offset}";
7993
}
8094
}
8195
else
8296
{
83-
ViewModel.SetCurrentCaretInfoText
84-
(
85-
$"选择范围: {selection.StartOffset.Offset} - {selection.EndOffset.Offset} 长度: {selection.Length}"
86-
);
97+
caretInfo =
98+
$"选择范围: {selection.StartOffset.Offset} - {selection.EndOffset.Offset} 长度: {selection.Length}";
8799
}
100+
101+
// 添加段落信息
102+
if (_currentTextEditor != null)
103+
{
104+
if (_currentTextEditor.TextEditorCore.TryGetRenderInfo(out var renderInfoProvider))
105+
{
106+
CaretOffset caretOffset = selection.StartOffset;
107+
var caretRenderInfo = renderInfoProvider.GetCaretRenderInfo(caretOffset);
108+
109+
caretInfo += $" 第 {caretRenderInfo.ParagraphIndex.Index}{caretRenderInfo.LineIndex} 行,行内第 {caretRenderInfo.HitLineCharOffset.Offset} 个字符";
110+
}
111+
}
112+
113+
ViewModel.SetCurrentCaretInfoText(caretInfo);
88114
}
89115

90116
private TextEditor? _currentTextEditor;

0 commit comments

Comments
 (0)