apply-orientation-lock.uts 5.07 KB
/**
 * 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
}