Line data Source code
1 : import { defineStore } from 'pinia'
2 : import { computed, ref } from 'vue'
3 :
4 : import { useCommandStore } from '@/platform/keybindings/commandStore'
5 : import type { ComfyExtension } from '@/types/comfy'
6 : import type { BottomPanelExtension } from '@/types/extensionTypes'
7 :
8 : type PanelType = 'terminal' | 'shortcuts'
9 :
10 : interface PanelState {
11 : tabs: BottomPanelExtension[]
12 : activeTabId: string
13 : visible: boolean
14 : }
15 :
16 2356 : export const useBottomPanelStore = defineStore('bottomPanel', () => {
17 : // Multi-panel state
18 2355 : const panels = ref<Record<PanelType, PanelState>>({
19 : terminal: { tabs: [], activeTabId: '', visible: false },
20 : shortcuts: { tabs: [], activeTabId: '', visible: false }
21 : })
22 :
23 2355 : const activePanel = ref<PanelType | null>(null)
24 :
25 : // Computed properties for active panel
26 2355 : const activePanelState = computed(() =>
27 : activePanel.value ? panels.value[activePanel.value] : null
28 : )
29 :
30 2355 : const activeBottomPanelTab = computed<BottomPanelExtension | null>(() => {
31 0 : const state = activePanelState.value
32 0 : if (!state) return null
33 0 : return state.tabs.find((tab) => tab.id === state.activeTabId) ?? null
34 : })
35 :
36 2355 : const bottomPanelVisible = computed({
37 : get: () => !!activePanel.value,
38 : set: (visible: boolean) => {
39 0 : if (!visible) {
40 0 : activePanel.value = null
41 : }
42 : }
43 : })
44 2355 : const bottomPanelTabs = computed(() => activePanelState.value?.tabs ?? [])
45 2355 : const activeBottomPanelTabId = computed({
46 : get: () => activePanelState.value?.activeTabId ?? '',
47 : set: (tabId: string) => {
48 6 : const state = activePanelState.value
49 6 : if (state) {
50 6 : state.activeTabId = tabId
51 : }
52 : }
53 : })
54 :
55 2355 : const togglePanel = (panelType: PanelType) => {
56 45 : const panel = panels.value[panelType]
57 45 : if (panel.tabs.length === 0) return
58 :
59 45 : if (activePanel.value === panelType) {
60 : // Hide current panel
61 5 : activePanel.value = null
62 : } else {
63 : // Show target panel
64 40 : activePanel.value = panelType
65 40 : if (!panel.activeTabId && panel.tabs.length > 0) {
66 0 : panel.activeTabId = panel.tabs[0].id
67 : }
68 : }
69 : }
70 :
71 2355 : const toggleBottomPanel = () => {
72 : // Toggles the terminal panel if available, otherwise falls back to shortcuts
73 : // Terminal tabs are loaded asynchronously, so may not be available immediately
74 : const terminalPanel = panels.value.terminal
75 26 : if (terminalPanel.tabs.length > 0) {
76 26 : togglePanel('terminal')
77 : } else {
78 : // Terminal tabs not loaded yet - fall back to shortcuts panel
79 : // If no panel is open, open shortcuts
80 : // If shortcuts is already open, close it
81 : // If another panel is open (shouldn't happen), switch to shortcuts
82 0 : togglePanel('shortcuts')
83 : }
84 : }
85 :
86 2355 : const setActiveTab = (tabId: string) => {
87 0 : const state = activePanelState.value
88 0 : if (state) {
89 0 : state.activeTabId = tabId
90 : }
91 : }
92 :
93 2355 : const toggleBottomPanelTab = (tabId: string) => {
94 : // Find which panel contains this tab
95 2 : for (const [panelType, panel] of Object.entries(panels.value)) {
96 : const tab = panel.tabs.find((t) => t.id === tabId)
97 2 : if (tab) {
98 2 : if (activePanel.value === panelType && panel.activeTabId === tabId) {
99 1 : activePanel.value = null
100 : } else {
101 1 : activePanel.value = panelType as PanelType
102 1 : panel.activeTabId = tabId
103 : }
104 2 : return
105 : }
106 : }
107 : }
108 2355 : const registerBottomPanelTab = (tab: BottomPanelExtension) => {
109 7065 : const targetPanel = tab.targetPanel ?? 'terminal'
110 7065 : const panel = panels.value[targetPanel]
111 :
112 7065 : panel.tabs = [...panel.tabs, tab]
113 7065 : if (panel.tabs.length === 1) {
114 4710 : panel.activeTabId = tab.id
115 : }
116 :
117 7065 : const tabName = tab.title || tab.titleKey || tab.id
118 7065 : useCommandStore().registerCommand({
119 : id: `Workspace.ToggleBottomPanelTab.${tab.id}`,
120 : icon: 'icon-[lucide--list]',
121 : label: `Toggle ${tabName} Bottom Panel`,
122 : category: 'view-controls' as const,
123 : function: () => toggleBottomPanelTab(tab.id),
124 : source: 'System'
125 : })
126 : }
127 :
128 2355 : const registerExtensionBottomPanelTabs = (extension: ComfyExtension) => {
129 82789 : if (extension.bottomPanelTabs) {
130 0 : extension.bottomPanelTabs.forEach(registerBottomPanelTab)
131 : }
132 : }
133 :
134 2355 : return {
135 : // Multi-panel API
136 : panels,
137 : activePanel,
138 : togglePanel,
139 :
140 : bottomPanelVisible,
141 : toggleBottomPanel,
142 : bottomPanelTabs,
143 : activeBottomPanelTab,
144 : activeBottomPanelTabId,
145 : setActiveTab,
146 : toggleBottomPanelTab,
147 : registerBottomPanelTab,
148 : registerExtensionBottomPanelTabs
149 : }
150 : })
|