Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some fixes to icon selection widget #193536

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/vs/base/browser/ui/icons/iconSelectBox.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

.icon-select-box>.icon-select-box-container {
.icon-select-box > .icon-select-box-container {
height: 100%;
}

.icon-select-box .icon-select-icons-container {
display: flex;
flex-wrap: wrap;
box-sizing: border-box;
height: 100%;
}

Expand All @@ -28,3 +25,9 @@
outline-offset: -1px;
background-color: var(--vscode-toolbar-hoverBackground);
}

.icon-select-box .icon-select-id-container .icon-select-id-label {
height: 24px;
padding: 10px;
opacity: .8;
}
87 changes: 58 additions & 29 deletions src/vs/base/browser/ui/icons/iconSelectBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

import 'vs/css!./iconSelectBox';
import * as dom from 'vs/base/browser/dom';
import { alert } from 'vs/base/browser/ui/aria/aria';
import { IInputBoxStyles, InputBox } from 'vs/base/browser/ui/inputbox/inputBox';
import { DomScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement';
import { Emitter } from 'vs/base/common/event';
import { IDisposable, DisposableStore, Disposable, MutableDisposable } from 'vs/base/common/lifecycle';
import { ThemeIcon } from 'vs/base/common/themables';
import { localize } from 'vs/nls';
import { IMatch } from 'vs/base/common/filters';
import { ScrollbarVisibility } from 'vs/base/common/scrollable';

export interface IIconSelectBoxOptions {
readonly icons: ThemeIcon[];
Expand All @@ -32,6 +34,7 @@ export class IconSelectBox extends Disposable {

private inputBox: InputBox | undefined;
private scrollableElement: DomScrollableElement | undefined;
private iconIdElement: HTMLElement | undefined;
private readonly iconContainerWidth = 36;
private readonly iconContainerHeight = 32;

Expand All @@ -58,8 +61,12 @@ export class IconSelectBox extends Disposable {

const iconsContainer = dom.$('.icon-select-icons-container');
iconsContainer.style.paddingRight = '10px';
this.scrollableElement = disposables.add(new DomScrollableElement(iconsContainer, { useShadows: false }));
this.scrollableElement = disposables.add(new DomScrollableElement(iconsContainer, {
useShadows: false,
horizontal: ScrollbarVisibility.Hidden,
}));
dom.append(iconSelectBoxContainer, this.scrollableElement.getDomNode());
this.iconIdElement = dom.append(dom.append(iconSelectBoxContainer, dom.$('.icon-select-id-container')), dom.$('.icon-select-id-label'));

const iconsDisposables = disposables.add(new MutableDisposable());
iconsDisposables.value = this.renderIcons(this.options.icons, iconsContainer);
Expand All @@ -82,29 +89,35 @@ export class IconSelectBox extends Disposable {
const focusedIcon = this.renderedIcons[this.focusedItemIndex]?.[0];
let focusedIconIndex = 0;
const renderedIcons: [ThemeIcon, HTMLElement][] = [];
for (let index = 0; index < icons.length; index++) {
const icon = icons[index];
const iconContainer = dom.append(container, dom.$('.icon-container'));
iconContainer.style.width = `${this.iconContainerWidth}px`;
iconContainer.style.height = `${this.iconContainerHeight}px`;
iconContainer.tabIndex = -1;
iconContainer.role = 'button';
iconContainer.title = icon.id;
dom.append(iconContainer, dom.$(ThemeIcon.asCSSSelector(icon)));
renderedIcons.push([icon, iconContainer]);

disposables.add(dom.addDisposableListener(iconContainer, dom.EventType.CLICK, (e: MouseEvent) => {
e.stopPropagation();
this.setSelection(index);
}));

disposables.add(dom.addDisposableListener(iconContainer, dom.EventType.MOUSE_OVER, (e: MouseEvent) => {
this.focusIcon(index);
}));

if (icon === focusedIcon) {
focusedIconIndex = index;
if (icons.length) {
for (let index = 0; index < icons.length; index++) {
const icon = icons[index];
const iconContainer = dom.append(container, dom.$('.icon-container'));
iconContainer.style.width = `${this.iconContainerWidth}px`;
iconContainer.style.height = `${this.iconContainerHeight}px`;
iconContainer.tabIndex = -1;
iconContainer.role = 'button';
iconContainer.title = icon.id;
dom.append(iconContainer, dom.$(ThemeIcon.asCSSSelector(icon)));
renderedIcons.push([icon, iconContainer]);

disposables.add(dom.addDisposableListener(iconContainer, dom.EventType.CLICK, (e: MouseEvent) => {
e.stopPropagation();
this.setSelection(index);
}));

disposables.add(dom.addDisposableListener(iconContainer, dom.EventType.MOUSE_OVER, (e: MouseEvent) => {
this.focusIcon(index);
}));

if (icon === focusedIcon) {
focusedIconIndex = index;
}
}
} else {
const noResults = localize('iconSelect.noResults', "No results");
dom.append(container, dom.$('.icon-no-results', undefined, noResults));
alert(noResults);
}

this.renderedIcons.splice(0, this.renderedIcons.length, ...renderedIcons);
Expand All @@ -125,6 +138,10 @@ export class IconSelectBox extends Disposable {
icon.classList.add('focused');
}

if (this.iconIdElement) {
this.iconIdElement.textContent = this.renderedIcons[index]?.[0].id;
}

this.reveal(index);
}

Expand Down Expand Up @@ -159,10 +176,6 @@ export class IconSelectBox extends Disposable {
layout(dimension: dom.Dimension): void {
this.domNode.style.width = `${dimension.width}px`;
this.domNode.style.height = `${dimension.height}px`;
if (this.scrollableElement) {
this.scrollableElement.getDomNode().style.height = `${dimension.height - 46}px`;
this.scrollableElement.scanDomNode();
}

const iconsContainerWidth = dimension.width - 40;
this.numberOfElementsPerRow = Math.floor(iconsContainerWidth / this.iconContainerWidth);
Expand All @@ -175,6 +188,11 @@ export class IconSelectBox extends Disposable {
for (const [, icon] of this.renderedIcons) {
icon.style.marginRight = `${margin}px`;
}

if (this.scrollableElement) {
this.scrollableElement.getDomNode().style.height = `${dimension.height - 80}px`;
this.scrollableElement.scanDomNode();
}
}

getFocus(): number[] {
Expand Down Expand Up @@ -203,11 +221,22 @@ export class IconSelectBox extends Disposable {
}

focusNextRow(): void {
this.focusIcon((this.focusedItemIndex + this.numberOfElementsPerRow) % this.renderedIcons.length);
let nextRowIndex = this.focusedItemIndex + this.numberOfElementsPerRow;
if (nextRowIndex >= this.renderedIcons.length) {
nextRowIndex = (nextRowIndex % this.numberOfElementsPerRow) + 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
nextRowIndex = (nextRowIndex % this.numberOfElementsPerRow) + 1;
nextRowIndex = (nextRowIndex + 1) % this.numberOfElementsPerRow;

Then, I don't think the next line is needed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Thanks

nextRowIndex = nextRowIndex === this.numberOfElementsPerRow ? 0 : nextRowIndex;
}
this.focusIcon(nextRowIndex);
}

focusPreviousRow(): void {
this.focusIcon((this.focusedItemIndex - this.numberOfElementsPerRow + this.renderedIcons.length) % this.renderedIcons.length);
let previousRowIndex = this.focusedItemIndex - this.numberOfElementsPerRow;
if (previousRowIndex < 0) {
const numberOfRows = Math.floor(this.renderedIcons.length / this.numberOfElementsPerRow);
previousRowIndex = this.focusedItemIndex + (this.numberOfElementsPerRow * numberOfRows) - 1;
previousRowIndex = previousRowIndex >= this.renderedIcons.length ? previousRowIndex - this.numberOfElementsPerRow : previousRowIndex;
}
this.focusIcon(previousRowIndex);
}

getFocusedIcon(): ThemeIcon {
Expand Down