Browse Source

Added property to enable scrollbar highlighting when scrolling (disabled by default).

Added property to hide scrollbar if it isn't currently used (macOS only, disabled by default).
pull/222/head
weisj 4 years ago
parent
commit
6f6c8dbc90
  1. 2
      core/src/main/java/com/github/weisj/darklaf/graphics/Animator.java
  2. 48
      core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/DarkMacScrollBarUI.java
  3. 51
      core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/DarkScrollBarListener.java
  4. 3
      core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/DarkScrollBarUI.java
  5. 1
      core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/ScrollBarConstants.java
  6. 6
      core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/ScrollBarUtil.java
  7. 1
      core/src/main/resources/com/github/weisj/darklaf/properties/platform/mac.properties
  8. 25
      core/src/main/resources/com/github/weisj/darklaf/properties/ui/scrollBar.properties

2
core/src/main/java/com/github/weisj/darklaf/graphics/Animator.java

@ -145,7 +145,7 @@ public abstract class Animator {
}
public void resume(final int startFrame, final JComponent target) {
if (!target.isVisible() || !target.isShowing()) {
if (target != null && (!target.isVisible() || !target.isShowing())) {
stopAnimation();
return;
}

48
core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/DarkMacScrollBarUI.java

@ -27,16 +27,23 @@ import java.awt.geom.RoundRectangle2D;
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import com.github.weisj.darklaf.graphics.Animator;
import com.github.weisj.darklaf.graphics.GraphicsContext;
import com.github.weisj.darklaf.graphics.GraphicsUtil;
public class DarkMacScrollBarUI extends DarkScrollBarUI {
private boolean hideScrollBar;
public static ComponentUI createUI(final JComponent c) {
return new DarkMacScrollBarUI();
}
@Override
protected void installDefaults() {
super.installDefaults();
hideScrollBar = UIManager.getBoolean("ScrollBar.macos.hideScrollBar");
}
@Override
protected void paintTrack(final Graphics g, final JComponent c, final Rectangle bounds) {}
@ -46,9 +53,26 @@ public class DarkMacScrollBarUI extends DarkScrollBarUI {
g.setComposite(COMPOSITE.derive(thumbAlpha));
boolean horizontal = scrollbar.getOrientation() == JScrollBar.HORIZONTAL;
int ins = 2;
int arc = horizontal ? (rect.height - 2 * ins) : (rect.width - 2 * ins);
RoundRectangle2D roundRect = new RoundRectangle2D.Float();
roundRect.setRoundRect(rect.x + ins, rect.y + ins, rect.width - 2 * ins, rect.height - 2 * ins, arc, arc);
int width = rect.width - 2 * ins;
int height = rect.height - 2 * ins;
int x = rect.x + ins;
int y = rect.y + ins;
if (hideScrollBar) {
if (horizontal) {
int newHeight = (int) (height * scrollBarListener.getTrackState());
y += (height - newHeight);
height = newHeight;
} else {
int newWidth = (int) (width * scrollBarListener.getTrackState());
if (scrollbar.getComponentOrientation().isLeftToRight()) {
x += (width - newWidth);
}
width = newWidth;
}
}
int arc = horizontal ? height : width;
roundRect.setRoundRect(x, y, width, height, arc, arc);
g.setColor(getThumbColor());
g.fill(roundRect);
g.setColor(getThumbBorderColor());
@ -68,14 +92,14 @@ public class DarkMacScrollBarUI extends DarkScrollBarUI {
super(scrollbar, ui);
}
@Override
protected Animator createTrackFadeinAnimator() {
return null;
}
@Override
protected Animator createTrackFadeoutAnimator() {
return null;
}
// @Override
// protected Animator createTrackFadeinAnimator() {
// return null;
// }
//
// @Override
// protected Animator createTrackFadeoutAnimator() {
// return null;
// }
}
}

51
core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/DarkScrollBarListener.java

@ -50,6 +50,8 @@ public class DarkScrollBarListener extends MouseAdapter implements AdjustmentLis
protected boolean mouseOverThumb = false;
protected boolean mouseOverTrack = false;
protected float trackState;
protected float thumbState;
protected float trackAlpha;
protected float thumbAlpha;
@ -82,6 +84,14 @@ public class DarkScrollBarListener extends MouseAdapter implements AdjustmentLis
return trackAlpha;
}
public float getTrackState() {
return trackState;
}
public float getThumbState() {
return thumbState;
}
public float getThumbAlpha() {
return thumbAlpha;
}
@ -164,16 +174,19 @@ public class DarkScrollBarListener extends MouseAdapter implements AdjustmentLis
if (!e.getValueIsAdjusting()) return;
JScrollBar scrollBar = (JScrollBar) e.getAdjustable();
int extent = scrollBar.getModel().getExtent();
int value = scrollBar.getValue() + extent;
if (value == extent || value == scrollBar.getMaximum()) return;
Point p = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(p, scrollbar);
if (!ui.getThumbBounds().contains(p) && !e.getValueIsAdjusting()) {
if (!thumbFadeinAnimator.isRunning()) {
mouseOverThumb = true;
resetThumbAnimator();
if (PropertyUtil.getBooleanProperty(scrollBar, KEY_HIGHLIGHT_ON_SCROLL)) {
int extent = scrollBar.getModel().getExtent();
int value = scrollBar.getValue() + extent;
if (value == extent || value == scrollBar.getMaximum()) return;
Point p = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(p, scrollbar);
if (!ui.getThumbBounds().contains(p)) {
if (!thumbFadeinAnimator.isRunning()) {
mouseOverThumb = true;
resetThumbAnimator();
}
}
}
}
@ -234,8 +247,9 @@ public class DarkScrollBarListener extends MouseAdapter implements AdjustmentLis
}
@Override
protected void updateValue(final float value) {
protected void updateValue(final float state, final float value) {
trackAlpha = value;
trackState = state;
}
}
@ -246,8 +260,9 @@ public class DarkScrollBarListener extends MouseAdapter implements AdjustmentLis
}
@Override
protected void updateValue(final float value) {
protected void updateValue(final float state, final float value) {
trackAlpha = value;
trackState = state;
}
}
@ -258,8 +273,9 @@ public class DarkScrollBarListener extends MouseAdapter implements AdjustmentLis
}
@Override
protected void updateValue(final float value) {
protected void updateValue(final float state, final float value) {
thumbAlpha = value;
thumbState = state;
}
@Override
@ -282,8 +298,9 @@ public class DarkScrollBarListener extends MouseAdapter implements AdjustmentLis
}
@Override
protected void updateValue(final float value) {
protected void updateValue(final float state, final float value) {
thumbAlpha = value;
thumbState = state;
}
}
@ -304,18 +321,18 @@ public class DarkScrollBarListener extends MouseAdapter implements AdjustmentLis
this.fadeIn = fadeIn;
}
protected abstract void updateValue(final float value);
protected abstract void updateValue(final float state, final float value);
@Override
public void paintNow(final float fraction) {
float fr = fadeIn ? fraction : (1 - fraction);
updateValue(minValue + maxValue * fr);
updateValue(fr, minValue + maxValue * fr);
repaint();
}
@Override
protected void paintCycleEnd() {
updateValue(fadeIn ? maxValue : minValue);
updateValue(fadeIn ? 1 : 0, fadeIn ? maxValue : minValue);
repaint();
}

3
core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/DarkScrollBarUI.java

@ -30,6 +30,7 @@ import javax.swing.plaf.basic.BasicScrollBarUI;
import com.github.weisj.darklaf.graphics.PaintUtil;
import com.github.weisj.darklaf.util.ColorUtil;
import com.github.weisj.darklaf.util.PropertyUtil;
/** @author Jannis Weis */
public class DarkScrollBarUI extends BasicScrollBarUI implements ScrollBarConstants {
@ -66,6 +67,8 @@ public class DarkScrollBarUI extends BasicScrollBarUI implements ScrollBarConsta
UIManager.put("ScrollBar.incrementButtonGap", incGap);
UIManager.put("ScrollBar.decrementButtonGap", decGap);
}
PropertyUtil.installProperty(scrollbar, KEY_HIGHLIGHT_ON_SCROLL,
UIManager.getBoolean("ScrollBar.highlightOnScroll"));
thumbBorderColor = UIManager.getColor("ScrollBar.thumbBorderColor");
thumbFadeStartColor = UIManager.getColor("ScrollBar.fadeStartColor");
thumbFadeEndColor = UIManager.getColor("ScrollBar.fadeEndColor");

1
core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/ScrollBarConstants.java

@ -28,6 +28,7 @@ import com.github.weisj.darklaf.util.PropertyUtil;
public interface ScrollBarConstants {
String KEY_SCROLL_PANE_PARENT = "JScrollBar.scrollPaneParent";
String KEY_FAST_WHEEL_SCROLLING = "JScrollBar.fastWheelScrolling";
String KEY_HIGHLIGHT_ON_SCROLL = "JScrollBar.highlightOnScroll";
String KEY_SMALL = "JComponent.small";
static boolean isSmall(final JScrollBar scrollBar) {

6
core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/ScrollBarUtil.java

@ -30,6 +30,12 @@ import com.github.weisj.darklaf.util.PropertyUtil;
public class ScrollBarUtil implements ScrollBarConstants {
public static JScrollPane getScrollPane(final JScrollBar scrollBar) {
Component parent = scrollBar.getParent();
if (parent instanceof JScrollPane) return (JScrollPane) parent;
return PropertyUtil.getObject(scrollBar, KEY_SCROLL_PANE_PARENT, JScrollPane.class);
}
@SuppressWarnings("MagicConstant")
public static void doScroll(final JScrollBar toScroll, final JViewport vp, final MouseWheelEvent e,
final boolean leftToRight) {

1
core/src/main/resources/com/github/weisj/darklaf/properties/platform/mac.properties

@ -28,6 +28,7 @@ macos.coloredTitleBar = true
ScrollBarUI = com.github.weisj.darklaf.ui.scrollpane.DarkMacScrollBarUI
ScrollBar.smallWidth = 10
ScrollBar.width = 12
ScrollBar.macos.hideScrollBar = false
TooltipUI = com.github.weisj.darklaf.ui.tooltip.DarkMacTooltipUI

25
core/src/main/resources/com/github/weisj/darklaf/properties/ui/scrollBar.properties

@ -24,15 +24,16 @@
#
# suppress inspection "UnusedProperty" for whole file
#
ScrollBarUI = com.github.weisj.darklaf.ui.scrollpane.DarkScrollBarUI
ScrollBar.fadeStartColor = %controlFadeStartSecondary
ScrollBar.fadeEndColor = %controlFadeEndSecondary
ScrollBar.trackColor = %controlTrack
ScrollBar.thumbBorderColor = %controlBorderSecondary
ScrollBar.thumb = %controlFillSecondary
ScrollBar.thumbShadow = null
ScrollBar.thumbHighlight = null
ScrollBar.smallWidth = 8
ScrollBar.width = 10
ScrollBar.thumbAlpha = 60
ScrollBar.animated = true
ScrollBarUI = com.github.weisj.darklaf.ui.scrollpane.DarkScrollBarUI
ScrollBar.fadeStartColor = %controlFadeStartSecondary
ScrollBar.fadeEndColor = %controlFadeEndSecondary
ScrollBar.trackColor = %controlTrack
ScrollBar.thumbBorderColor = %controlBorderSecondary
ScrollBar.thumb = %controlFillSecondary
ScrollBar.thumbShadow = null
ScrollBar.thumbHighlight = null
ScrollBar.smallWidth = 8
ScrollBar.width = 10
ScrollBar.thumbAlpha = 60
ScrollBar.animated = true
ScrollBar.highlightOnScroll = false

Loading…
Cancel
Save