titlebarPart.ts 2.5 KB
Newer Older
B
Benjamin Pasero 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import 'vs/css!./media/titlebarpart';
import { Builder, $, Dimension } from 'vs/base/browser/builder';
10
import DOM = require('vs/base/browser/dom');
B
Benjamin Pasero 已提交
11 12 13
import { Part } from 'vs/workbench/browser/part';
import { ITitleService } from 'vs/workbench/services/title/common/titleService';
import { getZoomFactor } from 'vs/base/browser/browser';
14 15
import { IWindowService } from 'vs/platform/windows/common/windows';
import errors = require('vs/base/common/errors');
B
Benjamin Pasero 已提交
16 17 18 19 20 21 22 23 24 25

export class TitlebarPart extends Part implements ITitleService {

	public _serviceBrand: any;

	private titleContainer: Builder;
	private title: Builder;
	private pendingTitle: string;
	private initialTitleFontSize: number;

26 27 28 29 30 31 32
	constructor(
		id: string,
		@IWindowService private windowService: IWindowService
	) {
		super(id);
	}

B
Benjamin Pasero 已提交
33 34 35 36 37 38 39 40 41
	public createContentArea(parent: Builder): Builder {
		this.titleContainer = $(parent);

		// Title
		this.title = $(this.titleContainer).div({ class: 'window-title' });
		if (this.pendingTitle) {
			this.title.text(this.pendingTitle);
		}

42 43 44 45 46 47 48
		// Maximize/Restore on doubleclick
		this.titleContainer.on(DOM.EventType.DBLCLICK, (e) => {
			DOM.EventHelper.stop(e);

			this.onTitleDoubleclick();
		});

B
Benjamin Pasero 已提交
49 50 51
		return this.titleContainer;
	}

52 53 54 55 56 57 58 59 60 61
	private onTitleDoubleclick(): void {
		this.windowService.isMaximized().then(maximized => {
			if (maximized) {
				this.windowService.unmaximizeWindow().done(null, errors.onUnexpectedError);
			} else {
				this.windowService.maximizeWindow().done(null, errors.onUnexpectedError);
			}
		}, errors.onUnexpectedError);
	}

B
Benjamin Pasero 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	public updateTitle(title: string): void {

		// Always set the native window title to identify us properly to the OS
		window.document.title = title;

		// Apply if we can
		if (this.title) {
			this.title.text(title);
		} else {
			this.pendingTitle = title;
		}
	}

	public layout(dimension: Dimension): Dimension[] {

		// To prevent zooming we need to adjust the font size with the zoom factor
		if (typeof this.initialTitleFontSize !== 'number') {
			this.initialTitleFontSize = parseInt(this.titleContainer.getComputedStyle().fontSize, 10);
		}
		this.titleContainer.style({ fontSize: `${this.initialTitleFontSize / getZoomFactor()}px` });

		return super.layout(dimension);
	}
}