Skip to content

Commit

Permalink
debt - implement focusWindow() via host
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Sep 25, 2019
1 parent 817eb6b commit e096ce1
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 58 deletions.
11 changes: 11 additions & 0 deletions src/vs/platform/electron/electron-main/electronMainService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ export class ElectronMainService implements AddContextToFunctions<IElectronServi
}
}

async focusWindow(windowId: number): Promise<void> {
const window = this.windowsMainService.getWindowById(windowId);
if (window) {
if (isMacintosh) {
window.win.show();
} else {
window.win.focus();
}
}
}

//#endregion

//#region Dialog
Expand Down
5 changes: 5 additions & 0 deletions src/vs/platform/electron/node/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ export interface IElectronService {

// Window
windowCount(): Promise<number>;

openEmptyWindow(options?: { reuse?: boolean, remoteAuthority?: string }): Promise<void>;

toggleFullScreen(): Promise<void>;

handleTitleDoubleClick(): Promise<void>;

isMaximized(): Promise<boolean>;
maximizeWindow(): Promise<void>;
unmaximizeWindow(): Promise<void>;
minimizeWindow(): Promise<void>;

focusWindow(): Promise<void>;

// Dialogs
showMessageBox(options: MessageBoxOptions): Promise<MessageBoxReturnValue>;
showSaveDialog(options: SaveDialogOptions): Promise<SaveDialogReturnValue>;
Expand Down
2 changes: 0 additions & 2 deletions src/vs/platform/windows/common/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export interface IWindowsService {
removeFromRecentlyOpened(paths: URI[]): Promise<void>;
clearRecentlyOpened(): Promise<void>;
getRecentlyOpened(windowId: number): Promise<IRecentlyOpened>;
focusWindow(windowId: number): Promise<void>;
isFocused(windowId: number): Promise<boolean>;

// Global methods
Expand Down Expand Up @@ -152,7 +151,6 @@ export interface IWindowService {
getRecentlyOpened(): Promise<IRecentlyOpened>;
addRecentlyOpened(recents: IRecent[]): Promise<void>;
removeFromRecentlyOpened(paths: URI[]): Promise<void>;
focusWindow(): Promise<void>;
openWindow(uris: IURIToOpen[], options?: IOpenSettings): Promise<void>;
isFocused(): Promise<boolean>;
}
Expand Down
1 change: 0 additions & 1 deletion src/vs/platform/windows/common/windowsIpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export class WindowsChannel implements IServerChannel {
case 'removeFromRecentlyOpened': return this.service.removeFromRecentlyOpened(arg.map(URI.revive));
case 'clearRecentlyOpened': return this.service.clearRecentlyOpened();
case 'getRecentlyOpened': return this.service.getRecentlyOpened(arg);
case 'focusWindow': return this.service.focusWindow(arg);
case 'isFocused': return this.service.isFocused(arg);
case 'openWindow': {
const urisToOpen: IURIToOpen[] = arg[1];
Expand Down
4 changes: 0 additions & 4 deletions src/vs/platform/windows/electron-browser/windowsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ export class WindowsService implements IWindowsService {
return recentlyOpened;
}

focusWindow(windowId: number): Promise<void> {
return this.channel.call('focusWindow', windowId);
}

isFocused(windowId: number): Promise<boolean> {
return this.channel.call('isFocused', windowId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { IRecentlyOpened, IRecent } from 'vs/platform/history/common/history';
import { IHistoryMainService } from 'vs/platform/history/electron-main/historyMainService';
import { IWorkspaceIdentifier, ISingleFolderWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
import { Schemas } from 'vs/base/common/network';
import { isMacintosh, IProcessEnvironment } from 'vs/base/common/platform';
import { IProcessEnvironment } from 'vs/base/common/platform';
import { ILogService } from 'vs/platform/log/common/log';

// @deprecated this should eventually go away and be implemented by host & electron service
Expand Down Expand Up @@ -96,16 +96,6 @@ export class LegacyWindowsMainService extends Disposable implements IWindowsServ
return this.withWindow(windowId, codeWindow => this.historyMainService.getRecentlyOpened(codeWindow.config.workspace, codeWindow.config.folderUri, codeWindow.config.filesToOpenOrCreate), () => this.historyMainService.getRecentlyOpened())!;
}

async focusWindow(windowId: number): Promise<void> {
this.logService.trace('windowsService#focusWindow', windowId);

if (isMacintosh) {
return this.withWindow(windowId, codeWindow => codeWindow.win.show());
} else {
return this.withWindow(windowId, codeWindow => codeWindow.win.focus());
}
}

async isFocused(windowId: number): Promise<boolean> {
this.logService.trace('windowsService#isFocused', windowId);

Expand Down
8 changes: 5 additions & 3 deletions src/vs/workbench/browser/dnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { IRecentFile } from 'vs/platform/history/common/history';
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
import { withNullAsUndefined } from 'vs/base/common/types';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { IHostService } from 'vs/workbench/services/host/browser/host';

export interface IDraggedResource {
resource: URI;
Expand Down Expand Up @@ -166,7 +167,8 @@ export class ResourcesDropHandler {
@IUntitledEditorService private readonly untitledEditorService: IUntitledEditorService,
@IEditorService private readonly editorService: IEditorService,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IWorkspaceEditingService private readonly workspaceEditingService: IWorkspaceEditingService
@IWorkspaceEditingService private readonly workspaceEditingService: IWorkspaceEditingService,
@IHostService private readonly hostService: IHostService
) {
}

Expand All @@ -177,7 +179,7 @@ export class ResourcesDropHandler {
}

// Make the window active to handle the drop properly within
await this.windowService.focusWindow();
await this.hostService.focus();

// Check for special things being dropped
const isWorkspaceOpening = await this.doHandleDrop(untitledOrFileResources);
Expand Down Expand Up @@ -292,7 +294,7 @@ export class ResourcesDropHandler {
}

// Pass focus to window
this.windowService.focusWindow();
this.hostService.focus();

// Open in separate windows if we drop workspaces or just one folder
if (urisToOpen.length > folderURIs.length || folderURIs.length === 1) {
Expand Down
8 changes: 0 additions & 8 deletions src/vs/workbench/browser/web.simpleservices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ export class SimpleWindowService extends Disposable implements IWindowService {
return this.storageService.store(SimpleWindowService.RECENTLY_OPENED_KEY, JSON.stringify(toStoreData(data)), StorageScope.GLOBAL);
}

focusWindow(): Promise<void> {
return Promise.resolve();
}

async openWindow(_uris: IURIToOpen[], _options?: IOpenSettings): Promise<void> {
const { openFolderInNewWindow } = this.shouldOpenNewWindow(_options);
for (let i = 0; i < _uris.length; i++) {
Expand Down Expand Up @@ -216,10 +212,6 @@ export class SimpleWindowsService implements IWindowsService {
});
}

focusWindow(_windowId: number): Promise<void> {
return Promise.resolve();
}

// Global methods
openWindow(_windowId: number, _uris: IURIToOpen[], _options: IOpenSettings): Promise<void> {
return Promise.resolve();
Expand Down
6 changes: 3 additions & 3 deletions src/vs/workbench/contrib/debug/browser/debugSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { IWorkspaceFolder, IWorkspaceContextService } from 'vs/platform/workspac
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { RunOnceScheduler } from 'vs/base/common/async';
import { generateUuid } from 'vs/base/common/uuid';
import { IWindowService } from 'vs/platform/windows/common/windows';
import { IHostService } from 'vs/workbench/services/host/browser/host';
import { IExtensionHostDebugService } from 'vs/platform/debug/common/extensionHostDebug';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { normalizeDriveLetter } from 'vs/base/common/labels';
Expand Down Expand Up @@ -70,7 +70,7 @@ export class DebugSession implements IDebugSession {
options: IDebugSessionOptions | undefined,
@IDebugService private readonly debugService: IDebugService,
@ITelemetryService private readonly telemetryService: ITelemetryService,
@IWindowService private readonly windowService: IWindowService,
@IHostService private readonly hostService: IHostService,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IViewletService private readonly viewletService: IViewletService,
@IWorkspaceContextService private readonly workspaceContextService: IWorkspaceContextService,
Expand Down Expand Up @@ -731,7 +731,7 @@ export class DebugSession implements IDebugSession {
}

if (this.configurationService.getValue<IDebugConfiguration>('debug').focusWindowOnBreak) {
this.windowService.focusWindow();
this.hostService.focus();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { IExtensionEnablementService, EnablementState, IExtensionManagementServe
import { getGalleryExtensionTelemetryData, getLocalExtensionTelemetryData, areSameExtensions, getMaliciousExtensionsSet, groupByExtension, ExtensionIdentifierWithVersion } from 'vs/platform/extensionManagement/common/extensionManagementUtil';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IWindowService } from 'vs/platform/windows/common/windows';
import { IHostService } from 'vs/workbench/services/host/browser/host';
import { URI } from 'vs/base/common/uri';
import { IExtension, ExtensionState, IExtensionsWorkbenchService, AutoUpdateConfigurationKey, AutoCheckUpdatesConfigurationKey } from 'vs/workbench/contrib/extensions/common/extensions';
import { IEditorService, SIDE_GROUP, ACTIVE_GROUP } from 'vs/workbench/services/editor/common/editorService';
Expand Down Expand Up @@ -496,7 +496,7 @@ export class ExtensionsWorkbenchService extends Disposable implements IExtension
@INotificationService private readonly notificationService: INotificationService,
@IURLService urlService: IURLService,
@IExtensionEnablementService private readonly extensionEnablementService: IExtensionEnablementService,
@IWindowService private readonly windowService: IWindowService,
@IHostService private readonly hostService: IHostService,
@IProgressService private readonly progressService: IProgressService,
@IExtensionManagementServerService private readonly extensionManagementServerService: IExtensionManagementServerService,
@IStorageService private readonly storageService: IStorageService,
Expand Down Expand Up @@ -1070,7 +1070,7 @@ export class ExtensionsWorkbenchService extends Disposable implements IExtension
const extension = local.filter(local => areSameExtensions(local.identifier, { id: extensionId }))[0];

if (extension) {
return this.windowService.focusWindow()
return this.hostService.focus()
.then(() => this.open(extension));
}
return this.queryGallery({ names: [extensionId], source: 'uri' }, CancellationToken.None).then(result => {
Expand All @@ -1080,7 +1080,7 @@ export class ExtensionsWorkbenchService extends Disposable implements IExtension

const extension = result.firstPage[0];

return this.windowService.focusWindow().then(() => {
return this.hostService.focus().then(() => {
return this.open(extension);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { DesktopDragAndDropData, ExternalElementsDragAndDropData, ElementsDragAn
import { isMacintosh } from 'vs/base/common/platform';
import { IDialogService, IConfirmationResult, IConfirmation, getConfirmMessage } from 'vs/platform/dialogs/common/dialogs';
import { ITextFileService, ITextFileOperationResult } from 'vs/workbench/services/textfile/common/textfiles';
import { IWindowService } from 'vs/platform/windows/common/windows';
import { IHostService } from 'vs/workbench/services/host/browser/host';
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
import { URI } from 'vs/base/common/uri';
import { ITask, sequence } from 'vs/base/common/async';
Expand Down Expand Up @@ -443,7 +443,7 @@ export class FileDragAndDrop implements ITreeDragAndDrop<ExplorerItem> {
@IConfigurationService private configurationService: IConfigurationService,
@IInstantiationService private instantiationService: IInstantiationService,
@ITextFileService private textFileService: ITextFileService,
@IWindowService private windowService: IWindowService,
@IHostService private hostService: IHostService,
@IWorkspaceEditingService private workspaceEditingService: IWorkspaceEditingService
) {
this.toDispose = [];
Expand Down Expand Up @@ -610,7 +610,7 @@ export class FileDragAndDrop implements ITreeDragAndDrop<ExplorerItem> {
const result = await this.fileService.resolveAll(droppedResources);

// Pass focus to window
this.windowService.focusWindow();
this.hostService.focus();

// Handle folders by adding to workspace if we are in workspace context
const folders = result.filter(r => r.success && r.stat && r.stat.isDirectory).map(result => ({ uri: result.stat!.resource }));
Expand Down
17 changes: 10 additions & 7 deletions src/vs/workbench/electron-browser/actions/windowActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ICommandHandler } from 'vs/platform/commands/common/commands';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IElectronService } from 'vs/platform/electron/node/electron';
import { IHostService } from 'vs/workbench/services/host/browser/host';

export class CloseCurrentWindowAction extends Action {

Expand Down Expand Up @@ -173,10 +174,10 @@ export abstract class BaseSwitchWindow extends Action {
private keybindingService: IKeybindingService,
private modelService: IModelService,
private modeService: IModeService,
private electronService: IElectronService
private electronService: IElectronService,
private hostService: IHostService
) {
super(id, label);

}

protected abstract isQuickNavigate(): boolean;
Expand Down Expand Up @@ -211,7 +212,7 @@ export abstract class BaseSwitchWindow extends Action {
});

if (pick) {
this.windowsService.focusWindow(pick.payload);
this.hostService.focus();
}
}
}
Expand All @@ -230,9 +231,10 @@ export class SwitchWindow extends BaseSwitchWindow {
@IKeybindingService keybindingService: IKeybindingService,
@IModelService modelService: IModelService,
@IModeService modeService: IModeService,
@IElectronService electronService: IElectronService
@IElectronService electronService: IElectronService,
@IHostService hostService: IHostService
) {
super(id, label, windowsService, windowService, quickInputService, keybindingService, modelService, modeService, electronService);
super(id, label, windowsService, windowService, quickInputService, keybindingService, modelService, modeService, electronService, hostService);
}

protected isQuickNavigate(): boolean {
Expand All @@ -254,9 +256,10 @@ export class QuickSwitchWindow extends BaseSwitchWindow {
@IKeybindingService keybindingService: IKeybindingService,
@IModelService modelService: IModelService,
@IModeService modeService: IModeService,
@IElectronService electronService: IElectronService
@IElectronService electronService: IElectronService,
@IHostService hostService: IHostService
) {
super(id, label, windowsService, windowService, quickInputService, keybindingService, modelService, modeService, electronService);
super(id, label, windowsService, windowService, quickInputService, keybindingService, modelService, modeService, electronService, hostService);
}

protected isQuickNavigate(): boolean {
Expand Down
4 changes: 4 additions & 0 deletions src/vs/workbench/services/host/browser/browserHostService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export class BrowserHostService implements IHostService {
}
}

async focus(): Promise<void> {
window.focus();
}

//#endregion

async restart(): Promise<void> {
Expand Down
5 changes: 5 additions & 0 deletions src/vs/workbench/services/host/browser/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export interface IHostService {
*/
toggleFullScreen(): Promise<void>;

/**
* Attempt to bring the window to the foreground and focus it.
*/
focus(): Promise<void>;

//#endregion

//#region Lifecycle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export class DesktopHostService implements IHostService {
return this.electronService.toggleFullScreen();
}

focus(): Promise<void> {
return this.electronService.focusWindow();
}

//#endregion

restart(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ export class WindowService extends Disposable implements IWindowService {
return this.windowsService.removeFromRecentlyOpened(paths);
}

focusWindow(): Promise<void> {
return this.windowsService.focusWindow(this.windowId);
}

isFocused(): Promise<boolean> {
return this.windowsService.isFocused(this.windowId);
}
Expand Down
10 changes: 2 additions & 8 deletions src/vs/workbench/test/workbenchTestServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1207,10 +1207,6 @@ export class TestWindowService implements IWindowService {
return Promise.resolve();
}

focusWindow(): Promise<void> {
return Promise.resolve();
}

openWindow(_uris: IURIToOpen[], _options?: IOpenSettings): Promise<void> {
return Promise.resolve();
}
Expand Down Expand Up @@ -1289,10 +1285,6 @@ export class TestWindowsService implements IWindowsService {
});
}

focusWindow(_windowId: number): Promise<void> {
return Promise.resolve();
}

// Global methods
openWindow(_windowId: number, _uris: IURIToOpen[], _options: IOpenSettings): Promise<void> {
return Promise.resolve();
Expand Down Expand Up @@ -1401,6 +1393,8 @@ export class TestHostService implements IHostService {
async reload(): Promise<void> { }
async closeWorkspace(): Promise<void> { }

async focus(): Promise<void> { }

async openEmptyWindow(options?: { reuse?: boolean, remoteAuthority?: string }): Promise<void> { }

async toggleFullScreen(): Promise<void> { }
Expand Down

0 comments on commit e096ce1

Please sign in to comment.