From aa77dd8741428e3e561809cce51b5e60acc81af0 Mon Sep 17 00:00:00 2001 From: William Bradley Date: Mon, 14 May 2018 23:34:03 +1200 Subject: [PATCH 1/3] Added Scroll Hack to return Scrolling Inertia to the Parent ScrollViewer if it exists so that Code Blocks don't steal scroll. --- .../Render/MarkdownRenderer.Blocks.cs | 27 +++++++++++++++++++ .../Render/MarkdownRenderer.Properties.cs | 6 +++++ .../Render/MarkdownRenderer.cs | 1 + 3 files changed, 34 insertions(+) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Blocks.cs b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Blocks.cs index d0d1c812a5c..1465a40eda6 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Blocks.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Blocks.cs @@ -12,9 +12,11 @@ using System; using System.Collections.Generic; +using System.Reflection; using Microsoft.Toolkit.Parsers.Markdown.Blocks; using Microsoft.Toolkit.Parsers.Markdown.Enums; using Microsoft.Toolkit.Parsers.Markdown.Render; +using Microsoft.Toolkit.Uwp.UI.Extensions; using Windows.UI.Text; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -328,6 +330,8 @@ protected override void RenderCode(CodeBlock element, IRenderContext context) LineHeight = FontSize * 1.4 }; + textBlock.PointerWheelChanged += TextBlock_PointerWheelChanged; + var paragraph = new Paragraph(); textBlock.Blocks.Add(paragraph); @@ -361,6 +365,29 @@ protected override void RenderCode(CodeBlock element, IRenderContext context) blockUIElementCollection.Add(viewer); } + private void TextBlock_PointerWheelChanged(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e) + { + var pointerPoint = e.GetCurrentPoint((UIElement)sender); + + if (pointerPoint.Properties.IsHorizontalMouseWheel) + { + e.Handled = false; + return; + } + + var rootViewer = VisualTree.FindAscendant(RootElement); + if (rootViewer != null) + { + wheelevent?.Invoke(rootViewer, new object[] { e }); + e.Handled = true; + } + } + + /// + /// Super Hack to retain inertia and passing the Scroll data onto the Parent ScrollViewer. + /// + private MethodInfo wheelevent = typeof(ScrollViewer).GetMethod("OnPointerWheelChanged", BindingFlags.NonPublic | BindingFlags.Instance); + /// /// Renders a table element. /// diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Properties.cs b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Properties.cs index cc47714b1c6..975b2de2054 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Properties.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Properties.cs @@ -12,6 +12,7 @@ using Windows.Foundation.Metadata; using Windows.UI.Text; +using Windows.UI.Xaml; using Windows.UI.Xaml.Media; namespace Microsoft.Toolkit.Uwp.UI.Controls.Markdown.Render @@ -26,6 +27,11 @@ public partial class MarkdownRenderer private static bool TextDecorationsSupported => (bool)(_textDecorationsSupported ?? (_textDecorationsSupported = ApiInformation.IsTypePresent("Windows.UI.Text.TextDecorations"))); + /// + /// Gets or sets the Root Framework Element. + /// + private FrameworkElement RootElement { get; set; } + /// /// Gets the interface that is used to register hyperlinks. /// diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.cs b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.cs index 60d3e664901..58d169e221e 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.cs @@ -50,6 +50,7 @@ public MarkdownRenderer(MarkdownDocument document, ILinkRegister linkRegister, I public UIElement Render() { var stackPanel = new StackPanel(); + RootElement = stackPanel; Render(new UIElementCollectionRenderContext(stackPanel.Children) { Foreground = Foreground }); // Set background and border properties. From d26824bae997d0db6ac3ffbe424e72e5470fae08 Mon Sep 17 00:00:00 2001 From: William Bradley Date: Thu, 17 May 2018 10:13:59 +1200 Subject: [PATCH 2/3] Moved things around. --- .../Render/MarkdownRenderer.Blocks.cs | 27 +------------------ .../Render/MarkdownRenderer.Properties.cs | 7 +++++ .../Render/MarkdownRenderer.cs | 19 +++++++++++++ 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Blocks.cs b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Blocks.cs index 1465a40eda6..28acd85b55d 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Blocks.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Blocks.cs @@ -12,11 +12,9 @@ using System; using System.Collections.Generic; -using System.Reflection; using Microsoft.Toolkit.Parsers.Markdown.Blocks; using Microsoft.Toolkit.Parsers.Markdown.Enums; using Microsoft.Toolkit.Parsers.Markdown.Render; -using Microsoft.Toolkit.Uwp.UI.Extensions; using Windows.UI.Text; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -330,7 +328,7 @@ protected override void RenderCode(CodeBlock element, IRenderContext context) LineHeight = FontSize * 1.4 }; - textBlock.PointerWheelChanged += TextBlock_PointerWheelChanged; + textBlock.PointerWheelChanged += Preventive_PointerWheelChanged; var paragraph = new Paragraph(); textBlock.Blocks.Add(paragraph); @@ -365,29 +363,6 @@ protected override void RenderCode(CodeBlock element, IRenderContext context) blockUIElementCollection.Add(viewer); } - private void TextBlock_PointerWheelChanged(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e) - { - var pointerPoint = e.GetCurrentPoint((UIElement)sender); - - if (pointerPoint.Properties.IsHorizontalMouseWheel) - { - e.Handled = false; - return; - } - - var rootViewer = VisualTree.FindAscendant(RootElement); - if (rootViewer != null) - { - wheelevent?.Invoke(rootViewer, new object[] { e }); - e.Handled = true; - } - } - - /// - /// Super Hack to retain inertia and passing the Scroll data onto the Parent ScrollViewer. - /// - private MethodInfo wheelevent = typeof(ScrollViewer).GetMethod("OnPointerWheelChanged", BindingFlags.NonPublic | BindingFlags.Instance); - /// /// Renders a table element. /// diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Properties.cs b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Properties.cs index 975b2de2054..43cd6fcf458 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Properties.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Properties.cs @@ -10,9 +10,11 @@ // THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE. // ****************************************************************** +using System.Reflection; using Windows.Foundation.Metadata; using Windows.UI.Text; using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; namespace Microsoft.Toolkit.Uwp.UI.Controls.Markdown.Render @@ -27,6 +29,11 @@ public partial class MarkdownRenderer private static bool TextDecorationsSupported => (bool)(_textDecorationsSupported ?? (_textDecorationsSupported = ApiInformation.IsTypePresent("Windows.UI.Text.TextDecorations"))); + /// + /// Super Hack to retain inertia and passing the Scroll data onto the Parent ScrollViewer. + /// + private static MethodInfo pointerWheelChanged = typeof(ScrollViewer).GetMethod("OnPointerWheelChanged", BindingFlags.NonPublic | BindingFlags.Instance); + /// /// Gets or sets the Root Framework Element. /// diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.cs b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.cs index 58d169e221e..3736b58415c 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.cs @@ -15,6 +15,7 @@ using Microsoft.Toolkit.Parsers.Markdown; using Microsoft.Toolkit.Parsers.Markdown.Inlines; using Microsoft.Toolkit.Parsers.Markdown.Render; +using Microsoft.Toolkit.Uwp.UI.Extensions; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Documents; @@ -208,5 +209,23 @@ private void RemoveSuperscriptRuns(IInlineContainer container, bool insertCaret) } } } + + private void Preventive_PointerWheelChanged(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e) + { + var pointerPoint = e.GetCurrentPoint((UIElement)sender); + + if (pointerPoint.Properties.IsHorizontalMouseWheel) + { + e.Handled = false; + return; + } + + var rootViewer = VisualTree.FindAscendant(RootElement); + if (rootViewer != null) + { + pointerWheelChanged?.Invoke(rootViewer, new object[] { e }); + e.Handled = true; + } + } } } \ No newline at end of file From dc3c1b586032c4b2114e95c83fd938a18fcb0d07 Mon Sep 17 00:00:00 2001 From: William Bradley Date: Thu, 17 May 2018 10:29:34 +1200 Subject: [PATCH 3/3] Applied ScrollFix to Image Scrollviewer. --- .../Render/MarkdownRenderer.Blocks.cs | 2 +- .../Render/MarkdownRenderer.Inlines.cs | 36 ++++++++++++------- .../Render/MarkdownRenderer.cs | 2 +- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Blocks.cs b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Blocks.cs index 28acd85b55d..2c21b4e2884 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Blocks.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Blocks.cs @@ -328,7 +328,7 @@ protected override void RenderCode(CodeBlock element, IRenderContext context) LineHeight = FontSize * 1.4 }; - textBlock.PointerWheelChanged += Preventive_PointerWheelChanged; + textBlock.PointerWheelChanged += Preventative_PointerWheelChanged; var paragraph = new Paragraph(); textBlock.Blocks.Add(paragraph); diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Inlines.cs b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Inlines.cs index c0ed84566ff..4fbd455c154 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Inlines.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Inlines.cs @@ -250,23 +250,33 @@ protected override async void RenderImage(ImageInline element, IRenderContext co return; } - var image = new Image(); - var scrollViewer = new ScrollViewer(); - var viewbox = new Viewbox(); - scrollViewer.Content = viewbox; - viewbox.Child = image; + var image = new Image + { + Source = resolvedImage, + HorizontalAlignment = HorizontalAlignment.Left, + VerticalAlignment = VerticalAlignment.Top, + Stretch = ImageStretch + }; + + var viewbox = new Viewbox + { + Child = image, + StretchDirection = StretchDirection.DownOnly + }; + + viewbox.PointerWheelChanged += Preventative_PointerWheelChanged; + + var scrollViewer = new ScrollViewer + { + Content = viewbox, + VerticalScrollMode = ScrollMode.Disabled, + VerticalScrollBarVisibility = ScrollBarVisibility.Disabled + }; + var imageContainer = new InlineUIContainer() { Child = scrollViewer }; LinkRegister.RegisterNewHyperLink(image, element.Url); - image.Source = resolvedImage; - image.HorizontalAlignment = HorizontalAlignment.Left; - image.VerticalAlignment = VerticalAlignment.Top; - image.Stretch = ImageStretch; - scrollViewer.VerticalScrollMode = ScrollMode.Disabled; - scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled; - viewbox.StretchDirection = StretchDirection.DownOnly; - if (ImageMaxHeight > 0) { viewbox.MaxHeight = ImageMaxHeight; diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.cs b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.cs index 3736b58415c..5763f65dfb9 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.cs @@ -210,7 +210,7 @@ private void RemoveSuperscriptRuns(IInlineContainer container, bool insertCaret) } } - private void Preventive_PointerWheelChanged(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e) + private void Preventative_PointerWheelChanged(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e) { var pointerPoint = e.GetCurrentPoint((UIElement)sender);