You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello. I made a custom VisualLineElementGenerator where I draw a box similar to how the control character boxes are done.
The difference with mine, I use a longer length than 1 in FormattedTextElement constructor which seems to cause issues with the caret position as it advances much further out than it should be the more tags that are written before it.
The idea is I want to hide text longer than 1 character within [] brackets and create a custom visual for these.
A way to replicate this.
public MessageEditor(){
InitializeComponent();
_textEditor.TextArea.TextView.ElementGenerators.Add(new ControlCodeElementGenerator(_textEditor.TextArea));
_textEditor.Text ="Test [Color::Red][Text::Bold]aaa This text cannot be selected at the right pos[Text::Normal]Test";}publicclassControlCodeElementGenerator:VisualLineElementGenerator{privatereadonlyRegex_regex;privatereadonlyTextArea_textArea;publicControlCodeElementGenerator(TextAreatextArea){_regex=new Regex(@"\[.*?\]");//[] text are tags where we want to custom display_textArea=textArea;}publicoverrideintGetFirstInterestedOffset(intstartOffset){//get the placement of the a tag [] for constructing a visual element at the text placevartext= CurrentContext.Document.Text;varmatch= _regex.Match(text, startOffset);return match.Success ? match.Index :-1;}publicoverride VisualLineElement ConstructElement(intoffset){vartext= CurrentContext.Document.Text;varmatch= _regex.Match(text, offset);if(match.Success && match.Index ==offset){//constructs a box like the control character boxvarrunProperties=new VisualLineElementTextRunProperties(CurrentContext.GlobalTextRunProperties);
runProperties.SetForegroundBrush(Brushes.White);varcontrolCode= match.Value;vartextLine= FormattedTextElement.PrepareText(
TextFormatter.Current,"[Tag]", runProperties);returnnew SpecialCharacterBoxElement(textLine, controlCode.Length);//only if length is 1, caret pos works correctly}returnnull;}}publicclassSpecialCharacterBoxElement:FormattedTextElement{privateint_length;publicSpecialCharacterBoxElement(TextLinetext,intlength):base(text, length)//todo only if length is 1, caret pos works correctly{_length=length;}publicoverrideintGetNextCaretPosition(intvisualColumn, AvaloniaEdit.Document.LogicalDirection direction,CaretPositioningModemode){if(mode== CaretPositioningMode.Normal ||mode== CaretPositioningMode.EveryCodepoint)returnbase.GetNextCaretPosition(visualColumn, direction, mode);elsereturn-1;}publicoverride TextRun CreateTextRun(intstartVisualColumn,ITextRunConstructionContextcontext){returnnew SpecialCharacterTextRun(this, TextRunProperties);}}publicclassSpecialCharacterTextRun:FormattedTextRun{privatestaticreadonlyISolidColorBrushDarkGrayBrush;internalconstdoubleBoxMargin=3;staticSpecialCharacterTextRun(){DarkGrayBrush=new ImmutableSolidColorBrush(Color.FromArgb(200,128,128,128));}publicSpecialCharacterTextRun(FormattedTextElementelement,TextRunPropertiesproperties):base(element, properties){}publicoverrideSizeSize{get{vars=base.Size;return s.WithWidth(s.Width +BoxMargin);}}publicoverridevoidDraw(DrawingContextdrawingContext,Pointorigin){var(x, y)= origin;varnewOrigin=new Point(x+(BoxMargin/2), y);var(width, height)= Size;varr=new Rect(x, y, width, height);
drawingContext.FillRectangle(DarkGrayBrush, r,2.5f);base.Draw(drawingContext, newOrigin);}}
The text was updated successfully, but these errors were encountered:
Hello. I made a custom VisualLineElementGenerator where I draw a box similar to how the control character boxes are done.
The difference with mine, I use a longer length than 1 in FormattedTextElement constructor which seems to cause issues with the caret position as it advances much further out than it should be the more tags that are written before it.
The idea is I want to hide text longer than 1 character within [] brackets and create a custom visual for these.
A way to replicate this.
The text was updated successfully, but these errors were encountered: