refactor: extract TopBar out of MainWindow.xaml — Phase 2, Commit 14

TopBar.xaml(.cs) takes the Row 0 top controls: logo/About button, REC/ON-AIR
pills + status signs, avatar, Sign In/Start/End buttons. The avatar fetch
(AvatarHttp/_loadedAvatarUrl/LoadAvatarImage/ShowAvatarFallback) moved into the
control with async semantics preserved; the window grabs the control via
FindName in its ctor and calls _topBar.RefreshAvatar() when IsConnected or
AccountAvatarUrl changes (previously OnViewModelPropertyChanged -> LoadAvatarImage).
AccountLight is pure binding (never touched by code-behind) so it moved with the XAML.

Zero behavior change; build 0 warnings; 246 pass, only the 2 known failures.
Also dropped the now-unused System.IO using from MainWindow.xaml.cs.
Docs: ViewModels/index.md Phase 2 tracker updated in the same commit.
This commit is contained in:
2026-08-30 09:13:47 -07:00
parent 691425288f
commit 90f5ad50dd
5 changed files with 197 additions and 165 deletions
+4 -51
View File
@@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Net.Http;
using System.Windows;
using System.Windows.Controls;
@@ -22,6 +21,7 @@ public partial class MainWindow : Window
{
private readonly MainViewModel _viewModel;
private readonly Dictionary<string, Storyboard> _activeBorderAnimations = new();
private Controls.TopBar _topBar = null!;
public MainWindow()
{
@@ -34,8 +34,8 @@ public partial class MainWindow : Window
_viewModel.ChatMessages.CollectionChanged += OnChatMessagesChanged;
UpdateTaskbarOverlay();
UpdateSelectionOverlay();
AvatarImage.Source = null;
AvatarInitial.Visibility = Visibility.Collapsed;
_topBar = (Controls.TopBar)FindName("TopBar")!;
_topBar.RefreshAvatar();
RestoreWindowGeometry();
AppLog.Write("MainWindow ctor: end");
}
@@ -116,7 +116,7 @@ public partial class MainWindow : Window
UpdateSelectionOverlay();
else if (e.PropertyName == nameof(MainViewModel.AccountAvatarUrl)
|| e.PropertyName == nameof(MainViewModel.IsConnected))
LoadAvatarImage();
_topBar.RefreshAvatar();
}
private void MainWindow_Closing(object? sender, CancelEventArgs e)
@@ -141,53 +141,6 @@ public partial class MainWindow : Window
: null;
}
private static readonly HttpClient AvatarHttp = new() { Timeout = TimeSpan.FromSeconds(15) };
private string? _loadedAvatarUrl;
private async void LoadAvatarImage()
{
var url = _viewModel.AccountAvatarUrl;
if (!_viewModel.IsConnected)
{
ShowAvatarFallback(null);
return;
}
if (string.IsNullOrWhiteSpace(url))
{
AppLog.Write("Avatar: connected but no profile image URL on the session");
ShowAvatarFallback(null);
return;
}
if (url == _loadedAvatarUrl && AvatarImage.Source != null) return;
try
{
var bytes = await AvatarHttp.GetByteArrayAsync(url);
using var ms = new MemoryStream(bytes);
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = ms;
bitmap.EndInit();
bitmap.Freeze();
_loadedAvatarUrl = url;
AvatarImage.Source = bitmap;
AvatarInitial.Visibility = Visibility.Collapsed;
}
catch (Exception ex)
{
AppLog.Write($"Avatar load failed ({url}): {ex.Message}");
ShowAvatarFallback(_viewModel.AccountDisplayName);
}
}
private void ShowAvatarFallback(string? name)
{
_loadedAvatarUrl = null;
AvatarImage.Source = null;
AvatarInitial.Text = string.IsNullOrWhiteSpace(name) ? "?" : name.Substring(0, 1).ToUpperInvariant();
AvatarInitial.Visibility = Visibility.Visible;
}
private void SourceNameBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (sender is TextBox { IsVisible: true } box)