-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
debt - introduce electron service and adopt for showMessageBox
- Loading branch information
Showing
20 changed files
with
232 additions
and
125 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
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
28 changes: 28 additions & 0 deletions
28
src/vs/platform/electron/electron-browser/electronService.ts
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,28 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { IMainProcessService } from 'vs/platform/ipc/electron-browser/mainProcessService'; | ||
|
||
export class ElectronService { | ||
|
||
_serviceBrand: undefined; | ||
|
||
constructor(@IMainProcessService mainProcessService: IMainProcessService) { | ||
const channel = mainProcessService.getChannel('electron'); | ||
|
||
// Proxy: forward any property access to the channel | ||
return new Proxy({}, { | ||
get(_target, propKey, _receiver) { | ||
if (typeof propKey === 'string') { | ||
return function (...args: any[]) { | ||
return channel.call(propKey, ...args); | ||
}; | ||
} | ||
|
||
throw new Error(`Not Implemented in ElectronService: ${String(propKey)}`); | ||
} | ||
}) as ElectronService; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/vs/platform/electron/electron-main/electronMainService.ts
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,59 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { IElectronService } from 'vs/platform/electron/node/electron'; | ||
import { IWindowsMainService, ICodeWindow } from 'vs/platform/windows/electron-main/windows'; | ||
import { MessageBoxOptions, MessageBoxReturnValue } from 'electron'; | ||
import { IServerChannel } from 'vs/base/parts/ipc/common/ipc'; | ||
import { Event } from 'vs/base/common/event'; | ||
|
||
export class ElectronMainService implements IElectronService { | ||
|
||
_serviceBrand: undefined; | ||
|
||
constructor( | ||
@IWindowsMainService private readonly windowsMainService: IWindowsMainService | ||
) { | ||
} | ||
|
||
private get window(): ICodeWindow | undefined { | ||
return this.windowsMainService.getFocusedWindow() || this.windowsMainService.getLastActiveWindow(); | ||
} | ||
|
||
async showMessageBox(options: MessageBoxOptions): Promise<MessageBoxReturnValue> { | ||
const result = await this.windowsMainService.showMessageBox(options, this.window); | ||
|
||
return { | ||
response: result.button, | ||
checkboxChecked: !!result.checkboxChecked | ||
}; | ||
} | ||
} | ||
|
||
export class ElectronChannel implements IServerChannel { | ||
|
||
private service: { [key: string]: unknown }; | ||
|
||
constructor(service: IElectronService) { | ||
this.service = service as unknown as { [key: string]: unknown }; | ||
} | ||
|
||
listen<T>(_: unknown, event: string): Event<T> { | ||
throw new Error(`Event not found: ${event}`); | ||
} | ||
|
||
call(_: unknown, command: string, arg?: any): Promise<any> { | ||
const target = this.service[command]; | ||
if (typeof target === 'function') { | ||
if (Array.isArray(arg)) { | ||
return target.apply(this.service, arg); | ||
} | ||
|
||
return target.call(this.service, arg); | ||
} | ||
|
||
throw new Error(`Call Not Found in ElectronService: ${command}`); | ||
} | ||
} |
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,17 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { MessageBoxOptions, MessageBoxReturnValue } from 'electron'; | ||
import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; | ||
|
||
export const IElectronService = createDecorator<IElectronService>('electronService'); | ||
|
||
export interface IElectronService { | ||
|
||
_serviceBrand: undefined; | ||
|
||
// Dialogs | ||
showMessageBox(options: MessageBoxOptions): Promise<MessageBoxReturnValue>; | ||
} |
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
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.