Skip to content

Commit 5debde8

Browse files
committed
add: 滚动条位置同步
1 parent 29130cc commit 5debde8

File tree

5 files changed

+116
-34
lines changed

5 files changed

+116
-34
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using LLCOM.Services;
4+
5+
namespace LLCOM.Models;
6+
7+
public class TerminalCache(int MaxCacheLines)
8+
{
9+
//展示画面变化时的事件
10+
public EventHandler? TerminalChangedEvent { get; set; }
11+
12+
13+
//MaxCacheLines表示终端缓存的行数,超过这个行数后会删除最上面的行
14+
private readonly int MaxCacheLines = Utils.Setting.TerminalBufferLines;
15+
16+
//可视范围内的宽高
17+
public int WindowWidth { get; set; }
18+
public int WindowHeight { get; set; }
19+
20+
//用于存放终端数据的缓存
21+
private List<List<TerminalBlock>> CacheLines { get; set; } = new();
22+
23+
//当前所在的行数相比较于终端最底部的行数,0表示在最底部,其余数字表示向上挪动的行数
24+
private int _currentLine = 0;
25+
26+
private int CurrentLine
27+
{
28+
get => _currentLine;
29+
set
30+
{
31+
_currentLine = value;
32+
TerminalChangedEvent?.Invoke(this, EventArgs.Empty);
33+
}
34+
}
35+
}

llcomNext/LLCOM/Services/Setting.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ public FontFamily? TerminalFontFamily
140140
//终端模式字体名称
141141
[ObservableProperty]
142142
private string _terminalFont = Database.Get(nameof(TerminalFont), "").Result;
143-
//终端模式的缓冲区行数
143+
//终端模式的缓冲区行数,重启后生效
144144
[ObservableProperty]
145-
private int _terminalBufferLines = Database.Get(nameof(TerminalBufferLines), 5000).Result;
145+
private int _terminalBufferLines = Database.Get(nameof(TerminalBufferLines), 9000).Result;
146146
//终端模式的字体大小
147147
private int _terminalFontSize = Database.Get(nameof(TerminalFontSize), 16).Result;
148148
[Range(14,50)]

llcomNext/LLCOM/Services/Utils.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public static (int, int) CalculateSize(double width, double height, string font,
229229
TextSize = (float)fontSize
230230
};
231231

232-
// 使用二分法计算出一行能放下多少个字符
232+
// 使用二分法计算出一行能放下多少个字符
233233
int columns = 1;
234234
int low = 1, high = 9999;
235235
char testChar = 'W'; // 测试字符
@@ -253,8 +253,12 @@ public static (int, int) CalculateSize(double width, double height, string font,
253253
int rows = (int)(height / charHeight);
254254

255255
//注意不要让行数和列数为0
256-
if (rows <= 0) rows = 1;
257-
if (columns <= 0) columns = 1;
256+
var minRows = 1;
257+
var minColumns = 20;
258+
if (rows < minRows)
259+
rows = minRows;
260+
if (columns < minColumns)
261+
columns = minColumns;
258262

259263
//返回
260264
return (columns, rows);

llcomNext/LLCOM/Views/DataViews/TerminalView.axaml

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,44 @@
1515
<Design.DataContext>
1616
<vm:TerminalViewModel />
1717
</Design.DataContext>
18-
<!--
19-
目前不打算支持操作光标位置、退格
20-
终端只负责不断打印新数据
21-
-->
22-
<Panel
23-
Name="MainArea"
24-
Margin="5"
25-
HorizontalAlignment="Stretch"
26-
VerticalAlignment="Stretch"
27-
PointerWheelChanged="MainArea_OnPointerWheelChanged">
28-
<SelectableTextBlock
29-
Name="MainTextBlock"
30-
Background="Transparent"
31-
FontFamily="{Binding TerminalFontFamily, Source={x:Static services:Utils.Setting}}"
32-
FontSize="{Binding TerminalFontSize, Source={x:Static services:Utils.Setting}}"
33-
SelectionBrush="{Binding TerminalTheme.SelectionBackground, Source={x:Static services:Utils.Setting}}"
34-
SelectionForegroundBrush="{Binding TerminalTheme.Background, Source={x:Static services:Utils.Setting}}" />
35-
<ScrollBar
36-
Name="MainScrollBar"
37-
HorizontalAlignment="Right"
38-
Scroll="MainScrollBar_OnScroll"
39-
Visibility="Visible" />
40-
</Panel>
18+
<Grid RowDefinitions="auto *">
19+
<Border
20+
Grid.Row="0"
21+
BorderBrush="{DynamicResource SemiColorBorder}"
22+
BorderThickness="0,0,0,1">
23+
<StackPanel
24+
Margin="5"
25+
HorizontalAlignment="Right"
26+
Orientation="Horizontal"
27+
Spacing="8">
28+
<TextBlock VerticalAlignment="Center" Text="选择数据输入源" />
29+
<ComboBox Width="200" SelectedIndex="0">
30+
<ComboBoxItem Content="串口1" />
31+
</ComboBox>
32+
<Button Content="清空数据" />
33+
</StackPanel>
34+
</Border>
35+
<Panel
36+
Name="MainArea"
37+
Grid.Row="1"
38+
Margin="5"
39+
HorizontalAlignment="Stretch"
40+
VerticalAlignment="Stretch"
41+
PointerWheelChanged="MainArea_OnPointerWheelChanged">
42+
<SelectableTextBlock
43+
Name="MainTextBlock"
44+
Background="Transparent"
45+
FontFamily="{Binding TerminalFontFamily, Source={x:Static services:Utils.Setting}}"
46+
FontSize="{Binding TerminalFontSize, Source={x:Static services:Utils.Setting}}"
47+
SelectionBrush="{Binding TerminalTheme.SelectionBackground, Source={x:Static services:Utils.Setting}}"
48+
SelectionForegroundBrush="{Binding TerminalTheme.Background, Source={x:Static services:Utils.Setting}}" />
49+
<ScrollBar
50+
Name="MainScrollBar"
51+
HorizontalAlignment="Right"
52+
AllowAutoHide="False"
53+
Scroll="MainScrollBar_OnScroll"
54+
Visibility="Visible"
55+
Value="100" />
56+
</Panel>
57+
</Grid>
4158
</UserControl>

llcomNext/LLCOM/Views/DataViews/TerminalView.axaml.cs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,15 @@ public TerminalView()
2323
{
2424
InitializeComponent();
2525
MainArea.PropertyChanged += MainArea_PropertyChanged;
26+
Debug.WriteLine("TerminalView initialized.");
2627
}
2728

29+
//被销毁后的事件
30+
protected override void OnUnloaded(RoutedEventArgs e)
31+
{
32+
base.OnUnloaded(e);
33+
Debug.WriteLine("TerminalView unloaded.");
34+
}
2835

2936
private void Control_OnLoaded(object? sender, RoutedEventArgs e)
3037
{
@@ -37,11 +44,6 @@ private void Control_OnLoaded(object? sender, RoutedEventArgs e)
3744
(WindowWidth, WindowHeight) = Utils.CalculateSize(
3845
MainArea.Bounds.Width, MainArea.Bounds.Height,
3946
Utils.Setting.TerminalFont, Utils.Setting.TerminalFontSize);
40-
//TEST
41-
// var line = new List<TerminalBlock>();
42-
// line.Add(new TerminalBlock(new string('A',WindowWidth), 0, 0, false, false, false));
43-
// AddLine(line);
44-
//TEST END
4547
RefreshText();
4648
});
4749
};
@@ -63,7 +65,17 @@ private void Control_OnLoaded(object? sender, RoutedEventArgs e)
6365
//用于存放终端数据的缓存
6466
private List<List<TerminalBlock>> CacheLines { get; set; } = new();
6567
//当前所在的行数相比较于终端最底部的行数,0表示在最底部,其余数字表示向上挪动的行数
66-
private int CurrentLine { get; set; } = 0;
68+
private int _currentLine = 0;
69+
70+
private int CurrentLine
71+
{
72+
get => _currentLine;
73+
set
74+
{
75+
_currentLine = value;
76+
RefreshScrollBar();
77+
}
78+
}
6779

6880
private void AddLine(List<TerminalBlock> line)
6981
{
@@ -153,10 +165,24 @@ private void RefreshText()
153165
}
154166
}
155167

168+
//更新滚动条的位置
169+
private void RefreshScrollBar()
170+
{
171+
if(_currentLine == 0 || CacheLines.Count < WindowHeight)
172+
MainScrollBar.Value = 100;
173+
else
174+
MainScrollBar.Value = 100.0 - (double)_currentLine / (CacheLines.Count - WindowHeight) * 100.0;
175+
}
156176
private void MainScrollBar_OnScroll(object? sender, ScrollEventArgs e)
157177
{
158178
var value = e.NewValue;
159179
//计算出要显示的行数范围
160180
//TODO)) 还要关联上CurrentLine的变化
181+
if(Math.Abs(value - 100.0) < 0.001 || CacheLines.Count < WindowHeight)
182+
_currentLine = 0;
183+
else
184+
_currentLine = (int)(CacheLines.Count - WindowHeight - value * (CacheLines.Count - WindowHeight) / 100.0);
185+
//刷新文本
186+
RefreshText();
161187
}
162188
}

0 commit comments

Comments
 (0)