apply-orientation-lock.uts
5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* App 端屏幕方向
*
* 「什么都不设定」时是否允许旋转:见 README-layout;常见为 Manifest 默认竖屏。
*
* - 小屏手持机:非 Pad 画像且长边 < 1024 → 锁竖屏(含 deviceType 非 phone / 为空 的机型);立即 + 延迟 + LayoutRoot 横屏窗口时补锁;
* - Pad/大屏:`layoutService.isPadLikeDevice()` → unlockOrientation(Android FULL_SENSOR);需配合 pages.json `globalStyle.pageOrientation: "auto"`,否则框架默认 portrait 会禁止旋转。
* - 其余:不调用 lock/unlock。
*/
// #ifdef APP
import { layoutService } from '@/services/LayoutService.uts'
import { lockOrientation, unlockOrientation } from '@/utils/plus-screen.uts'
// import { updateResolutionConfig } from '@/services/adapter/index.uts'
// #endif
var refreshTimer: number = 0
const POLICY_PHONE_LOCKED = 0
const POLICY_PAD_UNLOCKED = 1
const POLICY_NEUTRAL = 2
var lastAppliedOrientationPolicy: number = -1
function computeMaxScreenDim(): number {
const systemInfo = uni.getSystemInfoSync()
let w = systemInfo.screenWidth
let h = systemInfo.screenHeight
if (w == 0 || h == 0) {
const winInfo = uni.getWindowInfo()
if (w == 0) {
w = winInfo.windowWidth
}
if (h == 0) {
h = winInfo.windowHeight
}
}
return Math.max(w, h)
}
/**
* 是否应对当前设备锁竖屏:Pad 画像除外;长边 >=1024 视为大屏不锁(含折叠展开等)
* 不少机型 getDeviceInfo.deviceType 不是 "phone"(空/undefined/tv 等),仅判断 phone 会导致从不锁屏
*/
function shouldLockHandsetPortrait(): boolean {
if (layoutService.isPadLikeDevice()) {
return false
}
const maxDim = computeMaxScreenDim()
if (maxDim >= 1024) {
return false
}
return true
}
function logOrientationDiagnostics(context: string): void {
const dev = uni.getDeviceInfo()
const sys = uni.getSystemInfoSync()
const win = uni.getWindowInfo()
let devLine = "[orientation-diag] " + context + " getDeviceInfo=null"
if (dev != null) {
const dt = dev.deviceType != null ? (dev.deviceType as string) : "(null)"
const dm = dev.deviceModel != null ? dev.deviceModel : ""
const dor = dev.deviceOrientation != null ? dev.deviceOrientation : ""
devLine = "[orientation-diag] " + context + " getDeviceInfo.deviceType=" + dt + " deviceModel=" + dm + " deviceOrientation=" + dor
}
const sysOrient = sys.deviceOrientation as string
}
// function scheduleRefresh(delayMs: number): void {
// if (refreshTimer != 0) {
// clearTimeout(refreshTimer)
// }
// refreshTimer = setTimeout((): void => {
// layoutService.refreshScreenInfo()
// // updateResolutionConfig()
// refreshTimer = 0
// }, delayMs)
// }
/** Pad 解锁后尺寸变化较慢,二次刷新(第二段不用 refreshTimer,避免互相 clear) */
// function scheduleRefreshAfterPadUnlock(): void {
// scheduleRefresh(250)
// setTimeout((): void => {
// layoutService.refreshScreenInfo()
// // updateResolutionConfig()
// }, 750)
// }
/** globalStyle pageOrientation=auto 时,框架可能在 onLaunch 之后才生效,需延迟再锁竖屏 */
function applyPhonePortraitLocksWithRetry(): void {
console.log("[orientation-lock] 小屏手持机 → lockOrientation(portrait-primary)(含延迟补锁,对抗 auto)")
lockOrientation('portrait-primary')
setTimeout((): void => {
lockOrientation('portrait-primary')
console.log("[orientation-lock] 小屏 约280ms 补锁 portrait")
}, 280)
setTimeout((): void => {
lockOrientation('portrait-primary')
console.log("[orientation-lock] 小屏 约620ms 再补锁 portrait")
}, 620)
// scheduleRefresh(450)
}
export function applyScreenOrientationLock(): void {
// #ifdef APP
if (layoutService.isInitialized() == false) {
layoutService.init()
}
logOrientationDiagnostics("beforePolicy")
const lockHandset = shouldLockHandsetPortrait()
const padLike = layoutService.isPadLikeDevice()
if (lockHandset) {
lastAppliedOrientationPolicy = POLICY_PHONE_LOCKED
applyPhonePortraitLocksWithRetry()
} else if (padLike) {
if (lastAppliedOrientationPolicy == POLICY_PAD_UNLOCKED) {
return
}
lastAppliedOrientationPolicy = POLICY_PAD_UNLOCKED
unlockOrientation()
setTimeout((): void => {
unlockOrientation()
console.log("[orientation-lock] Pad 延迟再 unlock 一次(避免框架在 onLaunch 之后重新套用竖屏)")
}, 320)
// scheduleRefreshAfterPadUnlock()
} else {
if (lastAppliedOrientationPolicy == POLICY_NEUTRAL) {
return
}
lastAppliedOrientationPolicy = POLICY_NEUTRAL
}
// #endif
}
/**
* 窗口已横屏(宽>高)时再锁一次;供 LayoutRoot 轮询/尺寸变化调用,顶住 pageOrientation=auto
*/
export function reassertHandsetPortraitIfNeeded(): void {
// #ifdef APP
if (layoutService.isInitialized() == false) {
return
}
if (shouldLockHandsetPortrait() == false) {
return
}
const info = uni.getWindowInfo()
let w = info.windowWidth
let h = info.windowHeight
if (w == 0 && info.screenWidth > 0) {
w = info.screenWidth
if (info.screenHeight > 0) {
h = info.screenHeight
}
}
if (w == 0 || h == 0) {
return
}
if (w <= h) {
return
}
lockOrientation('portrait-primary')
// #endif
}