Skip to content

Commit

Permalink
Merge pull request #308 from doug24/right-to-left-flowdirection
Browse files Browse the repository at this point in the history
Add support for FlowDirection.RightToLeft as overall flow direction of the editor.
  • Loading branch information
dgrunwald authored Sep 4, 2022
2 parents ed6f2b5 + 67b186f commit 08717da
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 9 deletions.
13 changes: 13 additions & 0 deletions ICSharpCode.AvalonEdit/Editing/CaretNavigationCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ static ExecutedRoutedEventHandler OnMoveCaretBoxSelection(CaretMovementType dire
internal static void MoveCaret(TextArea textArea, CaretMovementType direction)
{
double desiredXPos = textArea.Caret.DesiredXPos;

if (textArea.FlowDirection == FlowDirection.RightToLeft) {
if (direction == CaretMovementType.CharLeft) {
direction = CaretMovementType.CharRight;
} else if (direction == CaretMovementType.CharRight) {
direction = CaretMovementType.CharLeft;
} else if (direction == CaretMovementType.WordRight) {
direction = CaretMovementType.WordLeft;
} else if (direction == CaretMovementType.WordLeft) {
direction = CaretMovementType.WordRight;
}
}

textArea.Caret.Position = GetNewCaretPosition(textArea.TextView, textArea.Caret.Position, direction, textArea.Selection.EnableVirtualSpace, ref desiredXPos);
textArea.Caret.DesiredXPos = desiredXPos;
}
Expand Down
11 changes: 11 additions & 0 deletions ICSharpCode.AvalonEdit/Editing/LineNumberMargin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ namespace ICSharpCode.AvalonEdit.Editing
/// </summary>
public class LineNumberMargin : AbstractMargin, IWeakEventListener
{
/// <summary>
/// Creates a new instance of a LineNumberMargin
/// </summary>
public LineNumberMargin()
:base()
{
// override Property Value Inheritance, and always render
// the line number margin left-to-right
FlowDirection = FlowDirection.LeftToRight;
}

static LineNumberMargin()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(LineNumberMargin),
Expand Down
11 changes: 8 additions & 3 deletions ICSharpCode.AvalonEdit/Rendering/TextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,8 @@ VisualLineTextParagraphProperties CreateParagraphProperties(TextRunProperties de
return new VisualLineTextParagraphProperties {
defaultTextRunProperties = defaultTextRunProperties,
textWrapping = canHorizontallyScroll ? TextWrapping.NoWrap : TextWrapping.Wrap,
tabSize = Options.IndentationSize * WideSpaceWidth
tabSize = Options.IndentationSize * WideSpaceWidth,
flowDirection = FlowDirection
};
}

Expand Down Expand Up @@ -1561,7 +1562,10 @@ void CalculateDefaultTextMetrics()
using (var line = formatter.FormatLine(
new SimpleTextSource("x", textRunProperties),
0, 32000,
new VisualLineTextParagraphProperties { defaultTextRunProperties = textRunProperties },
new VisualLineTextParagraphProperties {
defaultTextRunProperties = textRunProperties,
flowDirection = FlowDirection
},
null)) {
wideSpaceWidth = Math.Max(1, line.WidthIncludingTrailingWhitespace);
defaultBaseline = Math.Max(1, line.Baseline);
Expand Down Expand Up @@ -2029,7 +2033,8 @@ protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
|| e.Property == Control.FontSizeProperty
|| e.Property == Control.FontStretchProperty
|| e.Property == Control.FontStyleProperty
|| e.Property == Control.FontWeightProperty) {
|| e.Property == Control.FontWeightProperty
|| e.Property == Control.FlowDirectionProperty) {
// changing font properties requires recreating cached elements
RecreateCachedElements();
// and we need to re-measure the font metrics:
Expand Down
9 changes: 7 additions & 2 deletions ICSharpCode.AvalonEdit/Rendering/VisualLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ internal VisualLineDrawingVisual Render()
{
Debug.Assert(phase == LifetimePhase.Live);
if (visual == null)
visual = new VisualLineDrawingVisual(this);
visual = new VisualLineDrawingVisual(this, textView.FlowDirection);
return visual;
}
}
Expand All @@ -744,13 +744,18 @@ sealed class VisualLineDrawingVisual : DrawingVisual
public readonly double Height;
internal bool IsAdded;

public VisualLineDrawingVisual(VisualLine visualLine)
public VisualLineDrawingVisual(VisualLine visualLine, FlowDirection flow)
{
this.VisualLine = visualLine;
var drawingContext = RenderOpen();
double pos = 0;
foreach (TextLine textLine in visualLine.TextLines) {
if (flow == FlowDirection.LeftToRight) {
textLine.Draw(drawingContext, new Point(0, pos), InvertAxes.None);
} else {
// Invert Axis for RightToLeft (Arabic language) support
textLine.Draw(drawingContext, new Point(0, pos), InvertAxes.Horizontal);
}
pos += textLine.Height;
}
this.Height = pos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ sealed class VisualLineTextParagraphProperties : TextParagraphProperties
internal double tabSize;
internal double indent;
internal bool firstLineInParagraph;

internal FlowDirection flowDirection;
public override double DefaultIncrementalTab {
get { return tabSize; }
}

public override FlowDirection FlowDirection { get { return FlowDirection.LeftToRight; } }
public override FlowDirection FlowDirection { get { return flowDirection; } }
public override TextAlignment TextAlignment { get { return TextAlignment.Left; } }
public override double LineHeight { get { return double.NaN; } }
public override bool FirstLineInParagraph { get { return firstLineInParagraph; } }
Expand Down
2 changes: 0 additions & 2 deletions ICSharpCode.AvalonEdit/TextEditor.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
<Style TargetType="{x:Type AvalonEdit:TextEditor}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
<Setter Property="FlowDirection" Value="LeftToRight"/>
<!-- AvalonEdit does not support RTL, so ensure we use LTR by default -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type AvalonEdit:TextEditor}">
Expand Down

0 comments on commit 08717da

Please sign in to comment.