From c87caef4735c3ea3aba277a70e0c46a5e9a42493 Mon Sep 17 00:00:00 2001 From: sawka Date: Wed, 31 Jul 2024 19:22:15 -0700 Subject: [PATCH] implement Cmd-W to close blocks and tabs --- emain/emain.ts | 1 + frontend/app/app.tsx | 43 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/emain/emain.ts b/emain/emain.ts index 67ce4455f..4134899c0 100644 --- a/emain/emain.ts +++ b/emain/emain.ts @@ -588,6 +588,7 @@ function makeAppMenu() { }, { role: "close", + accelerator: "", // clear the accelerator click: () => { electron.BrowserWindow.getFocusedWindow()?.close(); }, diff --git a/frontend/app/app.tsx b/frontend/app/app.tsx index 8c7c757ca..efa1e7380 100644 --- a/frontend/app/app.tsx +++ b/frontend/app/app.tsx @@ -9,7 +9,11 @@ import * as keyutil from "@/util/keyutil"; import * as layoututil from "@/util/layoututil"; import * as util from "@/util/util"; import * as csstree from "css-tree"; -import { getLayoutStateAtomForTab, globalLayoutTransformsMap } from "frontend/layout/lib/layoutAtom"; +import { + deleteLayoutStateAtomForTab, + getLayoutStateAtomForTab, + globalLayoutTransformsMap, +} from "frontend/layout/lib/layoutAtom"; import * as jotai from "jotai"; import * as React from "react"; import { DndProvider } from "react-dnd"; @@ -17,6 +21,8 @@ import { HTML5Backend } from "react-dnd-html5-backend"; import { CenteredDiv } from "./element/quickelems"; import { useWaveObjectValue } from "@/app/store/wos"; +import { LayoutTreeActionType, LayoutTreeDeleteNodeAction } from "@/layout/index"; +import { layoutTreeStateReducer } from "@/layout/lib/layoutState"; import { getWebServerEndpoint } from "@/util/endpoints"; import clsx from "clsx"; import Color from "color"; @@ -81,7 +87,6 @@ function handleContextMenu(e: React.MouseEvent) { } function switchTab(offset: number) { - console.log("switch tab!", offset); const ws = globalStore.get(atoms.workspace); const activeTabId = globalStore.get(atoms.tabAtom).oid; let tabIdx = -1; @@ -298,6 +303,35 @@ function AppBackground() { return
; } +function genericClose(tabId: string) { + const tabORef = WOS.makeORef("tab", tabId); + const tabAtom = WOS.getWaveObjectAtom(tabORef); + const tabData = globalStore.get(tabAtom); + if (tabData == null) { + return; + } + if (tabData.blockids == null || tabData.blockids.length == 0) { + // close tab + services.WindowService.CloseTab(tabId); + deleteLayoutStateAtomForTab(tabId); + return; + } + // close block + const activeBlockId = globalStore.get(atoms.waveWindow)?.activeblockid; + if (activeBlockId == null) { + return; + } + const layoutStateAtom = getLayoutStateAtomForTab(tabId, tabAtom); + const layoutTreeState = globalStore.get(layoutStateAtom); + const curBlockLeafId = layoututil.findLeafIdFromBlockId(layoutTreeState, activeBlockId); + const deleteAction: LayoutTreeDeleteNodeAction = { + type: LayoutTreeActionType.DeleteNode, + nodeId: curBlockLeafId, + }; + globalStore.set(layoutStateAtom, layoutTreeStateReducer(layoutTreeState, deleteAction)); + services.ObjectService.DeleteBlock(activeBlockId); +} + const AppInner = () => { const client = jotai.useAtomValue(atoms.client); const windowData = jotai.useAtomValue(atoms.waveWindow); @@ -337,6 +371,11 @@ const AppInner = () => { switchBlock(tabId, 1, 0); return true; } + if (keyutil.checkKeyPressed(waveEvent, "Cmd:w")) { + // close block, if no more blocks, close tab + genericClose(tabId); + return true; + } return false; } React.useEffect(() => {