@@ -20,6 +20,10 @@ namespace LLCOM.Views;
2020
2121public partial class TerminalView : UserControl
2222{
23+ private DispatcherTimer _updateTimer ;
24+ private bool _dataChanged = false ;
25+ readonly List < List < TerminalBlock > > _terminalBlocks = [ ] ;
26+
2327 public TerminalView ( )
2428 {
2529 InitializeComponent ( ) ;
@@ -34,11 +38,49 @@ protected override void OnUnloaded(RoutedEventArgs e)
3438 MainArea . PropertyChanged -= MainArea_PropertyChanged ;
3539 Utils . Setting . TerminalChangedEvent -= TerminalChangedEvent ;
3640 ( ( TerminalViewModel ) DataContext ! ) . TerminalChangedEvent -= TerminalChangedEvent ;
41+ _updateTimer . Stop ( ) ;
3742 Debug . WriteLine ( "TerminalView unloaded." ) ;
3843 }
3944
4045 private void Control_OnLoaded ( object ? sender , RoutedEventArgs e )
4146 {
47+ //搞个定时器来更新UI,防止阻塞
48+ _updateTimer = new DispatcherTimer
49+ {
50+ Interval = TimeSpan . FromMilliseconds ( 100 )
51+ } ;
52+ _updateTimer . Tick += ( s , e ) =>
53+ {
54+ if ( _dataChanged )
55+ {
56+ lock ( _terminalBlocks )
57+ {
58+ // 执行更新逻辑
59+ //清空文本
60+ MainTextBlock . Inlines ! . Clear ( ) ;
61+ foreach ( var line in _terminalBlocks )
62+ {
63+ foreach ( var block in line )
64+ {
65+ //添加块
66+ var run = new Run ( block . Text )
67+ {
68+ [ ! Span . ForegroundProperty ] = new Binding ( block . ForegroundBindingName ) { Source = Utils . Setting } ,
69+ [ ! Span . BackgroundProperty ] = new Binding ( block . BackgroundBindingName ) { Source = Utils . Setting } ,
70+ FontWeight = block . IsBold ? FontWeight . Bold : FontWeight . Normal ,
71+ FontStyle = block . IsItalic ? FontStyle . Italic : FontStyle . Normal ,
72+ TextDecorations = block . IsUnderLine ? TextDecorations . Underline : null ,
73+ } ;
74+ MainTextBlock . Inlines . Add ( run ) ;
75+ }
76+ MainTextBlock . Inlines . Add ( new LineBreak ( ) ) ;
77+ }
78+ }
79+ _dataChanged = false ; // 重置标记
80+ }
81+ } ;
82+ _updateTimer . Start ( ) ;
83+
4284 Utils . Setting . TerminalChangedEvent += TerminalChangedEvent ;
4385 ( ( TerminalViewModel ) DataContext ! ) . TerminalChangedEvent += TerminalChangedEvent ;
4486 //加载完触发一次,顺便初始化窗口大小数据
@@ -80,27 +122,19 @@ private void MainScrollBar_OnScroll(object? sender, ScrollEventArgs e)
80122
81123 private void TerminalChangedEvent ( object ? sender , List < List < TerminalBlock > > e )
82124 {
83- Dispatcher . UIThread . Invoke ( ( ) =>
125+ lock ( _terminalBlocks )
84126 {
85- //清空文本
86- MainTextBlock . Inlines ! . Clear ( ) ;
127+ _terminalBlocks . Clear ( ) ;
87128 foreach ( var line in e )
88129 {
130+ var newLine = new List < TerminalBlock > ( ) ;
89131 foreach ( var block in line )
90132 {
91- //添加块
92- var run = new Run ( block . Text )
93- {
94- [ ! Span . ForegroundProperty ] = new Binding ( block . ForegroundBindingName ) { Source = Utils . Setting } ,
95- [ ! Span . BackgroundProperty ] = new Binding ( block . BackgroundBindingName ) { Source = Utils . Setting } ,
96- FontWeight = block . IsBold ? FontWeight . Bold : FontWeight . Normal ,
97- FontStyle = block . IsItalic ? FontStyle . Italic : FontStyle . Normal ,
98- TextDecorations = block . IsUnderLine ? TextDecorations . Underline : null ,
99- } ;
100- MainTextBlock . Inlines . Add ( run ) ;
133+ newLine . Add ( ( TerminalBlock ) block . Clone ( ) ) ;
101134 }
102- MainTextBlock . Inlines . Add ( new LineBreak ( ) ) ;
135+ _terminalBlocks . Add ( newLine ) ;
103136 }
104- } ) ;
137+ }
138+ _dataChanged = true ;
105139 }
106140}
0 commit comments