diff --git a/build/lib/i18n.resources.json b/build/lib/i18n.resources.json index 25c3b74b133..fb732ae1eaf 100644 --- a/build/lib/i18n.resources.json +++ b/build/lib/i18n.resources.json @@ -565,6 +565,10 @@ { "name": "vs/workbench/contrib/replNotebook", "project": "vscode-workbench" + }, + { + "name": "vs/workbench/contrib/list", + "project": "vscode-workbench" } ] } diff --git a/src/vs/base/browser/ui/table/tableWidget.ts b/src/vs/base/browser/ui/table/tableWidget.ts index b2c8959c3c5..db00502791d 100644 --- a/src/vs/base/browser/ui/table/tableWidget.ts +++ b/src/vs/base/browser/ui/table/tableWidget.ts @@ -193,7 +193,7 @@ export class Table implements ISpliceable, IDisposable { user: string, container: HTMLElement, private virtualDelegate: ITableVirtualDelegate, - columns: ITableColumn[], + private columns: ITableColumn[], renderers: ITableRenderer[], _options?: ITableOptions ) { @@ -231,6 +231,15 @@ export class Table implements ISpliceable, IDisposable { this.style(unthemedListStyles); } + getColumnLabels(): string[] { + return this.columns.map(c => c.label); + } + + resizeColumn(index: number, percentage: number): void { + const size = Math.round((percentage / 100.00) * this.cachedWidth); + this.splitview.resizeView(index, size); + } + updateOptions(options: ITableOptionsUpdate): void { this.list.updateOptions(options); } diff --git a/src/vs/workbench/contrib/list/browser/list.contribution.ts b/src/vs/workbench/contrib/list/browser/list.contribution.ts index dffda536058..e89365852d7 100644 --- a/src/vs/workbench/contrib/list/browser/list.contribution.ts +++ b/src/vs/workbench/contrib/list/browser/list.contribution.ts @@ -5,6 +5,8 @@ import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; import { IWorkbenchContribution, WorkbenchPhase, registerWorkbenchContribution2 } from 'vs/workbench/common/contributions'; +import { registerAction2 } from 'vs/platform/actions/common/actions'; +import { ListResizeColumnAction } from 'vs/workbench/contrib/list/browser/listResizeColumnAction'; export class ListContext implements IWorkbenchContribution { @@ -21,3 +23,5 @@ export class ListContext implements IWorkbenchContribution { } registerWorkbenchContribution2(ListContext.ID, ListContext, WorkbenchPhase.BlockStartup); +registerAction2(ListResizeColumnAction); + diff --git a/src/vs/workbench/contrib/list/browser/listResizeColumnAction.ts b/src/vs/workbench/contrib/list/browser/listResizeColumnAction.ts new file mode 100644 index 00000000000..9c106b6c3c9 --- /dev/null +++ b/src/vs/workbench/contrib/list/browser/listResizeColumnAction.ts @@ -0,0 +1,34 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { TableColumnResizeQuickPick } from 'vs/workbench/contrib/list/browser/tableColumnResizeQuickPick'; +import { Table } from 'vs/base/browser/ui/table/tableWidget'; +import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; +import { IListService, WorkbenchListFocusContextKey } from 'vs/platform/list/browser/listService'; +import { Action2 } from 'vs/platform/actions/common/actions'; +import { localize } from 'vs/nls'; + +export class ListResizeColumnAction extends Action2 { + constructor() { + super({ + id: 'list.resizeColumn', + title: { value: localize('list.resizeColumn', "Resize Column"), original: 'Resize Column' }, + category: { value: localize('list', "List"), original: 'List' }, + precondition: WorkbenchListFocusContextKey, + f1: true + }); + } + + async run(accessor: ServicesAccessor): Promise { + const listService = accessor.get(IListService); + const instantiationService = accessor.get(IInstantiationService); + + const list = listService.lastFocusedList; + if (list instanceof Table) { + await instantiationService.createInstance(TableColumnResizeQuickPick, list).show(); + } + } +} + diff --git a/src/vs/workbench/contrib/list/browser/tableColumnResizeQuickPick.ts b/src/vs/workbench/contrib/list/browser/tableColumnResizeQuickPick.ts new file mode 100644 index 00000000000..370baff0f86 --- /dev/null +++ b/src/vs/workbench/contrib/list/browser/tableColumnResizeQuickPick.ts @@ -0,0 +1,56 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { Table } from 'vs/base/browser/ui/table/tableWidget'; +import { Disposable } from 'vs/base/common/lifecycle'; +import Severity from 'vs/base/common/severity'; +import { localize } from 'vs/nls'; +import { IQuickInputService, IQuickPickItem } from 'vs/platform/quickinput/common/quickInput'; + +interface IColumnResizeQuickPickItem extends IQuickPickItem { + index: number; +} + +export class TableColumnResizeQuickPick extends Disposable { + constructor( + private readonly _table: Table, + @IQuickInputService private readonly _quickInputService: IQuickInputService, + ) { + super(); + } + + async show(): Promise { + const items: IColumnResizeQuickPickItem[] = []; + this._table.getColumnLabels().forEach((label, index) => { + if (label) { + items.push({ label, index }); + } + }); + const column = await this._quickInputService.pick(items, { placeHolder: localize('table.column.selection', "Select the column to resize, type to filter.") }); + if (!column) { + return; + } + const value = await this._quickInputService.input({ + placeHolder: localize('table.column.resizeValue.placeHolder', "i.e. 20, 60, 100..."), + prompt: localize('table.column.resizeValue.prompt', "Please enter a width in percentage for the '{0}' column.", column.label), + validateInput: (input: string) => this._validateColumnResizeValue(input) + }); + const percentageValue = value ? Number.parseInt(value) : undefined; + if (!percentageValue) { + return; + } + this._table.resizeColumn(column.index, percentageValue); + } + + private async _validateColumnResizeValue(input: string): Promise { + const percentage = Number.parseInt(input); + if (input && !Number.isInteger(percentage)) { + return localize('table.column.resizeValue.invalidType', "Please enter an integer."); + } else if (percentage < 0 || percentage > 100) { + return localize('table.column.resizeValue.invalidRange', "Please enter a number greater than 0 and less than or equal to 100."); + } + return null; + } +}