Browse Source

Fixed typo.

Fixed issue where ToolTips would not show up when the mouse entered through the right or bottom side of the component.
Fixed issue where tooltips would not vanish reliably.
pull/15/head
weisj 5 years ago
parent
commit
887bbf8a31
  1. 7
      src/main/java/com/weis/darklaf/ui/button/DarkButtonUI.java
  2. 47
      src/main/java/com/weis/darklaf/ui/tabframe/DarkPanelPopupUI.java
  3. 117
      src/main/java/com/weis/darklaf/ui/tooltip/DarkTooltipUI.java
  4. 2
      src/main/resources/com/weis/darklaf/properties/ui/toolTip.properties

7
src/main/java/com/weis/darklaf/ui/button/DarkButtonUI.java

@ -149,16 +149,19 @@ public class DarkButtonUI extends BasicButtonUI {
protected void paintButton(final Graphics g, @NotNull final JComponent c) {
Graphics2D g2 = (Graphics2D) g;
if (shouldDrawBackground(c)) {
var b = (AbstractButton) c;
var margin = b.getMargin();
int arc = getArc(c);
if (isShadowVariant(c)) {
var b = (AbstractButton) c;
if (b.isEnabled() && b.getModel().isRollover()) {
GraphicsUtil.setupAAPainting(g2);
g.setColor(getShadowColor(b));
if (isFullShadow(c)) {
g.fillRect(0, 0, c.getWidth(), c.getHeight());
} else {
DarkUIUtil.fillRoundRect((Graphics2D) g, 0, 0, c.getWidth(), c.getHeight(), arc);
DarkUIUtil.fillRoundRect((Graphics2D) g, margin.left, margin.top,
c.getWidth() - margin.left - margin.right,
c.getHeight() - margin.top - margin.bottom, arc);
}
}
} else {

47
src/main/java/com/weis/darklaf/ui/tabframe/DarkPanelPopupUI.java

@ -38,6 +38,7 @@ import javax.swing.FocusManager;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.InsetsUIResource;
import javax.swing.plaf.UIResource;
import java.awt.*;
import java.awt.event.AWTEventListener;
@ -265,9 +266,18 @@ public class DarkPanelPopupUI extends DarkPanelUI implements PropertyChangeListe
}
} else if ("peerInsets".equals(key)) {
updateBorder(false);
updateTooltip();
} else if ("alignment".equals(key)) {
updateTooltip();
}
}
protected void updateTooltip() {
var tabFrame = popupComponent.getTabFrame();
closeButton.setAlignment(popupComponent.getAlignment(),
tabFrame.getContentPane().isEnabled(tabFrame.getPeer(popupComponent.getAlignment())));
}
protected void updateBorder(final boolean notifyPeer) {
if (popupComponent.getTabFrame() != null) {
var tabFrame = popupComponent.getTabFrame();
@ -352,8 +362,8 @@ public class DarkPanelPopupUI extends DarkPanelUI implements PropertyChangeListe
protected static final class HeaderButton extends JButton implements UIResource {
private final ToolTipContext context = new ToolTipContext(this);
private final DarkPanelPopupUI ui;
protected final ToolTipContext context = new ToolTipContext(this);
protected final DarkPanelPopupUI ui;
public HeaderButton(@NotNull final Icon icon, final DarkPanelPopupUI ui) {
super(icon);
@ -362,8 +372,10 @@ public class DarkPanelPopupUI extends DarkPanelUI implements PropertyChangeListe
putClientProperty("JButton.alternativeArc", Boolean.TRUE);
putClientProperty("JButton.variant", "shadow");
setRolloverEnabled(true);
setMargin(new InsetsUIResource(2, 2, 2, 2));
setFocus(false);
setOpaque(false);
context.setToolTipInsets(new Insets(5, 5, 5, 5));
}
public void setFocus(final boolean focus) {
@ -382,5 +394,36 @@ public class DarkPanelPopupUI extends DarkPanelUI implements PropertyChangeListe
public JToolTip createToolTip() {
return context.getToolTip();
}
protected void setAlignment(@NotNull final Alignment a, final boolean peerEnabled) {
switch (a) {
case NORTH:
context.setCenterAlignment(peerEnabled ? Alignment.SOUTH : Alignment.SOUTH_WEST);
break;
case NORTH_EAST:
context.setCenterAlignment(Alignment.SOUTH_WEST);
break;
case EAST:
context.setCenterAlignment(Alignment.WEST);
break;
case SOUTH_EAST:
context.setCenterAlignment(Alignment.WEST);
break;
case SOUTH:
context.setCenterAlignment(Alignment.NORTH_WEST);
break;
case SOUTH_WEST:
context.setCenterAlignment(peerEnabled ? Alignment.NORTH : Alignment.NORTH_WEST);
break;
case WEST:
context.setCenterAlignment(peerEnabled ? Alignment.NORTH : Alignment.SOUTH);
break;
case NORTH_WEST:
context.setCenterAlignment(Alignment.SOUTH);
break;
case CENTER:
break;
}
}
}
}

117
src/main/java/com/weis/darklaf/ui/tooltip/DarkTooltipUI.java

@ -10,13 +10,18 @@ import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicToolTipUI;
import javax.swing.text.View;
import java.awt.*;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
/**
* @author Jannis Weis
*/
public class DarkTooltipUI extends BasicToolTipUI implements PropertyChangeListener {
public class DarkTooltipUI extends BasicToolTipUI implements PropertyChangeListener, HierarchyListener {
@NotNull
@Contract("_ -> new")
@ -28,8 +33,69 @@ public class DarkTooltipUI extends BasicToolTipUI implements PropertyChangeListe
}
}
protected JToolTip toolTip;
protected MouseListener mouseListener = new MouseAdapter() {
@Override
public void mouseEntered(@NotNull final MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) return;
/*
* We redispatch the event to the ToolTipManager with a corrected location.
* Because the ToolTipManager check for outside using >= width/height instead of > width/height and due to
* the nature of mouseEntered events most of the times having width/height as their coordinated ToolTips would
* not show up when the component is entered through the bottom/right side of the component.
*/
var p = e.getPoint();
var c = toolTip.getComponent();
if (p.x == c.getWidth()) p.x--;
if (p.y == c.getHeight()) p.y--;
p.x = Math.max(p.x, 0);
p.y = Math.max(p.y, 0);
ToolTipManager.sharedInstance()
.mouseEntered(new MouseEvent(c, e.getID(), e.getWhen(), e.getModifiersEx(), p.x, p.y,
e.getClickCount(), e.isPopupTrigger(), e.getButton()));
}
@Override
public void mouseExited(@NotNull final MouseEvent e) {
boolean inside = isInside(e);
if (!inside) {
ToolTipManager.sharedInstance().mouseExited(
new MouseEvent(toolTip.getComponent(), e.getID(), e.getWhen(), e.getModifiersEx(),
Integer.MIN_VALUE, Integer.MIN_VALUE, e.getClickCount(), e.isPopupTrigger(),
e.getButton()));
}
}
};
protected MouseListener exitListener = new MouseAdapter() {
@Override
public void mouseExited(@NotNull final MouseEvent e) {
ToolTipManager.sharedInstance().mouseExited(
new MouseEvent(toolTip.getComponent(), e.getID(), e.getWhen(), e.getModifiersEx(),
Integer.MIN_VALUE, Integer.MIN_VALUE, e.getClickCount(), e.isPopupTrigger(),
e.getButton()));
}
};
protected boolean isInside(@NotNull final MouseEvent e) {
var p = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), toolTip);
return contains(toolTip, p.x, p.y);
}
@Override
public boolean contains(@NotNull final JComponent c, final int x, final int y) {
var b = c.getBorder();
if (b instanceof DarkTooltipBorder) {
var insideArea = ((DarkTooltipBorder) b).getBackgroundArea(toolTip,
toolTip.getWidth(), toolTip.getHeight());
return insideArea.contains(x, y);
} else {
return super.contains(c, x, y);
}
}
@Override
public void installUI(final JComponent c) {
toolTip = (JToolTip) c;
super.installUI(c);
}
@ -43,16 +109,33 @@ public class DarkTooltipUI extends BasicToolTipUI implements PropertyChangeListe
}
}
@Override
public void uninstallUI(final JComponent c) {
super.uninstallUI(c);
toolTip = null;
}
@Override
protected void installListeners(final JComponent c) {
super.installListeners(c);
c.addHierarchyListener(e -> {
var w = SwingUtilities.getWindowAncestor(c);
if (w != null && !c.isLightweight() && !isDecorated(w)) {
w.setBackground(DarkUIUtil.TRANSPARENT_COLOR);
}
});
c.addHierarchyListener(this);
c.addPropertyChangeListener(this);
toolTip.addMouseListener(exitListener);
var comp = toolTip.getComponent();
if (comp != null) {
comp.addMouseListener(mouseListener);
}
}
@Override
protected void uninstallListeners(final JComponent c) {
super.uninstallListeners(c);
c.removePropertyChangeListener(this);
toolTip.removeMouseListener(exitListener);
var comp = toolTip.getComponent();
if (comp != null) {
comp.removeMouseListener(mouseListener);
}
}
@Override
@ -98,6 +181,14 @@ public class DarkTooltipUI extends BasicToolTipUI implements PropertyChangeListe
return false;
}
@Override
public void hierarchyChanged(final HierarchyEvent e) {
var w = SwingUtilities.getWindowAncestor(toolTip);
if (w != null && !toolTip.isLightweight() && !isDecorated(w)) {
w.setBackground(DarkUIUtil.TRANSPARENT_COLOR);
}
}
@Override
public void propertyChange(@NotNull final PropertyChangeEvent evt) {
var key = evt.getPropertyName();
@ -123,8 +214,20 @@ public class DarkTooltipUI extends BasicToolTipUI implements PropertyChangeListe
border.setPointerWidth((Integer) newVal);
}
tooltip.setComponent(tooltip.getComponent());
} else if ("JToolTip.insets".equals(key)) {
tooltip.setComponent(tooltip.getComponent());
} else if ("component".equals(key)) {
var oldComp = evt.getOldValue();
if (oldComp instanceof Component) {
((Component) oldComp).removeMouseListener(mouseListener);
}
var newComp = evt.getNewValue();
if (newComp instanceof Component) {
((Component) newComp).addMouseListener(mouseListener);
}
}
}
}
}
}

2
src/main/resources/com/weis/darklaf/properties/ui/toolTip.properties

@ -22,7 +22,7 @@
#
# suppress inspection "UnusedProperty" for whole file
ToolTipUI = com.weis.darklaf.ui.tooltip.DarkTooltipUI
Tooltip.background = %backgroundToolTip
ToolTip.background = %backgroundToolTip
ToolTip.borderColor = %borderTertiary
ToolTip.border = com.weis.darklaf.ui.tooltip.DarkTooltipBorder
ToolTip.borderShadowColor = %shadow
Loading…
Cancel
Save