Files
planet/client/src/main/orientation.ts
T
linkong 842cba4dc7
ci / backend (push) Has been cancelled
ci / frontend (push) Has been cancelled
release / images (push) Has been cancelled
release / client_release (push) Has been cancelled
ci / delivery (push) Has been cancelled
release: bump version to 0.77.0
2026-11-20 16:59:38 +08:00

104 lines
3.6 KiB
TypeScript

export type WindowOrientation = 'landscape' | 'portrait'
export interface Rect {
x: number
y: number
width: number
height: number
}
export const MIN_WINDOW_WIDTH = 360
export const MIN_WINDOW_HEIGHT = 480
export const DEFAULT_LANDSCAPE_WIDTH = 1600
export const DEFAULT_LANDSCAPE_HEIGHT = 960
export const EARTH_MOBILE_MAX_WIDTH = 820
export function detectWindowOrientation(
bounds: Pick<Rect, 'width' | 'height'>,
state: { isMaximized: boolean; isFullScreen: boolean },
): WindowOrientation {
if (!state.isMaximized && !state.isFullScreen && bounds.height > bounds.width) {
return 'portrait'
}
return 'landscape'
}
export function clampBoundsToWorkArea(bounds: Rect, workArea: Rect): Rect {
const minW = Math.min(MIN_WINDOW_WIDTH, workArea.width)
const minH = Math.min(MIN_WINDOW_HEIGHT, workArea.height)
const width = Math.round(Math.max(minW, Math.min(workArea.width, bounds.width)))
const height = Math.round(Math.max(minH, Math.min(workArea.height, bounds.height)))
const minX = workArea.x
const maxX = workArea.x + Math.max(0, workArea.width - width)
const minY = workArea.y
const maxY = workArea.y + Math.max(0, workArea.height - height)
return {
x: Math.round(Math.max(minX, Math.min(maxX, bounds.x))),
y: Math.round(Math.max(minY, Math.min(maxY, bounds.y))),
width,
height,
}
}
function centerOnAnchor(anchorBounds: Rect, width: number, height: number): { x: number; y: number } {
return {
x: Math.round(anchorBounds.x + (anchorBounds.width - width) / 2),
y: Math.round(anchorBounds.y + (anchorBounds.height - height) / 2),
}
}
export function computePortraitBounds(params: {
workArea: Rect
anchorBounds: Rect
savedPortraitBounds?: Rect | null
}): Rect {
const { workArea, anchorBounds, savedPortraitBounds } = params
let width: number
let height: number
let pos: { x: number; y: number } | null = null
if (
savedPortraitBounds &&
savedPortraitBounds.height > savedPortraitBounds.width &&
savedPortraitBounds.width <= EARTH_MOBILE_MAX_WIDTH
) {
height = Math.max(MIN_WINDOW_HEIGHT, Math.min(workArea.height, savedPortraitBounds.height))
const maxPortraitWidth = Math.min(EARTH_MOBILE_MAX_WIDTH, workArea.width, Math.max(MIN_WINDOW_WIDTH, height - 1))
width = Math.max(MIN_WINDOW_WIDTH, Math.min(maxPortraitWidth, savedPortraitBounds.width))
pos = { x: savedPortraitBounds.x, y: savedPortraitBounds.y }
} else {
height = Math.max(MIN_WINDOW_HEIGHT, Math.min(Math.round(workArea.height * 0.88), 1040, workArea.height))
width = Math.max(MIN_WINDOW_WIDTH, Math.min(560, Math.round((height * 9) / 16), Math.max(MIN_WINDOW_WIDTH, height - 40)))
}
const anchorPos = pos ?? centerOnAnchor(anchorBounds, width, height)
return clampBoundsToWorkArea({ x: anchorPos.x, y: anchorPos.y, width, height }, workArea)
}
export function computeLandscapeBounds(params: {
workArea: Rect
anchorBounds: Rect
savedLandscapeBounds?: Rect | null
}): Rect {
const { workArea, anchorBounds, savedLandscapeBounds } = params
let width: number
let height: number
let pos: { x: number; y: number } | null = null
if (savedLandscapeBounds && savedLandscapeBounds.width >= savedLandscapeBounds.height) {
width = savedLandscapeBounds.width
height = savedLandscapeBounds.height
pos = { x: savedLandscapeBounds.x, y: savedLandscapeBounds.y }
} else {
width = Math.min(DEFAULT_LANDSCAPE_WIDTH, workArea.width)
height = Math.min(DEFAULT_LANDSCAPE_HEIGHT, workArea.height)
}
const targetPos = pos ?? centerOnAnchor(anchorBounds, width, height)
return clampBoundsToWorkArea({ x: targetPos.x, y: targetPos.y, width, height }, workArea)
}