Skip to content

Commit

Permalink
feat: tab list context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Jun 3, 2024
1 parent e0726f4 commit 741c8dc
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 34 deletions.
6 changes: 4 additions & 2 deletions addons/launcher/locales/zh-CN.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"Launch#!launcher.1": "启动",
"Launcher#!launcher.2": "启动项",
"Launcher#!launcher.1": "启动项",
"Launch#!launcher.2": "启动",
"Open in External#!launcher.3": "在外部打开",
"Remove Launcher#!launcher.4": "移除启动项",
"Persistent Launcher Session#!settings.label.launcher.session.persist": "保留启动项会话",
"Persistent the history of a launcher after it has been closed#!settings.comments.0.launcher.session.persist": "在启动项关闭后保留其历史记录"
}
62 changes: 36 additions & 26 deletions addons/launcher/src/renderer/LauncherList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ function closeTab(tab: TerminalTab) {
commas.workspace.closeTerminalTab(tab)
}
function dropLauncher(launcher: Launcher) {
const index = launchers.findIndex(item => item.id === launcher.id)
removeLauncher(index)
}
function customizeLauncher(launcher: Launcher, title: string) {
const index = launchers.findIndex(item => item.id === launcher.id)
updateLauncher(index, {
Expand All @@ -93,25 +88,6 @@ function customizeLauncher(launcher: Launcher, title: string) {
})
}
function showLauncherScripts(launcher: Launcher, event: MouseEvent) {
const scripts = launcher.scripts ?? []
commas.ui.openContextMenu([
{
label: 'Launch#!launcher.1',
command: 'start-launcher',
args: [launcher],
},
{
type: 'separator',
},
...scripts.map((script, index) => ({
label: script.name,
command: 'run-script',
args: [launcher, index],
})),
], event)
}
function showLauncherMenu(event: MouseEvent) {
commas.workspace.showTabOptions(event, 'launcher')
}
Expand Down Expand Up @@ -195,6 +171,40 @@ function handleDrop(args: DraggableElementEventPayload<LauncherDraggableElementD
draggingEdges.set(args.self.data.launcher.id, null)
}
}
function openLauncherMenu(launcher: Launcher, tab: TerminalTab | undefined, event: MouseEvent) {
const scripts = launcher.scripts ?? []
const { updatingItems, deletingItems } = commas.workspace.createTerminalTabContextMenu()
commas.ui.openContextMenu([
...commas.ui.withContextMenuSeparator([
{
label: 'Launch#!launcher.2',
command: 'start-launcher',
args: [launcher],
},
{
label: 'Open in External#!launcher.3',
command: 'start-launcher-externally',
args: [launcher],
},
], []),
...commas.ui.withContextMenuSeparator(
scripts.map((script, index) => ({
label: script.name,
command: 'run-script',
args: [launcher, index],
})),
[],
),
...(tab ? commas.ui.withContextMenuSeparator(updatingItems, []) : []),
...(tab ? deletingItems : []),
{
label: 'Remove Launcher#!launcher.4',
command: 'remove-launcher',
args: [launcher],
},
], event)
}
</script>

<template>
Expand All @@ -221,7 +231,7 @@ function handleDrop(args: DraggableElementEventPayload<LauncherDraggableElementD
index,
launcher,
create: () => openLauncher(launcher),
dispose: () => dropLauncher(launcher),
dispose: () => removeLauncher(launcher),
}"
@dragstart="handleDragStart"
@drop="handleDragStop"
Expand Down Expand Up @@ -253,12 +263,12 @@ function handleDrop(args: DraggableElementEventPayload<LauncherDraggableElementD
@click="openLauncher(launcher, { tab })"
@close="closeTab(tab!)"
@customize="customizeLauncher(launcher, $event)"
@contextmenu="openLauncherMenu(launcher, tab, $event)"
>
<template #operations>
<div
class="button launch"
@click.stop="startLauncher(launcher)"
@contextmenu="showLauncherScripts(launcher, $event)"
>
<VisualIcon name="lucide-play" />
</div>
Expand Down
10 changes: 8 additions & 2 deletions addons/launcher/src/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as commas from 'commas:api/renderer'
import { watch } from 'vue'
import LauncherLink from './LauncherLink.vue'
import LauncherList from './LauncherList.vue'
import { getLauncherByTerminalTabCharacter, openLauncher, runLauncherScript, startLauncher, useLauncherCharacters, useLaunchers } from './launcher'
import { getLauncherByTerminalTabCharacter, openLauncher, removeLauncher, runLauncherScript, startLauncher, startLauncherExternally, useLauncherCharacters, useLaunchers } from './launcher'
import { clearLauncherSessions, LauncherSessionAddon } from './session'

declare module '../../../../src/typings/terminal' {
Expand All @@ -24,15 +24,21 @@ export default () => {
commas.ipcRenderer.on('start-launcher', (event, launcher) => {
startLauncher(launcher)
})
commas.ipcRenderer.on('start-launcher-externally', (event, launcher) => {
startLauncherExternally(launcher)
})
commas.ipcRenderer.on('run-script', (event, launcher, index) => {
runLauncherScript(launcher, index)
})
commas.ipcRenderer.on('remove-launcher', (event, launcher) => {
removeLauncher(launcher)
})

const characters = $(useLauncherCharacters())

commas.app.effect(() => {
commas.context.provide('terminal.category', {
title: 'Launcher#!launcher.2',
title: 'Launcher#!launcher.1',
characters,
command: 'open-launcher-character',
})
Expand Down
5 changes: 3 additions & 2 deletions addons/launcher/src/renderer/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ export function moveLauncher(launcher: Launcher, index: number, edge?: 'start' |
launchers = updated
}

export function removeLauncher(index: number) {
const launcherTabs = getTerminalTabsByLauncher(launchers[index])
export function removeLauncher(launcher: Launcher) {
const launcherTabs = getTerminalTabsByLauncher(launcher)
const updated = [...launchers]
const index = updated.findIndex(item => item.id === launcher.id)
updated.splice(index, 1)
launchers = updated
for (const tab of launcherTabs) {
Expand Down
2 changes: 2 additions & 0 deletions api/modules/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
useTerminalTabGroupSeparating,
useTerminalTabs,
} from '../../src/renderer/compositions/terminal'
import { createTerminalTabContextMenu } from '../../src/renderer/utils/terminal'
import type { TerminalInfo, TerminalTab, TerminalTabAddons, TerminalTabPane } from '../../src/typings/terminal'
import type { RendererAPIContext } from '../types'

Expand Down Expand Up @@ -129,4 +130,5 @@ export {
useTerminalTabs,
useTerminalTabGroupSeparating,
getTerminalTabIndex,
createTerminalTabContextMenu,
}
13 changes: 11 additions & 2 deletions src/renderer/components/TabList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import {
useTerminalTabs,
} from '../compositions/terminal'
import type { DraggableElementData, DraggableTabData } from '../utils/draggable'
import { openContextMenu } from '../utils/frame'
import { openContextMenu, withContextMenuSeparator } from '../utils/frame'
import { handleMousePressing } from '../utils/helper'
import { getShells } from '../utils/terminal'
import { createTerminalTabContextMenu, getShells } from '../utils/terminal'
import TabItem from './TabItem.vue'
import AutoScroll from './basic/AutoScroll.vue'
import DraggableElement from './basic/DraggableElement.vue'
Expand Down Expand Up @@ -167,6 +167,14 @@ function handleGroupSeparating(args: DraggableElementEventPayload<DraggableEleme
}
const enableSash = false
function openTabItemMenu(event: MouseEvent) {
const { updatingItems, deletingItems } = createTerminalTabContextMenu()
openContextMenu([
...withContextMenuSeparator(updatingItems, []),
...deletingItems,
], event)
}
</script>

<template>
Expand Down Expand Up @@ -205,6 +213,7 @@ const enableSash = false
:ref="draggable"
:tab="tab"
@click="activateTerminalTab(tab)"
@contextmenu="openTabItemMenu"
/>
</div>
</DropTarget>
Expand Down
26 changes: 26 additions & 0 deletions src/renderer/utils/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from 'node:path'
import { ipcRenderer } from 'electron'
import { parse } from 'shell-quote'
import { omitHome, resolveWindowsDisk } from '../../shared/terminal'
import type { MenuItem } from '../../typings/menu'
import type { TerminalTab } from '../../typings/terminal'
import icons from '../assets/icons'

Expand Down Expand Up @@ -100,3 +101,28 @@ export function getReadableSignal(code: number) {
?.[0]
}
}
export function createTerminalTabContextMenu() {
let updatingItems: MenuItem[] = [
{
label: 'Duplicate Tab#!menu.duplicatetab',
accelerator: 'CmdOrCtrl+D',
command: 'duplicate-tab',
},
{
label: 'Split Tab#!menu.splittab',
accelerator: 'CmdOrCtrl+Shift+D',
command: 'split-tab',
},
]
const deletingItems: MenuItem[] = [
{
label: 'Close Tab#!menu.closetab',
accelerator: 'CmdOrCtrl+W',
command: 'close-tab',
},
]
return {
updatingItems,
deletingItems,
}
}

0 comments on commit 741c8dc

Please sign in to comment.