Skip to content

Commit

Permalink
added title contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
jbicker committed Dec 6, 2024
1 parent 91c816e commit ebd823e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
3 changes: 3 additions & 0 deletions packages/core/src/browser/window/browser-window-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ import { ClipboardService } from '../clipboard-service';
import { BrowserClipboardService } from '../browser-clipboard-service';
import { SecondaryWindowService } from './secondary-window-service';
import { DefaultSecondaryWindowService } from './default-secondary-window-service';
import { bindContributionProvider } from '../../common';
import { WindowTitleContribution } from './window-title-service';

export default new ContainerModule(bind => {
bind(DefaultWindowService).toSelf().inSingletonScope();
bind(WindowService).toService(DefaultWindowService);
bind(FrontendApplicationContribution).toService(DefaultWindowService);
bind(ClipboardService).to(BrowserClipboardService).inSingletonScope();
bind(SecondaryWindowService).to(DefaultSecondaryWindowService).inSingletonScope();
bindContributionProvider(bind, WindowTitleContribution);
});
18 changes: 13 additions & 5 deletions packages/core/src/browser/window/window-title-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { inject, injectable, postConstruct } from 'inversify';
import { inject, injectable, named, postConstruct } from 'inversify';
import { escapeRegExpCharacters } from '../../common/strings';
import { Emitter, Event } from '../../common/event';
import { CorePreferences } from '../core-preferences';
import { FrontendApplicationConfigProvider } from '../frontend-application-config-provider';
import { ContributionProvider } from '../../common';

export const WindowTitleContribution = Symbol('WindowTitleAddOnContribution');
export interface WindowTitleContribution {
enhanceTitle(title: string, parts: Map<string, string | undefined>): string;
}

export const InitialWindowTitleParts = {
activeEditorShort: undefined,
Expand All @@ -43,6 +49,8 @@ export class WindowTitleService {
@inject(CorePreferences)
protected readonly preferences: CorePreferences;

@inject(ContributionProvider) @named(WindowTitleContribution) protected readonly titleContributions: ContributionProvider<WindowTitleContribution>;

protected _title = '';
protected titleTemplate?: string;

Expand Down Expand Up @@ -95,15 +103,15 @@ export class WindowTitleService {
}
const separatedTitle = title.split('${separator}').filter(e => e.trim().length > 0);
this._title = separatedTitle.join(this.separator);
const contributions = this.titleContributions.getContributions();
for (const contribution of contributions) {
this._title = contribution.enhanceTitle(this.title, this.titleParts);
}
}
const developmentHost = this.titleParts.get('developmentHost');
if (developmentHost) {
this._title = developmentHost + this.separator + this._title;
}
const devContainer = this.titleParts.get('devContainer');
if (devContainer) {
this._title = `${this._title} [${devContainer}]`;
}
document.title = this._title || FrontendApplicationConfigProvider.get().applicationName;
this.onDidChangeTitleEmitter.fire(this._title);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { bindWindowPreferences } from './electron-window-preferences';
import { ElectronWindowService } from './electron-window-service';
import { ExternalAppOpenHandler } from './external-app-open-handler';
import { ElectronUriHandlerContribution } from '../electron-uri-handler';
import { bindContributionProvider } from '../../common';
import { WindowTitleContribution } from '../../browser/window/window-title-service';

export default new ContainerModule((bind, unbind, isBound, rebind) => {
bind(ElectronMainWindowService).toDynamicValue(context =>
Expand All @@ -45,4 +47,5 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
bind(SecondaryWindowService).to(ElectronSecondaryWindowService).inSingletonScope();
bind(ExternalAppOpenHandler).toSelf().inSingletonScope();
bind(OpenHandler).toService(ExternalAppOpenHandler);
bindContributionProvider(bind, WindowTitleContribution);
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,30 @@

import { inject, injectable } from '@theia/core/shared/inversify';
import { FrontendApplicationContribution } from '@theia/core/lib/browser';
import type { ContainerInspectInfo } from 'dockerode';
import { RemoteContainerConnectionProvider } from '../electron-common/remote-container-connection-provider';
import { PortForwardingService } from '@theia/remote/lib/electron-browser/port-forwarding/port-forwarding-service';
import { getCurrentPort } from '@theia/core/lib/electron-browser/messaging/electron-local-ws-connection-source';
import { WindowTitleService } from '@theia/core/lib/browser/window/window-title-service';
import { WindowTitleContribution } from '@theia/core/lib/browser/window/window-title-service';
import { RemoteStatus, RemoteStatusService } from '@theia/remote/lib/electron-common/remote-status-service';

@injectable()
export class ContainerInfoContribution implements FrontendApplicationContribution {

export class ContainerInfoContribution implements FrontendApplicationContribution, WindowTitleContribution {
@inject(RemoteContainerConnectionProvider)
protected readonly connectionProvider: RemoteContainerConnectionProvider;

@inject(PortForwardingService)
protected readonly portForwardingService: PortForwardingService;

@inject(WindowTitleService)
protected readonly windowTitleService: WindowTitleService;

containerInfo: ContainerInspectInfo | undefined;
@inject(RemoteStatusService)
protected readonly remoteStatusService: RemoteStatusService;

initialize(): void {
this.connectionProvider.getCurrentContainerInfo(parseInt(new URLSearchParams(location.search).get('port') ?? '0')).then(info => {
if (info) {
this.containerInfo = info;
this.windowTitleService.update({
devContainer: `Dev Container @ ${info?.Platform ?? ''}`
});
}
});
}
protected status: RemoteStatus | undefined;

async onStart(): Promise<void> {
this.portForwardingService.forwardedPorts = Object.entries(this.containerInfo?.NetworkSettings.Ports ?? {}).flatMap(([_, ports]) => (
const containerPort = parseInt(new URLSearchParams(location.search).get('port') ?? '0');
const containerInfo = await this.connectionProvider.getCurrentContainerInfo(containerPort);
this.status = await this.remoteStatusService.getStatus(containerPort);

this.portForwardingService.forwardedPorts = Object.entries(containerInfo?.NetworkSettings.Ports ?? {}).flatMap(([_, ports]) => (
ports.map(port => ({
editing: false,
address: port.HostIp ?? '',
Expand All @@ -57,4 +48,12 @@ export class ContainerInfoContribution implements FrontendApplicationContributio
}))));
}

enhanceTitle(title: string, parts: Map<string, string | undefined>): string {
if (this.status && this.status.alive) {
const devcontainerName = this.status.name;
title = `${title} [Dev Container${devcontainerName ? ': ' + devcontainerName : ''}]`;
}
return title;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ContainerOutputProvider } from './container-output-provider';
import { ContainerInfoContribution } from './container-info-contribution';
import { FrontendApplicationContribution } from '@theia/core/lib/browser';
import { WorkspaceOpenHandlerContribution } from '@theia/workspace/lib/browser/workspace-service';
import { WindowTitleContribution } from '@theia/core/lib/browser/window/window-title-service';

export default new ContainerModule(bind => {
bind(ContainerConnectionContribution).toSelf().inSingletonScope();
Expand All @@ -37,4 +38,5 @@ export default new ContainerModule(bind => {

bind(ContainerInfoContribution).toSelf().inSingletonScope();
bind(FrontendApplicationContribution).toService(ContainerInfoContribution);
bind(WindowTitleContribution).toService(ContainerInfoContribution);
});

0 comments on commit ebd823e

Please sign in to comment.