Browse Source

Enable slider adjustment through mouse wheel rotation.

pull/214/head
weisj 4 years ago
parent
commit
bb276b4193
  1. 83
      core/src/main/java/com/github/weisj/darklaf/ui/slider/DarkSliderListener.java
  2. 59
      core/src/main/java/com/github/weisj/darklaf/ui/slider/DarkSliderUI.java

83
core/src/main/java/com/github/weisj/darklaf/ui/slider/DarkSliderListener.java

@ -0,0 +1,83 @@
/*
* MIT License
*
* Copyright (c) 2020 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.github.weisj.darklaf.ui.slider;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
public class DarkSliderListener extends MouseAdapter implements PropertyChangeListener {
private final DarkSliderUI ui;
private final JSlider slider;
private boolean muted = false;
private int oldValue;
public DarkSliderListener(final DarkSliderUI ui, final JSlider slider) {
this.ui = ui;
this.slider = slider;
}
@Override
public void mouseWheelMoved(final MouseWheelEvent e) {
super.mouseWheelMoved(e);
if (!slider.hasFocus()) return;
if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) return;
int amount = e.getWheelRotation();
boolean ltr = slider.getComponentOrientation().isLeftToRight();
if (ltr) amount *= -1;
if (slider.getSnapToTicks()) {
int spacing = slider.getMinorTickSpacing();
amount *= spacing;
}
ui.setValue(slider.getValue() + amount);
}
@Override
public void mouseClicked(final MouseEvent e) {
if (slider.isEnabled() && ui.showVolumeIcon(slider) && ui.iconRect.contains(e.getPoint())) {
if (muted && slider.getValue() == slider.getMinimum()) {
ui.setValue(oldValue);
muted = false;
} else {
oldValue = slider.getValue();
ui.setValue(slider.getMinimum());
muted = true;
}
}
}
@Override
public void propertyChange(final PropertyChangeEvent evt) {
String key = evt.getPropertyName();
if (DarkSliderUI.KEY_VARIANT.equals(key)) {
slider.repaint();
} else if (DarkSliderUI.KEY_SHOW_VOLUME_ICON.equals(key)) {
ui.calculateGeometry();
slider.repaint();
}
}
}

59
core/src/main/java/com/github/weisj/darklaf/ui/slider/DarkSliderUI.java

@ -23,10 +23,7 @@ package com.github.weisj.darklaf.ui.slider;
import java.awt.*; import java.awt.*;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.RoundRectangle2D; import java.awt.geom.RoundRectangle2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Dictionary; import java.util.Dictionary;
import java.util.Enumeration; import java.util.Enumeration;
@ -38,14 +35,13 @@ import com.github.weisj.darklaf.graphics.GraphicsContext;
import com.github.weisj.darklaf.graphics.GraphicsUtil; import com.github.weisj.darklaf.graphics.GraphicsUtil;
import com.github.weisj.darklaf.graphics.PaintUtil; import com.github.weisj.darklaf.graphics.PaintUtil;
import com.github.weisj.darklaf.icons.RotatableIcon; import com.github.weisj.darklaf.icons.RotatableIcon;
import com.github.weisj.darklaf.listener.MouseClickListener;
import com.github.weisj.darklaf.util.Alignment; import com.github.weisj.darklaf.util.Alignment;
import com.github.weisj.darklaf.util.DarkUIUtil; import com.github.weisj.darklaf.util.DarkUIUtil;
import com.github.weisj.darklaf.util.PropertyKey; import com.github.weisj.darklaf.util.PropertyKey;
import com.github.weisj.darklaf.util.PropertyUtil; import com.github.weisj.darklaf.util.PropertyUtil;
/** @author Jannis Weis */ /** @author Jannis Weis */
public class DarkSliderUI extends BasicSliderUI implements PropertyChangeListener { public class DarkSliderUI extends BasicSliderUI {
protected static final String KEY_PREFIX = "JSlider."; protected static final String KEY_PREFIX = "JSlider.";
public static final String KEY_THUMB_ARROW_SHAPE = KEY_PREFIX + "paintThumbArrowShape"; public static final String KEY_THUMB_ARROW_SHAPE = KEY_PREFIX + "paintThumbArrowShape";
@ -55,25 +51,9 @@ public class DarkSliderUI extends BasicSliderUI implements PropertyChangeListene
public static final String KEY_MANUAL_LABEL_ALIGN = KEY_PREFIX + "manualLabelAlign"; public static final String KEY_MANUAL_LABEL_ALIGN = KEY_PREFIX + "manualLabelAlign";
public static final String VARIANT_VOLUME = "volume"; public static final String VARIANT_VOLUME = "volume";
private final Rectangle iconRect = new Rectangle(0, 0, 0, 0); protected final Rectangle iconRect = new Rectangle(0, 0, 0, 0);
private final MouseListener mouseListener = new MouseClickListener() { private DarkSliderListener sliderListener;
private boolean muted = false;
private int oldValue;
@Override
public void mouseClicked(final MouseEvent e) {
if (slider.isEnabled() && showVolumeIcon(slider) && iconRect.contains(e.getPoint())) {
if (muted && slider.getValue() == slider.getMinimum()) {
setValue(oldValue);
muted = false;
} else {
oldValue = slider.getValue();
setValue(slider.getMinimum());
muted = true;
}
}
}
};
protected int plainThumbRadius; protected int plainThumbRadius;
protected int arcSize; protected int arcSize;
protected int trackSize; protected int trackSize;
@ -126,11 +106,11 @@ public class DarkSliderUI extends BasicSliderUI implements PropertyChangeListene
return new DarkSliderUI((JSlider) c); return new DarkSliderUI((JSlider) c);
} }
private static boolean showVolumeIcon(final JComponent c) { protected boolean showVolumeIcon(final JComponent c) {
return PropertyUtil.getBooleanProperty(c, KEY_SHOW_VOLUME_ICON); return PropertyUtil.getBooleanProperty(c, KEY_SHOW_VOLUME_ICON);
} }
private static boolean isVolumeSlider(final JComponent c) { protected boolean isVolumeSlider(final JComponent c) {
return PropertyUtil.isPropertyEqual(c, KEY_VARIANT, VARIANT_VOLUME); return PropertyUtil.isPropertyEqual(c, KEY_VARIANT, VARIANT_VOLUME);
} }
@ -142,15 +122,25 @@ public class DarkSliderUI extends BasicSliderUI implements PropertyChangeListene
@Override @Override
protected void installListeners(final JSlider slider) { protected void installListeners(final JSlider slider) {
super.installListeners(slider); super.installListeners(slider);
slider.addMouseListener(mouseListener); if (sliderListener == null) {
slider.addPropertyChangeListener(this); sliderListener = createSliderListener();
}
slider.addMouseListener(sliderListener);
slider.addMouseWheelListener(sliderListener);
slider.addPropertyChangeListener(sliderListener);
}
protected DarkSliderListener createSliderListener() {
return new DarkSliderListener(this, slider);
} }
@Override @Override
protected void uninstallListeners(final JSlider slider) { protected void uninstallListeners(final JSlider slider) {
super.uninstallListeners(slider); super.uninstallListeners(slider);
slider.removeMouseListener(mouseListener); slider.removeMouseListener(sliderListener);
slider.removePropertyChangeListener(this); slider.removeMouseWheelListener(sliderListener);
slider.removePropertyChangeListener(sliderListener);
sliderListener = null;
} }
@Override @Override
@ -760,17 +750,6 @@ public class DarkSliderUI extends BasicSliderUI implements PropertyChangeListene
return new Rectangle(thumbRect); return new Rectangle(thumbRect);
} }
@Override
public void propertyChange(final PropertyChangeEvent evt) {
String key = evt.getPropertyName();
if (KEY_VARIANT.equals(key)) {
slider.repaint();
} else if (DarkSliderUI.KEY_SHOW_VOLUME_ICON.equals(key)) {
calculateGeometry();
slider.repaint();
}
}
private int getSnappedValue(final int value) { private int getSnappedValue(final int value) {
// Now calculate if we should adjust the value // Now calculate if we should adjust the value
int snappedValue = value; int snappedValue = value;

Loading…
Cancel
Save