|
| 1 | +import { FrontendApplication } from '@theia/core/lib/browser/frontend-application'; |
| 2 | +import { SidebarMenu } from '@theia/core/lib/browser/shell/sidebar-menu-widget'; |
| 3 | +import { WindowService } from '@theia/core/lib/browser/window/window-service'; |
| 4 | +import { DisposableCollection } from '@theia/core/lib/common/disposable'; |
| 5 | +import { MenuPath } from '@theia/core/lib/common/menu'; |
| 6 | +import { nls } from '@theia/core/lib/common/nls'; |
| 7 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 8 | +import { CloudUserCommands, LEARN_MORE_URL } from '../auth/cloud-user-commands'; |
| 9 | +import { CreateFeatures } from '../create/create-features'; |
| 10 | +import { ArduinoMenus } from '../menu/arduino-menus'; |
| 11 | +import { |
| 12 | + Command, |
| 13 | + CommandRegistry, |
| 14 | + Contribution, |
| 15 | + MenuModelRegistry, |
| 16 | +} from './contribution'; |
| 17 | + |
| 18 | +export const accountMenu: SidebarMenu = { |
| 19 | + id: 'arduino-accounts-menu', |
| 20 | + iconClass: 'codicon codicon-account', |
| 21 | + title: nls.localize('arduino/account/menuTitle', 'Arduino Cloud'), |
| 22 | + menuPath: ArduinoMenus.ARDUINO_ACCOUNT__CONTEXT, |
| 23 | + order: 0, |
| 24 | +}; |
| 25 | + |
| 26 | +@injectable() |
| 27 | +export class Account extends Contribution { |
| 28 | + @inject(WindowService) |
| 29 | + private readonly windowService: WindowService; |
| 30 | + @inject(CreateFeatures) |
| 31 | + private readonly createFeatures: CreateFeatures; |
| 32 | + |
| 33 | + private readonly toDispose = new DisposableCollection(); |
| 34 | + private app: FrontendApplication; |
| 35 | + |
| 36 | + override onStart(app: FrontendApplication): void { |
| 37 | + this.app = app; |
| 38 | + this.updateSidebarCommand(); |
| 39 | + this.toDispose.push( |
| 40 | + this.createFeatures.onDidChangeEnabled((enabled) => |
| 41 | + this.updateSidebarCommand(enabled) |
| 42 | + ) |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + onStop(): void { |
| 47 | + this.toDispose.dispose(); |
| 48 | + } |
| 49 | + |
| 50 | + override registerCommands(registry: CommandRegistry): void { |
| 51 | + const openExternal = (url: string) => |
| 52 | + this.windowService.openNewWindow(url, { external: true }); |
| 53 | + registry.registerCommand(Account.Commands.LEARN_MORE, { |
| 54 | + execute: () => openExternal(LEARN_MORE_URL), |
| 55 | + isEnabled: () => !Boolean(this.createFeatures.session), |
| 56 | + }); |
| 57 | + registry.registerCommand(Account.Commands.GO_TO_PROFILE, { |
| 58 | + execute: () => openExternal('https://id.arduino.cc/'), |
| 59 | + isEnabled: () => Boolean(this.createFeatures.session), |
| 60 | + }); |
| 61 | + registry.registerCommand(Account.Commands.GO_TO_CLOUD_EDITOR, { |
| 62 | + execute: () => openExternal('https://create.arduino.cc/editor'), |
| 63 | + isEnabled: () => Boolean(this.createFeatures.session), |
| 64 | + }); |
| 65 | + registry.registerCommand(Account.Commands.GO_TO_IOT_CLOUD, { |
| 66 | + execute: () => openExternal('https://create.arduino.cc/iot/'), |
| 67 | + isEnabled: () => Boolean(this.createFeatures.session), |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + override registerMenus(registry: MenuModelRegistry): void { |
| 72 | + const register = ( |
| 73 | + menuPath: MenuPath, |
| 74 | + ...commands: (Command | [command: Command, menuLabel: string])[] |
| 75 | + ) => |
| 76 | + commands.forEach((command, index) => { |
| 77 | + const commandId = Array.isArray(command) ? command[0].id : command.id; |
| 78 | + const label = Array.isArray(command) ? command[1] : command.label; |
| 79 | + registry.registerMenuAction(menuPath, { |
| 80 | + label, |
| 81 | + commandId, |
| 82 | + order: String(index), |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + register(ArduinoMenus.ARDUINO_ACCOUNT__CONTEXT__SIGN_IN_GROUP, [ |
| 87 | + CloudUserCommands.LOGIN, |
| 88 | + nls.localize('arduino/cloud/signInToCloud', 'Sign in to Arduino Cloud'), |
| 89 | + ]); |
| 90 | + register(ArduinoMenus.ARDUINO_ACCOUNT__CONTEXT__LEARN_MORE_GROUP, [ |
| 91 | + Account.Commands.LEARN_MORE, |
| 92 | + nls.localize('arduino/cloud/learnMore', 'Learn more'), |
| 93 | + ]); |
| 94 | + register( |
| 95 | + ArduinoMenus.ARDUINO_ACCOUNT__CONTEXT__GO_TO_GROUP, |
| 96 | + [ |
| 97 | + Account.Commands.GO_TO_PROFILE, |
| 98 | + nls.localize('arduino/account/goToProfile', 'Go to Profile'), |
| 99 | + ], |
| 100 | + [ |
| 101 | + Account.Commands.GO_TO_CLOUD_EDITOR, |
| 102 | + nls.localize('arduino/account/goToCloudEditor', 'Go to Cloud Editor'), |
| 103 | + ], |
| 104 | + [ |
| 105 | + Account.Commands.GO_TO_IOT_CLOUD, |
| 106 | + nls.localize('arduino/account/goToIoTCloud', 'Go to IoT Cloud'), |
| 107 | + ] |
| 108 | + ); |
| 109 | + register( |
| 110 | + ArduinoMenus.ARDUINO_ACCOUNT__CONTEXT__SIGN_OUT_GROUP, |
| 111 | + CloudUserCommands.LOGOUT |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + private updateSidebarCommand( |
| 116 | + visible: boolean = this.preferences['arduino.cloud.enabled'] |
| 117 | + ): void { |
| 118 | + if (!this.app) { |
| 119 | + return; |
| 120 | + } |
| 121 | + const handler = this.app.shell.leftPanelHandler; |
| 122 | + if (visible) { |
| 123 | + handler.addBottomMenu(accountMenu); |
| 124 | + } else { |
| 125 | + handler.removeBottomMenu(accountMenu.id); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +export namespace Account { |
| 131 | + export namespace Commands { |
| 132 | + export const GO_TO_PROFILE: Command = { |
| 133 | + id: 'arduino-go-to-profile', |
| 134 | + }; |
| 135 | + export const GO_TO_CLOUD_EDITOR: Command = { |
| 136 | + id: 'arduino-go-to-cloud-editor', |
| 137 | + }; |
| 138 | + export const GO_TO_IOT_CLOUD: Command = { |
| 139 | + id: 'arduino-go-to-iot-cloud', |
| 140 | + }; |
| 141 | + export const LEARN_MORE: Command = { |
| 142 | + id: 'arduino-learn-more', |
| 143 | + }; |
| 144 | + } |
| 145 | +} |
0 commit comments