HumanBreak/src/plugin/uiController.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-11-19 11:30:14 +08:00
import { sleep } from 'mutate-animate';
2022-11-14 17:11:23 +08:00
import { Component, markRaw, ref, Ref, watch } from 'vue';
import Book from '../ui/book.vue';
export const bookOpened = ref(false);
let app: HTMLDivElement;
/** ui声明列表 */
2022-11-19 11:30:14 +08:00
const UI_LIST: [Ref<boolean>, Component][] = [[bookOpened, Book]];
2022-11-14 17:11:23 +08:00
/** ui栈 */
export const uiStack = ref<Component[]>([]);
export default function init() {
app = document.getElementById('root') as HTMLDivElement;
UI_LIST.forEach(([ref, com]) => {
watch(ref, n => {
if (n === true) {
uiStack.value.push(markRaw(com));
showApp();
} else {
const index = uiStack.value.findIndex(v => v === com);
uiStack.value.splice(index);
if (uiStack.value.length === 0) {
hideApp();
}
}
});
});
2022-11-19 11:30:14 +08:00
return { uiStack, bookOpened };
2022-11-14 17:11:23 +08:00
}
2022-11-19 11:30:14 +08:00
async function showApp() {
2022-11-14 17:11:23 +08:00
app.style.display = 'flex';
2022-11-19 11:30:14 +08:00
await sleep(50);
app.style.opacity = '1';
2022-11-14 17:11:23 +08:00
core.lockControl();
}
2022-11-19 11:30:14 +08:00
async function hideApp() {
app.style.opacity = '0';
await sleep(600);
2022-11-14 17:11:23 +08:00
app.style.display = 'none';
core.unlockControl();
}