From ab91d912b6fcb8a6efb5471649e40a45fd4e6f29 Mon Sep 17 00:00:00 2001 From: oxdc <29519076+oxdc@users.noreply.github.com> Date: Sun, 1 Sep 2024 17:08:41 +0800 Subject: [PATCH] auto locking focus on the latest tab in Zen Mode --- manifest.json | 2 +- src/components/NavigationContainer.tsx | 5 +++-- src/components/Tab.tsx | 12 +++++++++--- src/models/ViewState.ts | 7 +++++-- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/manifest.json b/manifest.json index 3bd411f..85365e8 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "vertical-tabs", "name": "Vertical Tabs", - "version": "0.6.6", + "version": "0.6.7", "minAppVersion": "1.6.2", "description": "Offer an alternative view that displays open tabs vertically, allowing users to group and organize tabs for a better navigation experience.", "author": "oxdc", diff --git a/src/components/NavigationContainer.tsx b/src/components/NavigationContainer.tsx index 5346585..5dc51ee 100644 --- a/src/components/NavigationContainer.tsx +++ b/src/components/NavigationContainer.tsx @@ -8,12 +8,13 @@ import { useViewState } from "src/models/ViewState"; export const NavigationContainer = () => { const plugin = usePlugin(); const { refresh, sort } = useTabCache(); - const { setActiveLeaf, insertToggleButtons, lockFocus } = useViewState(); + const { setLatestActiveLeaf, insertToggleButtons, lockFocus } = + useViewState(); const loadSettings = useSettings.use.loadSettings(); const toggleZenMode = useSettings.use.toggleZenMode(); const autoRefresh = () => { - setActiveLeaf(plugin); + setLatestActiveLeaf(plugin); insertToggleButtons(plugin.app); setTimeout(() => { refresh(plugin.app); diff --git a/src/components/Tab.tsx b/src/components/Tab.tsx index 2b7edf6..6757c4a 100644 --- a/src/components/Tab.tsx +++ b/src/components/Tab.tsx @@ -22,6 +22,7 @@ export const Tab = ({ leaf }: TabProps) => { const { bindPinningEvent } = useViewState(); const [isPinned, setIsPinned] = useState(leaf.getViewState().pinned); const { sort } = useTabCache(); + const { lockFocusOnLeaf } = useViewState(); const lastActiveLeaf = useViewState((state) => state.latestActiveLeaf); useEffect(() => { @@ -44,17 +45,22 @@ export const Tab = ({ leaf }: TabProps) => { if (!leaf.getViewState().pinned) leaf.detach(); }; - const midClickCloseTab = (event: React.MouseEvent) => { + const midClickCloseTab = ( + event: React.MouseEvent + ) => { if (event.button === 1) closeTab(); - } + }; const openTab = () => { const workspace = plugin.app.workspace as VT.Workspace; workspace.setActiveLeaf(leaf, { focus: true }); workspace.onLayoutChange(); + lockFocusOnLeaf(plugin.app, leaf); }; - const activeOrCloseTab = (event: React.MouseEvent) => { + const activeOrCloseTab = ( + event: React.MouseEvent + ) => { if (event.altKey) { closeTab(); } else { diff --git a/src/models/ViewState.ts b/src/models/ViewState.ts index fc594a3..8289aa6 100644 --- a/src/models/ViewState.ts +++ b/src/models/ViewState.ts @@ -24,7 +24,10 @@ interface ViewState { clear: () => void; setGroupTitle: (id: VT.Identifier, name: string) => void; toggleHiddenGroup: (id: VT.Identifier, isHidden: boolean) => void; - setActiveLeaf: (plugin: ObsidianVerticalTabs) => void; + setLatestActiveLeaf: ( + plugin: ObsidianVerticalTabs, + leaf?: VT.WorkspaceLeaf | null + ) => void; lockFocus: (plugin: ObsidianVerticalTabs) => void; lockFocusOnLeaf: (app: App, leaf: VT.WorkspaceLeaf) => void; resetFocusFlags: () => void; @@ -78,7 +81,7 @@ export const useViewState = create()((set, get) => ({ saveHiddenGroups(state.hiddenGroups); return state; }), - setActiveLeaf(plugin: ObsidianVerticalTabs) { + setLatestActiveLeaf(plugin: ObsidianVerticalTabs) { const workspace = plugin.app.workspace as VT.Workspace; const activeView = workspace.getActiveViewOfType(ItemView); if (!activeView) {