Handling text events

Text supports interactions through the subclasses of BrTextEventHandlerAttribute BlTextAttribute subclass: #BrTextEventHandlerAttribute instanceVariableNames: '' classVariableNames: '' package: 'Brick-Editor-Attributes' . 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 BrTextEventHandlerAttribute subclass: #BrTextCursorAttribute instanceVariableNames: 'enterAction leaveAction isInside' classVariableNames: '' package: 'Brick-Editor-Attributes' . The example below relies on TBlTextStyleable>>#onCursorEnter:leave: onCursorEnter: anEnterAction leave: aLeaveAction "Perform actions when a cursor enters and leaves a piece of text. The enter action is a block in the following form: [ :aTextEditor :aBrTextEditorCursorAddedEvent :anAttributeStart :anAttributeEnd | ] The leave action is a block in the following form: [ :aTextEditor :aBrTextEditorCursorRemovedEvent :anAttributeStart :anAttributeEnd | ]" self attributesBuilder attribute: (BrTextCursorAttribute new enterAction: anEnterAction; leaveAction: aLeaveAction) 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 hideMarkupInText <gtExample> <return: #BrEditor> | text leftMarker rightMarker | text := 'Move the cursor **here** to show the markup' asRopedText fontSize: 20. (text from: 19 to: 22) bold. (text from: 17 to: 18) foreground: Color blue; attribute: (leftMarker := BrTextInvisibleMarkerAttribute new). (text from: 23 to: 24) foreground: Color blue; attribute: (rightMarker := BrTextInvisibleMarkerAttribute new). (text from: 15 to: 26) onCursorEnter: [ :aTextEditor :anEvent :aFromIndex :aToIndex | | aSubText | (aSubText := aTextEditor text from: aFromIndex to: aToIndex) findAttributes: {leftMarker. rightMarker} indicesDo: [ :aMarkupStart :aMarkupEnd | (aSubText from: aMarkupStart to: aMarkupEnd) clearAttributesOfClass: BrTextHideAttribute ] ] leave: [ :aTextEditor :anEvent :aFromIndex :aToIndex | | aSubText | (aSubText := aTextEditor text from: aFromIndex to: aToIndex) findAttributes: {leftMarker. rightMarker} indicesDo: [ :aMarkupStart :aMarkupEnd | (aSubText from: aMarkupStart to: aMarkupEnd) attribute: BrTextHideAttribute new ] ]. ^ BrEditor new aptitude: BrGlamorousEditorAptitude new; text: text for a more elaborate example.

Clicks can also be handled through the dedicated BrTextClickAttribute BrTextEventHandlerAttribute subclass: #BrTextClickAttribute instanceVariableNames: 'action' classVariableNames: '' package: 'Brick-Editor-Attributes' . 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