Handling text events
Text supports interactions through the subclasses of BrTextEventHandlerAttribute
. For example, the link to the class from the previous sentence is an adornment, but if you get with the cursor next to it, you get the expanded markup language; and when the cursor goes away, the markup disappears.
We rely on this pattern often throughout the environment as it leads to a new kind of editing which is in between an edit/view and WYSWYG interfaces. This is achieved through a BrTextCursorAttribute
. The example below relies on TBlTextStyleable>>#onCursorEnter:leave:
to change the background of a text range depending on the cursor:
changingForeground <gtExample> <return: #BlRunRopedText> | text | text := 'Hello World - place the cursor here - second item' asRopedText fontSize: 20. (text from: 14 to: 35) foreground: Color blue; onCursorEnter: [ :aTextEditor :anEvent :aFromIndex :aToIndex | (aTextEditor text from: aFromIndex to: aToIndex) foreground: Color red ] leave: [ :aTextEditor :anEvent :aFromIndex :aToIndex | (aTextEditor text from: aFromIndex to: aToIndex) foreground: Color blue ]. ^ text
To get to the magic hiding on cursor move, we need a bit of a more elaborate logic. Take a look at: BrTextAttributesHowToGuide>>#hideMarkupInText
for a more elaborate example.
Clicks can also be handled through the dedicated BrTextClickAttribute
. For example, here can simulate a link-like behavior which beside clicking also handles hovering to show an underline decoration.
linkInText <gtExample> <return: #BlRunRopedText> | text | text := 'You can click on this.' asRopedText. (text from: 9 to: 13) foreground: Color blue; onClick: [ :aTBrTextEditorTextualPiece :aTarget :aTextEditor :anEvent | anEvent currentTarget phlow spawnObject: {aTBrTextEditorTextualPiece. aTarget. aTextEditor. anEvent} ]; attribute: (BrTextHoverStylableAttribute new attribute: (BlTextDecorationAttribute new underline color: Color blue)). ^ text