Files
vue-desktop/src/core/window/ui/WindowFormElement.ts
T

852 lines
24 KiB
TypeScript
Raw Normal View History

2025-09-15 15:33:58 +08:00
import { LitElement, html, css, unsafeCSS } from 'lit'
import { customElement, property } from 'lit/decorators.js';
import wfStyle from './wf.scss?inline'
import type { TWindowFormState } from '@/core/window/types/WindowFormTypes.ts'
2025-09-16 14:32:57 +08:00
import { wfem } from '@/core/events/WindowFormEventManager.ts'
import type { IObservable } from '@/core/state/IObservable.ts'
2025-09-15 15:33:58 +08:00
/** 拖拽移动开始的回调 */
type TDragStartCallback = (x: number, y: number) => void;
/** 拖拽移动中的回调 */
type TDragMoveCallback = (x: number, y: number) => void;
/** 拖拽移动结束的回调 */
type TDragEndCallback = (x: number, y: number) => void;
/** 拖拽调整尺寸的方向 */
type TResizeDirection =
| 't' // 上
| 'b' // 下
| 'l' // 左
| 'r' // 右
| 'tl' // 左上
| 'tr' // 右上
| 'bl' // 左下
| 'br'; // 右下
/** 拖拽调整尺寸回调数据 */
interface IResizeCallbackData {
/** 宽度 */
width: number;
/** 高度 */
height: number;
/** 顶点坐标(相对 offsetParent) */
top: number;
/** 左点坐标(相对 offsetParent) */
left: number;
/** 拖拽调整尺寸的方向 */
direction: TResizeDirection | null;
}
/** 元素边界 */
interface IElementRect {
/** 宽度 */
width: number;
/** 高度 */
height: number;
/** 顶点坐标(相对 offsetParent) */
top: number;
/** 左点坐标(相对 offsetParent) */
left: number;
}
2025-09-15 16:25:15 +08:00
export interface WindowFormEventMap {
'windowForm:dragStart': CustomEvent<TDragStartCallback>;
'windowForm:dragMove': CustomEvent<TDragMoveCallback>;
'windowForm:dragEnd': CustomEvent<TDragEndCallback>;
'windowForm:resizeStart': CustomEvent<IResizeCallbackData>;
'windowForm:resizeMove': CustomEvent<IResizeCallbackData>;
'windowForm:resizeEnd': CustomEvent<IResizeCallbackData>;
'windowForm:stateChange': CustomEvent<{ state: TWindowFormState }>;
2025-09-16 14:32:57 +08:00
'windowForm:stateChange:minimize': CustomEvent<{ state: TWindowFormState }>;
'windowForm:stateChange:maximize': CustomEvent<{ state: TWindowFormState }>;
'windowForm:stateChange:restore': CustomEvent<{ state: TWindowFormState }>;
'windowForm:close': CustomEvent;
2025-09-15 16:25:15 +08:00
}
2025-09-15 15:33:58 +08:00
@customElement('window-form-element')
export class WindowFormElement extends LitElement {
// ==== 公共属性 ====
2025-09-16 14:32:57 +08:00
@property({ type: String }) wid: string
@property({ type: String }) override title = 'Window'
@property({ type: Boolean }) resizable = true
@property({ type: Boolean }) minimizable = true
@property({ type: Boolean }) maximizable = true
@property({ type: Boolean }) closable = true
@property({ type: Boolean, reflect: true }) focused: boolean = true
@property({ type: String, reflect: true }) windowFormState: TWindowFormState = 'default'
@property({ type: Object }) dragContainer?: HTMLElement
@property({ type: Boolean }) allowOverflow = true // 允许窗口超出容器
@property({ type: Number }) snapDistance = 0 // 吸附距离
@property({ type: Boolean }) snapAnimation = true // 吸附动画
@property({ type: Number }) snapAnimationDuration = 300 // 吸附动画时长 ms
@property({ type: Number }) maxWidth?: number
@property({ type: Number }) minWidth?: number
@property({ type: Number }) maxHeight?: number
@property({ type: Number }) minHeight?: number
@property({ type: String }) taskbarElementId?: string
@property({ type: Object }) pos: IObservable<{ x: number, y: number }>;
private _listeners: Array<{ type: string; handler: EventListener }> = []
2025-09-15 15:33:58 +08:00
// ==== 拖拽/缩放状态(内部变量,不触发渲染) ====
2025-09-16 14:32:57 +08:00
private dragging = false
private resizeDir: TResizeDirection | null = null
private startX = 0
private startY = 0
private startWidth = 0
private startHeight = 0
private startX_host = 0
private startY_host = 0
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
private x = 0
private y = 0
private preX = 0
private preY = 0
private width = 640
private height = 360
private animationFrame?: number
private resizing = false
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
// private windowFormState: TWindowFormState = 'default';
2025-09-15 15:33:58 +08:00
/** 元素信息 */
2025-09-16 14:32:57 +08:00
private targetBounds: IElementRect
2025-09-15 15:33:58 +08:00
/** 最小化前的元素信息 */
2025-09-16 14:32:57 +08:00
private targetPreMinimizeBounds?: IElementRect
2025-09-15 15:33:58 +08:00
/** 最大化前的元素信息 */
2025-09-16 14:32:57 +08:00
private targetPreMaximizedBounds?: IElementRect
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
static override styles = css`
${unsafeCSS(wfStyle)}
`
2025-09-15 15:33:58 +08:00
2025-09-15 16:25:15 +08:00
protected override createRenderRoot() {
2025-09-16 14:32:57 +08:00
const root = this.attachShadow({ mode: 'closed' })
const sheet = new CSSStyleSheet()
sheet.replaceSync(wfStyle)
root.adoptedStyleSheets = [sheet]
2025-09-15 16:25:15 +08:00
return root
}
2025-09-16 14:32:57 +08:00
/**
* 添加受管理的事件监听
* @param type 事件类型
* @param handler 事件处理函数
*/
public addManagedEventListener(type: string, handler: EventListener) {
this.addEventListener(type, handler)
this._listeners.push({ type, handler })
}
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
/**
* 移除所有受管理事件监听
*/
public removeAllManagedListeners() {
for (const { type, handler } of this._listeners) {
this.removeEventListener(type, handler)
}
this._listeners = []
}
override firstUpdated() {
wfem.addEventListener('windowFormFocus', this.windowFormFocusFun)
window.addEventListener('pointerup', this.onPointerUp)
window.addEventListener('pointermove', this.onPointerMove)
this.addEventListener('pointerdown', this.toggleFocus)
const container = this.dragContainer || document.body
const containerRect = container.getBoundingClientRect()
this.x = containerRect.width / 2 - this.width / 2
this.y = containerRect.height / 2 - this.height / 2
this.style.width = `${this.width}px`
this.style.height = `${this.height}px`
this.style.transform = `translate(${this.x}px, ${this.y}px)`
2025-09-15 15:33:58 +08:00
this.targetBounds = {
width: this.offsetWidth,
height: this.offsetHeight,
top: this.x,
left: this.y,
2025-09-16 14:32:57 +08:00
}
2025-09-15 15:33:58 +08:00
}
override disconnectedCallback() {
2025-09-16 14:32:57 +08:00
super.disconnectedCallback()
window.removeEventListener('pointerup', this.onPointerUp)
window.removeEventListener('pointermove', this.onPointerMove)
this.removeEventListener('pointerdown', this.toggleFocus)
wfem.removeEventListener('windowFormFocus', this.windowFormFocusFun)
this.removeAllManagedListeners()
2025-09-15 15:33:58 +08:00
}
2025-09-16 14:32:57 +08:00
private windowFormFocusFun = (id: string) => {
if (id === this.wid) {
this.focused = true
} else {
this.focused = false
}
}
private toggleFocus = () => {
this.focused = !this.focused
wfem.notifyEvent('windowFormFocus', this.wid)
this.dispatchEvent(
new CustomEvent('update:focused', {
detail: this.focused,
bubbles: true,
composed: true,
}),
)
2025-09-15 15:33:58 +08:00
}
// ====== 拖拽 ======
private onTitlePointerDown = (e: PointerEvent) => {
2025-09-16 14:32:57 +08:00
if (e.pointerType === 'mouse' && e.button !== 0) return
if ((e.target as HTMLElement).closest('.controls')) return
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
e.preventDefault()
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
this.dragging = true
this.startX = e.clientX
this.startY = e.clientY
this.preX = this.x
this.preY = this.y
this.setPointerCapture?.(e.pointerId)
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
this.dispatchEvent(
new CustomEvent('windowForm:dragStart', {
detail: { x: this.x, y: this.y },
bubbles: true,
composed: true,
}),
)
}
2025-09-15 15:33:58 +08:00
private onPointerMove = (e: PointerEvent) => {
if (this.dragging) {
2025-09-16 14:32:57 +08:00
const dx = e.clientX - this.startX
const dy = e.clientY - this.startY
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
const x = this.preX + dx
const y = this.preY + dy
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
this.applyPosition(x, y, false)
this.dispatchEvent(
new CustomEvent('windowForm:dragMove', {
detail: { x, y },
bubbles: true,
composed: true,
}),
)
2025-09-15 15:33:58 +08:00
} else if (this.resizeDir) {
2025-09-16 14:32:57 +08:00
this.performResize(e)
2025-09-15 15:33:58 +08:00
}
2025-09-16 14:32:57 +08:00
}
2025-09-15 15:33:58 +08:00
private onPointerUp = (e: PointerEvent) => {
if (this.dragging) {
this.dragUp(e)
}
if (this.resizeDir) {
2025-09-16 14:32:57 +08:00
this.resizeUp(e)
2025-09-15 15:33:58 +08:00
}
2025-09-16 14:32:57 +08:00
this.dragging = false
this.resizing = false
this.resizeDir = null
2025-09-15 16:25:15 +08:00
document.body.style.cursor = ''
2025-09-16 14:32:57 +08:00
try {
this.releasePointerCapture?.(e.pointerId)
} catch {}
}
2025-09-15 15:33:58 +08:00
/** 获取所有吸附点 */
private getSnapPoints() {
2025-09-16 14:32:57 +08:00
const snapPoints = { x: [] as number[], y: [] as number[] }
const containerRect = (this.dragContainer || document.body).getBoundingClientRect()
const rect = this.getBoundingClientRect()
snapPoints.x = [0, containerRect.width - rect.width]
snapPoints.y = [0, containerRect.height - rect.height]
return snapPoints
2025-09-15 15:33:58 +08:00
}
/**
* 获取最近的吸附点
* @param x 左上角起始点x
* @param y 左上角起始点y
*/
private applySnapping(x: number, y: number) {
2025-09-16 14:32:57 +08:00
let snappedX = x,
snappedY = y
const containerSnap = this.getSnapPoints()
2025-09-15 15:33:58 +08:00
if (this.snapDistance > 0) {
2025-09-16 14:32:57 +08:00
for (const sx of containerSnap.x)
if (Math.abs(x - sx) <= this.snapDistance) {
snappedX = sx
break
}
for (const sy of containerSnap.y)
if (Math.abs(y - sy) <= this.snapDistance) {
snappedY = sy
break
}
2025-09-15 15:33:58 +08:00
}
2025-09-16 14:32:57 +08:00
return { x: snappedX, y: snappedY }
2025-09-15 15:33:58 +08:00
}
private dragUp(e: PointerEvent) {
2025-09-16 14:32:57 +08:00
const snapped = this.applySnapping(this.x, this.y)
2025-09-15 15:33:58 +08:00
if (this.snapAnimation) {
this.animateTo(snapped.x, snapped.y, this.snapAnimationDuration, () => {
2025-09-16 14:32:57 +08:00
this.updateTargetBounds(snapped.x, snapped.y)
this.dispatchEvent(
new CustomEvent('windowForm:dragEnd', {
detail: { x: snapped.x, y: snapped.y },
bubbles: true,
composed: true,
}),
)
})
} else {
this.applyPosition(snapped.x, snapped.y, true)
this.updateTargetBounds(snapped.x, snapped.y)
this.dispatchEvent(
new CustomEvent('windowForm:dragEnd', {
2025-09-15 15:33:58 +08:00
detail: { x: snapped.x, y: snapped.y },
bubbles: true,
2025-09-16 14:32:57 +08:00
composed: true,
}),
)
2025-09-15 15:33:58 +08:00
}
}
private applyPosition(x: number, y: number, isFinal: boolean) {
2025-09-16 14:32:57 +08:00
this.x = x
this.y = y
this.style.transform = `translate(${x}px, ${y}px)`
if (isFinal) this.applyBoundary()
2025-09-15 15:33:58 +08:00
}
private applyBoundary() {
2025-09-16 14:32:57 +08:00
if (this.allowOverflow) return
let { x, y } = { x: this.x, y: this.y }
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
const rect = this.getBoundingClientRect()
const containerRect = (this.dragContainer || document.body).getBoundingClientRect()
x = Math.min(Math.max(x, 0), containerRect.width - rect.width)
y = Math.min(Math.max(y, 0), containerRect.height - rect.height)
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
this.applyPosition(x, y, false)
2025-09-15 15:33:58 +08:00
}
private animateTo(targetX: number, targetY: number, duration: number, onComplete?: () => void) {
2025-09-16 14:32:57 +08:00
if (this.animationFrame) cancelAnimationFrame(this.animationFrame)
const startX = this.x
const startY = this.y
const deltaX = targetX - startX
const deltaY = targetY - startY
const startTime = performance.now()
2025-09-15 15:33:58 +08:00
const step = (now: number) => {
2025-09-16 14:32:57 +08:00
const elapsed = now - startTime
const progress = Math.min(elapsed / duration, 1)
const ease = 1 - Math.pow(1 - progress, 3)
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
const x = startX + deltaX * ease
const y = startY + deltaY * ease
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
this.applyPosition(x, y, false)
this.dispatchEvent(
new CustomEvent('windowForm:dragMove', {
detail: { x, y },
bubbles: true,
composed: true,
}),
)
2025-09-15 15:33:58 +08:00
if (progress < 1) {
2025-09-16 14:32:57 +08:00
this.animationFrame = requestAnimationFrame(step)
2025-09-15 15:33:58 +08:00
} else {
2025-09-16 14:32:57 +08:00
this.applyPosition(targetX, targetY, true)
onComplete?.()
2025-09-15 15:33:58 +08:00
}
2025-09-16 14:32:57 +08:00
}
this.animationFrame = requestAnimationFrame(step)
2025-09-15 15:33:58 +08:00
}
// ====== 缩放 ======
private startResize = (dir: TResizeDirection, e: PointerEvent) => {
2025-09-16 14:32:57 +08:00
if (!this.resizable) return
if (e.pointerType === 'mouse' && e.button !== 0) return
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
e.preventDefault()
e.stopPropagation()
this.resizing = true
this.resizeDir = dir
this.startX = e.clientX
this.startY = e.clientY
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
const rect = this.getBoundingClientRect()
this.startWidth = rect.width
this.startHeight = rect.height
this.startX_host = rect.left
this.startY_host = rect.top
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
const target = e.target as HTMLElement
document.body.style.cursor = target.style.cursor || window.getComputedStyle(target).cursor
2025-09-15 16:25:15 +08:00
2025-09-16 14:32:57 +08:00
this.setPointerCapture?.(e.pointerId)
this.dispatchEvent(
new CustomEvent('windowForm:resizeStart', {
detail: { x: this.x, y: this.y, width: this.width, height: this.height, dir },
bubbles: true,
composed: true,
}),
)
}
2025-09-15 15:33:58 +08:00
private performResize(e: PointerEvent) {
2025-09-16 14:32:57 +08:00
if (!this.resizeDir || !this.resizing) return
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
let newWidth = this.startWidth
let newHeight = this.startHeight
let newX = this.startX_host
let newY = this.startY_host
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
const dx = e.clientX - this.startX
const dy = e.clientY - this.startY
2025-09-15 15:33:58 +08:00
switch (this.resizeDir) {
2025-09-16 14:32:57 +08:00
case 'r':
newWidth += dx
break
case 'b':
newHeight += dy
break
case 'l':
newWidth -= dx
newX += dx
break
case 't':
newHeight -= dy
newY += dy
break
case 'tl':
newWidth -= dx
newX += dx
newHeight -= dy
newY += dy
break
case 'tr':
newWidth += dx
newHeight -= dy
newY += dy
break
case 'bl':
newWidth -= dx
newX += dx
newHeight += dy
break
case 'br':
newWidth += dx
newHeight += dy
break
2025-09-15 15:33:58 +08:00
}
2025-09-16 14:32:57 +08:00
const d = this.applyResizeBounds(newX, newY, newWidth, newHeight)
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
this.dispatchEvent(
new CustomEvent('windowForm:resizeMove', {
detail: { dir: this.resizeDir, width: d.width, height: d.height, left: d.left, top: d.top },
bubbles: true,
composed: true,
}),
)
2025-09-15 15:33:58 +08:00
}
/**
* 应用尺寸调整边界
* @param newX 新的X坐标
* @param newY 新的Y坐标
* @param newWidth 新的宽度
* @param newHeight 新的高度
* @private
*/
2025-09-16 14:32:57 +08:00
private applyResizeBounds(
newX: number,
newY: number,
newWidth: number,
newHeight: number,
): {
left: number
top: number
width: number
height: number
2025-09-15 15:33:58 +08:00
} {
// 最小/最大宽高限制
2025-09-16 14:32:57 +08:00
if (this.minWidth != null) newWidth = Math.max(this.minWidth, newWidth)
if (this.maxWidth != null) newWidth = Math.min(this.maxWidth, newWidth)
if (this.minHeight != null) newHeight = Math.max(this.minHeight, newHeight)
if (this.maxHeight != null) newHeight = Math.min(this.maxHeight, newHeight)
2025-09-15 15:33:58 +08:00
// 边界限制
if (this.allowOverflow) {
2025-09-16 14:32:57 +08:00
this.x = newX
this.y = newY
this.width = newWidth
this.height = newHeight
this.style.width = `${newWidth}px`
this.style.height = `${newHeight}px`
this.applyPosition(newX, newY, false)
2025-09-15 15:33:58 +08:00
return {
left: newX,
top: newY,
width: newWidth,
height: newHeight,
}
}
2025-09-16 14:32:57 +08:00
const containerRect = (this.dragContainer || document.body).getBoundingClientRect()
newX = Math.min(Math.max(0, newX), containerRect.width - newWidth)
newY = Math.min(Math.max(0, newY), containerRect.height - newHeight)
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
this.x = newX
this.y = newY
this.width = newWidth
this.height = newHeight
this.style.width = `${newWidth}px`
this.style.height = `${newHeight}px`
this.applyPosition(newX, newY, false)
2025-09-15 15:33:58 +08:00
return {
left: newX,
top: newY,
width: newWidth,
height: newHeight,
}
}
private resizeUp(e: PointerEvent) {
2025-09-16 14:32:57 +08:00
if (!this.resizable) return
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
this.updateTargetBounds(this.x, this.y, this.width, this.height)
this.dispatchEvent(
new CustomEvent('windowForm:resizeEnd', {
detail: {
dir: this.resizeDir,
width: this.width,
height: this.height,
left: this.x,
top: this.y,
},
bubbles: true,
composed: true,
}),
)
2025-09-15 15:33:58 +08:00
}
// ====== 窗口操作 ======
// 最小化到任务栏
private minimize() {
2025-09-16 14:32:57 +08:00
if (!this.taskbarElementId) return
if (this.windowFormState === 'minimized') return
2025-09-15 15:33:58 +08:00
this.targetPreMinimizeBounds = { ...this.targetBounds }
2025-09-16 14:32:57 +08:00
this.windowFormState = 'minimized'
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
const taskbar = document.querySelector(this.taskbarElementId)
if (!taskbar) throw new Error('任务栏元素未找到')
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
const rect = taskbar.getBoundingClientRect()
const startX = this.x
const startY = this.y
const startW = this.offsetWidth
const startH = this.offsetHeight
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
this.animateWindow(
startX,
startY,
startW,
startH,
rect.left,
rect.top,
rect.width,
rect.height,
400,
() => {
this.style.display = 'none'
this.dispatchEvent(
new CustomEvent('windowForm:stateChange:minimize', {
detail: { state: this.windowFormState },
bubbles: true,
composed: true,
}),
)
},
)
2025-09-15 15:33:58 +08:00
}
/** 最大化 */
private maximize() {
if (this.windowFormState === 'maximized') {
2025-09-16 14:32:57 +08:00
this.restore()
return
2025-09-15 15:33:58 +08:00
}
this.targetPreMaximizedBounds = { ...this.targetBounds }
2025-09-16 14:32:57 +08:00
this.windowFormState = 'maximized'
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
const rect = this.getBoundingClientRect()
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
const startX = this.x
const startY = this.y
const startW = rect.width
const startH = rect.height
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
const targetX = 0
const targetY = 0
const containerRect = (this.dragContainer || document.body).getBoundingClientRect()
const targetW = containerRect?.width ?? window.innerWidth
const targetH = containerRect?.height ?? window.innerHeight
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
this.animateWindow(
startX,
startY,
startW,
startH,
targetX,
targetY,
targetW,
targetH,
300,
() => {
this.dispatchEvent(
new CustomEvent('windowForm:stateChange:maximize', {
detail: { state: this.windowFormState },
bubbles: true,
composed: true,
}),
)
},
)
2025-09-15 15:33:58 +08:00
}
/** 恢复到默认窗体状态 */
private restore(onComplete?: () => void) {
2025-09-16 14:32:57 +08:00
if (this.windowFormState === 'default') return
let b: IElementRect
if (
(this.windowFormState as TWindowFormState) === 'minimized' &&
this.targetPreMinimizeBounds
) {
2025-09-15 15:33:58 +08:00
// 最小化恢复,恢复到最小化前的状态
2025-09-16 14:32:57 +08:00
b = this.targetPreMinimizeBounds
} else if (
(this.windowFormState as TWindowFormState) === 'maximized' &&
this.targetPreMaximizedBounds
) {
2025-09-15 15:33:58 +08:00
// 最大化恢复,恢复到最大化前的默认状态
2025-09-16 14:32:57 +08:00
b = this.targetPreMaximizedBounds
2025-09-15 15:33:58 +08:00
} else {
2025-09-16 14:32:57 +08:00
b = this.targetBounds
2025-09-15 15:33:58 +08:00
}
2025-09-16 14:32:57 +08:00
this.windowFormState = 'default'
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
this.style.display = 'block'
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
const startX = this.x
const startY = this.y
const startW = this.offsetWidth
const startH = this.offsetHeight
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
this.animateWindow(
startX,
startY,
startW,
startH,
b.left,
b.top,
b.width,
b.height,
300,
() => {
onComplete?.()
this.dispatchEvent(
new CustomEvent('windowForm:stateChange:restore', {
detail: { state: this.windowFormState },
bubbles: true,
composed: true,
}),
)
},
)
}
private windowFormClose() {
this.dispatchEvent(
new CustomEvent('windowForm:close', {
bubbles: true,
composed: true,
}),
)
2025-09-15 15:33:58 +08:00
}
/**
* 窗体最大化、最小化和恢复默认 动画
* @param startX
* @param startY
* @param startW
* @param startH
* @param targetX
* @param targetY
* @param targetW
* @param targetH
* @param duration
* @param onComplete
* @private
*/
private animateWindow(
startX: number,
startY: number,
startW: number,
startH: number,
targetX: number,
targetY: number,
targetW: number,
targetH: number,
duration: number,
2025-09-16 14:32:57 +08:00
onComplete?: () => void,
2025-09-15 15:33:58 +08:00
) {
2025-09-16 14:32:57 +08:00
const startTime = performance.now()
2025-09-15 15:33:58 +08:00
const step = (now: number) => {
2025-09-16 14:32:57 +08:00
const elapsed = now - startTime
const progress = Math.min(elapsed / duration, 1)
const ease = 1 - Math.pow(1 - progress, 3)
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
const x = startX + (targetX - startX) * ease
const y = startY + (targetY - startY) * ease
const w = startW + (targetW - startW) * ease
const h = startH + (targetH - startH) * ease
2025-09-15 15:33:58 +08:00
2025-09-16 14:32:57 +08:00
this.style.width = `${w}px`
this.style.height = `${h}px`
this.applyPosition(x, y, false)
2025-09-15 15:33:58 +08:00
if (progress < 1) {
2025-09-16 14:32:57 +08:00
requestAnimationFrame(step)
2025-09-15 15:33:58 +08:00
} else {
2025-09-16 14:32:57 +08:00
this.style.width = `${targetW}px`
this.style.height = `${targetH}px`
this.applyPosition(targetX, targetY, true)
onComplete?.()
this.dispatchEvent(
new CustomEvent('windowForm:stateChange', {
detail: { state: this.windowFormState },
bubbles: true,
composed: true,
}),
)
2025-09-15 15:33:58 +08:00
}
2025-09-16 14:32:57 +08:00
}
requestAnimationFrame(step)
2025-09-15 15:33:58 +08:00
}
private updateTargetBounds(left: number, top: number, width?: number, height?: number) {
this.targetBounds = {
2025-09-16 14:32:57 +08:00
left,
top,
2025-09-15 15:33:58 +08:00
width: width ?? this.offsetWidth,
2025-09-16 14:32:57 +08:00
height: height ?? this.offsetHeight,
}
2025-09-15 15:33:58 +08:00
}
// ====== 渲染 ======
override render() {
return html`
2025-09-16 14:32:57 +08:00
<div class="window">
2025-09-15 15:33:58 +08:00
<div class="titlebar" @pointerdown=${this.onTitlePointerDown}>
<div class="title" title=${this.title}>${this.title}</div>
<div class="controls">
2025-09-16 14:32:57 +08:00
${this.minimizable
? html`<button
class="ctrl"
@click=${(e: Event) => {
e.stopPropagation()
this.minimize()
}}
>
—
</button>`
: null}
${this.maximizable
? html`<button
class="ctrl"
@click=${(e: Event) => {
e.stopPropagation()
this.maximize()
}}
>
${this.windowFormState === 'maximized' ? '▣' : '▢'}
</button>`
: null}
${this.closable
? html`<button
class="ctrl"
@click=${(e: Event) => {
e.stopPropagation()
this.windowFormClose()
}}
>
✕
</button>`
: null}
2025-09-15 15:33:58 +08:00
</div>
</div>
<div class="content"><slot></slot></div>
2025-09-16 14:32:57 +08:00
${this.resizable
? html`
<div
class="resizer t"
@pointerdown=${(e: PointerEvent) => this.startResize('t', e)}
></div>
<div
class="resizer b"
@pointerdown=${(e: PointerEvent) => this.startResize('b', e)}
></div>
<div
class="resizer r"
@pointerdown=${(e: PointerEvent) => this.startResize('r', e)}
></div>
<div
class="resizer l"
@pointerdown=${(e: PointerEvent) => this.startResize('l', e)}
></div>
<div
class="resizer tr"
@pointerdown=${(e: PointerEvent) => this.startResize('tr', e)}
></div>
<div
class="resizer tl"
@pointerdown=${(e: PointerEvent) => this.startResize('tl', e)}
></div>
<div
class="resizer br"
@pointerdown=${(e: PointerEvent) => this.startResize('br', e)}
></div>
<div
class="resizer bl"
@pointerdown=${(e: PointerEvent) => this.startResize('bl', e)}
></div>
`
: null}
2025-09-15 15:33:58 +08:00
</div>
2025-09-16 14:32:57 +08:00
`
2025-09-15 15:33:58 +08:00
}
}
declare global {
interface HTMLElementTagNameMap {
'window-form-element': WindowFormElement;
}
}