|
| 1 | +using System; |
| 2 | +using System.Windows; |
| 3 | +using System.Windows.Controls; |
| 4 | +using System.Windows.Controls.Primitives; |
| 5 | +using System.Windows.Media; |
| 6 | + |
| 7 | +namespace GitHub.UI.Controls |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// A vertical stack panel which implements its own logical scrolling, allowing controls to be |
| 11 | + /// fixed horizontally in the scroll area. |
| 12 | + /// </summary> |
| 13 | + /// <remarks> |
| 14 | + /// This panel is needed by the PullRequestDetailsView because of #1698: there is no default |
| 15 | + /// panel in WPF which allows the horizontal scrollbar to always be present at the bottom while |
| 16 | + /// also making the PR description etc be fixed horizontally (non-scrollable) in the viewport. |
| 17 | + /// </remarks> |
| 18 | + public class ScollingVerticalStackPanel : Panel, IScrollInfo |
| 19 | + { |
| 20 | + const int lineSize = 16; |
| 21 | + const int mouseWheelSize = 48; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Attached property which when set to True on a child control, will cause it to be fixed |
| 25 | + /// horizontally within the scrollable viewport. |
| 26 | + /// </summary> |
| 27 | + public static readonly DependencyProperty IsFixedProperty = |
| 28 | + DependencyProperty.RegisterAttached( |
| 29 | + "IsFixed", |
| 30 | + typeof(bool), |
| 31 | + typeof(ScollingVerticalStackPanel), |
| 32 | + new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsMeasure)); |
| 33 | + |
| 34 | + public bool CanHorizontallyScroll |
| 35 | + { |
| 36 | + get { return true; } |
| 37 | + set { } |
| 38 | + } |
| 39 | + |
| 40 | + public bool CanVerticallyScroll |
| 41 | + { |
| 42 | + get { return true; } |
| 43 | + set { } |
| 44 | + } |
| 45 | + |
| 46 | + public double ExtentHeight { get; private set; } |
| 47 | + public double ExtentWidth { get; private set; } |
| 48 | + public double HorizontalOffset { get; private set; } |
| 49 | + public double VerticalOffset { get; private set; } |
| 50 | + public double ViewportHeight { get; private set; } |
| 51 | + public double ViewportWidth { get; private set; } |
| 52 | + public ScrollViewer ScrollOwner { get; set; } |
| 53 | + |
| 54 | + public static bool GetIsFixed(FrameworkElement control) |
| 55 | + { |
| 56 | + return (bool)control.GetValue(IsFixedProperty); |
| 57 | + } |
| 58 | + |
| 59 | + public static void SetIsFixed(FrameworkElement control, bool value) |
| 60 | + { |
| 61 | + control.SetValue(IsFixedProperty, value); |
| 62 | + } |
| 63 | + |
| 64 | + public void LineDown() => SetVerticalOffset(VerticalOffset + lineSize); |
| 65 | + public void LineLeft() => SetHorizontalOffset(HorizontalOffset - lineSize); |
| 66 | + public void LineRight() => SetHorizontalOffset(HorizontalOffset + lineSize); |
| 67 | + public void LineUp() => SetVerticalOffset(VerticalOffset - lineSize); |
| 68 | + public void MouseWheelDown() => SetVerticalOffset(VerticalOffset + mouseWheelSize); |
| 69 | + public void MouseWheelLeft() => SetHorizontalOffset(HorizontalOffset - mouseWheelSize); |
| 70 | + public void MouseWheelRight() => SetHorizontalOffset(HorizontalOffset + mouseWheelSize); |
| 71 | + public void MouseWheelUp() => SetVerticalOffset(VerticalOffset - mouseWheelSize); |
| 72 | + public void PageDown() => SetVerticalOffset(VerticalOffset + ViewportHeight); |
| 73 | + public void PageLeft() => SetHorizontalOffset(HorizontalOffset - ViewportWidth); |
| 74 | + public void PageRight() => SetHorizontalOffset(HorizontalOffset + ViewportWidth); |
| 75 | + public void PageUp() => SetVerticalOffset(VerticalOffset - ViewportHeight); |
| 76 | + |
| 77 | + public Rect MakeVisible(Visual visual, Rect rectangle) |
| 78 | + { |
| 79 | + var transform = visual.TransformToVisual(this); |
| 80 | + var rect = transform.TransformBounds(rectangle); |
| 81 | + var offsetX = HorizontalOffset; |
| 82 | + var offsetY = VerticalOffset; |
| 83 | + |
| 84 | + if (rect.Bottom > ViewportHeight) |
| 85 | + { |
| 86 | + var delta = rect.Bottom - ViewportHeight; |
| 87 | + offsetY += delta; |
| 88 | + rect.Y -= delta; |
| 89 | + } |
| 90 | + |
| 91 | + if (rect.Y < 0) |
| 92 | + { |
| 93 | + offsetY += rect.Y; |
| 94 | + } |
| 95 | + |
| 96 | + // We technially should be trying to also show the right-hand side of the rect here |
| 97 | + // using the same technique that we just used to show the bottom of the rect above, |
| 98 | + // but in the case of the PR details view, the left hand side of the item is much |
| 99 | + // more important than the right hand side and it actually feels better to not do |
| 100 | + // this. If this control is used elsewhere and this behavior is required, we could |
| 101 | + // put in a switch to enable it. |
| 102 | + |
| 103 | + if (rect.X < 0) |
| 104 | + { |
| 105 | + offsetX += rect.X; |
| 106 | + } |
| 107 | + |
| 108 | + SetHorizontalOffset(offsetX); |
| 109 | + SetVerticalOffset(offsetY); |
| 110 | + |
| 111 | + return rect; |
| 112 | + } |
| 113 | + |
| 114 | + public void SetHorizontalOffset(double offset) |
| 115 | + { |
| 116 | + var value = Math.Max(0, offset); |
| 117 | + |
| 118 | + if (value != HorizontalOffset) |
| 119 | + { |
| 120 | + HorizontalOffset = value; |
| 121 | + InvalidateArrange(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + public void SetVerticalOffset(double offset) |
| 126 | + { |
| 127 | + var value = Math.Max(0, offset); |
| 128 | + |
| 129 | + if (value != VerticalOffset) |
| 130 | + { |
| 131 | + VerticalOffset = value; |
| 132 | + InvalidateArrange(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + protected override Size MeasureOverride(Size availableSize) |
| 137 | + { |
| 138 | + var maxWidth = 0.0; |
| 139 | + var height = 0.0; |
| 140 | + |
| 141 | + foreach (FrameworkElement child in Children) |
| 142 | + { |
| 143 | + var isFixed = GetIsFixed(child); |
| 144 | + var childConstraint = new Size( |
| 145 | + isFixed ? availableSize.Width : double.PositiveInfinity, |
| 146 | + double.PositiveInfinity); |
| 147 | + child.Measure(childConstraint); |
| 148 | + maxWidth = Math.Max(maxWidth, child.DesiredSize.Width); |
| 149 | + height += child.DesiredSize.Height; |
| 150 | + } |
| 151 | + |
| 152 | + ExtentWidth = maxWidth; |
| 153 | + ExtentHeight = height; |
| 154 | + |
| 155 | + return new Size( |
| 156 | + Math.Min(maxWidth, availableSize.Width), |
| 157 | + Math.Min(height, availableSize.Height)); |
| 158 | + } |
| 159 | + |
| 160 | + protected override Size ArrangeOverride(Size finalSize) |
| 161 | + { |
| 162 | + var y = -VerticalOffset; |
| 163 | + |
| 164 | + foreach (FrameworkElement child in Children) |
| 165 | + { |
| 166 | + var isFixed = GetIsFixed(child); |
| 167 | + var x = isFixed ? 0 : -HorizontalOffset; |
| 168 | + child.Arrange(new Rect(x, y, child.DesiredSize.Width, child.DesiredSize.Height)); |
| 169 | + y += child.DesiredSize.Height; |
| 170 | + } |
| 171 | + |
| 172 | + ViewportWidth = finalSize.Width; |
| 173 | + ViewportHeight = finalSize.Height; |
| 174 | + ScrollOwner?.InvalidateScrollInfo(); |
| 175 | + return finalSize; |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments