From ee7e41b7d686893b3e8e648a944c1801c92a885f Mon Sep 17 00:00:00 2001 From: weisj Date: Sun, 20 Oct 2019 22:01:42 +0200 Subject: [PATCH] Decreased sensitivity for DnD to start in TabFrames. --- .../tabframe/DarkTabFrameTabContainerUI.java | 9 +++++++-- .../ui/tabframe/DarkTabFrameTabLabelUI.java | 5 +++-- .../darklaf/ui/tabframe/TabDragListener.java | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/weis/darklaf/ui/tabframe/DarkTabFrameTabContainerUI.java b/src/main/java/com/weis/darklaf/ui/tabframe/DarkTabFrameTabContainerUI.java index fe951338..89d7b77f 100644 --- a/src/main/java/com/weis/darklaf/ui/tabframe/DarkTabFrameTabContainerUI.java +++ b/src/main/java/com/weis/darklaf/ui/tabframe/DarkTabFrameTabContainerUI.java @@ -38,7 +38,6 @@ import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; -import java.awt.event.MouseMotionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; @@ -54,7 +53,7 @@ public class DarkTabFrameTabContainerUI extends DarkPanelUI implements PropertyC } } }; - private MouseMotionListener dragListener; + private TabDragListener dragListener; private HoverListener hoverListener; private Color selectedColor; private Color hoverColor; @@ -81,11 +80,13 @@ public class DarkTabFrameTabContainerUI extends DarkPanelUI implements PropertyC tabContainer.addMouseListener(hoverListener); tabContainer.addPropertyChangeListener(this); tabContainer.addMouseListener(mouseListener); + tabContainer.addMouseListener(dragListener); tabContainer.addMouseMotionListener(dragListener); var cont = tabContainer.getContent(); if (cont != null) { cont.addMouseListener(hoverListener); cont.addMouseListener(mouseListener); + cont.addMouseListener(dragListener); cont.addMouseMotionListener(dragListener); } } @@ -131,12 +132,14 @@ public class DarkTabFrameTabContainerUI extends DarkPanelUI implements PropertyC protected void uninstallListeners() { tabContainer.removeMouseListener(hoverListener); tabContainer.removeMouseListener(mouseListener); + tabContainer.removeMouseListener(dragListener); tabContainer.removePropertyChangeListener(this); tabContainer.removeMouseMotionListener(dragListener); var cont = tabContainer.getContent(); if (cont != null) { cont.removeMouseListener(hoverListener); cont.removeMouseListener(mouseListener); + cont.removeMouseListener(dragListener); cont.removeMouseMotionListener(dragListener); } dragListener = null; @@ -166,11 +169,13 @@ public class DarkTabFrameTabContainerUI extends DarkPanelUI implements PropertyC if (oldVal instanceof Component) { ((Component) oldVal).removeMouseListener(mouseListener); ((Component) oldVal).removeMouseListener(hoverListener); + ((Component) oldVal).removeMouseListener(dragListener); ((Component) oldVal).removeMouseMotionListener(dragListener); } if (newVal instanceof Component) { ((Component) newVal).addMouseListener(mouseListener); ((Component) newVal).addMouseListener(hoverListener); + ((Component) newVal).addMouseListener(dragListener); ((Component) newVal).addMouseMotionListener(dragListener); } } else if ("selected".equals(key)) { diff --git a/src/main/java/com/weis/darklaf/ui/tabframe/DarkTabFrameTabLabelUI.java b/src/main/java/com/weis/darklaf/ui/tabframe/DarkTabFrameTabLabelUI.java index d86c11e6..4e85f4e1 100644 --- a/src/main/java/com/weis/darklaf/ui/tabframe/DarkTabFrameTabLabelUI.java +++ b/src/main/java/com/weis/darklaf/ui/tabframe/DarkTabFrameTabLabelUI.java @@ -44,7 +44,6 @@ import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; -import java.awt.event.MouseMotionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; @@ -60,7 +59,7 @@ public class DarkTabFrameTabLabelUI extends DarkLabelUI implements PropertyChang } } }; - private MouseMotionListener dragListener; + private TabDragListener dragListener; private HoverListener hoverListener; private Color defaultFontColor; private Color selectedFontColor; @@ -153,6 +152,7 @@ public class DarkTabFrameTabLabelUI extends DarkLabelUI implements PropertyChang tabComponent.addMouseListener(mouseListener); installAccelerator(tabComponent.getTabFrame()); tabComponent.addMouseMotionListener(dragListener); + tabComponent.addMouseListener(dragListener); } @Override @@ -162,6 +162,7 @@ public class DarkTabFrameTabLabelUI extends DarkLabelUI implements PropertyChang tabComponent.removeMouseListener(mouseListener); uninstallAccelerator(tabComponent.getTabFrame()); tabComponent.removeMouseMotionListener(dragListener); + tabComponent.removeMouseListener(dragListener); dragListener = null; } diff --git a/src/main/java/com/weis/darklaf/ui/tabframe/TabDragListener.java b/src/main/java/com/weis/darklaf/ui/tabframe/TabDragListener.java index 0630e994..c5f82aa6 100644 --- a/src/main/java/com/weis/darklaf/ui/tabframe/TabDragListener.java +++ b/src/main/java/com/weis/darklaf/ui/tabframe/TabDragListener.java @@ -30,6 +30,7 @@ import org.jdesktop.jxlayer.JXLayer; import org.jetbrains.annotations.NotNull; import javax.swing.*; +import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; @@ -41,8 +42,22 @@ public class TabDragListener extends MouseAdapter { this.tabComponent = tabComponent; } + protected Point origin; + + @Override + public void mousePressed(@NotNull final MouseEvent e) { + origin = e.getPoint(); + } + + @Override + public void mouseReleased(final MouseEvent e) { + origin = null; + } + @Override public void mouseDragged(@NotNull final MouseEvent e) { + if (origin == null) origin = e.getPoint(); + if (distance(origin, e.getPoint()) < 100) return; var th = tabComponent.getTabFrame().getTransferHandler(); if (th != null && tabComponent.getTabFrame().isDndEnabled()) { var p = e.getPoint(); @@ -57,4 +72,8 @@ public class TabDragListener extends MouseAdapter { TransferHandler.MOVE); } } + + protected int distance(@NotNull final Point p1, @NotNull final Point p2) { + return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y); + } }