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 pathScrollingVerticalStackPanel.cs
More file actions
224 lines (191 loc) · 8.46 KB
/
Copy pathScrollingVerticalStackPanel.cs
File metadata and controls
224 lines (191 loc) · 8.46 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
224
using System;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
namespace GitHub.UI.Controls
{
/// <summary>
/// A vertical stack panel which implements its own logical scrolling, allowing controls to be
/// fixed horizontally in the scroll area.
/// </summary>
/// <remarks>
/// This panel is needed by the PullRequestDetailsView because of #1698: there is no default
/// panel in WPF which allows the horizontal scrollbar to always be present at the bottom while
/// also making the PR description etc be fixed horizontally (non-scrollable) in the viewport.
/// </remarks>
public class ScrollingVerticalStackPanel : Panel, IScrollInfo
{
const int lineSize = 16;
const int mouseWheelSize = 48;
/// <summary>
/// Attached property which when set to True on a child control, will cause it to be fixed
/// horizontally within the scrollable viewport.
/// </summary>
public static readonly DependencyProperty IsFixedProperty =
DependencyProperty.RegisterAttached(
"IsFixed",
typeof(bool),
typeof(ScrollingVerticalStackPanel),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsMeasure));
public bool CanHorizontallyScroll
{
get { return true; }
set { }
}
public bool CanVerticallyScroll
{
get { return true; }
set { }
}
public double ExtentHeight { get; private set; }
public double ExtentWidth { get; private set; }
public double HorizontalOffset { get; private set; }
public double VerticalOffset { get; private set; }
public double ViewportHeight { get; private set; }
public double ViewportWidth { get; private set; }
public ScrollViewer ScrollOwner { get; set; }
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Can only be applied to controls")]
public static bool GetIsFixed(FrameworkElement control)
{
return (bool)control.GetValue(IsFixedProperty);
}
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Can only be applied to controls")]
public static void SetIsFixed(FrameworkElement control, bool value)
{
control.SetValue(IsFixedProperty, value);
}
public void LineDown() => SetVerticalOffset(VerticalOffset + lineSize);
public void LineLeft() => SetHorizontalOffset(HorizontalOffset - lineSize);
public void LineRight() => SetHorizontalOffset(HorizontalOffset + lineSize);
public void LineUp() => SetVerticalOffset(VerticalOffset - lineSize);
public void MouseWheelDown() => SetVerticalOffset(VerticalOffset + mouseWheelSize);
public void MouseWheelLeft() => SetHorizontalOffset(HorizontalOffset - mouseWheelSize);
public void MouseWheelRight() => SetHorizontalOffset(HorizontalOffset + mouseWheelSize);
public void MouseWheelUp() => SetVerticalOffset(VerticalOffset - mouseWheelSize);
public void PageDown() => SetVerticalOffset(VerticalOffset + ViewportHeight);
public void PageLeft() => SetHorizontalOffset(HorizontalOffset - ViewportWidth);
public void PageRight() => SetHorizontalOffset(HorizontalOffset + ViewportWidth);
public void PageUp() => SetVerticalOffset(VerticalOffset - ViewportHeight);
public Rect MakeVisible(Visual visual, Rect rectangle)
{
var transform = visual.TransformToVisual(this);
var rect = transform.TransformBounds(rectangle);
var offsetX = HorizontalOffset;
var offsetY = VerticalOffset;
if (rect.Bottom > ViewportHeight)
{
var delta = rect.Bottom - ViewportHeight;
offsetY += delta;
rect.Y -= delta;
}
if (rect.Y < 0)
{
offsetY += rect.Y;
}
// We technially should be trying to also show the right-hand side of the rect here
// using the same technique that we just used to show the bottom of the rect above,
// but in the case of the PR details view, the left hand side of the item is much
// more important than the right hand side and it actually feels better to not do
// this. If this control is used elsewhere and this behavior is required, we could
// put in a switch to enable it.
if (rect.X < 0)
{
offsetX += rect.X;
}
SetHorizontalOffset(offsetX);
SetVerticalOffset(offsetY);
return rect;
}
public void SetHorizontalOffset(double offset)
{
var value = Math.Max(0, Math.Min(offset, ExtentWidth - ViewportWidth));
if (value != HorizontalOffset)
{
HorizontalOffset = value;
InvalidateArrange();
}
}
public void SetVerticalOffset(double offset)
{
var value = Math.Max(0, Math.Min(offset, ExtentHeight - ViewportHeight));
if (value != VerticalOffset)
{
VerticalOffset = value;
InvalidateArrange();
}
}
protected override void ParentLayoutInvalidated(UIElement child)
{
base.ParentLayoutInvalidated(child);
}
protected override Size MeasureOverride(Size availableSize)
{
var maxWidth = 0.0;
var height = 0.0;
foreach (FrameworkElement child in Children)
{
var isFixed = GetIsFixed(child);
var childConstraint = new Size(
isFixed ? availableSize.Width : double.PositiveInfinity,
double.PositiveInfinity);
child.Measure(childConstraint);
if (height - VerticalOffset < availableSize.Height)
{
maxWidth = Math.Max(maxWidth, child.DesiredSize.Width);
}
height += child.DesiredSize.Height;
}
UpdateScrollInfo(new Size(maxWidth, height), availableSize);
return new Size(
Math.Min(maxWidth, availableSize.Width),
Math.Min(height, availableSize.Height));
}
protected override Size ArrangeOverride(Size finalSize)
{
var y = -VerticalOffset;
var thisRect = new Rect(finalSize);
var visibleMaxWidth = 0.0;
foreach (FrameworkElement child in Children)
{
var isFixed = GetIsFixed(child);
var x = isFixed ? 0 : -HorizontalOffset;
var width = child.DesiredSize.Width;
if (isFixed)
{
switch (child.HorizontalAlignment)
{
case HorizontalAlignment.Stretch:
width = finalSize.Width;
break;
case HorizontalAlignment.Right:
x = finalSize.Width - child.DesiredSize.Width;
break;
case HorizontalAlignment.Center:
x = (finalSize.Width - child.DesiredSize.Width) / 2;
break;
}
}
var childRect = new Rect(x, y, width, child.DesiredSize.Height);
child.Arrange(childRect);
y += child.DesiredSize.Height;
if (childRect.IntersectsWith(thisRect) && childRect.Right > visibleMaxWidth)
{
visibleMaxWidth = childRect.Right;
}
}
UpdateScrollInfo(new Size(visibleMaxWidth, ExtentHeight), new Size(finalSize.Width, finalSize.Height));
return finalSize;
}
void UpdateScrollInfo(Size extent, Size viewport)
{
ExtentWidth = extent.Width;
ExtentHeight = extent.Height;
ScrollOwner?.InvalidateScrollInfo();
ViewportWidth = viewport.Width;
ViewportHeight = viewport.Height;
ScrollOwner?.InvalidateScrollInfo();
}
}
}