[Cuis-dev] dependency mechanism for TextModelMorph

Weslleymberg Lisboa weslleym.lisboa at gmail.com
Mon Mar 24 10:40:59 PDT 2025


Thats interesting... I tested it on a clean image updated to #7088. It 
asks to save the file on the first key stroke and generates the 
#clearUserEdits every time. Attached is a FileOut of the class I used.

When inserting text, TextModel's actualContent is mainly changed through 
TextModel>>#basicReplaceFrom:to:with: and not by #actualContents:. 
Unfortunately, I think this is the furthest we can go with a clean base 
image. Anything beyond that will require you to change it or subclass 
TextModel or TextModelMorph to make it generate the desired notifications.

I tried modifying #basicReplaceFrom:to:with: to include "self changed: 
#actualContents" but it caused a bug that makes cursor jump to the 
beginning of the string on every key stroke. Don't know why... since 
#basicReplaceFrom:to:with: does note return anything specific.

Em 24/03/2025 09:41, Mark Volkmann escreveu:
> Your solution does cause the update: method to be called, but only on 
> the first keystroke in the TextModelMorph. The argument passed to 
> update: is the symbol #clearUserEdits. I want a solution that invokes 
> update: on every keystroke. I know I could listen for key events, but 
> I'd really like to find a solution that utilizes the dependency 
> mechanism. Here is the latest code in the initialize method:
> 
> initialize
>      | tmm |
> 
>      tmm := TextModelMorph withText: '' :: acceptOnAny: true.
>      tmm model addDependent: self.
>      SystemWindow new
>          addMorph: tmm;
>          model: tmm model;
>          openInWorld.
> 
> On Sun, Mar 23, 2025 at 10:24 PM Weslleymberg Lisboa 
> <weslleym.lisboa at gmail.com <mailto:weslleym.lisboa at gmail.com>> wrote:
> 
>     This works:
> 
>     initialize
> 
>              | tmm window |
> 
>              tmm := TextModelMorph withText: '' :: acceptOnAny: true.
> 
>              "tmm model when: #changed: send: #update: to: self.
>     (Samething as below)"
>              tmm model addDependent: self.
> 
>              SystemWindow new addMorph: tmm; model: tmm model; openInWorld.
> 
> 
>     update: anEvent
> 
>              (anEvent asString, ' triggered!') print.
> 
>     Have no idea at the moment why this works and your example doesn't. But
>     I looked on how TextEditor class>>#open and SystemWindow
>     class>>#editString:label:wrap: does it to see what needs to be set
>     before opening the window.
> 
> 
>     Em 23/03/2025 18:31, Mark Volkmann escreveu:
>      > I must be doing a poor job of describing what I want to achieve.
>      > I've boiled it down to the minimum code needed to demonstrate.
>      > Here are the steps:
>      >
>      > - create a subclass of Object
>      >
>      > - add the following instance methods
>      >
>      > initialize
>      >      | tmm |
>      >
>      >      tmm := TextModelMorph withText: '' :: acceptOnAny: true.
>      >      SystemWindow new addMorph: tmm; openInWorld.
>      >
>      > update: arg
>      >      'update was called' print
>      >
>      > What can I do in the initialize method so update: is called after
>     every
>      > keystroke in the TextModelMorph?
>      > Here are two things I tried that do not work:
>      >
>      >      tmm model addDependent: self.
>      >
>      >      tmm model when: #actualContents send: #update: to: self.
>      >
>      > On Sun, Mar 23, 2025 at 11:45 AM Weslleymberg Lisboa via Cuis-dev
>     <cuis-
>      > dev at lists.cuis.st <mailto:dev at lists.cuis.st> <mailto:cuis-
>     dev at lists.cuis.st <mailto:cuis-dev at lists.cuis.st>>> wrote:
>      >
>      >     TextModel generates some events. Browse senders of #changed:
>     to see
>      >     it. Maybe the event that #addDepedent: listens to is not the
>     one you
>      >     need.
>      >
>      >     Cuis use the Observer Pattern as a dependency mechanism (see
>     [1]) so
>      >     #addDependent: and #changed: are "simplifications" of
>     #when:send:to:
>      >     and #triggerEvent:with:
>      >
>      >     IMHO if what you need is to be notified of changes in the
>     model, I'd
>      >     use it directly instead of making TextModelMorph notify me.
>      >
>      >     1 - <https://github.com/nmingotti/The-Cuis-CookBook/wiki/The-
>     <https://github.com/nmingotti/The-Cuis-CookBook/wiki/The->
>      >     Dependency-Mechanism--3 <https://github.com/nmingotti/The-
>     Cuis- <https://github.com/nmingotti/The-Cuis->
>      >     CookBook/wiki/The-Dependency-Mechanism--3>>
>      >
>      >
>      >     Em 23 de março de 2025 11:53:10 BRT, Mark Volkmann via Cuis-dev
>      >     <cuis-dev at lists.cuis.st <mailto:cuis-dev at lists.cuis.st>
>     <mailto:cuis-dev at lists.cuis.st <mailto:cuis-dev at lists.cuis.st>>>
>     escreveu:
>      >
>      >         As far as I can tell,TextModelmorph does not
>     send#changed:every
>      >         time its value changes, so that prevents me from
>     listening for
>      >         updates. I see that the TextModelmethodactualContents:does
>      >         send#changed:, but that is not invoked on every change to
>      >         aTextModelMorpheven when it is configured
>     withacceptOnAny: true.
>      >
>      >         On Sun, Mar 23, 2025 at 9:38 AM <ken.dickey at whidbey.com
>     <mailto:ken.dickey at whidbey.com>
>      >         <mailto:ken.dickey at whidbey.com
>     <mailto:ken.dickey at whidbey.com>>> wrote:
>      >
>      >             On 2025-03-23 06:35, Mark Volkmann via Cuis-dev wrote:
>      >
>      >              > ...
>      >              > myTextModelMorph addDependent: self.
>      >             ..
>      >              >
>      >              > I have a few questions.
>      >              > - Are there reasons why I should not want this
>     ability?
>      >
>      >             Generally, Morphs are Views and update to reflect
>     changes in an
>      >             underlying Model.
>      >
>      >             A Morph can be a Model if sensible.
>      >
>      >              > - Would it be better to listen for changes on the
>      >             underlying TextModel?
>      >
>      >             IMHO, yes.
>      >
>      >             $0.02,
>      >             -KenD
>      >
>      >
>      >
>      >
>      >     --
>      >     Att.
>      >     Wéslleymberg Lisboa
>      >     Graduado em Sistemas de Informação
>      >     Docente no IFFluminense - Campus Itaboraí
>      >     Ex-Bolsista de IC do Núcleo de Computação Científica (NC2-IFF) -
>      >     Projeto IFF Aerospace
>      >     Github: https://github.com/weslleymberg <https://github.com/
>     weslleymberg> <https://github.com/ <https://github.com/>
>      >     weslleymberg>
>      >     Tel.: +55 22 99931-2376
>      >     --
>      >     Cuis-dev mailing list
>      > Cuis-dev at lists.cuis.st <mailto:Cuis-dev at lists.cuis.st>
>     <mailto:Cuis-dev at lists.cuis.st <mailto:Cuis-dev at lists.cuis.st>>
>      > https://lists.cuis.st/mailman/listinfo/cuis-dev <https://
>     lists.cuis.st/mailman/listinfo/cuis-dev> <https://
>      > lists.cuis.st/mailman/listinfo/cuis-dev <http://lists.cuis.st/
>     mailman/listinfo/cuis-dev>>
>      >
>      >
>      >
>      > --
>      > R. Mark Volkmann
>      > Object Computing, Inc.
> 
>     -- 
>     Wéslleymberg Lisboa
>     Graduado em Sistemas de Informação
>     Docente no IFFluminense - Campus Itaboraí
> 
> 
> 
> -- 
> R. Mark Volkmann
> Object Computing, Inc.

-- 
Wéslleymberg Lisboa
Graduado em Sistemas de Informação
Docente no IFFluminense - Campus Itaboraí
-------------- next part --------------
'From Cuis7.3 [latest update: #7088] on 24 March 2025 at 12:00:05 am'!
!classDefinition: #NotificationExample category: #NotificationExample!
Object subclass: #NotificationExample
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'NotificationExample'!

!NotificationExample methodsFor: 'as yet unclassified' stamp: 'wbsl 3/23/2025 23:59:40'!
initialize

	| tmm window |
	
	tmm := TextModelMorph withText: '' :: acceptOnAny: true.
	
	"tmm model when: #changed: send: #update: to: self. (Samething as below)"
	tmm model addDependent: self.
	
	window := SystemWindow new addMorph: tmm.
	
	window model: tmm model.
	
	window openInWorld.! !

!NotificationExample methodsFor: 'as yet unclassified' stamp: 'wbsl 3/23/2025 20:29:39'!
update: anEvent
	
	(anEvent asString, ' triggered!!') print.! !


More information about the Cuis-dev mailing list