-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFolderTextBlock.cs
More file actions
223 lines (188 loc) · 9.72 KB
/
Copy pathFolderTextBlock.cs
File metadata and controls
223 lines (188 loc) · 9.72 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//
// Author:
// Michael Göricke
//
// Copyright (c) 2025
//
// This file is part of LocalTestServer.
//
// LocalTestServer is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see<http://www.gnu.org/licenses/>.
using System.Globalization;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
namespace WebServer
{
/// <summary>
/// The FolderTextBlock shortens a given full specified folder or file
/// path so that it fits into the given space (widths) of the control.
/// The shortening algorithm removes subfolders from the
/// middle of the path and replaces them with a single "...". E.g.
/// "d:\aaaa\bbbbb\ccccc\ddddd\eeeee\fffff\gggg"
/// may be shortened to
/// "d:\aaaa\bbbbb\...\fffff\gggg"
/// </summary>
public class FolderTextBlock : FrameworkElement
{
public static readonly DependencyProperty ForegroundProperty =
TextElement.ForegroundProperty.AddOwner(typeof(FolderTextBlock),
new FrameworkPropertyMetadata(TextElement.ForegroundProperty.DefaultMetadata.DefaultValue,
FrameworkPropertyMetadataOptions.AffectsRender));
public Brush Foreground
{
get => (Brush)GetValue(ForegroundProperty);
set => SetValue(ForegroundProperty, value);
}
////////////////////////////////////////////////////////////////////
public static readonly DependencyProperty FontFamilyProperty =
TextElement.FontFamilyProperty.AddOwner(typeof(FolderTextBlock),
new FrameworkPropertyMetadata(TextElement.FontFamilyProperty.DefaultMetadata.DefaultValue,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
public FontFamily FontFamily
{
get => (FontFamily)GetValue(FontFamilyProperty);
set => SetValue(FontFamilyProperty, value);
}
////////////////////////////////////////////////////////////////////
public static readonly DependencyProperty FontSizeProperty =
TextElement.FontSizeProperty.AddOwner(typeof(FolderTextBlock),
new FrameworkPropertyMetadata(TextElement.FontSizeProperty.DefaultMetadata.DefaultValue,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
public double FontSize
{
get => (double)GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value);
}
////////////////////////////////////////////////////////////////////
public static readonly DependencyProperty FontStretchProperty
= TextElement.FontStretchProperty.AddOwner(typeof(FolderTextBlock),
new FrameworkPropertyMetadata(TextElement.FontStretchProperty.DefaultMetadata.DefaultValue,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
public FontStretch FontStretch
{
get => (FontStretch)GetValue(FontStretchProperty);
set => SetValue(FontStretchProperty, value);
}
////////////////////////////////////////////////////////////////////
public static readonly DependencyProperty FontStyleProperty =
TextElement.FontStyleProperty.AddOwner(typeof(FolderTextBlock),
new FrameworkPropertyMetadata(SystemFonts.MessageFontStyle,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
public FontStyle FontStyle
{
get => (FontStyle)GetValue(FontStyleProperty);
set => SetValue(FontStyleProperty, value);
}
////////////////////////////////////////////////////////////////////
public static readonly DependencyProperty FontWeightProperty =
TextElement.FontWeightProperty.AddOwner(typeof(FolderTextBlock),
new FrameworkPropertyMetadata(TextElement.FontWeightProperty.DefaultMetadata.DefaultValue,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
public FontWeight FontWeight
{
get => (FontWeight)GetValue(FontWeightProperty);
set => SetValue(FontWeightProperty, value);
}
////////////////////////////////////////////////////////////////////
public static readonly DependencyProperty FolderProperty =
DependencyProperty.Register("Folder",
typeof(string),
typeof(FolderTextBlock),
new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender));
public string Folder
{
get => (string)GetValue(FolderProperty);
set => SetValue(FolderProperty, value);
}
////////////////////////////////////////////////////////////////////
protected override Size MeasureOverride(Size constraint)
{
var formattedText = new FormattedText(Folder,
CultureInfo.CurrentUICulture,
FlowDirection,
new Typeface(FontFamily, FontStyle, FontWeight, FontStretch),
FontSize,
Foreground,
1);
var width = constraint.Width == double.PositiveInfinity ? formattedText.Width : constraint.Width;
var height = constraint.Height == double.PositiveInfinity ? formattedText.Height : constraint.Height;
return new Size(width, height);
}
////////////////////////////////////////////////////////////////////
protected override void OnRender(DrawingContext ctx)
{
var fullFormattedText = new FormattedText(Folder,
CultureInfo.CurrentUICulture,
FlowDirection,
new Typeface(FontFamily, FontStyle, FontWeight, FontStretch),
FontSize,
Foreground,
1);
if (fullFormattedText.Width < ActualWidth)
{
ctx.DrawText(fullFormattedText, new Point(0, 0));
return;
}
var limitedFolderMin = FolderNameHelper.LimitPath(Folder, 1);
var formattedTextMin = new FormattedText(limitedFolderMin,
CultureInfo.CurrentUICulture,
FlowDirection,
new Typeface(FontFamily, FontStyle, FontWeight, FontStretch),
FontSize,
Foreground,
1);
if (formattedTextMin.Width > ActualWidth)
{
fullFormattedText.MaxTextWidth = ActualWidth;
fullFormattedText.MaxTextHeight = ActualHeight;
fullFormattedText.Trimming = TextTrimming.CharacterEllipsis;
ctx.DrawText(fullFormattedText, new Point(0, 0));
return;
}
int min = limitedFolderMin.Length;
int max = Folder.Length - 1;
FormattedText lastFit = null;
while (max - min > 1)
{
var current = (max - min) / 2 + min;
var limitedFolder = FolderNameHelper.LimitPath(Folder, current);
var formattedText = new FormattedText(limitedFolder,
CultureInfo.CurrentUICulture,
FlowDirection,
new Typeface(FontFamily, FontStyle, FontWeight, FontStretch),
FontSize,
Foreground,
1);
if (formattedText.Width > ActualWidth)
{
var len = limitedFolder.Length;
max = len;
}
else
{
min = current;
lastFit = formattedText;
}
}
if (lastFit == null)
{
lastFit = fullFormattedText;
lastFit.MaxTextWidth = ActualWidth;
lastFit.MaxTextHeight = ActualHeight;
lastFit.Trimming = TextTrimming.CharacterEllipsis;
}
ctx.DrawText(lastFit, new Point(0, 0));
}
}
}