Skip to content

Commit c3cffd6

Browse files
authored
Fix log text color for dark mode and live theme switch (#5060)
1 parent dba3937 commit c3cffd6

6 files changed

Lines changed: 93 additions & 32 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Avalonia;
2+
using Avalonia.Platform;
3+
using Avalonia.Styling;
4+
5+
namespace UniGetUI.Avalonia.Infrastructure;
6+
7+
// Reliable theme resolution for code that bakes colors in C# (log/output views). ActualThemeVariant
8+
// can read back as Default before it settles; resolve that against the OS theme so a resource lookup
9+
// never falls back to the light-theme (near-black) value on a dark background (#5032).
10+
public static class ThemeHelper
11+
{
12+
public static ThemeVariant Variant
13+
{
14+
get
15+
{
16+
var app = Application.Current;
17+
var variant = app?.ActualThemeVariant;
18+
if (variant == ThemeVariant.Dark) return ThemeVariant.Dark;
19+
if (variant == ThemeVariant.Light) return ThemeVariant.Light;
20+
return app?.PlatformSettings?.GetColorValues().ThemeVariant == PlatformThemeVariant.Dark
21+
? ThemeVariant.Dark
22+
: ThemeVariant.Light;
23+
}
24+
}
25+
26+
public static bool IsDark => Variant == ThemeVariant.Dark;
27+
}

‎src/UniGetUI.Avalonia/ViewModels/DialogPages/OperationOutputViewModel.cs‎

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,39 @@ public partial class OperationOutputViewModel : ObservableObject
1414
[ObservableProperty] private string _title = "";
1515
public ObservableCollection<LogLineItem> OutputLines { get; } = new();
1616

17-
private readonly IBrush _errorBrush;
18-
private readonly IBrush _debugBrush;
19-
private readonly IBrush _normalBrush;
17+
private IBrush _errorBrush = Brushes.Transparent;
18+
private IBrush _debugBrush = Brushes.Transparent;
19+
private IBrush _normalBrush = Brushes.Transparent;
2020

21-
private readonly ProgressLineCollapser _collapser = new();
21+
private readonly AbstractOperation _operation;
22+
private ProgressLineCollapser _collapser = new();
2223

2324
public OperationOutputViewModel(AbstractOperation operation)
2425
{
25-
var theme = Application.Current?.ActualThemeVariant ?? ThemeVariant.Default;
26-
_errorBrush = LookupBrush("StatusErrorForeground", theme, new SolidColorBrush(Color.Parse("#c62828")));
27-
_debugBrush = LookupBrush("LogOutputVerboseForeground", theme, new SolidColorBrush(Color.Parse("#767676")));
28-
_normalBrush = LookupBrush("SystemControlForegroundBaseHighBrush", theme, Brushes.White);
29-
26+
_operation = operation;
3027
Title = operation.Metadata.Title;
3128

32-
foreach (var (text, type) in operation.GetOutput())
33-
AddOrCollapseLine(text, type);
29+
Rebuild();
3430

3531
operation.LogLineAdded += (_, ev) =>
3632
Dispatcher.UIThread.Post(() => AddOrCollapseLine(ev.Item1, ev.Item2));
3733
}
3834

35+
// Rebuilds every line with brushes for the current theme; called on construction and whenever
36+
// the theme changes, so already-rendered output follows a live light/dark switch.
37+
public void Rebuild()
38+
{
39+
var theme = Infrastructure.ThemeHelper.Variant;
40+
_errorBrush = LookupBrush("StatusErrorForeground", theme, new SolidColorBrush(Color.Parse("#c62828")));
41+
_debugBrush = LookupBrush("LogOutputVerboseForeground", theme, new SolidColorBrush(Color.Parse("#767676")));
42+
_normalBrush = LookupBrush("SystemControlForegroundBaseHighBrush", theme, Brushes.White);
43+
44+
OutputLines.Clear();
45+
_collapser = new();
46+
foreach (var (text, type) in _operation.GetOutput())
47+
AddOrCollapseLine(text, type);
48+
}
49+
3950
// Progress indicators (carriage-return redraws like installer spinners) overwrite the previous
4051
// line instead of stacking up, mirroring how a terminal repaints in place. The first
4152
// non-progress line that follows settles into that same line.

‎src/UniGetUI.Avalonia/ViewModels/Pages/LogPages/BaseLogPageViewModel.cs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System.Collections.ObjectModel;
2-
using Avalonia;
32
using Avalonia.Media;
4-
using Avalonia.Styling;
53
using CommunityToolkit.Mvvm.ComponentModel;
64
using CommunityToolkit.Mvvm.Input;
75
using UniGetUI.Core.Logging;
@@ -40,8 +38,7 @@ protected BaseLogPageViewModel(bool logLevelEnabled, int initialLogLevelIndex =
4038

4139
public void ClearLog() => LogLines.Clear();
4240

43-
protected static bool IsDark =>
44-
Application.Current?.ActualThemeVariant == ThemeVariant.Dark;
41+
protected static bool IsDark => Infrastructure.ThemeHelper.IsDark;
4542

4643
protected static IBrush GetSeverityBrush(LogEntry.SeverityLevel severity, bool isDark)
4744
{

‎src/UniGetUI.Avalonia/Views/DialogPages/OperationFailedDialog.axaml.cs‎

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Avalonia.Controls;
33
using Avalonia.Layout;
44
using Avalonia.Media;
5+
using Avalonia.Styling;
56
using Avalonia.Threading;
67
using UniGetUI.Avalonia.ViewModels.Pages.LogPages;
78
using UniGetUI.Core.Tools;
@@ -13,8 +14,11 @@ namespace UniGetUI.Avalonia.Views.DialogPages;
1314

1415
public partial class OperationFailedDialog : Window
1516
{
17+
private readonly AbstractOperation _operation;
18+
1619
public OperationFailedDialog(AbstractOperation operation)
1720
{
21+
_operation = operation;
1822
InitializeComponent();
1923
UniGetUI.Avalonia.Infrastructure.MicaWindowHelper.Apply(this);
2024
Title = operation.Metadata.FailureMessage;
@@ -25,23 +29,9 @@ public OperationFailedDialog(AbstractOperation operation)
2529
"Please see the Command-line Output or refer to the Operation History for further information about the issue."
2630
);
2731

28-
var errorBrush = new SolidColorBrush(Color.Parse("#FF6B6B"));
29-
var debugBrush = new SolidColorBrush(Color.Parse("#888888"));
30-
var normalBrush = Application.Current?.FindResource("SystemControlForegroundBaseHighBrush") as IBrush
31-
?? Brushes.White;
32-
33-
var lines = new List<LogLineItem>();
34-
foreach (var (text, type) in operation.GetOutput())
35-
{
36-
IBrush brush = type switch
37-
{
38-
AbstractOperation.LineType.Error => errorBrush,
39-
AbstractOperation.LineType.VerboseDetails => debugBrush,
40-
_ => normalBrush,
41-
};
42-
lines.Add(new LogLineItem(text, brush));
43-
}
44-
OutputText.SetLines(lines);
32+
PopulateOutput();
33+
// Line brushes are baked per theme; recolor so they follow a live theme switch.
34+
ActualThemeVariantChanged += (_, _) => PopulateOutput();
4535

4636
var closeButton = new Button
4737
{
@@ -59,6 +49,36 @@ public OperationFailedDialog(AbstractOperation operation)
5949
Grid.SetColumn(closeButton, 1);
6050
}
6151

52+
private void PopulateOutput()
53+
{
54+
// Resolve against the actual theme variant; bare FindResource picks the light-theme
55+
// foreground (near-black) even in dark mode, making normal lines unreadable (#5032).
56+
var theme = Infrastructure.ThemeHelper.Variant;
57+
var errorBrush = new SolidColorBrush(Color.Parse("#FF6B6B"));
58+
var debugBrush = new SolidColorBrush(Color.Parse("#888888"));
59+
var normalBrush = LookupBrush("SystemControlForegroundBaseHighBrush", theme, Brushes.White);
60+
61+
var lines = new List<LogLineItem>();
62+
foreach (var (text, type) in _operation.GetOutput())
63+
{
64+
IBrush brush = type switch
65+
{
66+
AbstractOperation.LineType.Error => errorBrush,
67+
AbstractOperation.LineType.VerboseDetails => debugBrush,
68+
_ => normalBrush,
69+
};
70+
lines.Add(new LogLineItem(text, brush));
71+
}
72+
OutputText.SetLines(lines);
73+
}
74+
75+
private static IBrush LookupBrush(string key, ThemeVariant theme, IBrush fallback)
76+
{
77+
if (Application.Current?.TryGetResource(key, theme, out var resource) == true && resource is IBrush brush)
78+
return brush;
79+
return fallback;
80+
}
81+
6282
protected override void OnOpened(EventArgs e)
6383
{
6484
base.OnOpened(e);

‎src/UniGetUI.Avalonia/Views/DialogPages/OperationOutputWindow.axaml.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public OperationOutputWindow(AbstractOperation operation)
1818

1919
OutputText.SetLines(vm.OutputLines);
2020
vm.OutputLines.CollectionChanged += OnOutputLinesChanged;
21+
22+
// Recolor already-rendered output when the theme changes at runtime.
23+
ActualThemeVariantChanged += (_, _) => vm.Rebuild();
2124
}
2225

2326
private void OnOutputLinesChanged(object? sender, NotifyCollectionChangedEventArgs e)

‎src/UniGetUI.Avalonia/Views/Pages/LogPages/BaseLogPage.axaml.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ protected BaseLogPage(BaseLogPageViewModel viewModel)
2424
ViewModel.ScrollToBottomRequested += OnScrollToBottomRequested;
2525
ViewModel.LogLines.CollectionChanged += OnLogLinesChanged;
2626

27+
// Line brushes are baked per theme at load time; reload so they follow a live theme switch.
28+
ActualThemeVariantChanged += (_, _) => ViewModel.LoadLog();
29+
2730
LogEditor.SetLines(ViewModel.LogLines);
2831
}
2932

0 commit comments

Comments
 (0)