Skip to content

Commit

Permalink
fixes #1653
Browse files Browse the repository at this point in the history
I also made some clean up because I was using the dependent mechanism to update text (and then it was updated again by listening to presenters), this should now be fine.
  • Loading branch information
estebanlm committed Nov 15, 2024
1 parent 7393d70 commit 4c5d9b5
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 16 deletions.
13 changes: 13 additions & 0 deletions src/Spec2-Adapters-Morphic/SpMorphicBaseTextAdapter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ SpMorphicBaseTextAdapter >> cursorPositionIndex [
{ #category : 'widget API' }
SpMorphicBaseTextAdapter >> cursorPositionIndex: index [

self widgetDo: [ :w |
self cursorPositionIndex: index to: w ]
]

{ #category : 'widget API' }
SpMorphicBaseTextAdapter >> cursorPositionIndex: index to: aWidget [

self subclassResponsibility
]

Expand Down Expand Up @@ -289,6 +296,12 @@ SpMorphicBaseTextAdapter >> showContextMenu [
in: self currentWorld ] ]
]

{ #category : 'accessing' }
SpMorphicBaseTextAdapter >> text: aString [

self subclassResponsibility
]

{ #category : 'emulating' }
SpMorphicBaseTextAdapter >> type: aString [

Expand Down
19 changes: 14 additions & 5 deletions src/Spec2-Adapters-Morphic/SpMorphicTextAdapter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ SpMorphicTextAdapter >> buildWidget [
self setEnabled: self presenter isEnabled to: newWidget.
self setEditable: self presenter isEditable to: newWidget.
self setEditingModeFor: newWidget.

self setText: self presenter text to: newWidget.
self presenter selectionInterval
ifNotNil: [ :anInterval | self selectionInterval: anInterval on: newWidget ].
ifNotEmpty: [ :anInterval | self selectionInterval: anInterval on: newWidget ]
ifEmpty: [
self presenter text ifNotNil: [ :aString |
self cursorPositionIndex: aString size + 1 to: newWidget ] ].

self setWrapWord: self presenter isWrapWord to: newWidget.
self setUndoRedo: self presenter hasUndoRedoHistory to: newWidget.
self presenter whenTextChangedDo: [ :text |
self setText: text to: newWidget ].
self presenter whenSelectionChangedDo: [ :selectionInterval |
self selectionInterval: selectionInterval ].
self presenter whenPlaceholderChangedDo: [ :text |
Expand Down Expand Up @@ -132,9 +134,9 @@ SpMorphicTextAdapter >> cursorPosition [
]

{ #category : 'widget API' }
SpMorphicTextAdapter >> cursorPositionIndex: index [
SpMorphicTextAdapter >> cursorPositionIndex: index to: aWidget [

self widgetDo: [ :w | w textArea pointBlock: (w textArea paragraph characterBlockForIndex: index) ]
aWidget textArea pointBlock: (aWidget textArea paragraph characterBlockForIndex: index)
]

{ #category : 'commands' }
Expand Down Expand Up @@ -240,6 +242,13 @@ SpMorphicTextAdapter >> setWrapWord: aBoolean to: aWidget [
ifFalse: [ aWidget beNotWrapped ]
]

{ #category : 'accessing' }
SpMorphicTextAdapter >> text: aString [

self setText: aString to: widget.
self cursorPositionIndex: aString size + 1
]

{ #category : 'private' }
SpMorphicTextAdapter >> updateExtentPropagationOf: string on: aWidget [
| stringMorph width height |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ SpMorphicTextInputFieldAdapter >> buildWidget [
yourself.

self presenter selectionInterval
ifNotNil: [ :anInterval | self selectionInterval: anInterval on: newWidget ].
self presenter whenTextChangedDo: [ :text | newWidget setText: text ].
ifNotEmpty: [ :anInterval | self selectionInterval: anInterval on: newWidget ]
ifEmpty: [
self presenter text ifNotNil: [ :aString |
self cursorPositionIndex: aString size + 1 to: newWidget ] ].
self presenter whenSelectionChangedDo: [ :selectionInterval |
self selectionInterval: selectionInterval ].
self presenter whenPlaceholderChangedDo: [ :text |
Expand All @@ -62,9 +64,9 @@ SpMorphicTextInputFieldAdapter >> buildWidget [
]

{ #category : 'widget API' }
SpMorphicTextInputFieldAdapter >> cursorPositionIndex: index [
SpMorphicTextInputFieldAdapter >> cursorPositionIndex: index to: aWidget [

self widgetDo: [ :w | ^ w textArea editor selectAt: index ]
aWidget textArea editor selectAt: index
]

{ #category : 'widget API' }
Expand Down Expand Up @@ -116,7 +118,8 @@ SpMorphicTextInputFieldAdapter >> text: aString [

widget
setText: aString;
acceptTextInModel
acceptTextInModel.
self cursorPositionIndex: aString size + 1
]

{ #category : 'emulating' }
Expand Down
4 changes: 2 additions & 2 deletions src/Spec2-Core/SpAbstractListPresenter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ A base for list presenters, it defines basic functionality common to all lists.
Class {
#name : 'SpAbstractListPresenter',
#superclass : 'SpAbstractWidgetPresenter',
#traits : 'SpTHaveWrappingScrollBars + SpTContextMenu + SpTAlignableColumn + SpTSelectionMode',
#classTraits : 'SpTHaveWrappingScrollBars classTrait + SpTContextMenu classTrait + SpTAlignableColumn classTrait + SpTSelectionMode classTrait',
#traits : 'SpTHaveWrappingScrollBars + SpTContextMenu + SpTAlignableColumn + SpTSelectionMode + SpTSearchable',
#classTraits : 'SpTHaveWrappingScrollBars classTrait + SpTContextMenu classTrait + SpTAlignableColumn classTrait + SpTSelectionMode classTrait + SpTSearchable classTrait',
#instVars : [
'#activationBlock',
'#activateOnSingleClick',
Expand Down
10 changes: 9 additions & 1 deletion src/Spec2-Core/SpAbstractTextPresenter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ SpAbstractTextPresenter >> registerActions [
{ #category : 'initialization' }
SpAbstractTextPresenter >> registerEvents [

self whenTextChangedDo: [ self changed: #getText ].
self whenTextChangedDo: [ :aString |
self updateTextFromModel: aString ].
self
property: #selection
whenChangedDo: [ :aSelection | self updateSelectionFromModel: aSelection ]
Expand Down Expand Up @@ -338,6 +339,13 @@ SpAbstractTextPresenter >> updateSelectionFromModel: aSelection [
anAdapter setSelectionFromModel: aSelection ]
]

{ #category : 'private' }
SpAbstractTextPresenter >> updateTextFromModel: aString [

self withAdapterDo: [ :anAdapter |
anAdapter text: aString ]
]

{ #category : 'api - events' }
SpAbstractTextPresenter >> whenEditableChangedDo: aBlock [
"Inform when editable property has changed.
Expand Down
19 changes: 16 additions & 3 deletions src/Spec2-Tests/SpAbstractTextPresenterTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,14 @@ SpAbstractTextPresenterTest >> testCursorPositionIndex [

presenter text: (String loremIpsum: 80).
self openInstance. "cursor position does not works if instance is not opened"
"If not defined, cursor is at begining"
self assert: presenter cursorPositionIndex equals: 1.
"If not defined, cursor is at the end of a "
self assert: presenter cursorPositionIndex equals: 81.
"middle"
presenter cursorPositionIndex: 20.
self assert: presenter cursorPositionIndex equals: 20.
"end"
presenter cursorPositionIndex: 81.
self assert: presenter cursorPositionIndex equals: 81

]

{ #category : 'tests' }
Expand Down Expand Up @@ -162,6 +161,20 @@ SpAbstractTextPresenterTest >> testSelectAllWithoutOpening [
self assert: presenter selectionInterval equals: (1 to: 15)
]

{ #category : 'tests' }
SpAbstractTextPresenterTest >> testTextIsSetAndCursorPositionedCorrectly [

presenter text: 'aText'.
"no cursor position before is open"
self assert: presenter cursorPositionIndex equals: nil.
self openInstance.
"now it has to be placed at the end"
self assert: presenter cursorPositionIndex equals: 6.
"now change it already opened"
presenter text: 'aTextAText'.
self assert: presenter cursorPositionIndex equals: 11
]

{ #category : 'tests' }
SpAbstractTextPresenterTest >> testWhenResetDo [
| reseted |
Expand Down
1 change: 1 addition & 0 deletions src/Spec2-Tests/SpTextPresenterTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ SpTextPresenterTest >> testSelectLine [
with multiple
lines'.
self openInstance.
presenter cursorPositionIndex: 1.
presenter selectLine.
self assert: presenter selectionInterval equals: (1 to: 15)
]
Expand Down

0 comments on commit 4c5d9b5

Please sign in to comment.