[Cuis-dev] dependency mechanism for TextModelMorph

Mark Volkmann r.mark.volkmann at gmail.com
Mon Mar 24 05:56:05 PDT 2025


Using a suggestion from Hilaire, this works. It passes the String value to
update: on every keystroke. But it seems there should be a solution that
does not require listening for key events.

initialize
    | scroller tmm |

    tmm := TextModelMorph withText: '' ::
    acceptOnAny: true;
    askBeforeDiscardingEdits: false.
    scroller := tmm scroller.
    scroller addDependent: self.
    scroller setProperty: #keyStroke: toValue: [:event |
        scroller changed: scroller model actualContents string
    ].

SystemWindow new addMorph: tmm; openInWorld.

On Mon, Mar 24, 2025 at 7:41 AM Mark Volkmann <r.mark.volkmann at gmail.com>
wrote:

> 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> 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: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-
>> >     Dependency-Mechanism--3 <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>> 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>> 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>
>> >     Tel.: +55 22 99931-2376
>> >     --
>> >     Cuis-dev mailing list
>> >     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>
>> >
>> >
>> >
>> > --
>> > 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.
>


-- 
R. Mark Volkmann
Object Computing, Inc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20250324/d8a7c8db/attachment.htm>


More information about the Cuis-dev mailing list