pharo-spec / Spec

Spec is a framework in Pharo for describing user interfaces.
MIT License
61 stars 63 forks source link

Notebook's whenSelectedPageChangedDo: not working when pages are added after build #1541

Closed JanBliznicenko closed 4 months ago

JanBliznicenko commented 4 months ago

When notebook presenter is built (with Morphic) with no pages, and the pages are added afterwards, whenSelectedPageChangedDo: is never triggerer on tab changes by mouse click.

Possible test:

| changed ntb |
ntb := SpNotebookPresenter new.
ntb whenSelectedPageChangedDo: [ changed := true ].
ntb buildWithLayout: ntb defaultLayout.
ntb addPage: (ntb newNotebookPage presenterProvider: [ SpButtonPresenter new ]).
ntb addPage: (ntb newNotebookPage presenterProvider: [ SpButtonPresenter new ]).
ntb selectPageIndex: 1.
changed := false.
ntb adapter widget updatePageIndex: 2 oldIndex: 1.
self assert: changed.

It works if there is at least one page before the notebook presenter is built the first time, like this:

| changed ntb |
ntb := SpNotebookPresenter new.
ntb whenSelectedPageChangedDo: [ changed := true ].
ntb addPage: (ntb newNotebookPage presenterProvider: [ SpButtonPresenter new ]).
ntb buildWithLayout: ntb defaultLayout.
ntb addPage: (ntb newNotebookPage presenterProvider: [ SpButtonPresenter new ]).
ntb selectPageIndex: 1.
changed := false.
ntb adapter widget updatePageIndex: 2 oldIndex: 1.
self assert: changed.

The only difference between the two snippets is that ntb buildWithLayout: ntb defaultLayout. is on different line.

The problem is probably in SpMorphicNotebookAdapter>>#addModelTo: where it skips the setup if there are no pages at the time, not expecting that some can be added dynamically.

If needed, there is a visual example - run the code and switch tabs using mouse clicks:

| ntb |
ntb := SpNotebookPresenter new.
ntb whenSelectedPageChangedDo: [ self inform: 'TAB CHANGED' ].
ntb open.
ntb addPage: (ntb newNotebookPage presenterProvider: [ SpButtonPresenter new ]).
ntb addPage: (ntb newNotebookPage title: 'CLICK ME'; presenterProvider: [ SpButtonPresenter new ]).
ntb selectPageIndex: 1.

and the one that works, because pages are added beforehand:

| ntb |
ntb := SpNotebookPresenter new.
ntb whenSelectedPageChangedDo: [ self inform: 'TAB CHANGED' ].
ntb addPage: (ntb newNotebookPage presenterProvider: [ SpButtonPresenter new ]).
ntb open.
ntb addPage: (ntb newNotebookPage title: 'CLICK ME'; presenterProvider: [ SpButtonPresenter new ]).
ntb selectPageIndex: 1.