pharo-graphics / Toplo

A widget framework on top of Bloc
MIT License
17 stars 8 forks source link

Button API seems unfriendly for changing click action #44

Closed tinchodias closed 1 year ago

tinchodias commented 1 year ago

This is because whenClickedDo adds a new event handler for click each time. Example:

| but lab ico pane |
pane := ToPane new.
lab := ToLabel new text: 'Button'; yourself.
ico := ToImage inner: (self iconNamed: #remove).
pane addChildren: { ico. lab }.
but := ToGeneralButton new
    dresser: ToGeneralButtonDresser new;
    addChild: pane;
    whenClickedDo: [ self inform: '1' ];
    whenClickedDo: [ self inform: '2' ];
    yourself.
but openInOBlSpace 

it pop ups 2 messages each time.

plantec commented 1 year ago

In general, the whenXXXXDo: pattern leads to this kind of issue. I have to think about it

tinchodias commented 1 year ago

A possible solution: the button has in initialize:

action := [].
self whenClickedDo: [ action value ]

and it's prepared to somebody that updates the valuable via action:. And there is a single "official action" of the button.

tinchodias commented 1 year ago

It could be more efficient to remember in the button the event handler, and that the mutator method updates the block in the event handler. But not sure how much more efficient.

plantec commented 1 year ago

the event handler is now stored so that it can be replaced or removed