Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert more buttons and context menu items to commands #9490

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions tabby-core/src/api/commands.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import slugify from 'slugify'
import { BaseTabComponent } from '../components/baseTab.component'
import { MenuItemOptions } from './menu'
import { ToolbarButton } from './toolbarButtonProvider'
Expand All @@ -6,34 +7,33 @@ export enum CommandLocation {
LeftToolbar = 'left-toolbar',
RightToolbar = 'right-toolbar',
StartPage = 'start-page',
TabHeaderMenu = 'tab-header-menu',
TabBodyMenu = 'tab-body-menu',
}

export class Command {
id?: string
id: string
label: string
sublabel?: string
locations?: CommandLocation[]
run: () => Promise<void>
fullLabel?: string
locations: CommandLocation[]
run?: () => Promise<any>

/**
* Raw SVG icon code
*/
icon?: string

/**
* Optional Touch Bar icon ID
*/
touchBarNSImage?: string
weight?: number

/**
* Optional Touch Bar button label
*/
touchBarTitle?: string
parent?: string

weight?: number
group?: string

checked?: boolean

static fromToolbarButton (button: ToolbarButton): Command {
const command = new Command()
command.id = `legacy:${slugify(button.title)}`
command.label = button.title
command.run = async () => button.click?.()
command.icon = button.icon
Expand All @@ -44,18 +44,29 @@ export class Command {
if ((button.weight ?? 0) > 0) {
command.locations.push(CommandLocation.RightToolbar)
}
command.touchBarNSImage = button.touchBarNSImage
command.touchBarTitle = button.touchBarTitle
command.weight = button.weight
return command
}

static fromMenuItem (item: MenuItemOptions): Command {
const command = new Command()
command.label = item.commandLabel ?? item.label ?? ''
command.sublabel = item.sublabel
command.run = async () => item.click?.()
return command
static fromMenuItem (item: MenuItemOptions): Command[] {
if (item.type === 'separator') {
return []
}
const commands: Command[] = [{
id: `legacy:${slugify(item.commandLabel ?? item.label).toLowerCase()}`,
label: item.commandLabel ?? item.label,
run: async () => item.click?.(),
locations: [CommandLocation.TabBodyMenu, CommandLocation.TabHeaderMenu],
checked: item.checked,
}]
for (const submenu of item.submenu ?? []) {
commands.push(...Command.fromMenuItem(submenu).map(x => ({
...x,
id: `${commands[0].id}:${slugify(x.label).toLowerCase()}`,
parent: commands[0].id,
})))
}
return commands
}
}

Expand Down
1 change: 1 addition & 0 deletions tabby-core/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export { UpdaterService } from '../services/updater.service'
export { VaultService, Vault, VaultSecret, VaultFileSecret, VAULT_SECRET_TYPE_FILE, StoredVault, VaultSecretKey } from '../services/vault.service'
export { FileProvidersService } from '../services/fileProviders.service'
export { LocaleService } from '../services/locale.service'
export { CommandService } from '../services/commands.service'
export { TranslateService } from '@ngx-translate/core'
export * from '../utils'
export { UTF8Splitter } from '../utfSplitter'
12 changes: 8 additions & 4 deletions tabby-core/src/api/menu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export interface MenuItemOptions {
type?: 'normal' | 'separator' | 'submenu' | 'checkbox' | 'radio'
label?: string
export type MenuItemOptions = {
sublabel?: string
enabled?: boolean
checked?: boolean
Expand All @@ -9,4 +7,10 @@ export interface MenuItemOptions {

/** @hidden */
commandLabel?: string
}
} & ({
type: 'separator',
label?: string,
} | {
type?: 'normal' | 'submenu' | 'checkbox' | 'radio',
label: string,
})
10 changes: 0 additions & 10 deletions tabby-core/src/api/toolbarButtonProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ export interface ToolbarButton {

title: string

/**
* Optional Touch Bar icon ID
*/
touchBarNSImage?: string

/**
* Optional Touch Bar button label
*/
touchBarTitle?: string

weight?: number

click?: () => void
Expand Down
Loading
Loading