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

Adding settings to control color decorator behavior #184079

Merged
merged 5 commits into from
Jun 2, 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
15 changes: 14 additions & 1 deletion src/vs/editor/common/config/editorOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ export interface IEditorOptions {
* Enable inline color decorators and color picker rendering.
*/
colorDecorators?: boolean;
/**
* Controls what is the condition to spawn a color picker from a color dectorator
*/
colorDecoratorsActivatedOn?: 'clickAndHover' | 'click' | 'hover';
/**
* Controls the max number of color decorators that can be rendered in an editor at once.
*/
Expand Down Expand Up @@ -5062,7 +5066,8 @@ export const enum EditorOption {
tabFocusMode,
layoutInfo,
wrappingInfo,
defaultColorDecorators
defaultColorDecorators,
colorDecoratorsActivatedOn
}

export const EditorOptions = {
Expand Down Expand Up @@ -5211,6 +5216,14 @@ export const EditorOptions = {
EditorOption.colorDecorators, 'colorDecorators', true,
{ description: nls.localize('colorDecorators', "Controls whether the editor should render the inline color decorators and color picker.") }
)),
colorDecoratorActivatedOn: register(new EditorStringEnumOption(EditorOption.colorDecoratorsActivatedOn, 'colorDecoratorsActivatedOn', 'clickAndHover' as 'clickAndHover' | 'hover' | 'click', ['clickAndHover', 'hover', 'click'] as const, {
enumDescriptions: [
nls.localize('editor.colorDecoratorActivatedOn.clickAndHover', "Make the color picker appear both on click and hover of the color decorator"),
nls.localize('editor.colorDecoratorActivatedOn.hover', "Make the color picker appear on hover of the color decorator"),
nls.localize('editor.colorDecoratorActivatedOn.click', "Make the color picker appear on click of the color decorator")
],
description: nls.localize('colorDecoratorActivatedOn', "Controls the condition to make a color picker appear from a color decorator")
})),
colorDecoratorsLimit: register(new EditorIntOption(
EditorOption.colorDecoratorsLimit, 'colorDecoratorsLimit', 500, 1, 1000000,
{
Expand Down
3 changes: 2 additions & 1 deletion src/vs/editor/common/standalone/standaloneEnums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ export enum EditorOption {
tabFocusMode = 139,
layoutInfo = 140,
wrappingInfo = 141,
defaultColorDecorators = 142
defaultColorDecorators = 142,
colorDecoratorsActivatedOn = 143
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { Disposable } from 'vs/base/common/lifecycle';
import { ICodeEditor, IEditorMouseEvent, MouseTargetType } from 'vs/editor/browser/editorBrowser';
import { EditorContributionInstantiation, registerEditorContribution } from 'vs/editor/browser/editorExtensions';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { Range } from 'vs/editor/common/core/range';
import { IEditorContribution } from 'vs/editor/common/editorCommon';
import { ColorDecorationInjectedTextMarker } from 'vs/editor/contrib/colorPicker/browser/colorDetector';
Expand All @@ -31,6 +32,12 @@ export class ColorContribution extends Disposable implements IEditorContribution
}

private onMouseDown(mouseEvent: IEditorMouseEvent) {

const colorDecoratorsActivatedOn = this._editor.getOption(EditorOption.colorDecoratorsActivatedOn);
if (colorDecoratorsActivatedOn !== 'click' && colorDecoratorsActivatedOn !== 'clickAndHover') {
return;
}

const target = mouseEvent.target;

if (target.type !== MouseTargetType.CONTENT_TEXT) {
Expand All @@ -55,7 +62,7 @@ export class ColorContribution extends Disposable implements IEditorContribution
}
if (!hoverController.isColorPickerVisible()) {
const range = new Range(target.range.startLineNumber, target.range.startColumn + 1, target.range.endLineNumber, target.range.endColumn + 1);
hoverController.showContentHover(range, HoverStartMode.Immediate, HoverStartSource.Mouse, false);
hoverController.showContentHover(range, HoverStartMode.Immediate, HoverStartSource.Mouse, false, true);
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions src/vs/editor/contrib/hover/browser/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class ModesHoverController implements IEditorContribution {
private _hoverClicked: boolean;
private _isHoverEnabled!: boolean;
private _isHoverSticky!: boolean;
private _hoverActivatedByColorDecoratorClick: boolean = false;

static get(editor: ICodeEditor): ModesHoverController | null {
return editor.getContribution<ModesHoverController>(ModesHoverController.ID);
Expand Down Expand Up @@ -175,7 +176,15 @@ export class ModesHoverController implements IEditorContribution {
return;
}

if (!this._isHoverEnabled) {
const mouseOnDecorator = target.element?.classList.contains('colorpicker-color-decoration');
const decoratorActivatedOn = this._editor.getOption(EditorOption.colorDecoratorsActivatedOn);

if ((mouseOnDecorator && (
(decoratorActivatedOn === 'click' && !this._hoverActivatedByColorDecoratorClick) ||
(decoratorActivatedOn === 'hover' && !this._isHoverEnabled) ||
(decoratorActivatedOn === 'clickAndHover' && !this._isHoverEnabled && !this._hoverActivatedByColorDecoratorClick)))
|| !mouseOnDecorator && !this._isHoverEnabled && !this._hoverActivatedByColorDecoratorClick
) {
this._hideWidgets();
return;
}
Expand Down Expand Up @@ -219,7 +228,7 @@ export class ModesHoverController implements IEditorContribution {
if ((this._isMouseDown && this._hoverClicked && this._contentWidget?.isColorPickerVisible()) || InlineSuggestionHintsContentWidget.dropDownVisible) {
return;
}

this._hoverActivatedByColorDecoratorClick = false;
this._hoverClicked = false;
this._glyphWidget?.hide();
this._contentWidget?.hide();
Expand All @@ -236,7 +245,8 @@ export class ModesHoverController implements IEditorContribution {
return this._contentWidget?.isColorPickerVisible() || false;
}

public showContentHover(range: Range, mode: HoverStartMode, source: HoverStartSource, focus: boolean): void {
public showContentHover(range: Range, mode: HoverStartMode, source: HoverStartSource, focus: boolean, activatedByColorDecoratorClick: boolean = false): void {
this._hoverActivatedByColorDecoratorClick = activatedByColorDecoratorClick;
this._getOrCreateContentWidget().startShowingAtRange(range, mode, source, focus);
}

Expand Down
8 changes: 7 additions & 1 deletion src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3434,6 +3434,10 @@ declare namespace monaco.editor {
* Enable inline color decorators and color picker rendering.
*/
colorDecorators?: boolean;
/**
* Controls what is the condition to spawn a color picker from a color dectorator
*/
colorDecoratorsActivatedOn?: 'clickAndHover' | 'click' | 'hover';
/**
* Controls the max number of color decorators that can be rendered in an editor at once.
*/
Expand Down Expand Up @@ -4905,7 +4909,8 @@ declare namespace monaco.editor {
tabFocusMode = 139,
layoutInfo = 140,
wrappingInfo = 141,
defaultColorDecorators = 142
defaultColorDecorators = 142,
colorDecoratorsActivatedOn = 143
}

export const EditorOptions: {
Expand All @@ -4929,6 +4934,7 @@ declare namespace monaco.editor {
codeLensFontFamily: IEditorOption<EditorOption.codeLensFontFamily, string>;
codeLensFontSize: IEditorOption<EditorOption.codeLensFontSize, number>;
colorDecorators: IEditorOption<EditorOption.colorDecorators, boolean>;
colorDecoratorActivatedOn: IEditorOption<EditorOption.colorDecoratorsActivatedOn, 'clickAndHover' | 'click' | 'hover'>;
colorDecoratorsLimit: IEditorOption<EditorOption.colorDecoratorsLimit, number>;
columnSelection: IEditorOption<EditorOption.columnSelection, boolean>;
comments: IEditorOption<EditorOption.comments, Readonly<Required<IEditorCommentsOptions>>>;
Expand Down