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

add GlobalEvent for monitor visibilitychange/devicePixelRatio change #2155

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 11 additions & 55 deletions src/core/Browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isFunction } from './util/common';
import { IS_NODE } from './util/env';

let Browser = {};
const maps = {};
// const maps = {};

function getDevicePixelRatio() {
return (window.devicePixelRatio || (window.screen.deviceXDPI / window.screen.logicalXDPI));
Expand Down Expand Up @@ -126,11 +126,11 @@ if (!IS_NODE) {
monitorDPRChange: true,
supportsPassive,
proxy,
removeDPRListening: (map) => {
if (map) {
delete maps[map.id];
}
},
// removeDPRListening: (map) => {
// // if (map) {
// // delete maps[map.id];
// // }
// },
checkDevicePixelRatio: () => {
if (typeof window !== 'undefined' && Browser.monitorDPRChange) {
const devicePixelRatio = getDevicePixelRatio();
Expand All @@ -142,55 +142,11 @@ if (!IS_NODE) {
}
return false;
},
addDPRListening: (map) => {
if (map) {
maps[map.id] = map;
}
}
// addDPRListening: (map) => {
// // if (map) {
// // maps[map.id] = map;
// // }
// }
};
//monitor devicePixelRatio change
if (typeof window !== 'undefined' && window.matchMedia) {
for (let i = 1; i < 500; i++) {
const dpi = (i * 0.01).toFixed(2);
const screen = window.matchMedia(`screen and (resolution: ${dpi}dppx)`);
if (screen) {
if (screen.addEventListener) {
screen.addEventListener('change', Browser.checkDevicePixelRatio);
} else if (screen.addListener) {
screen.addListener(Browser.checkDevicePixelRatio);
}
}
}

}

if (Browser.devicePixelRatio) {
let tempDPI = Browser.devicePixelRatio;
Object.defineProperty(Browser, 'devicePixelRatio', {
get: () => {
return tempDPI;
},
set: (value) => {
if (value === tempDPI) {
return;
}
//when devicePixelRatio change force resize all layers
tempDPI = value;
if (!Browser.monitorDPRChange) {
return;
}
for (const mapId in maps) {
const map = maps[mapId];
if (!map || !map.options || map.options['devicePixelRatio'] || !map.checkSize || !map.getRenderer) {
continue;
}
const renderer = map.getRenderer();
if (renderer) {
map.checkSize(true);
}
}
}
});
}
}
export default Browser;
71 changes: 71 additions & 0 deletions src/core/GlobalEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import Eventable from './Eventable';
import Browser from './Browser';
import { addDomEvent } from './util/dom';

class Base {

}

class GlobalEventable extends Eventable(Base) {

}

// this is global event bus for doc state change
// such as : devicePixelRatio ,visibilitychange,... etc

export const GlobalEvent = new GlobalEventable();

export const EVENT_DPR_CHANGE = 'dprchange';
export const EVENT_DOC_VISIBILITY_CHANGE = 'docvisibilitychange';
export const EVENT_DOC_DRAGSTART = 'dragstart';
export const EVENT_DOC_DRAGEND = 'dragend';


//monitor devicePixelRatio change
if (typeof window !== 'undefined' && window.matchMedia) {
for (let i = 1; i < 500; i++) {
const dpi = (i * 0.01).toFixed(2);
const screen = window.matchMedia(`screen and (resolution: ${dpi}dppx)`);
if (screen) {
if (screen.addEventListener) {
screen.addEventListener('change', Browser.checkDevicePixelRatio);
} else if (screen.addListener) {
screen.addListener(Browser.checkDevicePixelRatio);
}
}
}

}

if (Browser.devicePixelRatio) {
let tempDPI = Browser.devicePixelRatio;
Object.defineProperty(Browser, 'devicePixelRatio', {
get: () => {
return tempDPI;
},
set: (value) => {
if (value === tempDPI) {
return;
}
//when devicePixelRatio change force resize all layers
tempDPI = value;
if (!Browser.monitorDPRChange) {
return;
}
GlobalEvent.fire(EVENT_DPR_CHANGE, { devicePixelRatio: value });
}
});
}

//monitor document visibilitychange change
if (Browser.webgl && typeof document !== 'undefined') {
addDomEvent(document, 'visibilitychange', () => {
GlobalEvent.fire(EVENT_DOC_VISIBILITY_CHANGE, { visibilityState: document.visibilityState });
});
addDomEvent(document, 'dragstart', () => {
GlobalEvent.fire(EVENT_DOC_DRAGSTART);
});
addDomEvent(document, 'dragend', () => {
GlobalEvent.fire(EVENT_DOC_DRAGEND);
});
}
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export { default as Promise } from './core/Promise';
// core classes
export { default as Class } from './core/Class';
export { default as Eventable } from './core/Eventable';
export { GlobalEvent } from './core/GlobalEvent';
export { default as JSONAble } from './core/JSONAble';
export { default as CollisionIndex } from './core/CollisionIndex';

Expand Down Expand Up @@ -74,7 +75,7 @@ import Actor from './core/worker/Actor';
* @namespace worker
*/
const worker = {
Actor : Actor
Actor: Actor
};

export { worker };
3 changes: 0 additions & 3 deletions src/map/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -1601,9 +1601,6 @@ class Map extends Handlerable(Eventable(Renderable(Class))) {
delete this.renderer;
this._fireEvent('removeend');
this._clearAllListeners();
if (Browser.removeDPRListening) {
Browser.removeDPRListening(this);
}
return this;
}

Expand Down
27 changes: 17 additions & 10 deletions src/renderer/map/MapCanvasRenderer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IS_NODE, isNumber, isFunction, requestAnimFrame, cancelAnimFrame, equalMapView, calCanvasSize } from '../../core/util';
import { createEl, preventSelection, computeDomPosition, addDomEvent, removeDomEvent } from '../../core/util/dom';
import { createEl, preventSelection, computeDomPosition } from '../../core/util/dom';
import { GlobalEvent, EVENT_DOC_DRAGEND, EVENT_DOC_VISIBILITY_CHANGE, EVENT_DPR_CHANGE, EVENT_DOC_DRAGSTART } from './../../core/GlobalEvent';
import Browser from '../../core/Browser';
import Point from '../../geo/Point';
import Canvas2D from '../../core/Canvas';
Expand Down Expand Up @@ -438,9 +439,13 @@ class MapCanvasRenderer extends MapRenderer {

remove() {
if (Browser.webgl && typeof document !== 'undefined') {
removeDomEvent(document, 'visibilitychange', this._thisDocVisibilitychange, this);
removeDomEvent(document, 'dragstart', this._thisDocDragStart, this);
removeDomEvent(document, 'dragend', this._thisDocDragEnd, this);
GlobalEvent.off(EVENT_DPR_CHANGE, this._thisDocDPRChange, this);
GlobalEvent.off(EVENT_DOC_VISIBILITY_CHANGE, this._thisDocVisibilitychange, this);
GlobalEvent.off(EVENT_DOC_DRAGSTART, this._thisDocDragStart, this);
GlobalEvent.off(EVENT_DOC_DRAGEND, this._thisDocDragEnd, this);
// removeDomEvent(document, 'visibilitychange', this._thisDocVisibilitychange, this);
// removeDomEvent(document, 'dragstart', this._thisDocDragStart, this);
// removeDomEvent(document, 'dragend', this._thisDocDragEnd, this);
}
if (this._resizeInterval) {
clearInterval(this._resizeInterval);
Expand Down Expand Up @@ -942,12 +947,14 @@ class MapCanvasRenderer extends MapRenderer {
});

if (Browser.webgl && typeof document !== 'undefined') {
addDomEvent(document, 'visibilitychange', this._thisDocVisibilitychange, this);
addDomEvent(document, 'dragstart', this._thisDocDragStart, this);
addDomEvent(document, 'dragend', this._thisDocDragEnd, this);
}
if (Browser.addDPRListening) {
Browser.addDPRListening(this.map);
GlobalEvent.on(EVENT_DPR_CHANGE, this._thisDocDPRChange, this);
GlobalEvent.on(EVENT_DOC_VISIBILITY_CHANGE, this._thisDocVisibilitychange, this);
GlobalEvent.on(EVENT_DOC_DRAGSTART, this._thisDocDragStart, this);
GlobalEvent.on(EVENT_DOC_DRAGEND, this._thisDocDragEnd, this);

// addDomEvent(document, 'visibilitychange', this._thisDocVisibilitychange, this);
// addDomEvent(document, 'dragstart', this._thisDocDragStart, this);
// addDomEvent(document, 'dragend', this._thisDocDragEnd, this);
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/renderer/map/MapRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class MapRenderer extends Class {
this._thisDocVisibilitychange = this._onDocVisibilitychange.bind(this);
this._thisDocDragStart = this._onDocDragStart.bind(this);
this._thisDocDragEnd = this._onDocDragEnd.bind(this);
this._thisDocDPRChange = this._onDocDPRChange.bind(this);
}

callInNextFrame(fn) {
Expand Down Expand Up @@ -137,6 +138,17 @@ class MapRenderer extends Class {
return;
}

_onDocDPRChange() {
const map = this.map;
if (!map || !map.options || map.options['devicePixelRatio'] || !map.checkSize || !map.getRenderer) {
return;
}
const renderer = map.getRenderer();
if (renderer) {
map.checkSize(true);
}
}

_containerIsOffscreen() {
const container = this.map.getContainer();
if (!container || !container.style || container.style.display === 'none') {
Expand Down