|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | +import { safeInnerHtml } from 'vs/base/browser/dom'; |
| 6 | +import { mainWindow } from 'vs/base/browser/window'; |
| 7 | +import { DisposableStore } from 'vs/base/common/lifecycle'; |
| 8 | +import { URI } from 'vs/base/common/uri'; |
| 9 | +import BaseHtml from 'vs/workbench/contrib/issue/browser/issueReporterPage'; |
| 10 | +import 'vs/css!./media/issueReporter'; |
| 11 | +import { IMenuService, MenuId } from 'vs/platform/actions/common/actions'; |
| 12 | +import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; |
| 13 | +import { PerformanceInfo, SystemInfo } from 'vs/platform/diagnostics/common/diagnostics'; |
| 14 | +import { ExtensionIdentifier, ExtensionIdentifierSet } from 'vs/platform/extensions/common/extensions'; |
| 15 | +import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; |
| 16 | +import { IIssueMainService, IssueReporterData, ProcessExplorerData } from 'vs/platform/issue/common/issue'; |
| 17 | +import product from 'vs/platform/product/common/product'; |
| 18 | +import { IssueWebReporter } from 'vs/workbench/contrib/issue/browser/issueReporterService'; |
| 19 | +import { AuxiliaryWindowMode, IAuxiliaryWindowService } from 'vs/workbench/services/auxiliaryWindow/browser/auxiliaryWindowService'; |
| 20 | + |
| 21 | +export class IssueFormService implements IIssueMainService { |
| 22 | + |
| 23 | + readonly _serviceBrand: undefined; |
| 24 | + |
| 25 | + private issueReporterWindow: Window | null = null; |
| 26 | + private extensionIdentifierSet: ExtensionIdentifierSet = new ExtensionIdentifierSet(); |
| 27 | + |
| 28 | + constructor( |
| 29 | + @IInstantiationService private readonly instantiationService: IInstantiationService, |
| 30 | + @IAuxiliaryWindowService private readonly auxiliaryWindowService: IAuxiliaryWindowService, |
| 31 | + @IMenuService private readonly menuService: IMenuService, |
| 32 | + @IContextKeyService private readonly contextKeyService: IContextKeyService, |
| 33 | + ) { |
| 34 | + |
| 35 | + // listen for messages from the main window |
| 36 | + mainWindow.addEventListener('message', async (event) => { |
| 37 | + if (event.data && event.data.sendChannel === 'vscode:triggerReporterMenu') { |
| 38 | + // creates menu from contributed |
| 39 | + const menu = this.menuService.createMenu(MenuId.IssueReporter, this.contextKeyService); |
| 40 | + |
| 41 | + // render menu and dispose |
| 42 | + const actions = menu.getActions({ renderShortTitle: true }).flatMap(entry => entry[1]); |
| 43 | + for (const action of actions) { |
| 44 | + try { |
| 45 | + if (action.item && 'source' in action.item && action.item.source?.id === event.data.extensionId) { |
| 46 | + this.extensionIdentifierSet.add(event.data.extensionId); |
| 47 | + await action.run(); |
| 48 | + } |
| 49 | + } catch (error) { |
| 50 | + console.error(error); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (!this.extensionIdentifierSet.has(event.data.extensionId)) { |
| 55 | + // send undefined to indicate no action was taken |
| 56 | + const replyChannel = `vscode:triggerReporterMenuResponse`; |
| 57 | + mainWindow.postMessage({ replyChannel }, '*'); |
| 58 | + } |
| 59 | + |
| 60 | + menu.dispose(); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + async openReporter(data: IssueReporterData): Promise<void> { |
| 67 | + if (data.extensionId && this.extensionIdentifierSet.has(data.extensionId)) { |
| 68 | + const replyChannel = `vscode:triggerReporterMenuResponse`; |
| 69 | + mainWindow.postMessage({ data, replyChannel }, '*'); |
| 70 | + this.extensionIdentifierSet.delete(new ExtensionIdentifier(data.extensionId)); |
| 71 | + } |
| 72 | + |
| 73 | + if (this.issueReporterWindow) { |
| 74 | + this.issueReporterWindow.focus(); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + const disposables = new DisposableStore(); |
| 79 | + |
| 80 | + // Auxiliary Window |
| 81 | + const auxiliaryWindow = disposables.add(await this.auxiliaryWindowService.open({ mode: AuxiliaryWindowMode.Normal })); |
| 82 | + |
| 83 | + this.issueReporterWindow = auxiliaryWindow.window; |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + if (auxiliaryWindow) { |
| 88 | + await auxiliaryWindow.whenStylesHaveLoaded; |
| 89 | + auxiliaryWindow.window.document.title = 'Issue Reporter'; |
| 90 | + auxiliaryWindow.window.document.body.classList.add('issue-reporter-body'); |
| 91 | + |
| 92 | + // custom issue reporter wrapper |
| 93 | + const div = document.createElement('div'); |
| 94 | + div.classList.add('monaco-workbench'); |
| 95 | + |
| 96 | + // removes preset monaco-workbench |
| 97 | + auxiliaryWindow.container.remove(); |
| 98 | + auxiliaryWindow.window.document.body.appendChild(div); |
| 99 | + safeInnerHtml(div, BaseHtml()); |
| 100 | + |
| 101 | + // create issue reporter and instantiate |
| 102 | + const issueReporter = this.instantiationService.createInstance(IssueWebReporter, false, data, { type: '', arch: '', release: '' }, product, auxiliaryWindow.window); |
| 103 | + issueReporter.render(); |
| 104 | + } else { |
| 105 | + console.error('Failed to open auxiliary window'); |
| 106 | + } |
| 107 | + |
| 108 | + // handle closing issue reporter |
| 109 | + this.issueReporterWindow?.addEventListener('beforeunload', () => { |
| 110 | + auxiliaryWindow.window.close(); |
| 111 | + this.issueReporterWindow = null; |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + async openProcessExplorer(data: ProcessExplorerData): Promise<void> { |
| 116 | + throw new Error('Method not implemented.'); |
| 117 | + } |
| 118 | + |
| 119 | + stopTracing(): Promise<void> { |
| 120 | + throw new Error('Method not implemented.'); |
| 121 | + } |
| 122 | + getSystemStatus(): Promise<string> { |
| 123 | + throw new Error('Method not implemented.'); |
| 124 | + } |
| 125 | + $getSystemInfo(): Promise<SystemInfo> { |
| 126 | + throw new Error('Method not implemented.'); |
| 127 | + } |
| 128 | + $getPerformanceInfo(): Promise<PerformanceInfo> { |
| 129 | + throw new Error('Method not implemented.'); |
| 130 | + } |
| 131 | + $reloadWithExtensionsDisabled(): Promise<void> { |
| 132 | + throw new Error('Method not implemented.'); |
| 133 | + } |
| 134 | + $showConfirmCloseDialog(): Promise<void> { |
| 135 | + throw new Error('Method not implemented.'); |
| 136 | + } |
| 137 | + $showClipboardDialog(): Promise<boolean> { |
| 138 | + throw new Error('Method not implemented.'); |
| 139 | + } |
| 140 | + $getIssueReporterUri(extensionId: string): Promise<URI> { |
| 141 | + throw new Error('Method not implemented.'); |
| 142 | + } |
| 143 | + $getIssueReporterData(extensionId: string): Promise<string> { |
| 144 | + throw new Error('Method not implemented.'); |
| 145 | + } |
| 146 | + $getIssueReporterTemplate(extensionId: string): Promise<string> { |
| 147 | + throw new Error('Method not implemented.'); |
| 148 | + } |
| 149 | + $getReporterStatus(extensionId: string, extensionName: string): Promise<boolean[]> { |
| 150 | + throw new Error('Method not implemented.'); |
| 151 | + } |
| 152 | + |
| 153 | + async $sendReporterMenu(extensionId: string, extensionName: string): Promise<IssueReporterData | undefined> { |
| 154 | + const sendChannel = `vscode:triggerReporterMenu`; |
| 155 | + mainWindow.postMessage({ sendChannel, extensionId, extensionName }, '*'); |
| 156 | + |
| 157 | + const result = await new Promise((resolve, reject) => { |
| 158 | + const timeout = setTimeout(() => { |
| 159 | + mainWindow.removeEventListener('message', listener); |
| 160 | + reject(new Error('Timeout exceeded')); |
| 161 | + }, 5000); // Set the timeout value in milliseconds (e.g., 5000 for 5 seconds) |
| 162 | + |
| 163 | + const listener = (event: MessageEvent) => { |
| 164 | + const replyChannel = `vscode:triggerReporterMenuResponse`; |
| 165 | + if (event.data && event.data.replyChannel === replyChannel) { |
| 166 | + clearTimeout(timeout); |
| 167 | + mainWindow.removeEventListener('message', listener); |
| 168 | + resolve(event.data.data); |
| 169 | + } |
| 170 | + }; |
| 171 | + mainWindow.addEventListener('message', listener); |
| 172 | + }); |
| 173 | + |
| 174 | + return result as IssueReporterData | undefined; |
| 175 | + } |
| 176 | + |
| 177 | + async $closeReporter(): Promise<void> { |
| 178 | + this.issueReporterWindow?.close(); |
| 179 | + } |
| 180 | +} |
0 commit comments