provide way to adjust table column size via keyboard (#225081)

This commit is contained in:
Megan Rogge 2024-08-11 08:35:27 -07:00 коммит произвёл GitHub
Родитель 4b78857fd9
Коммит 7c2663f1a7
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
5 изменённых файлов: 108 добавлений и 1 удалений

Просмотреть файл

@ -565,6 +565,10 @@
{
"name": "vs/workbench/contrib/replNotebook",
"project": "vscode-workbench"
},
{
"name": "vs/workbench/contrib/list",
"project": "vscode-workbench"
}
]
}

Просмотреть файл

@ -193,7 +193,7 @@ export class Table<TRow> implements ISpliceable<TRow>, IDisposable {
user: string,
container: HTMLElement,
private virtualDelegate: ITableVirtualDelegate<TRow>,
columns: ITableColumn<TRow, TCell>[],
private columns: ITableColumn<TRow, TCell>[],
renderers: ITableRenderer<TCell, unknown>[],
_options?: ITableOptions<TRow>
) {
@ -231,6 +231,15 @@ export class Table<TRow> implements ISpliceable<TRow>, 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);
}

Просмотреть файл

@ -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);

Просмотреть файл

@ -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<void> {
const listService = accessor.get(IListService);
const instantiationService = accessor.get(IInstantiationService);
const list = listService.lastFocusedList;
if (list instanceof Table) {
await instantiationService.createInstance(TableColumnResizeQuickPick, list).show();
}
}
}

Просмотреть файл

@ -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<any>,
@IQuickInputService private readonly _quickInputService: IQuickInputService,
) {
super();
}
async show(): Promise<void> {
const items: IColumnResizeQuickPickItem[] = [];
this._table.getColumnLabels().forEach((label, index) => {
if (label) {
items.push({ label, index });
}
});
const column = await this._quickInputService.pick<IColumnResizeQuickPickItem>(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<string | { content: string; severity: Severity } | null | undefined> {
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;
}
}