add horizontal scroll bar for text editors

This commit is contained in:
haven1433 2023-07-09 23:34:20 -05:00
parent fa1bcb2d66
commit addc0fae41
2 changed files with 29 additions and 8 deletions

View File

@ -4,41 +4,42 @@
xmlns:local="clr-namespace:HavenSoft.HexManiac.WPF.Controls">
<Grid Background="{DynamicResource Backlight}" TextBlock.FontFamily="Consolas" ClipToBounds="True">
<Canvas ClipToBounds="False" Width="0" HorizontalAlignment="Left">
<TextBlock Name="BasicLayer" Foreground="{DynamicResource Primary}" Margin="2" Text="{Binding PlainContent}" Width="{Binding ActualWidth, ElementName=TransparentLayer}">
<TextBlock Name="BasicLayer" Foreground="{DynamicResource Primary}" Margin="2" Text="{Binding PlainContent}">
<TextBlock.RenderTransform>
<TranslateTransform />
</TextBlock.RenderTransform>
</TextBlock>
<TextBlock Name="AccentLayer" Foreground="{DynamicResource Accent}" Margin="2" Text="{Binding AccentContent}" Width="{Binding ActualWidth, ElementName=TransparentLayer}">
<TextBlock Name="AccentLayer" Foreground="{DynamicResource Accent}" Margin="2" Text="{Binding AccentContent}">
<TextBlock.RenderTransform>
<TranslateTransform />
</TextBlock.RenderTransform>
</TextBlock>
<TextBlock Name="ConstantsLayer" Foreground="{DynamicResource Data1}" Margin="2" Text="{Binding ConstantContent}" Width="{Binding ActualWidth, ElementName=TransparentLayer}">
<TextBlock Name="ConstantsLayer" Foreground="{DynamicResource Data1}" Margin="2" Text="{Binding ConstantContent}">
<TextBlock.RenderTransform>
<TranslateTransform />
</TextBlock.RenderTransform>
</TextBlock>
<TextBlock Name="TextLayer" Foreground="{DynamicResource Text1}" Margin="2" Text="{Binding TextContent}" Width="{Binding ActualWidth, ElementName=TransparentLayer}">
<TextBlock Name="TextLayer" Foreground="{DynamicResource Text1}" Margin="2" Text="{Binding TextContent}">
<TextBlock.RenderTransform>
<TranslateTransform />
</TextBlock.RenderTransform>
</TextBlock>
<TextBlock Name="NumericLayer" Foreground="{DynamicResource Data2}" Margin="2" Text="{Binding NumericContent}" Width="{Binding ActualWidth, ElementName=TransparentLayer}">
<TextBlock Name="NumericLayer" Foreground="{DynamicResource Data2}" Margin="2" Text="{Binding NumericContent}">
<TextBlock.RenderTransform>
<TranslateTransform />
</TextBlock.RenderTransform>
</TextBlock>
<TextBlock Name="CommentLayer" Foreground="{DynamicResource Secondary}" Margin="2" Width="{Binding ActualWidth, ElementName=TransparentLayer}">
<TextBlock Name="CommentLayer" Foreground="{DynamicResource Secondary}" Margin="2">
<TextBlock.RenderTransform>
<TranslateTransform />
</TextBlock.RenderTransform>
</TextBlock>
</Canvas>
<TextBox Name="TransparentLayer" Background="Transparent" CaretBrush="{DynamicResource Primary}" Foreground="Transparent"
UndoLimit="0" AcceptsReturn="True" AcceptsTab="True" ScrollViewer.ScrollChanged="TextScrollChanged"
UndoLimit="0" AcceptsReturn="True" AcceptsTab="True" ScrollViewer.ScrollChanged="TextScrollChanged" HorizontalScrollBarVisibility="Auto"
Tag="{Binding Tag, RelativeSource={RelativeSource AncestorType=local:TextEditor}}"
ContextMenu="{Binding ContextMenuOverride, RelativeSource={RelativeSource AncestorType=local:TextEditor}}"
Text="{Binding Content, UpdateSourceTrigger=PropertyChanged}" VerticalScrollBarVisibility="Visible" />
<Rectangle Name="CornerCover" HorizontalAlignment="Right" VerticalAlignment="Bottom" Fill="{DynamicResource Background}" Margin="1" SnapsToDevicePixels="True" />
</Grid>
</UserControl>

View File

@ -1,6 +1,8 @@
using HavenSoft.HexManiac.Core.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
@ -51,12 +53,28 @@ namespace HavenSoft.HexManiac.WPF.Controls {
public TextEditorViewModel ViewModel => (TextEditorViewModel)DataContext;
private IEnumerable<TextBlock> Layers => new[] { BasicLayer, AccentLayer, ConstantsLayer, NumericLayer, CommentLayer, TextLayer };
public TextEditor() {
InitializeComponent();
TransparentLayer.SelectionChanged += (sender, e) => {
SelectionChanged?.Invoke(this, e);
};
DataContextChanged += HandleDataContextChanged;
// ExtentWidth is not a DependencyProperty, so check for the horizontal scroll bar when the text chanegs
TransparentLayer.TextChanged += (sender, e) => {
// measure the width of the text, since ExtentWidth hasn't been updated yet.
var typeface = new Typeface(TransparentLayer.FontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
var width = new FormattedText(TransparentLayer.Text, CultureInfo.CurrentCulture, FlowDirection, typeface, TransparentLayer.FontSize, Brushes.Transparent, 1).Width;
foreach (var layer in Layers) layer.Width = width;
if (width > TransparentLayer.ViewportWidth) {
CornerCover.Width = 16;
CornerCover.Height = 17;
} else {
CornerCover.Width = 0;
CornerCover.Height = 0;
}
};
}
private void HandleDataContextChanged(object sender, DependencyPropertyChangedEventArgs e) {
@ -99,9 +117,11 @@ namespace HavenSoft.HexManiac.WPF.Controls {
public void ScrollToVerticalOffset(double offset) => TransparentLayer.ScrollToVerticalOffset(offset);
private void TextScrollChanged(object sender, ScrollChangedEventArgs e) {
foreach (var layer in new[] { BasicLayer, AccentLayer, ConstantsLayer, NumericLayer, CommentLayer, TextLayer }) {
foreach (var layer in Layers) {
var transform = (TranslateTransform)layer.RenderTransform;
transform.Y = -TransparentLayer.VerticalOffset;
transform.X = -TransparentLayer.HorizontalOffset;
layer.Width = TransparentLayer.ExtentWidth;
}
}