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

Support disabled state of slider #577

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading