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.
publicMessageEditor(){InitializeComponent();_textEditor.TextArea.TextView.ElementGenerators.Add(newControlCodeElementGenerator(_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=newRegex(@"\[.*?\]");//[] 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);returnmatch.Success?match.Index:-1;}publicoverrideVisualLineElementConstructElement(intoffset){vartext=CurrentContext.Document.Text;varmatch=_regex.Match(text,offset);if(match.Success&&match.Index==offset){//constructs a box like the control character boxvarrunProperties=newVisualLineElementTextRunProperties(CurrentContext.GlobalTextRunProperties);runProperties.SetForegroundBrush(Brushes.White);varcontrolCode=match.Value;vartextLine=FormattedTextElement.PrepareText(TextFormatter.Current,"[Tag]",runProperties);returnnewSpecialCharacterBoxElement(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.LogicalDirectiondirection,CaretPositioningModemode){if(mode==CaretPositioningMode.Normal||mode==CaretPositioningMode.EveryCodepoint)returnbase.GetNextCaretPosition(visualColumn,direction,mode);elsereturn-1;}publicoverrideTextRunCreateTextRun(intstartVisualColumn,ITextRunConstructionContextcontext){returnnewSpecialCharacterTextRun(this,TextRunProperties);}}publicclassSpecialCharacterTextRun:FormattedTextRun{privatestaticreadonlyISolidColorBrushDarkGrayBrush;internalconstdoubleBoxMargin=3;staticSpecialCharacterTextRun(){DarkGrayBrush=newImmutableSolidColorBrush(Color.FromArgb(200,128,128,128));}publicSpecialCharacterTextRun(FormattedTextElementelement,TextRunPropertiesproperties):base(element,properties){}publicoverrideSizeSize{get{vars=base.Size;returns.WithWidth(s.Width+BoxMargin);}}publicoverridevoidDraw(DrawingContextdrawingContext,Pointorigin){var(x,y)=origin;varnewOrigin=newPoint(x+(BoxMargin/2),y);var(width,height)=Size;varr=newRect(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: