Skip to content

Commit

Permalink
feat(api): add commas.workspace.effectTerminalTab
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Feb 17, 2022
1 parent e761208 commit 48e6094
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 40 deletions.
45 changes: 10 additions & 35 deletions addons/power-mode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,19 @@ module.exports = function (commas) {

const { PowerMode } = commas.bundler.extract('power-mode/renderer/xterm.ts')

const instances = []

const openPowerMode = tab => {
const xterm = tab.xterm
if (!xterm) return
const powerMode = new PowerMode()
tab.addons.powerMode = powerMode
xterm.loadAddon(powerMode)
instances.push(powerMode)
}

const enable = () => {
const tabsRef = commas.workspace.useTerminalTabs()
tabsRef.value.forEach(openPowerMode)
commas.app.events.on('terminal-tab-mounted', openPowerMode)
}

const disable = () => {
instances.forEach(instance => {
instance.dispose()
})
commas.app.events.off('terminal-tab-mounted', openPowerMode)
}

commas.app.onCleanup(() => {
disable()
const toggle = commas.workspace.effectTerminalTab((tab, active) => {
if (active && !tab.addons.powerMode) {
const powerMode = new PowerMode()
tab.addons.powerMode = powerMode
tab.xterm.loadAddon(powerMode)
} else if (!active && tab.addons.powerMode) {
tab.addons.powerMode.dispose()
delete tab.addons.powerMode
}
})

let enabled = false

commas.ipcRenderer.on('toggle-power-mode', (event, active) => {
if (enabled && !active) {
enabled = false
disable()
} else if (!enabled && active) {
enabled = true
enable()
}
toggle(active)
})

}
Expand Down
2 changes: 2 additions & 0 deletions api/modules/ipc-renderer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ipcRenderer } from 'electron'
import type { IpcRendererEvent } from 'electron'
import { useSettings } from '../../renderer/compositions/settings'
import { injectIPC } from '../../renderer/utils/compositions'
import type { CommasContext } from '../types'

Expand All @@ -13,4 +14,5 @@ function on(this: CommasContext, channel: string, listener: (event: IpcRendererE
export {
on,
injectIPC as inject,
useSettings,
}
41 changes: 36 additions & 5 deletions api/modules/workspace.ts
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,
}

0 comments on commit 48e6094

Please sign in to comment.