dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.03k stars 1.16k forks source link

[API Proposal]: Add GetCharacterIndexFromPoint method to TextBlock #9235

Open rampaa opened 3 months ago

rampaa commented 3 months ago

Background and motivation

In my app I need to get the character index from the mouse position. Even though I don't need the capabilities of a TextBox, because TextBlock does not have a method like GetCharacterIndexFromPoint, I need to use the TextBox control instead. The issue is my app creates lots of TextBoxes (which could have been TextBlocks if a TextBlock supported the aforementioned method) and a TextBox is more resource hungry than a TextBlock. So I'd like to avoid paying the price of a TextBox unnecessarily.

API Proposal

public int GetCharacterIndexFromPoint (System.Windows.Point point, bool snapToText);

API Usage

TextBlock textBlock = new();
int characterPosition = textBlock.GetCharacterIndexFromPoint(Mouse.GetPosition(textBlock), false);

Alternative Designs

No response

Risks

No response

miloush commented 3 months ago

Depending on the application, you might be better with lower-level API in System.Windows.Media.TextFormatting like TextLine.

The API request is not unreasonable but also it is non-trivial to implement.

lindexi commented 2 months ago

How about a Readonly TextBox ?

rampaa commented 2 months ago

How about a Readonly TextBox ?

I'm already using TextBoxes by setting their IsReadOnly property to true, if that's what you are asking. I am also setting IsUndoEnabled to false, UndoLimit to 0 and AutoWordSelection to false for good measure.