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

Remove popup when marker is behind terrain #3865

Merged
27 changes: 19 additions & 8 deletions src/ui/marker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,15 @@ describe('marker', () => {
expect(!marker.getPopup()).toBeTruthy();
});

test('Marker#togglePopup opens a popup that was closed', () => {
test('Marker#togglePopup opens a popup that was closed', async () => {
HarelM marked this conversation as resolved.
Show resolved Hide resolved
const map = createMap();
const marker = new Marker()
.setLngLat([0, 0])
.addTo(map)
.setPopup(new Popup())
.togglePopup();
.setPopup(new Popup());

await sleep(500);
marker.togglePopup();

expect(marker.getPopup().isOpen()).toBeTruthy();

Expand All @@ -184,13 +186,15 @@ describe('marker', () => {
map.remove();
});

test('Enter key on Marker opens a popup that was closed', () => {
test('Enter key on Marker opens a popup that was closed', async () => {
const map = createMap();
const marker = new Marker()
.setLngLat([0, 0])
.addTo(map)
.setPopup(new Popup());

await sleep(500);

// popup not initially open
expect(marker.getPopup().isOpen()).toBeFalsy();

Expand All @@ -202,13 +206,15 @@ describe('marker', () => {
map.remove();
});

test('Space key on Marker opens a popup that was closed', () => {
test('Space key on Marker opens a popup that was closed', async () => {
const map = createMap();
const marker = new Marker()
.setLngLat([0, 0])
.addTo(map)
.setPopup(new Popup());

await sleep(500);

// popup not initially open
expect(marker.getPopup().isOpen()).toBeFalsy();

Expand Down Expand Up @@ -292,14 +298,16 @@ describe('marker', () => {
map.remove();
});

test('Popup anchors around default Marker', () => {
test('Popup anchors around default Marker', async () => {
const map = createMap();

const marker = new Marker()
.setLngLat([0, 0])
.setPopup(new Popup().setText('Test'))
.addTo(map);

await sleep(500);

// open the popup
marker.togglePopup();

Expand Down Expand Up @@ -361,14 +369,16 @@ describe('marker', () => {
map.remove();
});

test('Popup is opened at its marker position after marker is moved to another globe', () => {
test('Popup is opened at its marker position after marker is moved to another globe', async () => {
const map = createMap({width: 3000});

const marker = new Marker()
.setLngLat([0, 0])
.setPopup(new Popup().setText('Test'))
.addTo(map);

await sleep(500);

marker._pos = new Point(2999, 242);
marker._lngLat = map.unproject(marker._pos);
marker.togglePopup();
Expand All @@ -377,7 +387,7 @@ describe('marker', () => {
map.remove();
});

test('Popup is re-opened at its marker position after marker is moved to another globe', () => {
test('Popup is re-opened at its marker position after marker is moved to another globe', async () => {
const map = createMap({width: 3000});

const marker = new Marker()
Expand All @@ -387,6 +397,7 @@ describe('marker', () => {
.togglePopup()
.togglePopup();

await sleep(500);
marker._pos = new Point(2999, 242);
marker._lngLat = map.unproject(marker._pos);
marker.togglePopup();
Expand Down
16 changes: 10 additions & 6 deletions src/ui/marker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class Marker extends Evented {
_pitchAlignment: Alignment;
_rotationAlignment: Alignment;
_originalTabIndex: string; // original tabindex of _element
_opacity: string;
_fullOpacity: string;
HarelM marked this conversation as resolved.
Show resolved Hide resolved
_opacityWhenCovered: string;
_opacityTimeout: ReturnType<typeof setTimeout>;

Expand Down Expand Up @@ -515,6 +515,8 @@ export class Marker extends Evented {
togglePopup(): this {
const popup = this._popup;

if (this._element.style.opacity !== this._fullOpacity) return this;

if (!popup) return this;
else if (popup.isOpen()) popup.remove();
else {
Expand All @@ -527,7 +529,7 @@ export class Marker extends Evented {
_updateOpacity(force: boolean = false) {
const terrain = this._map?.terrain;
if (!terrain) {
if (this._element.style.opacity !== this._opacity) { this._element.style.opacity = this._opacity; }
if (this._element.style.opacity !== this._fullOpacity) { this._element.style.opacity = this._fullOpacity; }
return;
}
if (force) {
Expand All @@ -549,7 +551,7 @@ export class Marker extends Evented {

const forgiveness = .006;
if (markerDistance - terrainDistance < forgiveness) {
this._element.style.opacity = this._opacity;
this._element.style.opacity = this._fullOpacity;
return;
}
// If the base is obscured, use the offset to check if the marker's center is obscured.
Expand All @@ -559,7 +561,9 @@ export class Marker extends Evented {
const markerDistanceCenter = map.transform.lngLatToCameraDepth(this._lngLat, elevation + elevationToCenter);
// Display at full opacity if center is visible.
const centerIsInvisible = markerDistanceCenter - terrainDistanceCenter > forgiveness;
this._element.style.opacity = centerIsInvisible ? this._opacityWhenCovered : this._opacity;

if (this._popup && centerIsInvisible) this._popup.remove();
this._element.style.opacity = centerIsInvisible ? this._opacityWhenCovered : this._fullOpacity;
}

_update = (e?: { type: 'move' | 'moveend' | 'terrain' | 'render' }) => {
Expand Down Expand Up @@ -835,11 +839,11 @@ export class Marker extends Evented {
*/
setOpacity(opacity?: string, opacityWhenCovered?: string): this {
if (opacity === undefined && opacityWhenCovered === undefined) {
this._opacity = '1';
this._fullOpacity = '1';
this._opacityWhenCovered = '0.2';
}
if (opacity !== undefined) {
this._opacity = opacity;
this._fullOpacity = opacity;
}
if (opacityWhenCovered !== undefined) {
this._opacityWhenCovered = opacityWhenCovered;
Expand Down
Loading