diff --git a/src/Spec2-Adapters-Morphic/SpMorphicBaseTextAdapter.class.st b/src/Spec2-Adapters-Morphic/SpMorphicBaseTextAdapter.class.st index 0ae1adb6..fec479c9 100644 --- a/src/Spec2-Adapters-Morphic/SpMorphicBaseTextAdapter.class.st +++ b/src/Spec2-Adapters-Morphic/SpMorphicBaseTextAdapter.class.st @@ -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 ] @@ -289,6 +296,12 @@ SpMorphicBaseTextAdapter >> showContextMenu [ in: self currentWorld ] ] ] +{ #category : 'accessing' } +SpMorphicBaseTextAdapter >> text: aString [ + + self subclassResponsibility +] + { #category : 'emulating' } SpMorphicBaseTextAdapter >> type: aString [ diff --git a/src/Spec2-Adapters-Morphic/SpMorphicTextAdapter.class.st b/src/Spec2-Adapters-Morphic/SpMorphicTextAdapter.class.st index ea146944..2deda2e0 100644 --- a/src/Spec2-Adapters-Morphic/SpMorphicTextAdapter.class.st +++ b/src/Spec2-Adapters-Morphic/SpMorphicTextAdapter.class.st @@ -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 | @@ -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' } @@ -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 | diff --git a/src/Spec2-Adapters-Morphic/SpMorphicTextInputFieldAdapter.class.st b/src/Spec2-Adapters-Morphic/SpMorphicTextInputFieldAdapter.class.st index 20bff443..b8f1f4e2 100644 --- a/src/Spec2-Adapters-Morphic/SpMorphicTextInputFieldAdapter.class.st +++ b/src/Spec2-Adapters-Morphic/SpMorphicTextInputFieldAdapter.class.st @@ -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 | @@ -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' } @@ -116,7 +118,8 @@ SpMorphicTextInputFieldAdapter >> text: aString [ widget setText: aString; - acceptTextInModel + acceptTextInModel. + self cursorPositionIndex: aString size + 1 ] { #category : 'emulating' } diff --git a/src/Spec2-Core/SpAbstractListPresenter.class.st b/src/Spec2-Core/SpAbstractListPresenter.class.st index a99e5f5e..94c603bf 100644 --- a/src/Spec2-Core/SpAbstractListPresenter.class.st +++ b/src/Spec2-Core/SpAbstractListPresenter.class.st @@ -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', diff --git a/src/Spec2-Core/SpAbstractTextPresenter.class.st b/src/Spec2-Core/SpAbstractTextPresenter.class.st index f8c5e6c6..e996ac16 100644 --- a/src/Spec2-Core/SpAbstractTextPresenter.class.st +++ b/src/Spec2-Core/SpAbstractTextPresenter.class.st @@ -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 ] @@ -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. diff --git a/src/Spec2-Tests/SpAbstractTextPresenterTest.class.st b/src/Spec2-Tests/SpAbstractTextPresenterTest.class.st index 370269aa..4cc9f02a 100644 --- a/src/Spec2-Tests/SpAbstractTextPresenterTest.class.st +++ b/src/Spec2-Tests/SpAbstractTextPresenterTest.class.st @@ -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' } @@ -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 | diff --git a/src/Spec2-Tests/SpTextPresenterTest.class.st b/src/Spec2-Tests/SpTextPresenterTest.class.st index 865b4ea3..31c7cd10 100644 --- a/src/Spec2-Tests/SpTextPresenterTest.class.st +++ b/src/Spec2-Tests/SpTextPresenterTest.class.st @@ -113,6 +113,7 @@ SpTextPresenterTest >> testSelectLine [ with multiple lines'. self openInstance. + presenter cursorPositionIndex: 1. presenter selectLine. self assert: presenter selectionInterval equals: (1 to: 15) ]