diff --git a/src/pages/Desktop/Desk/Apps/index.tsx b/src/pages/Desktop/Desk/Apps/index.tsx index c54c9a28db5ae7b96cbe6464bd0d9a733a8924b2..b7cd88426c32c7f2064f0ce21910fe5818994681 100644 --- a/src/pages/Desktop/Desk/Apps/index.tsx +++ b/src/pages/Desktop/Desk/Apps/index.tsx @@ -5,10 +5,14 @@ import useStore, { MyState } from '@/store' const Apps: React.FC = () => { const appList = useStore((state: MyState) => state.appList) + const openWindow = useStore((state: MyState) => state.openWindow) return (
{appList.map(app => ( -
+
openWindow(app)}>
diff --git a/src/pages/Desktop/StartMenu/Application/index.tsx b/src/pages/Desktop/StartMenu/Application/index.tsx index 2b9bc85d625e5458c3c2f4f32645569a0bd72832..226ef307add54c27eff26b8014f62df6b9ea4503 100644 --- a/src/pages/Desktop/StartMenu/Application/index.tsx +++ b/src/pages/Desktop/StartMenu/Application/index.tsx @@ -6,12 +6,20 @@ import ImageComponent from '@/components/ImageComponent' const Application: React.FC = () => { const startMenuList = useStore((state: MyState) => state.startMenuList) + const openWindow = useStore((state: MyState) => state.openWindow) + const setShowStartMenu = useStore((state: MyState) => state.setShowStartMenu) return (
{startMenuList.map(app => ( -
+
{ + openWindow(app) + setShowStartMenu(false) + }}>
diff --git a/src/pages/Desktop/StartToolsBar/WindonBar/index.tsx b/src/pages/Desktop/StartToolsBar/WindonBar/index.tsx index 88b3c6e54cd01f10c8665d01a977aca9b2582768..52cd93e786c676c9f7a0168ef57b9130ffbf3e4d 100644 --- a/src/pages/Desktop/StartToolsBar/WindonBar/index.tsx +++ b/src/pages/Desktop/StartToolsBar/WindonBar/index.tsx @@ -20,7 +20,7 @@ const WindowBar: React.FC = () => { className={classNames(styles.windowBox, { [styles.active]: window.id === windowActionId })} - onClick={() => setWindowActionId(window.id)}> + onClick={() => setWindowActionId(window.id, 1)}>
diff --git a/src/store/windowSlice.ts b/src/store/windowSlice.ts index 8e43f25558227447f47ad3ffad836931427165cb..022063c86125d1e17679650d5b6b16cba46abab9 100644 --- a/src/store/windowSlice.ts +++ b/src/store/windowSlice.ts @@ -4,21 +4,27 @@ import _ from 'lodash' import getId from '@utils/getId' import logoImg from '@img/icon/start/home.png' import type { Image } from '@/components/ImageComponent' +import { App } from '@/store/deskSlice' +import { WINDOW_STATUS } from '@/common/constants' + +const defaultWindowStyle = { + display: 'block', + width: 600, + height: 600, + top: 200, + left: 100, + zIndex: 1000 +} const id = getId() const win1 = { id, - appId: 1, + appId: 99, title: '窗口标题名称', status: 0, style: { - display: 'block', - width: 800, - height: 800, - top: 200, - left: 100, - zIndex: 1 + ...defaultWindowStyle }, image: { type: 0, @@ -26,25 +32,6 @@ const win1 = { } } -const win2 = { - id: getId(), - appId: 2, - title: '窗口标题名称2', - status: 0, - style: { - display: 'block', - width: 800, - height: 800, - top: 230, - left: 130, - zIndex: 3 - }, - image: { - type: 1, - name: 'ConsoleSqlOutlined' - } -} - export interface Window { id: string appId: number @@ -66,26 +53,78 @@ export interface WindowStyle { export interface WindowSlice { windowActionId: string windowList: Window[] + openWindow: (app: App) => void setWindowList: (windowList: Window[]) => void editWindow: (window: Window, id: string) => void setWindowStatus: (status: number, id: string) => void - setWindowActionId: (id: string) => void + setWindowActionId: (id: string, type?: number) => void closeWindow: (id: string) => void closeWindowAll: () => void } +function resetWindowListZIndex(windowList: Window[], index: number) { + const newWindowList = _.cloneDeep(windowList) + const window = newWindowList[index] as Window + const curWindowZIndex = _.clone(window.style.zIndex) // 当前窗口zIndex值 + const windowZIndexMax = Math.max(...windowList.map(row => row.style.zIndex)) + newWindowList.map(row => { + const win = { + ...row + } + if (row.style.zIndex === windowZIndexMax) { + win.style.zIndex = curWindowZIndex + } + return win + }) + window.style.zIndex = windowZIndexMax + return newWindowList +} + const windowListSlice = ( set: StoreApi['setState'], get: StoreApi['getState'] ) => ({ windowActionId: id, // 当前正在打开窗口的id - windowList: [win1, win2], // 窗口列表 + windowList: [win1], // 窗口列表 + // 打开窗口 + openWindow: (app: App) => { + set(({ windowList, setWindowActionId }) => { + const windowIndex = windowList.findIndex(row => row.appId === app.id) + let newWindowList + let windowId + if (windowIndex >= 0) { + const window = windowList[windowIndex] + newWindowList = resetWindowListZIndex(windowList, windowIndex) + windowId = window.id + } else { + newWindowList = _.cloneDeep(windowList) + const windowNum = windowList.length + const window: Window = { + id: getId(), + appId: app.id, + title: app.title, + image: app.image, + status: WINDOW_STATUS.NORMAL, + style: { + ...defaultWindowStyle, + left: defaultWindowStyle.left + 20 * windowNum, + top: defaultWindowStyle.top + 20 * windowNum, + zIndex: defaultWindowStyle.zIndex + 100 * windowNum + } + } + newWindowList.push(window) + windowId = window.id + } + setWindowActionId(windowId) + return { windowList: newWindowList } + }) + }, + // 设置窗口列表 setWindowList: (windowList: Window[]) => { - // 设置窗口列表 set(() => ({ windowList: windowList })) }, + // 窗口属性编辑 editWindow: (window: Window, id: string) => { - // 窗口属性编辑 set(({ windowList }) => { const list = _.cloneDeep(windowList) const windowIndex = list.findIndex(row => row.id === id) @@ -95,6 +134,7 @@ const windowListSlice = ( } }) }, + // 设置窗口最大化、最小化、正常 setWindowStatus: (status: number, id: string) => { set(({ windowList }) => { const list = _.cloneDeep(windowList) @@ -106,8 +146,17 @@ const windowListSlice = ( } }) }, - setWindowActionId: (id: string) => { - set(() => ({ windowActionId: id })) + setWindowActionId: (id: string, type = 0) => { + set(({ windowList }) => { + const res = { windowActionId: id, windowList } + // 重新排序zIndex + if (type === 1) { + const newWindowList = _.cloneDeep(windowList) + const windowIndex = windowList.findIndex(row => row.id === id) + res.windowList = resetWindowListZIndex(windowList, windowIndex) + } + return res + }) }, closeWindow: (id: string) => { set(({ windowList, windowActionId }) => {