I am working on GTK3 app in Linux using C style APIs. I want to have a input field in my app to write a message (maybe multi-line) and in this message, user can tag other user’s using ‘@’ character.
When user presses ‘@’, I want to show selection table with list of user names and upon selecting, it should be added to the TextView as a separate token (maybe having some styles to differentiate from other normal text).
Note that with these type of token-like text, there will be plain text too in the same TextView.
Just to understand better, there is a NSTokenField artifact in MacOS as shown in attached GIF. Do we have any similar artifact in GTK?
No, there is no existing widget inside GTK that works like NSTokenField, and it definitely won’t be included inside GTK3, since that series is feature and API frozen.
There is an old draft merge request for libadwaita to iterate on the “tagged entry” widget, but theoretically nothing prevents you from writing a “token field” equivalent in your own application.
The GtkSourceView library extends GtkTextView with features that are useful for this. There is the GtkSourceCompletion framework.
You can achieve that with GtkTextTag.
For text selection, to select the token (double-click or with keyboard shortcuts), there are some vfuncs or signals to override the default behavior. The same for a “smart backspace/delete” keys, to delete the entire token.
So, it’s achievable but requires some work. The completion system is flexible enough to support this.