Skip to content

Commit

Permalink
Color selector swatches misdrawn at 200% scale eclipse-platform#261
Browse files Browse the repository at this point in the history
Use a ImageDataProvider to provide the image data at the correct scale
based on the current zoom given to us by SWT.

Also provides a JFace example snippet for this widget because we didn't
have one.

Fixes eclipse-platform#261

Signed-off-by: Mat Booth <mat.booth@gmail.com>
  • Loading branch information
mbooth101 authored and sravanlakkimsetti committed Oct 19, 2022
1 parent 1cda433 commit f6c0a6f
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2015 IBM Corporation and others.
* Copyright (c) 2000, 2022 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -26,6 +26,8 @@
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageDataProvider;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Button;
Expand All @@ -36,7 +38,8 @@

/**
* The <code>ColorSelector</code> is a wrapper for a button that displays a
* selected <code>Color</code> and allows the user to change the selection.
* swatch of the selected color and allows the user to change the selection
* using the operating system's native color chooser dialog.
*/
public class ColorSelector extends EventManager {
/**
Expand Down Expand Up @@ -65,12 +68,7 @@ public class ColorSelector extends EventManager {
public ColorSelector(Composite parent) {
fButton = new Button(parent, SWT.PUSH);
fExtent = computeImageSize(parent);
fImage = new Image(parent.getDisplay(), fExtent.x, fExtent.y);
GC gc = new GC(fImage);
gc.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BORDER));
gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
gc.dispose();
fButton.setImage(fImage);
updateColorImage();
fButton.addSelectionListener(widgetSelectedAdapter(event -> open()));
fButton.addDisposeListener(event -> {
if (fImage != null) {
Expand Down Expand Up @@ -173,12 +171,34 @@ public void setEnabled(boolean state) {
* setting.
*/
protected void updateColorImage() {
Display display = fButton.getDisplay();
GC gc = new GC(fImage);
Color color = new Color(display, fColorValue);
gc.setBackground(color);
gc.fillRectangle(1, 1, fExtent.x - 2, fExtent.y - 2);
gc.dispose();
if (fImage != null) {
fImage.dispose();
}

final Display display = fButton.getDisplay();

fImage = new Image(display, new ImageDataProvider() {

@Override
public ImageData getImageData(int zoom) {
Image image = new Image(display, fExtent.x, fExtent.y);
GC gc = new GC(image);

RGB color = getColorValue();
gc.setForeground(display.getSystemColor(SWT.COLOR_WIDGET_BORDER));
if (color != null) {
gc.setBackground(new Color(display, color));
gc.fillRectangle(image.getBounds());
}
gc.setLineWidth(2);
gc.drawRectangle(image.getBounds());
gc.dispose();

ImageData data = image.getImageData(zoom);
image.dispose();
return data;
}
});
fButton.setImage(fImage);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*******************************************************************************
* Copyright (c) 2022 Mat Booth and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.jface.snippets.dialogs;

import org.eclipse.jface.preference.ColorSelector;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
* Demo of the JFace {@link ColorSelector} which is compound widget comprising a
* color swatch button and the native color chooser dialog.
*/
public class Snippet082ColorSelectDialog {

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(2, false));
shell.setText("Color Select Dialog Demo");
shell.setSize(320, 240);
shell.setBackgroundMode(SWT.INHERIT_DEFAULT);

Label bgLabel = new Label(shell, SWT.NONE);
bgLabel.setText("Background Color:");
ColorSelector bgColorSelector = new ColorSelector(shell);
bgColorSelector.setColorValue(shell.getBackground().getRGB());
Button bgColorButton = bgColorSelector.getButton();

Label fgLabel = new Label(shell, SWT.NONE);
fgLabel.setText("Foreground Color:");
ColorSelector fgColorSelector = new ColorSelector(shell);
fgColorSelector.setColorValue(shell.getForeground().getRGB());
Button fgColorButton = fgColorSelector.getButton();

bgColorButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
shell.setBackground(new Color(bgColorSelector.getColorValue()));
}));

fgColorButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
Color color = new Color(fgColorSelector.getColorValue());
shell.setForeground(color);
// On Windows 10, foreground color is not automatically inherited by default
bgLabel.setForeground(color);
fgLabel.setForeground(color);
}));

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}

display.dispose();
}
}

0 comments on commit f6c0a6f

Please sign in to comment.