Skip to content

Commit 1394415

Browse files
committed
refactor(panel): TaskBarElement subscribes to window events
Replace push-based MarkDirty() calls in WindowStateService with event-driven approach. TaskBarElement now subscribes to WindowCreated, WindowClosed, WindowActivated, and WindowStateChanged events from WindowStateService, calling Invalidate() on change.
1 parent 90d9703 commit 1394415

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

SharpConsoleUI/Core/WindowStateService.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -931,9 +931,6 @@ public Window AddWindow(Window window, bool activateWindow = true)
931931

932932
window.WindowIsAdded();
933933

934-
// Notify panels that window list changed (updates taskbar, etc.)
935-
context.PanelStateService.MarkDirty();
936-
937934
_logService?.LogDebug($"Window added successfully: {window.Title}", "Window");
938935
return window;
939936
}
@@ -1042,9 +1039,6 @@ public bool CloseWindow(Window? window, bool activateParent = true, bool force =
10421039
// STEP 3: Complete the close (fire OnClosed, dispose controls)
10431040
window.CompleteClose();
10441041

1045-
// Notify panels that window list changed (updates taskbar, etc.)
1046-
context.PanelStateService.MarkDirty();
1047-
10481042
// Clear only the closed window's area (not entire screen!)
10491043
if (_renderer != null && _consoleDriver != null)
10501044
{

SharpConsoleUI/Panel/Elements/TaskBarElement.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using SharpConsoleUI.Core;
12
using SharpConsoleUI.Helpers;
23
using SharpConsoleUI.Layout;
34
using 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>
1012
public 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

Comments
 (0)