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. 33
      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. 1
      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) { public void resume(final int startFrame, final JComponent target) {
if (!target.isVisible() || !target.isShowing()) { if (target != null && (!target.isVisible() || !target.isShowing())) {
stopAnimation(); stopAnimation();
return; 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.*;
import javax.swing.plaf.ComponentUI; 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.GraphicsContext;
import com.github.weisj.darklaf.graphics.GraphicsUtil; import com.github.weisj.darklaf.graphics.GraphicsUtil;
public class DarkMacScrollBarUI extends DarkScrollBarUI { public class DarkMacScrollBarUI extends DarkScrollBarUI {
private boolean hideScrollBar;
public static ComponentUI createUI(final JComponent c) { public static ComponentUI createUI(final JComponent c) {
return new DarkMacScrollBarUI(); return new DarkMacScrollBarUI();
} }
@Override
protected void installDefaults() {
super.installDefaults();
hideScrollBar = UIManager.getBoolean("ScrollBar.macos.hideScrollBar");
}
@Override @Override
protected void paintTrack(final Graphics g, final JComponent c, final Rectangle bounds) {} 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)); g.setComposite(COMPOSITE.derive(thumbAlpha));
boolean horizontal = scrollbar.getOrientation() == JScrollBar.HORIZONTAL; boolean horizontal = scrollbar.getOrientation() == JScrollBar.HORIZONTAL;
int ins = 2; int ins = 2;
int arc = horizontal ? (rect.height - 2 * ins) : (rect.width - 2 * ins);
RoundRectangle2D roundRect = new RoundRectangle2D.Float(); 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.setColor(getThumbColor());
g.fill(roundRect); g.fill(roundRect);
g.setColor(getThumbBorderColor()); g.setColor(getThumbBorderColor());
@ -68,14 +92,14 @@ public class DarkMacScrollBarUI extends DarkScrollBarUI {
super(scrollbar, ui); super(scrollbar, ui);
} }
@Override // @Override
protected Animator createTrackFadeinAnimator() { // protected Animator createTrackFadeinAnimator() {
return null; // return null;
} // }
//
@Override // @Override
protected Animator createTrackFadeoutAnimator() { // protected Animator createTrackFadeoutAnimator() {
return null; // return null;
} // }
} }
} }

33
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 mouseOverThumb = false;
protected boolean mouseOverTrack = false; protected boolean mouseOverTrack = false;
protected float trackState;
protected float thumbState;
protected float trackAlpha; protected float trackAlpha;
protected float thumbAlpha; protected float thumbAlpha;
@ -82,6 +84,14 @@ public class DarkScrollBarListener extends MouseAdapter implements AdjustmentLis
return trackAlpha; return trackAlpha;
} }
public float getTrackState() {
return trackState;
}
public float getThumbState() {
return thumbState;
}
public float getThumbAlpha() { public float getThumbAlpha() {
return thumbAlpha; return thumbAlpha;
} }
@ -164,19 +174,22 @@ public class DarkScrollBarListener extends MouseAdapter implements AdjustmentLis
if (!e.getValueIsAdjusting()) return; if (!e.getValueIsAdjusting()) return;
JScrollBar scrollBar = (JScrollBar) e.getAdjustable(); JScrollBar scrollBar = (JScrollBar) e.getAdjustable();
if (PropertyUtil.getBooleanProperty(scrollBar, KEY_HIGHLIGHT_ON_SCROLL)) {
int extent = scrollBar.getModel().getExtent(); int extent = scrollBar.getModel().getExtent();
int value = scrollBar.getValue() + extent; int value = scrollBar.getValue() + extent;
if (value == extent || value == scrollBar.getMaximum()) return; if (value == extent || value == scrollBar.getMaximum()) return;
Point p = MouseInfo.getPointerInfo().getLocation(); Point p = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(p, scrollbar); SwingUtilities.convertPointFromScreen(p, scrollbar);
if (!ui.getThumbBounds().contains(p) && !e.getValueIsAdjusting()) { if (!ui.getThumbBounds().contains(p)) {
if (!thumbFadeinAnimator.isRunning()) { if (!thumbFadeinAnimator.isRunning()) {
mouseOverThumb = true; mouseOverThumb = true;
resetThumbAnimator(); resetThumbAnimator();
} }
} }
} }
}
protected boolean isOverThumb(final Point p) { protected boolean isOverThumb(final Point p) {
final Rectangle bounds = ui.getThumbBounds(); final Rectangle bounds = ui.getThumbBounds();
@ -234,8 +247,9 @@ public class DarkScrollBarListener extends MouseAdapter implements AdjustmentLis
} }
@Override @Override
protected void updateValue(final float value) { protected void updateValue(final float state, final float value) {
trackAlpha = value; trackAlpha = value;
trackState = state;
} }
} }
@ -246,8 +260,9 @@ public class DarkScrollBarListener extends MouseAdapter implements AdjustmentLis
} }
@Override @Override
protected void updateValue(final float value) { protected void updateValue(final float state, final float value) {
trackAlpha = value; trackAlpha = value;
trackState = state;
} }
} }
@ -258,8 +273,9 @@ public class DarkScrollBarListener extends MouseAdapter implements AdjustmentLis
} }
@Override @Override
protected void updateValue(final float value) { protected void updateValue(final float state, final float value) {
thumbAlpha = value; thumbAlpha = value;
thumbState = state;
} }
@Override @Override
@ -282,8 +298,9 @@ public class DarkScrollBarListener extends MouseAdapter implements AdjustmentLis
} }
@Override @Override
protected void updateValue(final float value) { protected void updateValue(final float state, final float value) {
thumbAlpha = value; thumbAlpha = value;
thumbState = state;
} }
} }
@ -304,18 +321,18 @@ public class DarkScrollBarListener extends MouseAdapter implements AdjustmentLis
this.fadeIn = fadeIn; this.fadeIn = fadeIn;
} }
protected abstract void updateValue(final float value); protected abstract void updateValue(final float state, final float value);
@Override @Override
public void paintNow(final float fraction) { public void paintNow(final float fraction) {
float fr = fadeIn ? fraction : (1 - fraction); float fr = fadeIn ? fraction : (1 - fraction);
updateValue(minValue + maxValue * fr); updateValue(fr, minValue + maxValue * fr);
repaint(); repaint();
} }
@Override @Override
protected void paintCycleEnd() { protected void paintCycleEnd() {
updateValue(fadeIn ? maxValue : minValue); updateValue(fadeIn ? 1 : 0, fadeIn ? maxValue : minValue);
repaint(); 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.graphics.PaintUtil;
import com.github.weisj.darklaf.util.ColorUtil; import com.github.weisj.darklaf.util.ColorUtil;
import com.github.weisj.darklaf.util.PropertyUtil;
/** @author Jannis Weis */ /** @author Jannis Weis */
public class DarkScrollBarUI extends BasicScrollBarUI implements ScrollBarConstants { 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.incrementButtonGap", incGap);
UIManager.put("ScrollBar.decrementButtonGap", decGap); UIManager.put("ScrollBar.decrementButtonGap", decGap);
} }
PropertyUtil.installProperty(scrollbar, KEY_HIGHLIGHT_ON_SCROLL,
UIManager.getBoolean("ScrollBar.highlightOnScroll"));
thumbBorderColor = UIManager.getColor("ScrollBar.thumbBorderColor"); thumbBorderColor = UIManager.getColor("ScrollBar.thumbBorderColor");
thumbFadeStartColor = UIManager.getColor("ScrollBar.fadeStartColor"); thumbFadeStartColor = UIManager.getColor("ScrollBar.fadeStartColor");
thumbFadeEndColor = UIManager.getColor("ScrollBar.fadeEndColor"); 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 { public interface ScrollBarConstants {
String KEY_SCROLL_PANE_PARENT = "JScrollBar.scrollPaneParent"; String KEY_SCROLL_PANE_PARENT = "JScrollBar.scrollPaneParent";
String KEY_FAST_WHEEL_SCROLLING = "JScrollBar.fastWheelScrolling"; String KEY_FAST_WHEEL_SCROLLING = "JScrollBar.fastWheelScrolling";
String KEY_HIGHLIGHT_ON_SCROLL = "JScrollBar.highlightOnScroll";
String KEY_SMALL = "JComponent.small"; String KEY_SMALL = "JComponent.small";
static boolean isSmall(final JScrollBar scrollBar) { 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 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") @SuppressWarnings("MagicConstant")
public static void doScroll(final JScrollBar toScroll, final JViewport vp, final MouseWheelEvent e, public static void doScroll(final JScrollBar toScroll, final JViewport vp, final MouseWheelEvent e,
final boolean leftToRight) { 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 ScrollBarUI = com.github.weisj.darklaf.ui.scrollpane.DarkMacScrollBarUI
ScrollBar.smallWidth = 10 ScrollBar.smallWidth = 10
ScrollBar.width = 12 ScrollBar.width = 12
ScrollBar.macos.hideScrollBar = false
TooltipUI = com.github.weisj.darklaf.ui.tooltip.DarkMacTooltipUI TooltipUI = com.github.weisj.darklaf.ui.tooltip.DarkMacTooltipUI

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

@ -36,3 +36,4 @@ ScrollBar.smallWidth = 8
ScrollBar.width = 10 ScrollBar.width = 10
ScrollBar.thumbAlpha = 60 ScrollBar.thumbAlpha = 60
ScrollBar.animated = true ScrollBar.animated = true
ScrollBar.highlightOnScroll = false

Loading…
Cancel
Save