-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathScrollableTextBlock.cs
executable file
·262 lines (226 loc) · 8.9 KB
/
ScrollableTextBlock.cs
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//
// This is the source code of Telegram for Windows Phone v. 3.x.x.
// It is licensed under GNU GPL v. 2 or later.
// You should have received a copy of the license in this archive (see LICENSE).
//
// Copyright Evgeny Nadymov, 2013-present.
//
using System;
using System.Collections.Generic;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Tasks;
namespace Telegram.Controls
{
public class ScrollableTextBlock : Control
{
private const int MaxSymbolsChunk = 1024;
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text",
typeof (string),
typeof (ScrollableTextBlock),
new PropertyMetadata(OnTextPropertyChanged));
private TextBlock _measureText;
private StackPanel _stackPanel;
public ScrollableTextBlock()
{
DefaultStyleKey = typeof (ScrollableTextBlock);
}
public string Text
{
get { return (string) GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ScrollableTextBlock) d).ParseText((string) e.NewValue);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_stackPanel = GetTemplateChild("StackPanel") as StackPanel;
ParseText(Text);
}
private void ParseText(string value)
{
if (string.IsNullOrEmpty(value) || _stackPanel == null)
return;
_stackPanel.Children.Clear();
var maxTextSize = GetMaxTextSize();
if (value.Length < maxTextSize)
{
AddTextChunk(value);
}
else
SplitText(value);
}
private static readonly Regex HyperlinkRegex = new Regex("(?i)\\b(((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))|([a-z0-9.\\-]+(\\.ru|\\.com|\\.net|\\.org|\\.us|\\.it|\\.co\\.uk)(?![a-z0-9]))|([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]*[a-zA-Z0-9-]+))");
private void AddTextChunk(string value)
{
var index = 0;
foreach (Match match in HyperlinkRegex.Matches(value))
{
string text;
if (match.Index != index)
{
text = value.Substring(index, match.Index - index);
_stackPanel.Children.Add(GetTextElement(text));
}
var link = match.Value;
var flag = link.EndsWith(",");
if (flag && link.Length > 1)
link = link.Substring(0, link.Length - 1);
if (!link.StartsWith("http", StringComparison.OrdinalIgnoreCase))
link = "http://" + link;
text = match.Value;
_stackPanel.Children.Add(GeHyperlinkElement(text, link));
index = match.Index + match.Length;
}
if (index < value.Length)
{
var textElement = GetTextElement(value.Substring(index));
_stackPanel.Children.Add(textElement);
}
}
private FrameworkElement GeHyperlinkElement(string text, string uri)
{
var textBlock = GetTextBlock();
textBlock.Text = text;
textBlock.Tap += (sender, args) =>
{
if (textBlock.Text.Contains("@"))
{
var task = new EmailComposeTask();
task.To = uri.ToLowerInvariant().Replace("http://", string.Empty);
task.Show();
}
else
{
var task = new WebBrowserTask();
task.URL = HttpUtility.UrlEncode(uri);
task.Show();
}
};
textBlock.TextDecorations = TextDecorations.Underline;
var border = GetBackgroundBorder();
border.Child = textBlock;
return border;
//var hyperlink = GetHyperlinkButton();
//var content = GetTextBlock();
//content.Text = text;
//hyperlink.Content = content;
//hyperlink.NavigateUri = new Uri(uri, UriKind.Absolute);
//var border = GetBackgroundBorder();
//border.Child = hyperlink;
//return border;
}
private FrameworkElement GetTextElement(string text)
{
var textBlock = GetTextBlock();
textBlock.Text = text;
var border = GetBackgroundBorder();
border.Child = textBlock;
return border;
}
private static readonly Dictionary<char, char> Delimiters = new Dictionary<char, char>
{
{' ', ' '},
{'.', ' '},
{',', ' '},
{'!', ' '},
{'?', ' '},
{':', ' '},
{';', ' '},
{']', ' '},
{')', ' '},
{'}', ' '},
};
private void SplitText(string text)
{
while (true)
{
var symbolsCount = Math.Min(MaxSymbolsChunk, text.Length);
var stepsBack = 0;
if (symbolsCount != text.Length)
{
if (!Delimiters.ContainsKey(text[symbolsCount - 1]))
{
for (var i = 1; i < 24 && (symbolsCount - 1 - i) >= 0; i++)
{
if (Delimiters.ContainsKey(text[symbolsCount - 1 - i]))
{
stepsBack += i;
break;
}
}
}
}
symbolsCount -= stepsBack;
var currentChunk = text.Substring(0, symbolsCount);
AddTextChunk(currentChunk);
var nextChunk = text.Substring(symbolsCount, text.Length - symbolsCount);
if (nextChunk.Length > 0)
{
text = nextChunk;
continue;
}
break;
}
}
private Size MeasureString(string text)
{
if (_measureText == null)
_measureText = GetTextBlock();
_measureText.Text = text;
return new Size(_measureText.ActualWidth, _measureText.ActualHeight);
}
private int GetMaxTextSize()
{
var size = MeasureString("W");
return (int) (Width/size.Width)*(int) (2048.0/size.Height)/2;
}
private TextBlock GetTextBlock()
{
return new TextBlock
{
TextWrapping = TextWrapping.Wrap,
FontSize = FontSize,
FontFamily = FontFamily,
FontWeight = FontWeight,
Foreground = Foreground,
};
}
private HyperlinkButton GetHyperlinkButton()
{
//var resources = new generic();
//Uri resourceLocater = new Uri("/Telegram.Controls;component/Themes/generic.xaml", UriKind.Relative);
//ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater);
//groupStyle.ContainerStyle = resourceDictionary["GroupHeaderStyle"] as Style;
return new HyperlinkButton
{
//Style = (Style) resources["HyperlinkButtonWrappingStyle"],
ClickMode = ClickMode.Release,
TargetName = "_blank",
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(0.0),
FontSize = FontSize,
FontFamily = FontFamily,
FontWeight = FontWeight,
Foreground = Foreground,
};
}
private Border GetBackgroundBorder()
{
return new Border
{
Background = Background,
Margin = new Thickness(0.0, -2.0, 0.0, 0.0),
Padding = new Thickness(Padding.Left, 0.0, Padding.Right, 0.0),
//BorderBrush = new SolidColorBrush(Colors.Blue),
//BorderThickness = new Thickness(1.0)
};
}
}
}