Skip to content
This repository was archived by the owner on Apr 30, 2024. It is now read-only.

Commit

Permalink
Use Runs for inline code block
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew4850 committed Apr 28, 2022
1 parent 26d0fc6 commit 1b15552
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 15 deletions.
72 changes: 60 additions & 12 deletions src/Libs/Quarrel.Markdown/Rendering/MessageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

namespace Quarrel.Markdown
{
Expand All @@ -28,6 +32,10 @@ public sealed class MessageRenderer : Control
nameof(Context), typeof(BindableMessage), typeof(MessageRenderer), new PropertyMetadata(null, OnPropertyChanged));

private RichTextBlock? _richBlock;
private Canvas? _canvas;
private Grid? _grid;

private List<Inline> _codeBlocks = new List<Inline>();

public MessageRenderer()
{
Expand All @@ -36,9 +44,39 @@ public MessageRenderer()

protected override void OnApplyTemplate()
{
_richBlock = (RichTextBlock)GetTemplateChild(RichBlockPartName);
//_richBlock = (RichTextBlock)GetTemplateChild(RichBlockPartName);
_canvas = (Canvas)GetTemplateChild("Canvas");
_grid = (Grid)GetTemplateChild("Grid");
}

private int a;
private void RichBlock_SizeChanged(object sender, object e)
{
UpdateOverlays();
}

private void UpdateOverlays()
{
_canvas.Children.Clear();
foreach (var inline in _codeBlocks)
{
var startRect = inline.ElementStart.GetCharacterRect(inline.ElementStart.LogicalDirection);
var endRect = inline.ElementEnd.GetCharacterRect(inline.ElementStart.LogicalDirection);
int heightPadding = 1;
var rec = new Rectangle()
{
Width = Math.Max(endRect.Right - startRect.Left, 0),
Height = Math.Max(endRect.Bottom - startRect.Top + heightPadding*2, 0),
Fill = new SolidColorBrush(Color.FromArgb(255, 30, 30, 30)),
RadiusX = 2,
RadiusY = 2
};
_canvas.Children.Add(rec);
Canvas.SetTop(rec, startRect.Top - heightPadding);
Canvas.SetLeft(rec, startRect.Left);
}
}

public string Text
{
get => (string)GetValue(TextProperty);
Expand All @@ -60,16 +98,24 @@ private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChan
// TODO: Allow context free rendering
return;
}

var tree = Parser.ParseAST(messageRenderer.Text, true, false);
var modTree = AdjustTree(tree);
messageRenderer.RenderMarkdown(modTree);
}

private void RenderMarkdown(IList<ASTRoot> tree)
{
if (_richBlock != null)
{
_richBlock.SizeChanged -= RichBlock_SizeChanged;
_grid.Children.Remove(_richBlock);
}

_richBlock = new RichTextBlock();
_richBlock.SizeChanged += RichBlock_SizeChanged;
BlockCollection blocks = _richBlock.Blocks;
blocks.Clear();
_codeBlocks.Clear();

foreach (var root in tree)
{
Expand Down Expand Up @@ -171,16 +217,17 @@ private void RenderMarkdown(IList<ASTRoot> tree)
break;
case InlineCode inlineCode:
{
InlineUIContainer container = new InlineUIContainer();
inlineCollection.Add(container);
container.Child = new InlineCodeElement(inlineCode)
var inline = new Span();
inlineCollection.Add(inline);
inline.Inlines.Add(new Run() { Text = " " });
inline.Inlines.Add(new Run()
{
FontSize = container.FontSize,
FontWeight = container.FontWeight,
FontStretch = container.FontStretch,
TextDecorations = container.TextDecorations,
};
}
FontFamily = new FontFamily("Consolas"),
Text = inlineCode.Content,
});
inline.Inlines.Add(new Run() { Text = " " });
_codeBlocks.Add(inline);
}
break;
case Timestamp timeStamp:
{
Expand Down Expand Up @@ -285,6 +332,7 @@ private void RenderMarkdown(IList<ASTRoot> tree)
}
}
}
_grid.Children.Add(_richBlock);
}

/// <summary>
Expand Down
8 changes: 5 additions & 3 deletions src/Libs/Quarrel.Markdown/Themes/Generic.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,11 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MessageRenderer">
<RichTextBlock x:Name="RichBlock"
IsTextSelectionEnabled="False"
TextWrapping="WrapWholeWords"/>
<Grid x:Name="Grid">
<Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="Canvas">

</Canvas>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
Expand Down

0 comments on commit 1b15552

Please sign in to comment.