Skip to content

Commit fdc1572

Browse files
refactor ios screen handle code
1 parent 4864aca commit fdc1572

3 files changed

Lines changed: 57 additions & 30 deletions

File tree

packages/webgal/index.html

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,45 @@
159159
<div id="html-body__panic-overlay"></div>
160160
<!-- 应用挂载点 -->
161161
<div id="root"></div>
162-
<!-- 在窗口大小改变时进行强制缩放 -->
163162
<script>
164163
(() => {
165-
const userAgent = navigator.userAgent;
166-
const platform = navigator.platform;
164+
const userAgent = navigator.userAgent || '';
165+
const platform = navigator.userAgentData?.platform || navigator.platform || '';
167166
const maxTouchPoints = navigator.maxTouchPoints || 0;
168-
const isIPhoneLike = /iPhone|iPod/i.test(userAgent);
169-
const isIPadLike = /iPad/i.test(userAgent) || (platform === 'MacIntel' && maxTouchPoints > 1);
170-
const isIOS = isIPhoneLike || isIPadLike;
171-
const isIOSPhone = isIOS && isIPhoneLike;
167+
const isIOSPhone = /iPhone|iPod/i.test(userAgent);
168+
const isIPad = /iPad/i.test(userAgent) || (/Mac/i.test(platform) && maxTouchPoints > 1);
169+
170+
window.__WEBGAL_DEVICE_INFO__ = {
171+
isIOS: isIOSPhone || isIPad,
172+
isIOSPhone,
173+
isIPad,
174+
};
175+
})();
176+
</script>
177+
<!-- 在窗口大小改变时进行强制缩放 -->
178+
<script>
179+
(() => {
180+
const deviceInfo = window.__WEBGAL_DEVICE_INFO__ ?? { isIOS: false };
181+
const isIOS = deviceInfo.isIOS;
182+
const layoutConfig = isIOS
183+
? {
184+
bottomInset: 15,
185+
resizeDelay: 500,
186+
viewport:
187+
'width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover',
188+
hideEffectBackground: true,
189+
}
190+
: {
191+
bottomInset: 0,
192+
resizeDelay: 0,
193+
viewport: 'width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no',
194+
hideEffectBackground: false,
195+
};
172196
const root = document.querySelector('#root');
173197
const titleEnter = document.querySelector('.html-body__title-enter');
174198
const effectBackground = document.querySelector('.html-body__effect-background');
175199
let viewportMeta = document.querySelector('meta[name="viewport"]');
176200

177-
window.__WEBGAL_DEVICE_INFO__ = { isIOS, isIOSPhone };
178-
179201
const ensureViewportMeta = () => {
180202
if (!viewportMeta) {
181203
viewportMeta = document.createElement('meta');
@@ -190,8 +212,7 @@
190212
const targetWidth = 2560; // 目标宽度
191213
const h = window.innerHeight; // 窗口高度
192214
const w = window.innerWidth; // 窗口宽度
193-
const iosBottomInset = isIOS ? 15 : 0;
194-
const layoutHeight = Math.max(h - iosBottomInset, 1);
215+
const layoutHeight = Math.max(h - layoutConfig.bottomInset, 1);
195216
const zoomH = layoutHeight / targetHeight; // 以窗口高度为基准的变换比
196217
const zoomW = w / targetWidth; // 以窗口宽度为基准的变换比
197218
const zoomH2 = w / targetHeight; // 竖屏时以窗口高度为基础的变换比
@@ -256,33 +277,32 @@
256277
};
257278
let iosResizeTimer = null;
258279
const scheduleResize = () => {
259-
if (isIOS) {
280+
if (layoutConfig.resizeDelay > 0) {
260281
if (iosResizeTimer) {
261282
window.clearTimeout(iosResizeTimer);
262283
}
263284
iosResizeTimer = window.setTimeout(() => {
264285
resize();
265286
iosResizeTimer = null;
266-
}, 500);
287+
}, layoutConfig.resizeDelay);
267288
return;
268289
}
269290
resize();
270291
};
271292
if (isIOS) {
272293
const styleTag = document.createElement('style');
273-
styleTag.textContent = `
274-
* { font-synthesis: none !important; }
275-
#ebg,
276-
#ebgOverlay {
277-
display: none !important;
278-
}
279-
`;
294+
styleTag.textContent = `* { font-synthesis: none !important; }`;
295+
if (layoutConfig.hideEffectBackground) {
296+
styleTag.textContent += `
297+
#ebg,
298+
#ebgOverlay {
299+
display: none !important;
300+
}
301+
`;
302+
}
280303
document.head.appendChild(styleTag);
281-
ensureViewportMeta().content =
282-
'width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover';
283-
} else {
284-
ensureViewportMeta().content = 'width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no';
285304
}
305+
ensureViewportMeta().content = layoutConfig.viewport;
286306
scheduleResize();
287307
window.onload = scheduleResize;
288308
window.onresize = scheduleResize;
@@ -299,7 +319,7 @@
299319
<!-- 注册 Service Worker -->
300320
<script>
301321
(() => {
302-
const isIOS = window.__WEBGAL_DEVICE_INFO__?.isIOS ?? !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
322+
const isIOS = window.__WEBGAL_DEVICE_INFO__?.isIOS ?? false;
303323
if ('serviceWorker' in navigator && !isIOS) {
304324
navigator.serviceWorker
305325
.register('./webgal-serviceworker.js')
@@ -315,7 +335,10 @@
315335
<!-- 首屏加载 -->
316336
<script>
317337
(() => {
318-
const isIOS = window.__WEBGAL_DEVICE_INFO__?.isIOS ?? !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
338+
const isIOS = window.__WEBGAL_DEVICE_INFO__?.isIOS ?? false;
339+
const enterConfig = isIOS
340+
? { skipAnimation: true, hideDelay: 0 }
341+
: { skipAnimation: false, hideDelay: 2000 };
319342
let enterPromiseResolve;
320343
const enterPromise = new Promise((res) => {
321344
enterPromiseResolve = res;
@@ -341,7 +364,7 @@
341364
/** 点击屏幕,进入引擎主界面 */
342365
const enter = () => {
343366
const titleEnter = document.querySelector('.html-body__title-enter');
344-
if (isIOS) {
367+
if (enterConfig.skipAnimation) {
345368
if (titleEnter) {
346369
titleEnter.style.pointerEvents = 'none';
347370
titleEnter.style.transition = 'none';
@@ -376,7 +399,7 @@
376399
if (titleEnter) {
377400
titleEnter.style.display = 'none';
378401
}
379-
}, 2000); // 将落地页设置为不显示
402+
}, enterConfig.hideDelay); // 将落地页设置为不显示
380403
enterPromiseResolve();
381404
};
382405
const titleEnter = document.querySelector('.html-body__title-enter');

packages/webgal/src/Core/initializeScript.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ import { __INFO } from '@/config/info';
1616
import { WebGAL } from '@/Core/WebGAL';
1717
import { loadTemplate } from '@/Core/util/coreInitialFunction/templateLoader';
1818

19-
const u = navigator.userAgent;
20-
export const isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); // 判断是否是 iOS终端
19+
export const isIOS = window.__WEBGAL_DEVICE_INFO__?.isIOS ?? false; // 判断是否是 iOS 终端
2120

2221
/**
2322
* 引擎初始化函数

packages/webgal/src/types/electron.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ export {};
22

33
declare global {
44
interface Window {
5+
__WEBGAL_DEVICE_INFO__?: {
6+
isIOS: boolean;
7+
isIOSPhone: boolean;
8+
isIPad: boolean;
9+
};
510
electronFuncs?: {
611
steam?: {
712
initialize: (appId: string) => boolean | Promise<boolean>;

0 commit comments

Comments
 (0)