-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add
commas.workspace.effectTerminalTab
- Loading branch information
Showing
3 changed files
with
48 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,67 @@ | ||
import { shallowReactive, markRaw } from 'vue' | ||
import { shallowReactive, markRaw, unref } from 'vue' | ||
import { activateOrAddTerminalTab, useTerminalTabs } from '../../renderer/compositions/terminal' | ||
import { createIDGenerator } from '../../renderer/utils/helper' | ||
import type { TerminalTab, TerminalTabPane } from '../../typings/terminal' | ||
import type { CommasContext } from '../types' | ||
|
||
const tabs = shallowReactive<Record<string, TerminalTab>>({}) | ||
const panes = shallowReactive<Record<string, TerminalTab>>({}) | ||
const generateID = createIDGenerator() | ||
|
||
function registerTabPane(this: CommasContext, name: string, pane: TerminalTabPane) { | ||
tabs[name] = markRaw({ | ||
panes[name] = markRaw({ | ||
pid: generateID(), | ||
process: '', | ||
title: '', | ||
cwd: '', | ||
pane, | ||
} as TerminalTab) | ||
this.$.app.onCleanup(() => { | ||
delete tabs[name] | ||
delete panes[name] | ||
}) | ||
} | ||
|
||
function getPaneTab(name: string) { | ||
return tabs[name] | ||
return panes[name] | ||
} | ||
|
||
function openPaneTab(name: string) { | ||
activateOrAddTerminalTab(getPaneTab(name)) | ||
} | ||
|
||
function effectTerminalTab( | ||
this: CommasContext, | ||
callback: (tab: TerminalTab, active: boolean) => void, | ||
immediate?: boolean, | ||
) { | ||
let active = false | ||
|
||
this.$.app.events.on('terminal-tab-effect', tab => { | ||
callback(tab, active) | ||
}) | ||
|
||
const toggle = (enabled: boolean) => { | ||
if (active === enabled) return | ||
active = enabled | ||
const tabs = unref(useTerminalTabs()) | ||
tabs.forEach(tab => { | ||
callback(tab, active) | ||
}) | ||
} | ||
|
||
this.$.app.onCleanup(() => { | ||
toggle(false) | ||
}) | ||
if (immediate) { | ||
toggle(true) | ||
} | ||
|
||
return toggle | ||
} | ||
|
||
export { | ||
registerTabPane, | ||
getPaneTab, | ||
openPaneTab, | ||
useTerminalTabs, | ||
effectTerminalTab, | ||
} |