Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MarkdownTextBlock Code Block Scrolling Fix #2093

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ protected override void RenderCode(CodeBlock element, IRenderContext context)
LineHeight = FontSize * 1.4
};

textBlock.PointerWheelChanged += Preventative_PointerWheelChanged;

var paragraph = new Paragraph();
textBlock.Blocks.Add(paragraph);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +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
Expand All @@ -26,6 +29,16 @@ public partial class MarkdownRenderer
private static bool TextDecorationsSupported => (bool)(_textDecorationsSupported ??
(_textDecorationsSupported = ApiInformation.IsTypePresent("Windows.UI.Text.TextDecorations")));

/// <summary>
/// Super Hack to retain inertia and passing the Scroll data onto the Parent ScrollViewer.
/// </summary>
private static MethodInfo pointerWheelChanged = typeof(ScrollViewer).GetMethod("OnPointerWheelChanged", BindingFlags.NonPublic | BindingFlags.Instance);

/// <summary>
/// Gets or sets the Root Framework Element.
/// </summary>
private FrameworkElement RootElement { get; set; }

/// <summary>
/// Gets the interface that is used to register hyperlinks.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,6 +51,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.
Expand Down Expand Up @@ -207,5 +209,23 @@ private void RemoveSuperscriptRuns(IInlineContainer container, bool insertCaret)
}
}
}

private void Preventative_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<ScrollViewer>(RootElement);
if (rootViewer != null)
{
pointerWheelChanged?.Invoke(rootViewer, new object[] { e });
e.Handled = true;
}
}
}
}