@@ -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