-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathHeaderedControl.cs
229 lines (183 loc) · 7.6 KB
/
HeaderedControl.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
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Telegram.Controls
{
public partial class HeaderedControl : ItemsControl
{
private Grid ContentRoot;
public HeaderedControl()
{
DefaultStyleKey = typeof(HeaderedControl);
//ItemContainerTransitions = new TransitionCollection
//{
// new RepositionThemeTransition()
//};
}
protected override void OnApplyTemplate()
{
ContentRoot = GetTemplateChild(nameof(ContentRoot)) as Grid;
VisualStateManager.GoToState(this, IsFooterAtBottom ? "FooterBottomLeft" : "FooterTopRight", false);
base.OnApplyTemplate();
}
#region Header
public string Header
{
get => (string)GetValue(HeaderProperty);
set => SetValue(HeaderProperty, value);
}
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register("Header", typeof(string), typeof(HeaderedControl), new PropertyMetadata(null));
#endregion
#region Footer
public string Footer
{
get => (string)GetValue(FooterProperty);
set => SetValue(FooterProperty, value);
}
public static readonly DependencyProperty FooterProperty =
DependencyProperty.Register("Footer", typeof(string), typeof(HeaderedControl), new PropertyMetadata(null));
#endregion
#region IsFooterAtBottom
public bool IsFooterAtBottom
{
get { return (bool)GetValue(IsFooterAtBottomProperty); }
set { SetValue(IsFooterAtBottomProperty, value); }
}
public static readonly DependencyProperty IsFooterAtBottomProperty =
DependencyProperty.Register("IsFooterAtBottom", typeof(bool), typeof(HeaderedControl), new PropertyMetadata(true, OnIsFooterAtBottomChanged));
private static void OnIsFooterAtBottomChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((HeaderedControl)d).OnIsFooterAtBottomChanged((bool)e.NewValue, (bool)e.OldValue);
}
private void OnIsFooterAtBottomChanged(bool newValue, bool oldValue)
{
VisualStateManager.GoToState(this, newValue ? "FooterBottomLeft" : "FooterTopRight", false);
}
#endregion
#region IsFooterLink
public bool IsFooterLink
{
get { return (bool)GetValue(IsFooterLinkProperty); }
set { SetValue(IsFooterLinkProperty, value); }
}
public static readonly DependencyProperty IsFooterLinkProperty =
DependencyProperty.Register("IsFooterLink", typeof(bool), typeof(HeaderedControl), new PropertyMetadata(false));
#endregion
#region ItemPresenterStyle
public Style ItemPresenterStyle
{
get { return (Style)GetValue(ItemPresenterStyleProperty); }
set { SetValue(ItemPresenterStyleProperty, value); }
}
public static readonly DependencyProperty ItemPresenterStyleProperty =
DependencyProperty.Register("ItemPresenterStyle", typeof(Style), typeof(HeaderedControl), new PropertyMetadata(null));
#endregion
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
if (element is ContentPresenter presenter)
{
presenter.Style = ItemPresenterStyle;
}
base.PrepareContainerForItemOverride(element, item);
}
public event EventHandler<TextUrlClickEventArgs> Click;
// Used by TextBlockHelper
public void OnClick(string url)
{
Click?.Invoke(this, new TextUrlClickEventArgs(url));
}
protected override Size MeasureOverride(Size availableSize)
{
if (ContentRoot == null)
{
return base.MeasureOverride(availableSize);
}
ContentRoot.Measure(availableSize);
if (ItemsPanelRoot?.DesiredSize.Height > 0)
{
return ContentRoot.DesiredSize;
}
return new Size(0, 0);
}
protected override Size ArrangeOverride(Size finalSize)
{
if (ContentRoot == null)
{
return base.ArrangeOverride(finalSize);
}
ContentRoot.Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height));
return finalSize;
}
}
public partial class TextUrlClickEventArgs
{
public TextUrlClickEventArgs(string url)
{
Url = url;
}
public string Url { get; }
}
public partial class HeaderedControlPanel : StackPanel
{
protected override Size MeasureOverride(Size availableSize)
{
var last = true;
var first = default(UIElement);
for (int i = Children.Count - 1; i >= 0; i--)
{
var child = Children[i];
if (child.Visibility == Visibility.Visible)
{
switch (child)
{
case ContentPresenter presenter:
presenter.BorderThickness = new Thickness(0, 0, 0, last ? 0 : 1);
presenter.CornerRadius = new CornerRadius(0, 0, last ? 4 : 0, last ? 4 : 0);
break;
case Control control:
control.BorderThickness = new Thickness(0, 0, 0, last ? 0 : 1);
control.CornerRadius = new CornerRadius(0, 0, last ? 4 : 0, last ? 4 : 0);
break;
case Grid grid:
grid.BorderThickness = new Thickness(0, 0, 0, last ? 0 : 1);
grid.CornerRadius = new CornerRadius(0, 0, last ? 4 : 0, last ? 4 : 0);
break;
case Border border:
border.BorderThickness = new Thickness(0, 0, 0, last ? 0 : 1);
border.CornerRadius = new CornerRadius(0, 0, last ? 4 : 0, last ? 4 : 0);
break;
}
last = false;
first = child;
}
}
if (first != null)
{
switch (first)
{
case ContentPresenter presenter:
presenter.CornerRadius = new CornerRadius(4, 4, presenter.CornerRadius.BottomRight, presenter.CornerRadius.BottomLeft);
break;
case Control control:
control.CornerRadius = new CornerRadius(4, 4, control.CornerRadius.BottomRight, control.CornerRadius.BottomLeft);
break;
case Grid grid:
grid.CornerRadius = new CornerRadius(4, 4, grid.CornerRadius.BottomRight, grid.CornerRadius.BottomLeft);
break;
case Border border:
border.CornerRadius = new CornerRadius(4, 4, border.CornerRadius.BottomRight, border.CornerRadius.BottomLeft);
break;
}
}
return base.MeasureOverride(availableSize);
}
}
}