Skip to content

Commit 2c1168e

Browse files
committed
Dim directories in changed-file lists to emphasize filenames
1 parent f08ca51 commit 2c1168e

3 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎src/Views/ChangeCollectionView.axaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
IsVisible="{Binding IsConflicted}"/>
141141

142142
<v:ChangePathTextBlock Grid.Column="2" Margin="4,0,0,0"
143+
DirectoryForeground="{DynamicResource Brush.FG2}"
143144
Path="{Binding Path}"/>
144145
</v:ChangeListRow>
145146
</DataTemplate>

‎src/Views/ChangePathTextBlock.cs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using Avalonia;
55
using Avalonia.Controls;
66
using Avalonia.Media;
7+
using Avalonia.Media.TextFormatting;
8+
using Avalonia.Utilities;
79
using Avalonia.VisualTree;
810

911
namespace SourceGit.Views
@@ -78,6 +80,15 @@ protected override Size MeasureOverride(Size availableSize)
7880

7981
public class ChangePathTextBlock : TextBlock
8082
{
83+
public static readonly StyledProperty<IBrush> DirectoryForegroundProperty =
84+
AvaloniaProperty.Register<ChangePathTextBlock, IBrush>(nameof(DirectoryForeground), Brushes.Gray);
85+
86+
public IBrush DirectoryForeground
87+
{
88+
get => GetValue(DirectoryForegroundProperty);
89+
set => SetValue(DirectoryForegroundProperty, value);
90+
}
91+
8192
public static readonly StyledProperty<string> PathProperty =
8293
AvaloniaProperty.Register<ChangePathTextBlock, string>(nameof(Path), string.Empty);
8394

@@ -92,6 +103,29 @@ static ChangePathTextBlock()
92103
AffectsMeasure<ChangePathTextBlock>(PathProperty);
93104
}
94105

106+
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
107+
{
108+
base.OnPropertyChanged(change);
109+
if (change.Property == DirectoryForegroundProperty)
110+
InvalidateTextLayout();
111+
}
112+
113+
protected override TextLayout CreateTextLayout(string text)
114+
{
115+
var path = Path ?? string.Empty;
116+
var filenameLength = path.Length - (path.LastIndexOf('/') + 1);
117+
var directoryLength = text.Length - filenameLength;
118+
if (directoryLength <= 0)
119+
return base.CreateTextLayout(text);
120+
121+
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
122+
var properties = new GenericTextRunProperties(typeface, FontSize, foregroundBrush: DirectoryForeground);
123+
return new TextLayout(text, typeface, FontSize, Foreground,
124+
textAlignment: TextAlignment, flowDirection: FlowDirection,
125+
lineHeight: LineHeight, letterSpacing: LetterSpacing,
126+
textStyleOverrides: [new ValueSpan<TextRunProperties>(0, directoryLength, properties)]);
127+
}
128+
95129
private Size MeasureText(string text)
96130
{
97131
var formatted = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection,

‎tests/ChangePathChecks/Program.cs‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Avalonia;
22
using Avalonia.Controls;
3+
using Avalonia.Media;
34
using Avalonia.Threading;
45
using Avalonia.VisualTree;
56
using SourceGit.Views;
@@ -10,9 +11,11 @@ internal static class Program
1011
private static void Main()
1112
{
1213
SourceGit.App.BuildAvaloniaApp().SetupWithoutStarting();
13-
var text = new ChangePathTextBlock { Path = "very/long/directory/structure/filename.cs" };
14+
var text = new ChangePathTextBlock { Foreground = Brushes.White, Path = "very/long/directory/structure/filename.cs" };
1415
Measure(text, 1000);
1516
Check(text.Text == text.Path, "Full path fits");
17+
text.DirectoryForeground = Brushes.DarkGray;
18+
Measure(text, 1000);
1619
Measure(text, 180);
1720
Check(text.Text.StartsWith("…") && text.Text.EndsWith("filename.cs"), "Leading ellipsis preserves filename");
1821
Check(text.DesiredSize.Width <= 180, "Trimmed path fits");
@@ -57,12 +60,14 @@ private static void Main()
5760
Dispatcher.UIThread.RunJobs();
5861
var label = view.GetVisualDescendants().OfType<ChangePathTextBlock>().Single();
5962
Check(label.Text.StartsWith("…") && label.Text.EndsWith("filename.cs"), "Actual staged/unstaged template truncates correctly");
63+
CheckPathColors(label);
6064
var preferences = SourceGit.ViewModels.Preferences.Instance;
6165
preferences.SetCanModify();
6266
preferences.TrimLeadingPathsInChanges = false;
6367
for (var i = 0; i < 10; i++)
6468
Dispatcher.UIThread.RunJobs();
6569
Check(label.Text == label.Path, "Preference disables trimming immediately");
70+
CheckPathColors(label);
6671
preferences.TrimLeadingPathsInChanges = true;
6772
for (var i = 0; i < 10; i++)
6873
Dispatcher.UIThread.RunJobs();
@@ -73,15 +78,33 @@ private static void Main()
7378
Check(label.Text == label.Path, "Actual template responds to resize");
7479
window.Close();
7580
}
76-
Console.WriteLine("PASS: 18 changed-path layout checks");
81+
Console.WriteLine("PASS: 18 changed-path layout checks and directory/filename color checks");
7782
}
7883

7984
private static void Measure(Control control, double width)
8085
{
8186
control.InvalidateMeasure();
8287
control.Measure(new Size(width, 24));
88+
if (control is ChangePathTextBlock path)
89+
CheckPathColors(path);
8390
}
8491

92+
private static void CheckPathColors(ChangePathTextBlock label)
93+
{
94+
var filenameLength = label.Path.Length - (label.Path.LastIndexOf('/') + 1);
95+
var directoryLength = label.Text.Length - filenameLength;
96+
var position = 0;
97+
foreach (var run in label.TextLayout.TextLines.SelectMany(line => line.TextRuns))
98+
{
99+
if (position >= label.Text.Length)
100+
break;
101+
var expected = position < directoryLength ? label.DirectoryForeground : label.Foreground;
102+
Check(Equals(run.Properties.ForegroundBrush, expected), "Directory is gray and filename keeps normal foreground");
103+
Check(position >= directoryLength || position + run.Length <= directoryLength, "Color boundary preserves full filename");
104+
position += run.Length;
105+
}
106+
Check(position >= label.Text.Length, "Color checks cover displayed text");
107+
}
85108
private static void Check(bool condition, string message)
86109
{
87110
if (!condition)

0 commit comments

Comments
 (0)