Skip to content

Commit f9aecef

Browse files
committed
add: 拆分终端代码,方便维护
1 parent 5debde8 commit f9aecef

File tree

8 files changed

+236
-184
lines changed

8 files changed

+236
-184
lines changed

llcomNext/LLCOM/Models/TerminalCache.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using CommunityToolkit.Mvvm.ComponentModel;
4+
using LLCOM.Services;
5+
6+
namespace LLCOM.Models;
7+
8+
public class TerminalObject
9+
{
10+
public TerminalObject()
11+
{
12+
13+
}
14+
15+
//展示画面变化时的事件
16+
public EventHandler<List<List<TerminalBlock>>>? TerminalChangedEvent { get; set; }
17+
private void TerminalChanged()
18+
{
19+
if(TerminalChangedEvent is null)
20+
return;
21+
//触发更新事件
22+
TerminalChangedEvent?.Invoke(this, GetShowLines());
23+
}
24+
25+
/// <summary>
26+
/// 获取可以显示的行数据
27+
/// </summary>
28+
/// <returns>一行行的数据</returns>
29+
public List<List<TerminalBlock>> GetShowLines()
30+
{
31+
List<List<TerminalBlock>> cacheLines = new();
32+
33+
//计算出要显示的行数范围
34+
int allLines = _cacheLines.Count;
35+
//起始行和结束行,闭区间,从0开始,代表CacheLines的项目下标
36+
int startLine = allLines - _windowHeight - _currentLine;
37+
if (startLine < 0)
38+
startLine = 0;
39+
int endLine = startLine + _windowHeight - 1;
40+
if (endLine >= allLines)
41+
endLine = allLines - 1;
42+
43+
//添加行
44+
for (int i = startLine; i <= endLine; i++)
45+
{
46+
//添加行
47+
var line = _cacheLines[i];
48+
cacheLines.Add(line);
49+
}
50+
51+
return cacheLines;
52+
}
53+
54+
//MaxCacheLines表示终端缓存的行数,超过这个行数后会删除最上面的行
55+
private readonly int _maxCacheLines = Utils.Setting.TerminalBufferLines;
56+
57+
//可视范围内的宽高
58+
private int _windowWidth;
59+
private int _windowHeight;
60+
//窗口大小变化
61+
public void ChangeWindowSize(int width, int height)
62+
{
63+
_windowWidth = width;
64+
_windowHeight = height;
65+
TerminalChanged();
66+
}
67+
68+
//用于存放终端数据的缓存
69+
private readonly List<List<TerminalBlock>> _cacheLines = new();
70+
71+
//当前所在的行数相比较于终端最底部的行数,0表示在最底部,其余数字表示向上挪动的行数
72+
private int _currentLine = 0;
73+
74+
private double CurrentLine2ScrollValue =>
75+
(_currentLine == 0 || _cacheLines.Count < _windowHeight)
76+
? 100
77+
: 100.0 - (double)_currentLine / (_cacheLines.Count - _windowHeight) * 100.0;
78+
//向上移动的行数
79+
public double CurrentLineMoveUp(int delta)
80+
{
81+
var lastCurrentLine = _currentLine;
82+
_currentLine += delta;
83+
if(_currentLine > _cacheLines.Count - _windowHeight)
84+
_currentLine = _cacheLines.Count - _windowHeight;
85+
if(_currentLine < 0)
86+
_currentLine = 0;
87+
88+
if(lastCurrentLine != _currentLine)
89+
TerminalChanged();
90+
91+
return CurrentLine2ScrollValue;
92+
}
93+
//滚轮事件
94+
public void ScrollBarChanged(double value)
95+
{
96+
var lastCurrentLine = _currentLine;
97+
if(Math.Abs(value - 100.0) < 0.001 || _cacheLines.Count < _windowHeight)
98+
_currentLine = 0;
99+
else
100+
_currentLine = (int)(_cacheLines.Count - _windowHeight - value * (_cacheLines.Count - _windowHeight) / 100.0);
101+
102+
if(lastCurrentLine != _currentLine)
103+
TerminalChanged();
104+
}
105+
106+
107+
//TODO)) 仅供测试使用
108+
public void AddLine(List<TerminalBlock> line)
109+
{
110+
//添加行
111+
_cacheLines.Add(line);
112+
//如果超过了最大行数,删除最上面的行
113+
if (_cacheLines.Count > _maxCacheLines)
114+
_cacheLines.RemoveAt(0);
115+
if (_currentLine != 0)
116+
{
117+
_currentLine--;
118+
//如果当前行数超过了最大行数,设置为最大行数
119+
if (_currentLine > _cacheLines.Count - _windowHeight)
120+
_currentLine = _cacheLines.Count - _windowHeight;
121+
}
122+
123+
//更新数据
124+
TerminalChanged();
125+
}
126+
}

llcomNext/LLCOM/Services/Setting.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,25 +140,16 @@ public FontFamily? TerminalFontFamily
140140
//终端模式字体名称
141141
[ObservableProperty]
142142
private string _terminalFont = Database.Get(nameof(TerminalFont), "").Result;
143-
//终端模式的缓冲区行数,重启后生效
144-
[ObservableProperty]
145-
private int _terminalBufferLines = Database.Get(nameof(TerminalBufferLines), 9000).Result;
146143
//终端模式的字体大小
147-
private int _terminalFontSize = Database.Get(nameof(TerminalFontSize), 16).Result;
148-
[Range(14,50)]
149-
public int TerminalFontSize
150-
{
151-
get => _terminalFontSize;
152-
set
153-
{
154-
_terminalFontSize = value;
155-
OnPropertyChanged();
156-
TerminalChangedEvent?.Invoke(this, EventArgs.Empty);
157-
}
158-
}
144+
[ObservableProperty]
145+
private double _terminalFontSize = Database.Get(nameof(TerminalFontSize), 14).Result;
159146
//用于通知字体大小改变的事件
160147
public EventHandler? TerminalChangedEvent = null;
161-
partial void OnTerminalFontChanged(string _) => TerminalChangedEvent?.Invoke(this, EventArgs.Empty);
148+
partial void OnTerminalFontChanged(string s) => TerminalChangedEvent?.Invoke(this, EventArgs.Empty);
149+
partial void OnTerminalFontSizeChanged(double n) => TerminalChangedEvent?.Invoke(this, EventArgs.Empty);
150+
//终端模式的缓冲区行数,重启后生效
151+
[ObservableProperty]
152+
private int _terminalBufferLines = Database.Get(nameof(TerminalBufferLines), 9000).Result;
162153

163154

164155
//终端模式的主题

llcomNext/LLCOM/Services/Utils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,12 @@ public static (int, int) CalculateSize(double width, double height, string font,
248248
}
249249
}
250250

251-
//计算出能放下多少行 TODO)) 待修正
251+
//计算出能放下多少行
252252
var charHeight = paint.FontSpacing;
253253
int rows = (int)(height / charHeight);
254254

255255
//注意不要让行数和列数为0
256-
var minRows = 1;
256+
var minRows = 2;
257257
var minColumns = 20;
258258
if (rows < minRows)
259259
rows = minRows;

llcomNext/LLCOM/ViewModels/DataViews/TerminalViewModel.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,34 @@ public TerminalViewModel() {}
1919
public TerminalViewModel(Func<Type, ViewModelBase> getService)
2020
{
2121
_getService = getService;
22+
23+
TerminalObject.TerminalChangedEvent += (sender, args) =>
24+
{
25+
if(TerminalChangedEvent == null)
26+
return;
27+
//更新数据
28+
TerminalChangedEvent?.Invoke(this, args);
29+
};
30+
31+
for(int i=0;i<200;i++)
32+
{
33+
var line = new List<TerminalBlock>();
34+
line.Add(new TerminalBlock($"test string {i} line.", 0, 0, false, false, false));
35+
line.Add(new TerminalBlock($"balabala", 0, 31, false, false, false));
36+
TerminalObject.AddLine(line);
37+
}
2238
}
39+
40+
//终端对象,后续需要为每项操作加锁 TODO))
41+
public readonly TerminalObject TerminalObject = new TerminalObject();
42+
43+
//窗口大小变化
44+
public void ChangeWindowSize((int,int) size) => TerminalObject.ChangeWindowSize(size.Item1, size.Item2);
45+
46+
//滚轮事件
47+
public double MoveUp(int delta) => TerminalObject.CurrentLineMoveUp(delta);
48+
//滚动条变化
49+
public void ScrollBarChanged(double value) => TerminalObject.ScrollBarChanged(value);
50+
//接管更新事件
51+
public EventHandler<List<List<TerminalBlock>>>? TerminalChangedEvent;
2352
}

llcomNext/LLCOM/Views/DataViews/TerminalView.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
d:DesignHeight="757"
1010
d:DesignWidth="762"
1111
x:DataType="vm:TerminalViewModel"
12-
Background="{Binding TerminalTheme.Background, Source={x:Static services:Utils.Setting}}"
1312
Loaded="Control_OnLoaded"
1413
mc:Ignorable="d">
1514
<Design.DataContext>
@@ -35,12 +34,13 @@
3534
<Panel
3635
Name="MainArea"
3736
Grid.Row="1"
38-
Margin="5"
3937
HorizontalAlignment="Stretch"
4038
VerticalAlignment="Stretch"
39+
Background="{Binding TerminalTheme.Background, Source={x:Static services:Utils.Setting}}"
4140
PointerWheelChanged="MainArea_OnPointerWheelChanged">
4241
<SelectableTextBlock
4342
Name="MainTextBlock"
43+
Margin="5"
4444
Background="Transparent"
4545
FontFamily="{Binding TerminalFontFamily, Source={x:Static services:Utils.Setting}}"
4646
FontSize="{Binding TerminalFontSize, Source={x:Static services:Utils.Setting}}"

0 commit comments

Comments
 (0)