Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue whereby tooltips loaded dynamically while moving the map were never shown. #8672

Merged
merged 19 commits into from
May 18, 2023

Conversation

theGOTOguy
Copy link
Contributor

Slava Ukraini, leaflet team!

This pull request resolves #8591, whereby Tooltips were not added to the DOM while the map was moving.

@BasieP's analysis of the offending code was correct, as is the workaround suggested; However, I am using react-leaflet, where this workaround is not an option.

An automatic test is included.

@theGOTOguy theGOTOguy changed the title Fix issue whereby tooltips loaded dynamically while moving were never shown. Fix issue whereby tooltips loaded dynamically while moving the map were never shown. Nov 19, 2022
src/layer/Tooltip.js Outdated Show resolved Hide resolved
@Malvoz Malvoz added the bug label Dec 9, 2022
@jonkoops
Copy link
Collaborator

@theGOTOguy could you stop adding new commits to this PR? It creates a lot of unnecessary noise. Besides, we will rebase and quash the commits either way.

@jonkoops
Copy link
Collaborator

Also please rebase your PR on main so we can merge if needed (no merge commits).

@theGOTOguy theGOTOguy force-pushed the tooltip-loaded-while-dragging branch 2 times, most recently from 30306b8 to f5a57c1 Compare December 23, 2022 11:55
@theGOTOguy theGOTOguy force-pushed the tooltip-loaded-while-dragging branch from f5a57c1 to fa40d46 Compare December 25, 2022 04:59
Copy link
Member

@Falke-Design Falke-Design left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check for map dragging was added in #7862 to prevent adding / removing of the tooltip when occasionally hovering over the marker with the mouse while dragging.

src/layer/Tooltip.js Outdated Show resolved Hide resolved
src/layer/Tooltip.js Outdated Show resolved Hide resolved
src/layer/Tooltip.js Show resolved Hide resolved
Comment on lines +207 to +210
this._moving = false;
Draggable._dragging = false;

if (fireDragend) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mourner @jonkoops @IvanSanchez do you see any problems with setting the flags to false before fireing the event?

Copy link
Contributor Author

@theGOTOguy theGOTOguy Jan 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the only way this would matter would be if someone was looking at _moving or _dragging during the handling of a dragend event. We're not doing it internally to Leaflet, but it's possible that someone who depends on us might. To do so would be inadvisable, because there would be a race between the event firing and _moving and _dragging being updated after the event is fired. Maybe mark this as a breaking API change in case someone depends on the state of _moving or _dragging in an event triggered by dragend?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no issue with that, this is internal state so folks should not be relying on it anyways.

Comment on lines 422 to 428
if (!this._openOnceFlag) {
this._openOnceFlag = true;
this._map.once('moveend', () => {
this._openOnceFlag = false;
this._openTooltip(e);
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mourner @jonkoops @IvanSanchez have someone a good Idea to make this cleaner? The once listener should not be added multiple times and it is important to pass e as argument from the original _openTooltip call.

Copy link
Contributor Author

@theGOTOguy theGOTOguy Dec 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a couple of ideas:

Superficially, you could do something like this:

                // If the map is moving, we will show the tooltip after it's done.
                if (this._map.dragging && this._map.dragging.moving() && !this._openOnceFlag) {
                        this._openOnceFlag = true;
                        this._map.once('moveend', () => {
                            this._openOnceFlag = false;
                            this._openTooltip(e);
                        });
                        return;
                }

It's not satisfying because we still have to store _openOnceFlag.

However, maybe there's a better solution. There are only four places where _openTooltip is called at all, and they're all here in Tooltip.js.

We could replace all instances of _openTooltip with _openTooltipWhenNotMoving, with:

_openTooltipWhenNotMoving(e) {
    if (this._map.dragging && this._map.dragging.moving()) {
        this._map.once('moveend', () => {
            this._openTooltip(e);
        });
    } else {
        this._openTooltip(e);
    }
}

Then remove the moving checks from _openTooltip proper.

In this way, we avoid storing an extra variable, and call _openTooltip at most once at an appropriate time when it would be needed.

Edit: I'm concerned that the _openOnceFlag might actually be incorrect. If the Tooltip was closed for any reason (say, permanent is set to false by some external code), then we could find that if it is later set to true again then it would no longer appear if _openTooltip is called while dragging.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've implemented the former solution, which at least resolves a little bit of the nesting.

I don't see a better solution than the _openOnceFlag that you implemented, and I've convinced myself that it is correct even in case where some external code toggles permanent.

@theGOTOguy
Copy link
Contributor Author

This is still an issue for me in production, and I'm wondering if you could take a look at the changes I've made and let me know if the solution as it sits is appropriate or if more changes are desired.

@theGOTOguy theGOTOguy requested a review from Falke-Design May 10, 2023 18:22
Copy link
Member

@Falke-Design Falke-Design left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine for me but we need an approve from another maintainer @IvanSanchez @mourner

@Falke-Design Falke-Design added this to the 1.9.x milestone May 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tooltips are not added to dom, when moving the map (or later)
5 participants