1
This commit is contained in:
@@ -10,49 +10,64 @@ export interface IWindowFormEvent extends IEventMap {
|
||||
* 窗口最小化
|
||||
* @param id 窗口id
|
||||
*/
|
||||
windowFormMinimize: (id: string) => void;
|
||||
windowFormMinimize: (id: string) => void
|
||||
/**
|
||||
* 窗口最大化
|
||||
* @param id 窗口id
|
||||
*/
|
||||
windowFormMaximize: (id: string) => void;
|
||||
windowFormMaximize: (id: string) => void
|
||||
/**
|
||||
* 窗口还原
|
||||
* @param id 窗口id
|
||||
*/
|
||||
windowFormRestore: (id: string) => void;
|
||||
windowFormRestore: (id: string) => void
|
||||
/**
|
||||
* 窗口关闭
|
||||
* @param id 窗口id
|
||||
*/
|
||||
windowFormClose: (id: string) => void;
|
||||
windowFormClose: (id: string) => void
|
||||
/**
|
||||
* 窗口聚焦
|
||||
* @param id 窗口id
|
||||
*/
|
||||
windowFormFocus: (id: string) => void;
|
||||
windowFormFocus: (id: string) => void
|
||||
/**
|
||||
* 窗口数据更新
|
||||
* @param data 窗口数据
|
||||
*/
|
||||
windowFormDataUpdate: (data: IWindowFormDataUpdateParams) => void;
|
||||
windowFormDataUpdate: (data: IWindowFormDataUpdateParams) => void
|
||||
/**
|
||||
* 窗口创建完成
|
||||
*/
|
||||
windowFormCreated: () => void;
|
||||
windowFormCreated: () => void
|
||||
/**
|
||||
* 开始调整窗体尺寸
|
||||
* @param id 窗口id
|
||||
*/
|
||||
windowFormResizeStart: (id: string) => void
|
||||
/**
|
||||
* 调整尺寸过程中
|
||||
* @param data 窗口数据
|
||||
*/
|
||||
windowFormResizing: (data: IWindowFormDataUpdateParams) => void
|
||||
/**
|
||||
* 完成窗体尺寸调整
|
||||
* @param id 窗口id
|
||||
*/
|
||||
windowFormResizeEnd: (id: string) => void
|
||||
}
|
||||
|
||||
interface IWindowFormDataUpdateParams {
|
||||
export interface IWindowFormDataUpdateParams {
|
||||
/** 窗口id */
|
||||
id: string;
|
||||
id: string
|
||||
/** 窗口状态 */
|
||||
state: TWindowFormState,
|
||||
state: TWindowFormState
|
||||
/** 窗口宽度 */
|
||||
width: number,
|
||||
width: number
|
||||
/** 窗口高度 */
|
||||
height: number,
|
||||
height: number
|
||||
/** 窗口x坐标(左上角) */
|
||||
x: number,
|
||||
x: number
|
||||
/** 窗口y坐标(左上角) */
|
||||
y: number
|
||||
}
|
||||
|
||||
@@ -508,7 +508,10 @@ export class EventCommunicationService {
|
||||
|
||||
if (subscribers.length === 0) {
|
||||
message.status = MessageStatus.FAILED
|
||||
console.warn(`[EventCommunication] 没有找到频道 ${message.channel} 的订阅者[消息 ID: ${message.id}]`)
|
||||
// 只对非系统频道显示警告信息
|
||||
if (message.channel !== 'system') {
|
||||
console.warn(`[EventCommunication] 没有找到频道 ${message.channel} 的订阅者[消息 ID: ${message.id}]`)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import { EventCommunicationService } from './EventCommunicationService'
|
||||
import { ApplicationSandboxEngine } from './ApplicationSandboxEngine'
|
||||
import { ApplicationLifecycleManager } from './ApplicationLifecycleManager'
|
||||
import { externalAppDiscovery } from './ExternalAppDiscovery'
|
||||
import type { IWindowFormDataUpdateParams } from '@/events/WindowFormEventManager'
|
||||
|
||||
/**
|
||||
* 系统服务配置接口
|
||||
@@ -404,6 +405,64 @@ export class SystemServiceIntegration {
|
||||
}
|
||||
})
|
||||
|
||||
// 监听窗体数据更新事件
|
||||
this.eventBus.addEventListener(
|
||||
'onWindowFormDataUpdate',
|
||||
(data: IWindowFormDataUpdateParams) => {
|
||||
console.log(`[SystemIntegration] 接收到窗体数据更新事件:`, data)
|
||||
// 只有在有订阅者时才发送消息
|
||||
if (this.eventService.getChannelSubscriberCount('window-form-data-update') > 0) {
|
||||
this.eventService.sendMessage('system', 'window-form-data-update', data)
|
||||
console.log(`[SystemIntegration] 已发送 window-form-data-update 消息到事件通信服务`)
|
||||
} else {
|
||||
console.log(`[SystemIntegration] 无订阅者,跳过发送 window-form-data-update 消息`)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
// 监听窗体调整尺寸开始事件
|
||||
this.eventBus.addEventListener('onResizeStart', (windowId: string) => {
|
||||
console.log(`[SystemIntegration] 接收到窗体调整尺寸开始事件: ${windowId}`)
|
||||
// 只有在有订阅者时才发送消息
|
||||
if (this.eventService.getChannelSubscriberCount('window-form-resize-start') > 0) {
|
||||
this.eventService.sendMessage('system', 'window-form-resize-start', { windowId })
|
||||
} else {
|
||||
console.log(`[SystemIntegration] 无订阅者,跳过发送 window-form-resize-start 消息`)
|
||||
}
|
||||
})
|
||||
|
||||
// 监听窗体调整尺寸过程中事件
|
||||
this.eventBus.addEventListener(
|
||||
'onResizing',
|
||||
(windowId: string, width: number, height: number) => {
|
||||
console.log(`[SystemIntegration] 接收到窗体调整尺寸过程中事件: ${windowId}`, {
|
||||
width,
|
||||
height,
|
||||
})
|
||||
// 只有在有订阅者时才发送消息
|
||||
if (this.eventService.getChannelSubscriberCount('window-form-resizing') > 0) {
|
||||
this.eventService.sendMessage('system', 'window-form-resizing', {
|
||||
windowId,
|
||||
width,
|
||||
height,
|
||||
})
|
||||
} else {
|
||||
console.log(`[SystemIntegration] 无订阅者,跳过发送 window-form-resizing 消息`)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
// 监听窗体调整尺寸结束事件
|
||||
this.eventBus.addEventListener('onResizeEnd', (windowId: string) => {
|
||||
console.log(`[SystemIntegration] 接收到窗体调整尺寸结束事件: ${windowId}`)
|
||||
// 只有在有订阅者时才发送消息
|
||||
if (this.eventService.getChannelSubscriberCount('window-form-resize-end') > 0) {
|
||||
this.eventService.sendMessage('system', 'window-form-resize-end', { windowId })
|
||||
} else {
|
||||
console.log(`[SystemIntegration] 无订阅者,跳过发送 window-form-resize-end 消息`)
|
||||
}
|
||||
})
|
||||
|
||||
// 监听资源配额超出
|
||||
this.eventBus.addEventListener(
|
||||
'onResourceQuotaExceeded',
|
||||
@@ -567,8 +626,18 @@ export class SystemServiceIntegration {
|
||||
return this.windowService.setWindowSize(windowId, data.width, data.height)
|
||||
|
||||
case 'move':
|
||||
// 需要实现窗体移动功能
|
||||
return true
|
||||
// 实现窗体移动功能
|
||||
const window = this.windowService.getWindow(windowId);
|
||||
if (window && window.element) {
|
||||
// 更新窗体位置
|
||||
window.config.x = data.x;
|
||||
window.config.y = data.y;
|
||||
window.element.style.left = `${data.x}px`;
|
||||
window.element.style.top = `${data.y}px`;
|
||||
window.element.style.transform = 'none'; // 确保移除transform
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
case 'minimize':
|
||||
return this.windowService.minimizeWindow(windowId)
|
||||
@@ -583,14 +652,14 @@ export class SystemServiceIntegration {
|
||||
return this.lifecycleManager.stopApp(appId)
|
||||
|
||||
case 'getState':
|
||||
const window = this.windowService.getWindow(windowId)
|
||||
return window?.state
|
||||
const windowInfo = this.windowService.getWindow(windowId)
|
||||
return windowInfo?.state
|
||||
|
||||
case 'getSize':
|
||||
const windowInfo = this.windowService.getWindow(windowId)
|
||||
const windowData = this.windowService.getWindow(windowId)
|
||||
return {
|
||||
width: windowInfo?.config.width,
|
||||
height: windowInfo?.config.height,
|
||||
width: windowData?.config.width,
|
||||
height: windowData?.config.height,
|
||||
}
|
||||
|
||||
default:
|
||||
@@ -870,4 +939,4 @@ export class SystemServiceIntegration {
|
||||
console.log(`[SystemService] ${message}`, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { ref, reactive } from 'vue'
|
||||
import type { IEventBuilder, IEventMap } from '@/events/IEventBuilder'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import type { ResizeDirection, ResizeState } from '@/ui/types/WindowFormTypes'
|
||||
|
||||
/**
|
||||
* 窗体状态枚举
|
||||
@@ -132,6 +133,10 @@ export interface WindowInstance {
|
||||
* 窗体更新时间
|
||||
*/
|
||||
updatedAt: Date
|
||||
/**
|
||||
* 拖拽调整尺寸状态
|
||||
*/
|
||||
resizeState?: ResizeState
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,6 +149,9 @@ export interface WindowEvents extends IEventMap {
|
||||
onFocus: (windowId: string) => void
|
||||
onBlur: (windowId: string) => void
|
||||
onClose: (windowId: string) => void
|
||||
onResizeStart: (windowId: string) => void
|
||||
onResizing: (windowId: string, width: number, height: number) => void
|
||||
onResizeEnd: (windowId: string) => void
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,6 +165,7 @@ export class WindowService {
|
||||
|
||||
constructor(eventBus: IEventBuilder<WindowEvents>) {
|
||||
this.eventBus = eventBus
|
||||
this.setupGlobalResizeEvents()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -248,6 +257,9 @@ export class WindowService {
|
||||
window.element.style.display = 'none'
|
||||
}
|
||||
|
||||
// 发送窗体数据更新事件
|
||||
this.notifyWindowFormDataUpdate(windowId)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -276,10 +288,15 @@ export class WindowService {
|
||||
width: '100vw',
|
||||
height: 'calc(100vh - 40px)', // 减去任务栏高度
|
||||
display: 'block',
|
||||
transform: 'none' // 确保移除transform
|
||||
})
|
||||
}
|
||||
|
||||
this.setActiveWindow(windowId)
|
||||
|
||||
// 发送窗体数据更新事件
|
||||
this.notifyWindowFormDataUpdate(windowId)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -312,14 +329,22 @@ export class WindowService {
|
||||
Object.assign(window.element.style, {
|
||||
width: originalWidth ? `${originalWidth}px` : `${window.config.width}px`,
|
||||
height: originalHeight ? `${originalHeight}px` : `${window.config.height}px`,
|
||||
left: originalX ? `${originalX}px` : '50%',
|
||||
top: originalY ? `${originalY}px` : '50%',
|
||||
transform: originalX && originalY ? 'none' : 'translate(-50%, -50%)',
|
||||
left: originalX ? `${originalX}px` : '0px',
|
||||
top: originalY ? `${originalY}px` : '0px',
|
||||
transform: 'none' // 确保移除transform
|
||||
})
|
||||
|
||||
// 更新配置中的位置
|
||||
if (originalX) window.config.x = parseFloat(originalX);
|
||||
if (originalY) window.config.y = parseFloat(originalY);
|
||||
}
|
||||
}
|
||||
|
||||
this.setActiveWindow(windowId)
|
||||
|
||||
// 发送窗体数据更新事件
|
||||
this.notifyWindowFormDataUpdate(windowId)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -341,6 +366,9 @@ export class WindowService {
|
||||
}
|
||||
}
|
||||
|
||||
// 发送窗体数据更新事件
|
||||
this.notifyWindowFormDataUpdate(windowId)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -352,14 +380,8 @@ export class WindowService {
|
||||
if (!window) return false
|
||||
|
||||
// 检查尺寸限制
|
||||
const finalWidth = Math.max(
|
||||
window.config.minWidth || 200,
|
||||
Math.min(window.config.maxWidth || Infinity, width),
|
||||
)
|
||||
const finalHeight = Math.max(
|
||||
window.config.minHeight || 150,
|
||||
Math.min(window.config.maxHeight || Infinity, height),
|
||||
)
|
||||
const finalWidth = this.clampDimension(width, window.config.minWidth, window.config.maxWidth)
|
||||
const finalHeight = this.clampDimension(height, window.config.minHeight, window.config.maxHeight)
|
||||
|
||||
window.config.width = finalWidth
|
||||
window.config.height = finalHeight
|
||||
@@ -371,6 +393,10 @@ export class WindowService {
|
||||
}
|
||||
|
||||
this.eventBus.notifyEvent('onResize', windowId, finalWidth, finalHeight)
|
||||
|
||||
// 发送窗体数据更新事件
|
||||
this.notifyWindowFormDataUpdate(windowId)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -410,6 +436,10 @@ export class WindowService {
|
||||
}
|
||||
|
||||
this.eventBus.notifyEvent('onFocus', windowId)
|
||||
|
||||
// 发送窗体数据更新事件
|
||||
this.notifyWindowFormDataUpdate(windowId)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -434,21 +464,36 @@ export class WindowService {
|
||||
windowElement.className = 'system-window'
|
||||
windowElement.id = `window-${id}`
|
||||
|
||||
// 计算初始位置
|
||||
let left = config.x;
|
||||
let top = config.y;
|
||||
|
||||
// 如果没有指定位置,则居中显示
|
||||
if (left === undefined || top === undefined) {
|
||||
const centerX = Math.max(0, (window.innerWidth - config.width) / 2);
|
||||
const centerY = Math.max(0, (window.innerHeight - config.height) / 2);
|
||||
left = left !== undefined ? left : centerX;
|
||||
top = top !== undefined ? top : centerY;
|
||||
}
|
||||
|
||||
// 设置基本样式
|
||||
Object.assign(windowElement.style, {
|
||||
position: 'fixed',
|
||||
width: `${config.width}px`,
|
||||
height: `${config.height}px`,
|
||||
left: config.x ? `${config.x}px` : '50%',
|
||||
top: config.y ? `${config.y}px` : '50%',
|
||||
transform: config.x && config.y ? 'none' : 'translate(-50%, -50%)',
|
||||
left: `${left}px`,
|
||||
top: `${top}px`,
|
||||
zIndex: windowInstance.zIndex.toString(),
|
||||
backgroundColor: '#fff',
|
||||
border: '1px solid #ccc',
|
||||
borderRadius: '8px',
|
||||
boxShadow: '0 4px 20px rgba(0,0,0,0.15)',
|
||||
overflow: 'hidden',
|
||||
})
|
||||
});
|
||||
|
||||
// 保存初始位置到配置中
|
||||
windowInstance.config.x = left;
|
||||
windowInstance.config.y = top;
|
||||
|
||||
// 创建窗体标题栏
|
||||
const titleBar = this.createTitleBar(windowInstance)
|
||||
@@ -496,6 +541,11 @@ export class WindowService {
|
||||
|
||||
windowElement.appendChild(contentArea)
|
||||
|
||||
// 添加拖拽调整尺寸功能
|
||||
if (config.resizable !== false) {
|
||||
this.addResizeFunctionality(windowElement, windowInstance)
|
||||
}
|
||||
|
||||
// 添加到页面
|
||||
document.body.appendChild(windowElement)
|
||||
|
||||
@@ -624,6 +674,17 @@ export class WindowService {
|
||||
let startTop = 0
|
||||
|
||||
titleBar.addEventListener('mousedown', (e) => {
|
||||
// 检查是否正在调整尺寸,如果是则不处理拖拽
|
||||
if (windowInstance.resizeState?.isResizing) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查是否点击在调整尺寸手柄上,如果是则不处理拖拽
|
||||
const target = e.target as HTMLElement;
|
||||
if (target.classList.contains('resize-handle')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!windowInstance.element) return
|
||||
|
||||
isDragging = true
|
||||
@@ -631,6 +692,17 @@ export class WindowService {
|
||||
startY = e.clientY
|
||||
|
||||
const rect = windowInstance.element.getBoundingClientRect()
|
||||
|
||||
// 如果使用了transform,需要转换为实际坐标
|
||||
if (windowInstance.element.style.transform && windowInstance.element.style.transform.includes('translate')) {
|
||||
// 移除transform并设置实际的left/top值
|
||||
windowInstance.element.style.transform = 'none';
|
||||
windowInstance.config.x = rect.left;
|
||||
windowInstance.config.y = rect.top;
|
||||
windowInstance.element.style.left = `${rect.left}px`;
|
||||
windowInstance.element.style.top = `${rect.top}px`;
|
||||
}
|
||||
|
||||
startLeft = rect.left
|
||||
startTop = rect.top
|
||||
|
||||
@@ -638,9 +710,15 @@ export class WindowService {
|
||||
this.setActiveWindow(windowInstance.id)
|
||||
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
})
|
||||
|
||||
document.addEventListener('mousemove', (e) => {
|
||||
// 检查是否正在调整尺寸,如果是则不处理拖拽
|
||||
if (windowInstance.resizeState?.isResizing) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isDragging || !windowInstance.element) return
|
||||
|
||||
const deltaX = e.clientX - startX
|
||||
@@ -651,13 +729,15 @@ export class WindowService {
|
||||
|
||||
windowInstance.element.style.left = `${newLeft}px`
|
||||
windowInstance.element.style.top = `${newTop}px`
|
||||
windowInstance.element.style.transform = 'none'
|
||||
|
||||
// 更新配置
|
||||
windowInstance.config.x = newLeft
|
||||
windowInstance.config.y = newTop
|
||||
|
||||
this.eventBus.notifyEvent('onMove', windowInstance.id, newLeft, newTop)
|
||||
|
||||
// 发送窗体数据更新事件
|
||||
this.notifyWindowFormDataUpdate(windowInstance.id)
|
||||
})
|
||||
|
||||
document.addEventListener('mouseup', () => {
|
||||
@@ -665,6 +745,382 @@ export class WindowService {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加窗体调整尺寸功能
|
||||
*/
|
||||
private addResizeFunctionality(windowElement: HTMLElement, windowInstance: WindowInstance): void {
|
||||
// 初始化调整尺寸状态
|
||||
windowInstance.resizeState = {
|
||||
isResizing: false,
|
||||
direction: 'none',
|
||||
startX: 0,
|
||||
startY: 0,
|
||||
startWidth: 0,
|
||||
startHeight: 0,
|
||||
startXPosition: 0,
|
||||
startYPosition: 0
|
||||
}
|
||||
|
||||
// 创建8个调整尺寸的手柄
|
||||
const resizeHandles = this.createResizeHandles(windowElement)
|
||||
|
||||
// 添加鼠标事件监听器
|
||||
resizeHandles.forEach(handle => {
|
||||
this.addResizeHandleEvents(handle, windowElement, windowInstance)
|
||||
})
|
||||
|
||||
// 添加窗口边缘检测
|
||||
windowElement.addEventListener('mousemove', (e) => {
|
||||
if (!windowInstance.resizeState || windowInstance.resizeState.isResizing) return
|
||||
this.updateCursorForResize(e, windowElement, windowInstance)
|
||||
})
|
||||
|
||||
windowElement.addEventListener('mouseleave', () => {
|
||||
if (!windowInstance.resizeState || windowInstance.resizeState.isResizing) return
|
||||
windowElement.style.cursor = 'default'
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建调整尺寸的手柄
|
||||
*/
|
||||
private createResizeHandles(windowElement: HTMLElement): HTMLElement[] {
|
||||
const handles: HTMLElement[] = []
|
||||
const directions: ResizeDirection[] = [
|
||||
'topLeft', 'top', 'topRight',
|
||||
'right', 'bottomRight', 'bottom',
|
||||
'bottomLeft', 'left'
|
||||
]
|
||||
|
||||
directions.forEach(direction => {
|
||||
const handle = document.createElement('div')
|
||||
handle.className = `resize-handle resize-handle-${direction}`
|
||||
|
||||
// 设置手柄样式
|
||||
handle.style.position = 'absolute'
|
||||
handle.style.zIndex = '1001'
|
||||
|
||||
// 根据方向设置位置和光标
|
||||
switch (direction) {
|
||||
case 'topLeft':
|
||||
handle.style.top = '-6px'
|
||||
handle.style.left = '-6px'
|
||||
handle.style.cursor = 'nw-resize'
|
||||
break
|
||||
case 'top':
|
||||
handle.style.top = '-4px'
|
||||
handle.style.left = '6px'
|
||||
handle.style.right = '6px'
|
||||
handle.style.cursor = 'n-resize'
|
||||
break
|
||||
case 'topRight':
|
||||
handle.style.top = '-6px'
|
||||
handle.style.right = '-6px'
|
||||
handle.style.cursor = 'ne-resize'
|
||||
break
|
||||
case 'right':
|
||||
handle.style.top = '6px'
|
||||
handle.style.bottom = '6px'
|
||||
handle.style.right = '-4px'
|
||||
handle.style.cursor = 'e-resize'
|
||||
break
|
||||
case 'bottomRight':
|
||||
handle.style.bottom = '-6px'
|
||||
handle.style.right = '-6px'
|
||||
handle.style.cursor = 'se-resize'
|
||||
break
|
||||
case 'bottom':
|
||||
handle.style.bottom = '-4px'
|
||||
handle.style.left = '6px'
|
||||
handle.style.right = '6px'
|
||||
handle.style.cursor = 's-resize'
|
||||
break
|
||||
case 'bottomLeft':
|
||||
handle.style.bottom = '-6px'
|
||||
handle.style.left = '-6px'
|
||||
handle.style.cursor = 'sw-resize'
|
||||
break
|
||||
case 'left':
|
||||
handle.style.top = '6px'
|
||||
handle.style.bottom = '6px'
|
||||
handle.style.left = '-4px'
|
||||
handle.style.cursor = 'w-resize'
|
||||
break
|
||||
}
|
||||
|
||||
// 设置手柄尺寸
|
||||
if (direction === 'top' || direction === 'bottom') {
|
||||
handle.style.height = '8px'
|
||||
} else if (direction === 'left' || direction === 'right') {
|
||||
handle.style.width = '8px'
|
||||
} else {
|
||||
// 对角方向的手柄需要更大的点击区域
|
||||
handle.style.width = '12px'
|
||||
handle.style.height = '12px'
|
||||
}
|
||||
|
||||
windowElement.appendChild(handle)
|
||||
handles.push(handle)
|
||||
})
|
||||
|
||||
return handles
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加调整尺寸手柄的事件监听器
|
||||
*/
|
||||
private addResizeHandleEvents(
|
||||
handle: HTMLElement,
|
||||
windowElement: HTMLElement,
|
||||
windowInstance: WindowInstance
|
||||
): void {
|
||||
const direction = handle.className.split(' ').find(cls => cls.startsWith('resize-handle-'))?.split('-')[2] as ResizeDirection
|
||||
|
||||
handle.addEventListener('mousedown', (e) => {
|
||||
if (!windowInstance.resizeState) return
|
||||
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
|
||||
// 确保窗体位置是最新的
|
||||
if (windowInstance.element) {
|
||||
const rect = windowInstance.element.getBoundingClientRect();
|
||||
// 如果使用了transform,需要转换为实际坐标
|
||||
if (windowInstance.element.style.transform && windowInstance.element.style.transform.includes('translate')) {
|
||||
// 移除transform并设置实际的left/top值
|
||||
windowInstance.element.style.transform = 'none';
|
||||
windowInstance.config.x = rect.left;
|
||||
windowInstance.config.y = rect.top;
|
||||
windowInstance.element.style.left = `${rect.left}px`;
|
||||
windowInstance.element.style.top = `${rect.top}px`;
|
||||
}
|
||||
}
|
||||
|
||||
// 开始调整尺寸
|
||||
windowInstance.resizeState.isResizing = true
|
||||
windowInstance.resizeState.direction = direction
|
||||
windowInstance.resizeState.startX = e.clientX
|
||||
windowInstance.resizeState.startY = e.clientY
|
||||
windowInstance.resizeState.startWidth = windowInstance.config.width
|
||||
windowInstance.resizeState.startHeight = windowInstance.config.height
|
||||
windowInstance.resizeState.startXPosition = windowInstance.config.x || 0
|
||||
windowInstance.resizeState.startYPosition = windowInstance.config.y || 0
|
||||
|
||||
// 添加半透明遮罩效果
|
||||
windowElement.style.opacity = '0.8'
|
||||
|
||||
// 触发开始调整尺寸事件
|
||||
this.eventBus.notifyEvent('onResizeStart', windowInstance.id)
|
||||
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据鼠标位置更新光标样式
|
||||
*/
|
||||
private updateCursorForResize(
|
||||
e: MouseEvent,
|
||||
windowElement: HTMLElement,
|
||||
windowInstance: WindowInstance
|
||||
): void {
|
||||
if (!windowInstance.resizeState) return
|
||||
|
||||
const rect = windowElement.getBoundingClientRect()
|
||||
const x = e.clientX - rect.left
|
||||
const y = e.clientY - rect.top
|
||||
const edgeSize = 8
|
||||
|
||||
// 检查鼠标位置确定调整方向
|
||||
let direction: ResizeDirection = 'none'
|
||||
|
||||
// 优先检查角落区域,使用更精确的检测
|
||||
if (x >= 0 && x < edgeSize && y >= 0 && y < edgeSize) {
|
||||
direction = 'topLeft'
|
||||
} else if (x > rect.width - edgeSize && x <= rect.width && y >= 0 && y < edgeSize) {
|
||||
direction = 'topRight'
|
||||
} else if (x >= 0 && x < edgeSize && y > rect.height - edgeSize && y <= rect.height) {
|
||||
direction = 'bottomLeft'
|
||||
} else if (x > rect.width - edgeSize && x <= rect.width && y > rect.height - edgeSize && y <= rect.height) {
|
||||
direction = 'bottomRight'
|
||||
}
|
||||
// 然后检查边缘区域
|
||||
else if (x >= 0 && x < edgeSize && y >= edgeSize && y <= rect.height - edgeSize) {
|
||||
direction = 'left'
|
||||
} else if (x > rect.width - edgeSize && x <= rect.width && y >= edgeSize && y <= rect.height - edgeSize) {
|
||||
direction = 'right'
|
||||
} else if (y >= 0 && y < edgeSize && x >= edgeSize && x <= rect.width - edgeSize) {
|
||||
direction = 'top'
|
||||
} else if (y > rect.height - edgeSize && y <= rect.height && x >= edgeSize && x <= rect.width - edgeSize) {
|
||||
direction = 'bottom'
|
||||
}
|
||||
|
||||
// 更新光标样式
|
||||
windowElement.style.cursor = direction === 'none' ? 'default' : `${direction.replace(/([A-Z])/g, '-$1').toLowerCase()}-resize`
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置全局调整尺寸事件监听器
|
||||
*/
|
||||
private setupGlobalResizeEvents(): void {
|
||||
document.addEventListener('mousemove', (e) => {
|
||||
// 处理调整尺寸过程中的鼠标移动
|
||||
this.handleResizeMouseMove(e)
|
||||
})
|
||||
|
||||
document.addEventListener('mouseup', () => {
|
||||
// 处理调整尺寸结束
|
||||
this.handleResizeMouseUp()
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理调整尺寸过程中的鼠标移动
|
||||
*/
|
||||
private handleResizeMouseMove(e: MouseEvent): void {
|
||||
// 找到正在调整尺寸的窗体
|
||||
const resizingWindow = Array.from(this.windows.values()).find(
|
||||
window => window.resizeState?.isResizing
|
||||
)
|
||||
|
||||
// 如果没有正在调整尺寸的窗体,直接返回
|
||||
if (!resizingWindow || !resizingWindow.resizeState || !resizingWindow.element) return
|
||||
|
||||
const {
|
||||
direction,
|
||||
startX,
|
||||
startY,
|
||||
startWidth,
|
||||
startHeight,
|
||||
startXPosition,
|
||||
startYPosition
|
||||
} = resizingWindow.resizeState
|
||||
|
||||
const deltaX = e.clientX - startX
|
||||
const deltaY = e.clientY - startY
|
||||
|
||||
let newWidth = startWidth
|
||||
let newHeight = startHeight
|
||||
let newX = startXPosition
|
||||
let newY = startYPosition
|
||||
|
||||
// 根据调整方向计算新尺寸和位置
|
||||
switch (direction) {
|
||||
case 'topLeft':
|
||||
newWidth = Math.max(200, startWidth - deltaX)
|
||||
newHeight = Math.max(150, startHeight - deltaY)
|
||||
newX = startXPosition + (startWidth - newWidth)
|
||||
newY = startYPosition + (startHeight - newHeight)
|
||||
break
|
||||
case 'top':
|
||||
newHeight = Math.max(150, startHeight - deltaY)
|
||||
newY = startYPosition + (startHeight - newHeight)
|
||||
break
|
||||
case 'topRight':
|
||||
newWidth = Math.max(200, startWidth + deltaX)
|
||||
newHeight = Math.max(150, startHeight - deltaY)
|
||||
newY = startYPosition + (startHeight - newHeight)
|
||||
break
|
||||
case 'right':
|
||||
newWidth = Math.max(200, startWidth + deltaX)
|
||||
break
|
||||
case 'bottomRight':
|
||||
newWidth = Math.max(200, startWidth + deltaX)
|
||||
newHeight = Math.max(150, startHeight + deltaY)
|
||||
break
|
||||
case 'bottom':
|
||||
newHeight = Math.max(150, startHeight + deltaY)
|
||||
break
|
||||
case 'bottomLeft':
|
||||
newWidth = Math.max(200, startWidth - deltaX)
|
||||
newHeight = Math.max(150, startHeight + deltaY)
|
||||
newX = startXPosition + (startWidth - newWidth)
|
||||
break
|
||||
case 'left':
|
||||
newWidth = Math.max(200, startWidth - deltaX)
|
||||
newX = startXPosition + (startWidth - newWidth)
|
||||
break
|
||||
}
|
||||
|
||||
// 应用尺寸限制
|
||||
newWidth = this.clampDimension(newWidth, resizingWindow.config.minWidth, resizingWindow.config.maxWidth)
|
||||
newHeight = this.clampDimension(newHeight, resizingWindow.config.minHeight, resizingWindow.config.maxHeight)
|
||||
|
||||
// 应用新尺寸和位置
|
||||
resizingWindow.config.width = newWidth
|
||||
resizingWindow.config.height = newHeight
|
||||
resizingWindow.config.x = newX
|
||||
resizingWindow.config.y = newY
|
||||
|
||||
if (resizingWindow.element) {
|
||||
resizingWindow.element.style.width = `${newWidth}px`
|
||||
resizingWindow.element.style.height = `${newHeight}px`
|
||||
resizingWindow.element.style.left = `${newX}px`
|
||||
resizingWindow.element.style.top = `${newY}px`
|
||||
resizingWindow.element.style.transform = 'none'
|
||||
}
|
||||
|
||||
// 触发调整尺寸事件
|
||||
this.eventBus.notifyEvent('onResizing', resizingWindow.id, newWidth, newHeight)
|
||||
|
||||
// 发送窗体数据更新事件
|
||||
this.notifyWindowFormDataUpdate(resizingWindow.id)
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理调整尺寸结束
|
||||
*/
|
||||
private handleResizeMouseUp(): void {
|
||||
// 找到正在调整尺寸的窗体
|
||||
const resizingWindow = Array.from(this.windows.values()).find(
|
||||
window => window.resizeState?.isResizing
|
||||
)
|
||||
|
||||
if (!resizingWindow || !resizingWindow.resizeState || !resizingWindow.element) return
|
||||
|
||||
// 结束调整尺寸
|
||||
resizingWindow.resizeState.isResizing = false
|
||||
|
||||
// 移除半透明遮罩效果
|
||||
resizingWindow.element.style.opacity = '1'
|
||||
|
||||
// 触发调整尺寸结束事件
|
||||
this.eventBus.notifyEvent('onResizeEnd', resizingWindow.id)
|
||||
|
||||
// 发送窗体数据更新事件
|
||||
this.notifyWindowFormDataUpdate(resizingWindow.id)
|
||||
}
|
||||
|
||||
/**
|
||||
* 限制尺寸在最小值和最大值之间
|
||||
*/
|
||||
private clampDimension(value: number, min: number = 0, max: number = Infinity): number {
|
||||
return Math.max(min, Math.min(max, value))
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送窗体数据更新事件
|
||||
*/
|
||||
private notifyWindowFormDataUpdate(windowId: string): void {
|
||||
const window = this.windows.get(windowId)
|
||||
if (!window || !window.element) return
|
||||
|
||||
// 获取窗体数据
|
||||
const rect = window.element.getBoundingClientRect()
|
||||
const data = {
|
||||
id: windowId,
|
||||
state: window.state,
|
||||
width: window.config.width,
|
||||
height: window.config.height,
|
||||
x: window.config.x !== undefined ? window.config.x : rect.left,
|
||||
y: window.config.y !== undefined ? window.config.y : rect.top
|
||||
}
|
||||
|
||||
// 发送事件到事件总线
|
||||
this.eventBus.notifyEvent('onWindowFormDataUpdate', data)
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载应用
|
||||
*/
|
||||
@@ -728,5 +1184,8 @@ export class WindowService {
|
||||
// 所有状态变化都应该触发事件,这是正常的系统行为
|
||||
console.log(`[WindowService] 窗体状态变化: ${windowId} ${oldState} -> ${newState}`)
|
||||
this.eventBus.notifyEvent('onStateChange', windowId, newState, oldState)
|
||||
|
||||
// 发送窗体数据更新事件
|
||||
this.notifyWindowFormDataUpdate(windowId)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,3 +8,35 @@ export interface WindowFormPos {
|
||||
|
||||
/** 窗口状态 */
|
||||
export type TWindowFormState = 'default' | 'minimized' | 'maximized';
|
||||
|
||||
/** 拖拽方向 */
|
||||
export type ResizeDirection =
|
||||
| 'topLeft'
|
||||
| 'top'
|
||||
| 'topRight'
|
||||
| 'right'
|
||||
| 'bottomRight'
|
||||
| 'bottom'
|
||||
| 'bottomLeft'
|
||||
| 'left'
|
||||
| 'none'
|
||||
|
||||
/** 拖拽状态 */
|
||||
export interface ResizeState {
|
||||
/** 是否正在调整尺寸 */
|
||||
isResizing: boolean
|
||||
/** 调整方向 */
|
||||
direction: ResizeDirection
|
||||
/** 起始鼠标位置 */
|
||||
startX: number
|
||||
/** 起始鼠标位置 */
|
||||
startY: number
|
||||
/** 起始窗体宽度 */
|
||||
startWidth: number
|
||||
/** 起始窗体高度 */
|
||||
startHeight: number
|
||||
/** 起始窗体X坐标 */
|
||||
startXPosition: number
|
||||
/** 起始窗体Y坐标 */
|
||||
startYPosition: number
|
||||
}
|
||||
Reference in New Issue
Block a user