-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
187 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { BaseTabComponent } from '../components/baseTab.component' | ||
import { MenuItemOptions } from './menu' | ||
import { ToolbarButton } from './toolbarButtonProvider' | ||
|
||
export class Command { | ||
label: string | ||
sublabel?: string | ||
click?: () => void | ||
|
||
/** | ||
* Raw SVG icon code | ||
*/ | ||
icon?: string | ||
|
||
static fromToolbarButton (button: ToolbarButton): Command { | ||
const command = new Command() | ||
command.label = button.commandLabel ?? button.title | ||
command.click = button.click | ||
command.icon = button.icon | ||
return command | ||
} | ||
|
||
static fromMenuItem (item: MenuItemOptions): Command { | ||
const command = new Command() | ||
command.label = item.commandLabel ?? item.label ?? '' | ||
command.sublabel = item.sublabel | ||
command.click = item.click | ||
return command | ||
} | ||
} | ||
|
||
export interface CommandContext { | ||
tab?: BaseTabComponent, | ||
} |
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
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 |
---|---|---|
|
@@ -38,3 +38,8 @@ input { | |
border-radius: 0; | ||
border: none; | ||
} | ||
|
||
profile-icon { | ||
width: 14px; | ||
height: 14px; | ||
} |
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 |
---|---|---|
|
@@ -94,3 +94,5 @@ hotkeys: | |
- 'Ctrl-Alt-T' | ||
profile-selector: | ||
- 'Ctrl-Shift-E' | ||
command-selector: | ||
- 'Ctrl-Shift-P' |
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 |
---|---|---|
|
@@ -93,5 +93,7 @@ hotkeys: | |
- '⌘-E' | ||
switch-profile: | ||
- '⌘-Shift-E' | ||
command-selector: | ||
- '⌘-Shift-P' | ||
appearance: | ||
vibrancy: true |
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 |
---|---|---|
|
@@ -95,3 +95,5 @@ hotkeys: | |
- 'Ctrl-Alt-T' | ||
profile-selector: | ||
- 'Ctrl-Shift-E' | ||
command-selector: | ||
- 'Ctrl-Shift-P' |
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
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Inject, Injectable, Optional } from '@angular/core' | ||
import { AppService, Command, CommandContext, ConfigService, MenuItemOptions, SplitTabComponent, TabContextMenuItemProvider, ToolbarButton, ToolbarButtonProvider, TranslateService } from '../api' | ||
import { SelectorService } from './selector.service' | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class CommandService { | ||
constructor ( | ||
private selector: SelectorService, | ||
private config: ConfigService, | ||
private app: AppService, | ||
private translate: TranslateService, | ||
@Optional() @Inject(TabContextMenuItemProvider) protected contextMenuProviders: TabContextMenuItemProvider[], | ||
@Inject(ToolbarButtonProvider) private toolbarButtonProviders: ToolbarButtonProvider[], | ||
) { | ||
this.contextMenuProviders.sort((a, b) => a.weight - b.weight) | ||
} | ||
|
||
async getCommands (context: CommandContext): Promise<Command[]> { | ||
let buttons: ToolbarButton[] = [] | ||
this.config.enabledServices(this.toolbarButtonProviders).forEach(provider => { | ||
buttons = buttons.concat(provider.provide()) | ||
}) | ||
buttons = buttons | ||
.sort((a: ToolbarButton, b: ToolbarButton) => (a.weight ?? 0) - (b.weight ?? 0)) | ||
|
||
let items: MenuItemOptions[] = [] | ||
if (context.tab) { | ||
for (const tabHeader of [false, true]) { | ||
// Top-level tab menu | ||
for (let section of await Promise.all(this.contextMenuProviders.map(x => x.getItems(context.tab!, tabHeader)))) { | ||
// eslint-disable-next-line @typescript-eslint/no-loop-func | ||
section = section.filter(item => !items.some(ex => ex.label === item.label)) | ||
items = items.concat(section) | ||
} | ||
if (context.tab instanceof SplitTabComponent) { | ||
const tab = context.tab.getFocusedTab() | ||
if (tab) { | ||
for (let section of await Promise.all(this.contextMenuProviders.map(x => x.getItems(tab, tabHeader)))) { | ||
// eslint-disable-next-line @typescript-eslint/no-loop-func | ||
section = section.filter(item => !items.some(ex => ex.label === item.label)) | ||
items = items.concat(section) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
items = items.filter(x => (x.enabled ?? true) && x.type !== 'separator') | ||
|
||
const flatItems: MenuItemOptions[] = [] | ||
function flattenItem (item: MenuItemOptions, prefix?: string): void { | ||
if (item.submenu) { | ||
item.submenu.forEach(x => flattenItem(x, (prefix ? `${prefix} > ` : '') + (item.commandLabel ?? item.label))) | ||
} else { | ||
flatItems.push({ | ||
...item, | ||
label: (prefix ? `${prefix} > ` : '') + (item.commandLabel ?? item.label), | ||
}) | ||
} | ||
} | ||
items.forEach(x => flattenItem(x)) | ||
|
||
let commands = buttons.map(x => Command.fromToolbarButton(x)) | ||
commands = commands.concat(flatItems.map(x => Command.fromMenuItem(x))) | ||
|
||
return commands | ||
} | ||
|
||
async showSelector (): Promise<void> { | ||
const context: CommandContext = {} | ||
const tab = this.app.activeTab | ||
if (tab instanceof SplitTabComponent) { | ||
context.tab = tab.getFocusedTab() ?? undefined | ||
} | ||
const commands = await this.getCommands(context) | ||
await this.selector.show( | ||
this.translate.instant('Commands'), | ||
commands.map(c => ({ | ||
name: c.label, | ||
callback: c.click, | ||
description: c.sublabel, | ||
icon: c.icon, | ||
})), | ||
) | ||
} | ||
} |
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
Oops, something went wrong.