From 82de1a35ae0575972527c88dfb0e23981a6b0913 Mon Sep 17 00:00:00 2001 From: weisj Date: Wed, 9 Oct 2019 01:41:54 +0200 Subject: [PATCH] Fixed rendering while dragging when custom tab components are installed. --- .../components/ClosableTabbedPane.java | 2 +- .../ui/tabbedpane/DarkTabbedPaneUI.java | 62 ++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/weis/darklaf/components/ClosableTabbedPane.java b/src/main/java/com/weis/darklaf/components/ClosableTabbedPane.java index 192063a9..c184c745 100644 --- a/src/main/java/com/weis/darklaf/components/ClosableTabbedPane.java +++ b/src/main/java/com/weis/darklaf/components/ClosableTabbedPane.java @@ -16,7 +16,7 @@ public class ClosableTabbedPane extends JTabbedPane { return; } super.insertTab(title, icon, component, tip, index); - setTabComponentAt(index, new ClosableTabComponent(this)); + setTabComponentAt(indexOfComponent(component), new ClosableTabComponent(this)); notifyTabListeners(new TabEvent(this, TabEvent.TAB_OPENED, "tabOpened", index)); } diff --git a/src/main/java/com/weis/darklaf/ui/tabbedpane/DarkTabbedPaneUI.java b/src/main/java/com/weis/darklaf/ui/tabbedpane/DarkTabbedPaneUI.java index 3f3c1f1f..97a39481 100644 --- a/src/main/java/com/weis/darklaf/ui/tabbedpane/DarkTabbedPaneUI.java +++ b/src/main/java/com/weis/darklaf/ui/tabbedpane/DarkTabbedPaneUI.java @@ -596,6 +596,9 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge { super.paint(g); if (dragging) { paintTab(g, tabPane.getTabPlacement(), dragRect, tabPane.getSelectedIndex(), iconRect, textRect); + var comp = tabPane.getTabComponentAt(dropSourceIndex); + g.translate(comp.getX(), comp.getY()); + comp.print(g); } } @@ -1202,7 +1205,7 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge { } } } - super.layoutTabComponents(); + layoutTabComponents(); if (shouldChangeFocus) { if (!requestFocusForVisibleComponent()) { tabPane.requestFocus(); @@ -1211,6 +1214,11 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge { } } + @Override + protected void layoutTabComponents() { + DarkTabbedPaneUI.this.layoutTabComponents(); + } + @SuppressWarnings("SuspiciousNameCombination") @Override protected void calculateTabRects(final int tabPlacement, final int tabCount) { @@ -1761,6 +1769,58 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge { } } } + + @Override + protected void layoutTabComponents() { + DarkTabbedPaneUI.this.layoutTabComponents(); + } + } + + protected void layoutTabComponents() { + if (tabContainer == null) { + return; + } + Rectangle rect = new Rectangle(); + Point delta = new Point(-tabContainer.getX(), -tabContainer.getY()); + if (scrollableTabLayoutEnabled()) { + translatePointToTabPanel(0, 0, delta); + } + for (int i = 0; i < tabPane.getTabCount(); i++) { + Component c = tabPane.getTabComponentAt(i); + if (c == null) { + continue; + } + getTabBounds(i, rect); + + //Adjust dragged component position. + if (i == dropSourceIndex) { + if (dragging) { + rect.setBounds(dragRect); + var p = rect.getLocation(); + var vl = tabScroller.viewport.getLocation(); + p.x += vl.x; + p.y += vl.y; + rect.setLocation(p); + } else { + rect.setBounds(0, 0, 0, 0); + } + } + + Dimension preferredSize = c.getPreferredSize(); + Insets insets = getTabInsets(tabPane.getTabPlacement(), i); + int outerX = rect.x + insets.left + delta.x; + int outerY = rect.y + insets.top + delta.y; + int outerWidth = rect.width - insets.left - insets.right; + int outerHeight = rect.height - insets.top - insets.bottom; + //centralize component + int x = outerX + (outerWidth - preferredSize.width) / 2; + int y = outerY + (outerHeight - preferredSize.height) / 2; + int tabPlacement = tabPane.getTabPlacement(); + boolean isSeleceted = i == tabPane.getSelectedIndex(); + c.setBounds(x + getTabLabelShiftX(tabPlacement, i, isSeleceted), + y + getTabLabelShiftY(tabPlacement, i, isSeleceted), + preferredSize.width, preferredSize.height); + } } public class DarkHandler extends Handler {