[Cuis-dev] listening for TextModelMorph changes
Weslleymberg Lisboa
weslleym.lisboa at gmail.com
Mon Jan 13 20:02:47 PST 2025
Em 13/01/2025 22:29, Mark Volkmann escreveu:
> Thanks so much for this tip! Clearly a key to getting this to work is
> knowing the symbols that the watched object passes to the changed:
> method. I see that the TextModel instance method actualContents: uses
> the symbol #actualContents. So I tried the following to test whether I
> could make a sound every time the text in a TextModelMorph is modified:
>
> Preferences at: #soundsEnabled put: true.
> model := TextModel withText: 'Hello'.
> model when: #actualContents send: #primitiveBeep to: Smalltalk.
> input := TextModelMorph withModel: model.
> input openInWorld.
If you look the implementation of #changed: you will see that it
triggers a #changed: event passing along whatever symbol it received as
argument. This should print 'actualContents' in the Transcript:
model := TextModel withText: 'Hello'.
model when: #changed: send: #log: to: Transcript.
model actualContents: 'Hello!'.
It won't work with #primitiveBeep because the #changed: event always
comes with an argument. But the following should produce a sound:
model := TextModel withText: 'Hello'.
model when: #changedContents send: #primitiveBeep to: Smalltalk.
model triggerEvent: #changedContents.
> I don't hear sounds when modifying the value. I thought perhaps the
> problem is that the TextModel instance method actualContents: is not
> called every time the value is modified. But even when I explicitly
> invoke it with model actualContents: 'Test', I don't hear a sound. I do
> hear a sound when I evaluate Smalltalk primitiveBeep.
In fact, TextModelMorph never calls #actualContents:. This is
interesting. I did some digging and following a key stroke the Text
instance that TextModel holds will be modified and the morphs involved
in the process will redraw. So the "Observer Pattern events" we are
trying to catch here will only happen when the TextModel changes for
some reason outside the event processing (mainly keyboard and mouse
events) done by TextModelMorph.
In summary, if a change in the model is caused by an interaction with
TextModelMorph, the Morph knows about the change and will update itself.
But it has no way to know if the model changes by other means. This is
where the Observer Pattern comes into play.
I just learned a lot trying to understand all this :)
--
Wéslleymberg Lisboa
Graduado em Sistemas de Informação
Docente no IFFluminense - Campus Itaboraí
More information about the Cuis-dev
mailing list