1+ using SharpConsoleUI . Core ;
12using SharpConsoleUI . Helpers ;
23using SharpConsoleUI . Layout ;
34using SharpConsoleUI . Parsing ;
@@ -6,13 +7,15 @@ namespace SharpConsoleUI.Panel;
67
78/// <summary>
89/// A panel element that displays a clickable list of windows (task bar).
10+ /// Subscribes to WindowStateService events for automatic updates.
911/// </summary>
1012public class TaskBarElement : PanelElement
1113{
1214 private const int MaxTitleLength = 15 ;
1315 private const int TitleEllipsisLength = 7 ;
1416 private List < ( Window window , int startX , int endX ) > _windowPositions = new ( ) ;
1517 private int _lastStateHash ;
18+ private WindowStateService ? _subscribedService ;
1619
1720 /// <summary>
1821 /// Initializes a new TaskBarElement.
@@ -47,6 +50,8 @@ public override void Render(CharacterBuffer buffer, int x, int y, int width, Col
4750 if ( WindowSystem == null || width <= 0 )
4851 return ;
4952
53+ EnsureSubscribed ( ) ;
54+
5055 _windowPositions . Clear ( ) ;
5156
5257 // Get top-level taskbar windows sorted by creation order
@@ -144,6 +149,36 @@ public override bool ProcessMouseEvent(Events.MouseEventArgs args, int elementX,
144149 return false ;
145150 }
146151
152+ private void EnsureSubscribed ( )
153+ {
154+ var service = WindowSystem ? . WindowStateService ;
155+ if ( service == null || service == _subscribedService )
156+ return ;
157+
158+ Unsubscribe ( ) ;
159+ _subscribedService = service ;
160+ service . WindowCreated += OnWindowChanged ;
161+ service . WindowClosed += OnWindowChanged ;
162+ service . WindowActivated += OnWindowActivated ;
163+ service . WindowStateChanged += OnWindowStateChanged ;
164+ }
165+
166+ private void Unsubscribe ( )
167+ {
168+ if ( _subscribedService != null )
169+ {
170+ _subscribedService . WindowCreated -= OnWindowChanged ;
171+ _subscribedService . WindowClosed -= OnWindowChanged ;
172+ _subscribedService . WindowActivated -= OnWindowActivated ;
173+ _subscribedService . WindowStateChanged -= OnWindowStateChanged ;
174+ _subscribedService = null ;
175+ }
176+ }
177+
178+ private void OnWindowChanged ( object ? sender , WindowEventArgs e ) => Invalidate ( ) ;
179+ private void OnWindowActivated ( object ? sender , WindowActivatedEventArgs e ) => Invalidate ( ) ;
180+ private void OnWindowStateChanged ( object ? sender , WindowStateEventArgs e ) => Invalidate ( ) ;
181+
147182 private static int ComputeStateHash ( List < Window > windows )
148183 {
149184 unchecked
0 commit comments