Skip to content

Commit 1d49ac6

Browse files
committed
Support PreeditText
1 parent f6ca16b commit 1d49ac6

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

src/AvaloniaEdit/Editing/CaretLayer.cs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// DEALINGS IN THE SOFTWARE.
1818

1919
using System;
20+
using System.Globalization;
2021
using Avalonia;
2122
using Avalonia.Controls;
2223
using Avalonia.Media;
@@ -90,7 +91,44 @@ private void StopBlinkAnimation()
9091
public override void Render(DrawingContext drawingContext)
9192
{
9293
base.Render(drawingContext);
93-
94+
95+
var caretRect = _caretRectangle;
96+
97+
if (!string.IsNullOrEmpty(_textArea.PreeditText))
98+
{
99+
var caretPos = new Point(
100+
caretRect.X - TextView.HorizontalOffset,
101+
caretRect.Y - TextView.VerticalOffset
102+
);
103+
104+
var formattedText = new FormattedText(
105+
_textArea.PreeditText,
106+
CultureInfo.CurrentCulture,
107+
_textArea.FlowDirection,
108+
new Typeface(_textArea.FontFamily, _textArea.FontStyle, _textArea.FontWeight,
109+
_textArea.FontStretch),
110+
_textArea.FontSize,
111+
Brushes.Black
112+
);
113+
114+
var textBounds = new Rect(
115+
caretPos.X,
116+
caretPos.Y,
117+
formattedText.Width,
118+
formattedText.Height
119+
);
120+
drawingContext.FillRectangle(Brushes.White, textBounds);
121+
122+
drawingContext.DrawText(formattedText, caretPos);
123+
124+
caretRect = new Rect(
125+
caretRect.X + formattedText.Width,
126+
caretRect.Y,
127+
caretRect.Width,
128+
caretRect.Height
129+
);
130+
}
131+
94132
if (_isVisible && _blink)
95133
{
96134
var caretBrush = CaretBrush ?? TextView.GetValue(TextBlock.ForegroundProperty);
@@ -105,13 +143,14 @@ public override void Render(DrawingContext drawingContext)
105143
}
106144
}
107145

108-
var r = new Rect(_caretRectangle.X - TextView.HorizontalOffset,
109-
_caretRectangle.Y - TextView.VerticalOffset,
110-
_caretRectangle.Width,
111-
_caretRectangle.Height);
146+
var r = new Rect(caretRect.X - TextView.HorizontalOffset,
147+
caretRect.Y - TextView.VerticalOffset,
148+
caretRect.Width,
149+
caretRect.Height);
112150

113-
drawingContext.FillRectangle(caretBrush, PixelSnapHelpers.Round(r, PixelSnapHelpers.GetPixelSize(this)));
151+
drawingContext.FillRectangle(caretBrush,
152+
PixelSnapHelpers.Round(r, PixelSnapHelpers.GetPixelSize(this)));
114153
}
115154
}
116155
}
117-
}
156+
}

src/AvaloniaEdit/Editing/TextArea.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,15 @@ public IBrush CaretBrush
774774
get => GetValue(CaretBrushProperty);
775775
set => SetValue(CaretBrushProperty, value);
776776
}
777+
778+
/// <summary>
779+
/// Gets the preedit text (text currently being composed using an input method).
780+
/// </summary>
781+
internal string PreeditText
782+
{
783+
get; private set;
784+
}
785+
777786
#endregion
778787

779788
#region Focus Handling (Show/Hide Caret)
@@ -1223,7 +1232,7 @@ public override Rect CursorRectangle
12231232

12241233
public override Visual TextViewVisual => _textArea;
12251234

1226-
public override bool SupportsPreedit => false;
1235+
public override bool SupportsPreedit => true;
12271236

12281237
public override bool SupportsSurroundingText => true;
12291238

@@ -1296,7 +1305,12 @@ private void Caret_PositionChanged(object sender, EventArgs e)
12961305

12971306
public override void SetPreeditText(string text)
12981307
{
1308+
if (_textArea == null)
1309+
return;
1310+
1311+
_textArea.PreeditText = text;
12991312

1313+
_textArea.TextView.InvalidateLayer(KnownLayer.Caret);
13001314
}
13011315
}
13021316
}

0 commit comments

Comments
 (0)