From 35d8f164033fe81d8ffddfe30b5401614a69e5f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Mon, 18 Mar 2024 17:13:44 +0100 Subject: [PATCH] Support disabled state of slider 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. --- .../opal/nebulaslider/NebulaSlider.java | 82 +++++++++++++------ 1 file changed, 57 insertions(+), 25 deletions(-) diff --git a/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider/src/org/eclipse/nebula/widgets/opal/nebulaslider/NebulaSlider.java b/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider/src/org/eclipse/nebula/widgets/opal/nebulaslider/NebulaSlider.java index cf4b39e21..0823a1fb0 100644 --- a/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider/src/org/eclipse/nebula/widgets/opal/nebulaslider/NebulaSlider.java +++ b/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider/src/org/eclipse/nebula/widgets/opal/nebulaslider/NebulaSlider.java @@ -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; @@ -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(); @@ -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(); @@ -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(); @@ -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); @@ -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); @@ -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); @@ -222,17 +252,19 @@ public IntFunction 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 -> {