85893ea709
The first true decomposition of the 4105-line god-object, not another partial shuffle. MainViewModel.Chat.cs 194 -> 44 lines of thin delegation; all chat behavior now lives in a self-contained Services/ChatOverlayLayer.cs (199): - Owns the message buffer (Messages), ChatBoxRenderer, fade + mock-preview timers, per-source preview renders, and the live-output RenderFrame path. - The VM keeps only the binding surface: ChatMessages delegates to _chatLayer.Messages (so XAML ItemsSource + LeftPanel CollectionChanged hold), CanAddYouTubeChat / ShowChatInactiveMessage stay computed on the VM (OnPropertyChanged raised from VM setters + XAML-bound), and thin forwards. - Scenes handed in as args (no Func seam), so the layer owns no scene graph. An AI reading ChatOverlayLayer.cs now sees the entire chat feature in one self-contained unit. Zero behavior change; build 0 warnings; 246 pass, only the 2 known failures. Docs: ViewModels/index.md tracker updated in same commit.
200 lines
8.4 KiB
C#
200 lines
8.4 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Windows;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Threading;
|
|
using ytLive.Helpers;
|
|
using ytLive.Models;
|
|
|
|
namespace ytLive.Services;
|
|
|
|
/// <summary>Owns the YouTube-chat overlay behavior: the message buffer, the
|
|
/// renderer, and the fade/mock-preview timers. The MainViewModel keeps only the
|
|
/// thin binding surface (the shared <see cref="Messages"/> collection + two
|
|
/// computed gate props) and hands the scene list in at each call — this class
|
|
/// owns all chat logic so an AI can read one self-contained unit.</summary>
|
|
public sealed class ChatOverlayLayer
|
|
{
|
|
private readonly ChatBoxRenderer _renderer;
|
|
|
|
// Mock chat messages for the ChatBox preview — branded placeholders so the
|
|
// creator sees where the overlay lands before going live.
|
|
private static readonly IReadOnlyList<ChatMessage> MockChatMessages = new[]
|
|
{
|
|
new ChatMessage { AuthorName = "LlamaCasty", Message = "welcome to the stream!", Timestamp = DateTime.Now.AddMinutes(-4), IsMember = true },
|
|
new ChatMessage { AuthorName = "LlamaCastyFan", Message = "$10 — LlamaCasty is the best!", Timestamp = DateTime.Now.AddMinutes(-3), IsSuperChat = true },
|
|
new ChatMessage { AuthorName = "GamerDude", Message = "what streaming software is this?", Timestamp = DateTime.Now.AddMinutes(-2) },
|
|
new ChatMessage { AuthorName = "LlamaCasty", Message = "thanks for watching! llamacasty.com", Timestamp = DateTime.Now.AddMinutes(-1), IsMember = true },
|
|
new ChatMessage { AuthorName = "LlamaDog", Message = "Subscribe so they can buy the subscription at llamacasty.com!", Timestamp = DateTime.Now, IsMember = true },
|
|
};
|
|
|
|
// Chat fade timer: clears chat overlay after N seconds of inactivity.
|
|
private DispatcherTimer? _fadeTimer;
|
|
private CancellationTokenSource? _mockCts;
|
|
|
|
/// <summary>The live chat message buffer. Shared with the ViewModel (which
|
|
/// binds it to the panel) via <c>ChatMessages => _chatLayer.Messages</c>.</summary>
|
|
public ObservableCollection<ChatMessage> Messages { get; } = new();
|
|
|
|
public ChatOverlayLayer(ChatBoxRenderer renderer) => _renderer = renderer;
|
|
|
|
/// <summary>Ingest a message from the poller. UI-thread marshalled: appended
|
|
/// to the buffer (capped at 500), mock preview stopped, swipe to real
|
|
/// messages, and the fade timer reset. Scenes are handed in so the layer can
|
|
/// refresh every ChatBox preview without owning the scene graph.</summary>
|
|
public void OnMessageReceived(ChatMessage message, IEnumerable<Scene> scenes)
|
|
{
|
|
System.Windows.Application.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
Messages.Add(message);
|
|
if (Messages.Count > 500)
|
|
Messages.RemoveAt(0);
|
|
|
|
// Reset chat fade timer on every message
|
|
ResetChatFadeTimer(scenes);
|
|
|
|
StopMockPreview();
|
|
|
|
// Switch ChatBox previews from mock to real messages
|
|
foreach (var chatBox in scenes.SelectMany(s => s.Elements).OfType<Source>()
|
|
.Where(s => s.Type == SourceType.ChatBox))
|
|
UpdatePreview(chatBox);
|
|
});
|
|
}
|
|
|
|
private void ResetChatFadeTimer(IEnumerable<Scene> scenes)
|
|
{
|
|
_fadeTimer?.Stop();
|
|
var chatBoxes = scenes.SelectMany(s => s.Elements).OfType<Source>()
|
|
.Where(s => s.Type == SourceType.ChatBox && s.ChatFadeSeconds > 0).ToList();
|
|
if (chatBoxes.Count == 0) return;
|
|
|
|
var delay = chatBoxes.Min(s => s.ChatFadeSeconds);
|
|
_fadeTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(delay) };
|
|
_fadeTimer.Tick += (_, _) =>
|
|
{
|
|
_fadeTimer?.Stop();
|
|
_fadeTimer = null;
|
|
Messages.Clear();
|
|
foreach (var chatBox in scenes.SelectMany(s => s.Elements).OfType<Source>()
|
|
.Where(s => s.Type == SourceType.ChatBox))
|
|
UpdatePreview(chatBox);
|
|
StartMockPreview(scenes);
|
|
};
|
|
_fadeTimer.Start();
|
|
}
|
|
|
|
/// <summary>Renders the live-output frame for one ChatBox element (the
|
|
/// compositor path), or null when no messages are buffered yet. Mirrors the
|
|
/// preview render but without writing onto the source's VideoImageSource.</summary>
|
|
public VideoFrame? RenderFrame(Source chatBox)
|
|
{
|
|
if (Messages.Count == 0) return null;
|
|
|
|
var width = (int)Math.Max(1, chatBox.Width);
|
|
var height = (int)Math.Max(1, chatBox.Height);
|
|
|
|
return _renderer.Render(
|
|
Messages,
|
|
width,
|
|
height,
|
|
chatBox.ChatFontSize,
|
|
chatBox.ChatFontFamily,
|
|
chatBox.ChatFontColor,
|
|
chatBox.ChatBackgroundColor,
|
|
chatBox.ChatMaxMessages,
|
|
chatBox.ChatShowTimestamps,
|
|
chatBox.ChatShowBadges,
|
|
chatBox.ChatMemberColor,
|
|
chatBox.ChatSuperChatColor);
|
|
}
|
|
|
|
/// <summary>Renders one ChatBox preview: real messages when connected, mock
|
|
/// when not. Writes the resulting bitmap onto the source's
|
|
/// <c>VideoImageSource</c>.</summary>
|
|
public void UpdatePreview(Source chatBox, IReadOnlyList<ChatMessage>? messages = null)
|
|
{
|
|
try
|
|
{
|
|
messages ??= Messages.Count > 0 ? (IReadOnlyList<ChatMessage>)Messages : MockChatMessages;
|
|
var width = (int)Math.Max(32, chatBox.Width);
|
|
var height = (int)Math.Max(32, chatBox.Height);
|
|
|
|
var frame = _renderer.Render(
|
|
messages,
|
|
width,
|
|
height,
|
|
chatBox.ChatFontSize,
|
|
chatBox.ChatFontFamily,
|
|
chatBox.ChatFontColor,
|
|
chatBox.ChatBackgroundColor,
|
|
chatBox.ChatMaxMessages,
|
|
chatBox.ChatShowTimestamps,
|
|
chatBox.ChatShowBadges,
|
|
chatBox.ChatMemberColor,
|
|
chatBox.ChatSuperChatColor);
|
|
|
|
var bitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null);
|
|
bitmap.WritePixels(new Int32Rect(0, 0, width, height), frame.BgraPixels, frame.Stride, 0);
|
|
chatBox.VideoImageSource = bitmap;
|
|
}
|
|
catch (Exception ex) { AppLog.Write($"UpdateChatBoxPreview failed: {ex.Message}"); }
|
|
}
|
|
|
|
private async Task RunMockPreviewAsync(Source chatBox, CancellationToken ct)
|
|
{
|
|
var n = 0;
|
|
while (!ct.IsCancellationRequested)
|
|
{
|
|
UpdatePreview(chatBox, MockChatMessages.Take(n + 1).ToList());
|
|
await Task.Delay(1000, ct);
|
|
n++;
|
|
if (n == MockChatMessages.Count)
|
|
{
|
|
await Task.Delay(3000, ct);
|
|
n = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void StartMockPreview(IEnumerable<Scene> scenes)
|
|
{
|
|
StopMockPreview();
|
|
_mockCts = new CancellationTokenSource();
|
|
var ct = _mockCts.Token;
|
|
foreach (var chatBox in scenes.SelectMany(s => s.Elements).OfType<Source>()
|
|
.Where(s => s.Type == SourceType.ChatBox && s.ChatPreviewEnabled))
|
|
_ = RunMockPreviewAsync(chatBox, ct);
|
|
}
|
|
|
|
public void StopMockPreview()
|
|
{
|
|
_mockCts?.Cancel();
|
|
_mockCts?.Dispose();
|
|
_mockCts = null;
|
|
}
|
|
|
|
/// <summary>Render previews + wire property-change subscriptions for all
|
|
/// ChatBox sources across every scene. Called after layout load so existing
|
|
/// ChatBox sources show mock content and respond to config changes.</summary>
|
|
public void UpdateConfiguredBoxes(IEnumerable<Scene> scenes)
|
|
{
|
|
foreach (var scene in scenes)
|
|
foreach (var chatBox in scene.Elements.OfType<Source>().Where(s => s.Type == SourceType.ChatBox))
|
|
{
|
|
chatBox.PropertyChanged += (_, e) =>
|
|
{
|
|
if (e.PropertyName is nameof(Source.ChatFontSize) or nameof(Source.ChatFontColor)
|
|
or nameof(Source.ChatBackgroundColor) or nameof(Source.ChatBackgroundOpacity)
|
|
or nameof(Source.ChatMaxMessages) or nameof(Source.ChatShowTimestamps)
|
|
or nameof(Source.ChatShowBadges) or nameof(Source.ChatMemberColor)
|
|
or nameof(Source.ChatSuperChatColor) or nameof(Source.ChatFontFamily)
|
|
or nameof(Source.ChatFadeSeconds) or nameof(Source.Width) or nameof(Source.Height))
|
|
UpdatePreview(chatBox);
|
|
};
|
|
UpdatePreview(chatBox);
|
|
}
|
|
}
|
|
}
|