This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathTrimmedPathTextBlock.cs
More file actions
166 lines (144 loc) · 5.71 KB
/
Copy pathTrimmedPathTextBlock.cs
File metadata and controls
166 lines (144 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace GitHub.UI
{
/// <summary>
/// TextBlock that displays a path and intelligently trims with ellipsis when the path doesn't
/// fit in the allocated size.
/// </summary>
/// <remarks>
/// When displaying a path that is too long for its allocated space, we need to trim the path
/// with ellipses intelligently instead of simply trimming the end (as this is the filename
/// which is the most important part!). This control trims a path in the following manner with
/// decreasing allocated space:
///
/// - VisualStudio\src\GitHub.UI\Controls\TrimmedPathTextBlock.cs
/// - VisualStudio\...\GitHub.UI\Controls\TrimmedPathTextBlock.cs
/// - VisualStudio\...\...\Controls\TrimmedPathTextBlock.cs
/// - VisualStudio\...\...\...\TrimmedPathTextBlock.cs
/// - ...\...\...\...\TrimmedPathTextBlock.cs
/// </remarks>
public class TrimmedPathTextBlock : FrameworkElement
{
public static readonly DependencyProperty FontFamilyProperty =
TextBlock.FontFamilyProperty.AddOwner(typeof(TrimmedPathTextBlock));
public static readonly DependencyProperty FontSizeProperty =
TextBlock.FontSizeProperty.AddOwner(typeof(TrimmedPathTextBlock));
public static readonly DependencyProperty FontStretchProperty =
TextBlock.FontStretchProperty.AddOwner(typeof(TrimmedPathTextBlock));
public static readonly DependencyProperty FontStyleProperty =
TextBlock.FontStyleProperty.AddOwner(typeof(TrimmedPathTextBlock));
public static readonly DependencyProperty FontWeightProperty =
TextBlock.FontWeightProperty.AddOwner(typeof(TrimmedPathTextBlock));
public static readonly DependencyProperty ForegroundProperty =
TextBlock.ForegroundProperty.AddOwner(typeof(TrimmedPathTextBlock));
public static readonly DependencyProperty TextProperty =
TextBlock.TextProperty.AddOwner(
typeof(TrimmedPathTextBlock),
new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender,
TextChanged));
FormattedText formattedText;
FormattedText renderText;
public FontFamily FontFamily
{
get { return (FontFamily)GetValue(FontFamilyProperty); }
set { SetValue(FontFamilyProperty, value); }
}
public double FontSize
{
get { return (double)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}
public FontStretch FontStretch
{
get { return (FontStretch)GetValue(FontStretchProperty); }
set { SetValue(FontStretchProperty, value); }
}
public FontStyle FontStyle
{
get { return (FontStyle)GetValue(FontStyleProperty); }
set { SetValue(FontStyleProperty, value); }
}
public FontWeight FontWeight
{
get { return (FontWeight)GetValue(FontWeightProperty); }
set { SetValue(FontWeightProperty, value); }
}
public Brush Foreground
{
get { return (Brush)GetValue(ForegroundProperty); }
set { SetValue(ForegroundProperty, value); }
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
protected FormattedText FormattedText
{
get
{
if (formattedText == null && Text != null)
{
formattedText = CreateFormattedText(Text);
}
return formattedText;
}
}
protected override Size MeasureOverride(Size availableSize)
{
if (Text == null)
{
return new Size();
}
var parts = Text
.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar })
.ToList();
var nextPart = Math.Min(1, parts.Count - 1);
while (true)
{
renderText = CreateFormattedText(string.Join(new string(Path.DirectorySeparatorChar, 1), parts));
if (renderText.Width <= availableSize.Width || nextPart == -1)
break;
parts[nextPart] = "\u2026";
if (nextPart == 0)
nextPart = -1;
else if (nextPart == parts.Count - 2)
nextPart = 0;
else
nextPart++;
};
return new Size(renderText.Width, renderText.Height);
}
protected override void OnRender(DrawingContext drawingContext)
{
drawingContext.DrawText(renderText, new Point());
}
FormattedText CreateFormattedText(string text)
{
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(FontFamily, FontStyle, FontWeight, FontStretch),
FontSize,
Foreground);
}
static void TextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var textBlock = sender as TrimmedPathTextBlock;
if (textBlock != null)
{
textBlock.formattedText = null;
textBlock.renderText = null;
}
}
}
}