Skip to content

Commit

Permalink
Support disabled state of slider
Browse files Browse the repository at this point in the history
Currently one can make the slider disabled but it then shows as before
and one can simply no longer move it, so the user is unable to see that
this control is different.

This now adds a new disabled state where all colours are simply
converted to gray.
  • Loading branch information
laeubi committed Mar 18, 2024
1 parent 476fe0f commit 35d8f16
Showing 1 changed file with 57 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
Expand Down Expand Up @@ -114,16 +115,16 @@ private void paintControl(final GC gc) {
// Compute xPosition
xPosition = computeXPosition();
}

drawBar(gc);
drawSelectionPart(gc);
drawSelector(gc);
boolean enabled = isEnabled();
drawBar(gc, enabled);
drawSelectionPart(gc, enabled);
drawSelector(gc, enabled);
}

private void drawBar(final GC gc) {
private void drawBar(final GC gc, boolean enabled) {
final Rectangle rect = getClientArea();
gc.setForeground(renderer.getBarBorderColor());
gc.setBackground(renderer.getBarInsideColor());
gc.setForeground(getColor(renderer.getBarBorderColor(), enabled));
gc.setBackground(getColor(renderer.getBarInsideColor(), enabled));

final int hMargin = renderer.getHorizontalMargin();
final int selectorWidth = renderer.getSelectorWidth();
Expand All @@ -137,10 +138,10 @@ private void drawBar(final GC gc) {
gc.drawRoundRectangle(x, y, width, barHeight, barHeight, barHeight);
}

private void drawSelectionPart(final GC gc) {
private void drawSelectionPart(final GC gc, boolean enabled) {
final Rectangle rect = getClientArea();
gc.setForeground(renderer.getBarBorderColor());
gc.setBackground(renderer.getBarSelectionColor());
gc.setForeground(getColor(renderer.getBarBorderColor(), enabled));
gc.setBackground(getColor(renderer.getBarSelectionColor(), enabled));

final int barHeight = renderer.getBarHeight();

Expand All @@ -158,10 +159,10 @@ private int computeXPosition() {
return position;
}

private void drawSelector(final GC gc) {
private void drawSelector(final GC gc, boolean enabled) {
final Rectangle rect = getClientArea();
gc.setForeground(renderer.getSelectorColorBorder());
gc.setBackground(renderer.getSelectorColor());
gc.setForeground(getColor(renderer.getSelectorColorBorder(), enabled));
gc.setBackground(getColor(renderer.getSelectorColor(), enabled));

final int hMargin = renderer.getHorizontalMargin();

Expand All @@ -175,7 +176,7 @@ private void drawSelector(final GC gc) {
gc.drawRoundRectangle(hMargin + xPosition, y, selectorWidth, selectorHeight, selectorHeight, selectorHeight);

// Draw the arrows
gc.setForeground(renderer.getArrowColor());
gc.setForeground(getColor(renderer.getArrowColor(), enabled));
gc.setLineWidth(renderer.getArrowLineWidth());
final int baseY = y + selectorHeight / 2;
gc.drawLine(hMargin + xPosition + 10, baseY, hMargin + xPosition + 17, baseY - 7);
Expand All @@ -185,7 +186,7 @@ private void drawSelector(final GC gc) {
gc.drawLine(hMargin + xPosition + selectorWidth - 10, baseY, hMargin + xPosition + selectorWidth - 17, baseY + 7);

// And the value
gc.setForeground(renderer.getSelectorTextColor());
gc.setForeground(getColor(renderer.getSelectorTextColor(), enabled));
gc.setFont(renderer.getTextFont());
final String valueAsString = stringValueOf(value);
final Point textSize = gc.textExtent(valueAsString);
Expand All @@ -196,6 +197,35 @@ private void drawSelector(final GC gc) {
gc.drawText(valueAsString, xText - textSize.x / 2, yText - textSize.y / 2, true);
}

@Override
public void setBounds(int x, int y, int width, int height) {
xPosition = -1;
super.setBounds(x, y, width, height);
}

@Override
public void setEnabled(boolean enabled) {
if(!enabled && moving) {
moving = false;
}
super.setEnabled(enabled);
}

private Color getColor(Color color, boolean enabled) {
if(enabled) {
return color;
}
// see https://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
if(red == green && green == blue) {
return color;
}
int g = (int)(0.299 * red + 0.587 * green + 0.114 * blue);
return new Color(g, g, g);
}

private String stringValueOf(int value) {
if(format != null) {
return format.apply(value);
Expand All @@ -222,17 +252,19 @@ public IntFunction<String> getLabelFormatProvider() {
private void addMouseListeners() {

addListener(SWT.MouseDown, e -> {
final int selectorWidth = renderer.getSelectorWidth();
final int selectorHeight = renderer.getSelectorHeight();

final int y = (getClientArea().height - selectorHeight) / 2;
final Rectangle rect = new Rectangle(xPosition + renderer.getHorizontalMargin(), y, selectorWidth, selectorHeight);
if (!rect.contains(e.x, e.y)) {
return;
if(isEnabled()) {
final int selectorWidth = renderer.getSelectorWidth();
final int selectorHeight = renderer.getSelectorHeight();

final int y = (getClientArea().height - selectorHeight) / 2;
final Rectangle rect = new Rectangle(xPosition + renderer.getHorizontalMargin(), y, selectorWidth, selectorHeight);
if (!rect.contains(e.x, e.y)) {
return;
}
moving = true;
movingValue = value;
mouseDeltaX = xPosition - e.x;
}
moving = true;
movingValue = value;
mouseDeltaX = xPosition - e.x;
});

addListener(SWT.MouseUp, e -> {
Expand Down

0 comments on commit 35d8f16

Please sign in to comment.