Browse Source

Fixed tabframe layout when font is large.

pull/130/head
weisj 5 years ago
parent
commit
772f82283e
  1. 34
      core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/TabbedPaneTransferHandler.java
  2. 92
      core/src/main/java/com/github/weisj/darklaf/ui/tabframe/TabFrameLayout.java
  3. 14
      core/src/main/java/com/github/weisj/darklaf/ui/tabframe/TabFrameTransferHandler.java
  4. 22
      core/src/main/java/com/github/weisj/darklaf/util/ImageUtil.java

34
core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/TabbedPaneTransferHandler.java

@ -26,7 +26,7 @@ package com.github.weisj.darklaf.ui.tabbedpane;
import com.github.weisj.darklaf.util.ImageUtil;
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.TabbedPaneUI;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
@ -34,7 +34,6 @@ import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.*;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.lang.reflect.Field;
/**
@ -161,8 +160,6 @@ public class TabbedPaneTransferHandler extends TransferHandler implements DropTa
ui.setRolloverTab(-1);
createDragImage(tabPane, ui);
ui.setSourceIndicator(currentTransferable.transferData.tabIndex);
} else {
createDragImage(tabPane, null);
}
if ((ui != null && !ui.scrollableTabLayoutEnabled())
|| tabPane.getTabLayoutPolicy() == JTabbedPane.WRAP_TAB_LAYOUT) {
@ -172,24 +169,11 @@ public class TabbedPaneTransferHandler extends TransferHandler implements DropTa
}
protected void createDragImage(final JTabbedPane tabbedPane, final DarkTabbedPaneUI ui) {
Image tabImage = ImageUtil.scaledImageFromComponent(tabbedPane, currentTransferable.transferData.tabBounds);
Color color = ui != null ? ui.getDragBorderColor()
: tabbedPane.getBackgroundAt(currentTransferable.transferData.tabIndex);
Image tabImage = ImageUtil.createDragImage(tabbedPane, currentTransferable.transferData.tabBounds, 2, color);
int w = tabImage.getWidth(null);
int h = tabImage.getHeight(null);
Graphics g = tabImage.getGraphics();
if (ui != null) {
g.setColor(ui.getDragBorderColor());
} else {
g.setColor(tabbedPane.getBackgroundAt(currentTransferable.transferData.tabIndex).brighter());
}
int lw = 2;
g.fillRect(0, 0, w, lw);
g.fillRect(0, 0, lw, h);
g.fillRect(w - lw, 0, lw, h);
g.fillRect(0, h - lw, w, lw);
g.dispose();
setDragImageOffset(new Point(w / 2, h / 2));
setDragImage(tabImage);
}
@ -208,17 +192,11 @@ public class TabbedPaneTransferHandler extends TransferHandler implements DropTa
private DarkTabbedPaneUI supportsIndicator(final Component c) {
JComponent jc = (JComponent) c;
ComponentUI ui;
try {
Field field = JComponent.class.getDeclaredField("ui");
field.setAccessible(true);
ui = (ComponentUI) field.get(jc);
if (c instanceof JTabbedPane) {
TabbedPaneUI ui = ((JTabbedPane) c).getUI();
if (ui instanceof DarkTabbedPaneUI) {
return ((DarkTabbedPaneUI) ui);
}
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}
return null;
}

92
core/src/main/java/com/github/weisj/darklaf/ui/tabframe/TabFrameLayout.java

@ -84,15 +84,15 @@ public class TabFrameLayout implements LayoutManager {
@Override
public void layoutContainer(final Container parent) {
Dimension dim = parent.getSize();
int topSize = tabFrame.getTopTabCount();
int bottomSize = tabFrame.getBottomTabCount();
int leftSize = tabFrame.getLeftTabCount();
int rightSize = tabFrame.getRightTabCount();
int topCount = tabFrame.getTopTabCount();
int bottomCount = tabFrame.getBottomTabCount();
int leftCount = tabFrame.getLeftTabCount();
int rightCount = tabFrame.getRightTabCount();
if (isDraggedOver(Alignment.NORTH)) topSize++;
if (isDraggedOver(Alignment.SOUTH)) bottomSize++;
if (isDraggedOver(Alignment.EAST)) rightSize++;
if (isDraggedOver(Alignment.WEST)) leftSize++;
if (isDraggedOver(Alignment.NORTH)) topCount++;
if (isDraggedOver(Alignment.SOUTH)) bottomCount++;
if (isDraggedOver(Alignment.EAST)) rightCount++;
if (isDraggedOver(Alignment.WEST)) leftCount++;
ui.getDropComponentBottom().setSize(0, 0);
ui.getDropComponentLeft().setSize(0, 0);
@ -104,10 +104,10 @@ public class TabFrameLayout implements LayoutManager {
leftHeight = calculateMaxTabSize(Alignment.WEST);
rightHeight = calculateMaxTabSize(Alignment.EAST);
layoutTopTab(dim, topSize, leftSize, rightSize);
layoutBottomTab(dim, bottomSize, leftSize, rightSize);
layoutLeftTab(dim, leftSize);
layoutRightTab(dim, rightSize);
layoutTopTab(dim, topCount, leftCount, rightCount);
layoutBottomTab(dim, bottomCount, leftCount, rightCount);
layoutLeftTab(dim, leftCount);
layoutRightTab(dim, rightCount);
Component leftPane = ui.getLeftContainer();
Component rightPane = ui.getRightContainer();
@ -118,58 +118,57 @@ public class TabFrameLayout implements LayoutManager {
dim.height - topPane.getHeight() - bottomPane.getHeight());
}
protected void layoutTopTab(final Dimension dim, final int topSize, final int leftSize, final int rightSize) {
protected void layoutTopTab(final Dimension dim, final int topCount, final int leftCount, final int rightCount) {
Component topComp = tabFrame.getTopTabContainer();
if (topSize > 0) {
topComp.setBounds(0, 0, dim.width, tabFrame.getTabSize());
layoutHorizontal(dim, Alignment.NORTH, Alignment.NORTH_EAST, 0, leftSize, rightSize, topHeight);
if (topCount > 0) {
topComp.setBounds(0, 0, dim.width, topHeight);
layoutHorizontal(dim, Alignment.NORTH, Alignment.NORTH_EAST, 0, leftCount, rightCount, topHeight);
} else if (draggedOver[getIndex(Alignment.NORTH)]) {
int size = tabFrame.getTabSize();
topComp.setBounds(0, 0, dim.width, size);
topComp.setBounds(0, 0, dim.width, topHeight);
if (ui.getDestIndex() >= -1) {
layoutHorizontalDrop(Alignment.NORTH, leftSize, rightSize, size, 0);
layoutHorizontalDrop(Alignment.NORTH, leftCount, rightCount, topHeight, 0);
}
} else {
topComp.setBounds(0, 0, 0, 0);
}
}
protected void layoutBottomTab(final Dimension dim, final int bottomSize, final int leftSize, final int rightSize) {
protected void layoutBottomTab(final Dimension dim, final int bottomCount, final int leftCount,
final int rightCount) {
Component bottomComp = tabFrame.getBottomTabContainer();
if (bottomSize > 0) {
if (bottomCount > 0) {
bottomComp.setBounds(0, dim.height - bottomHeight, dim.width, bottomHeight);
layoutHorizontal(dim, Alignment.SOUTH_WEST, Alignment.SOUTH, 1, leftSize, rightSize, bottomHeight);
layoutHorizontal(dim, Alignment.SOUTH_WEST, Alignment.SOUTH, 1, leftCount, rightCount, bottomHeight);
} else if (draggedOver[getIndex(Alignment.SOUTH)]) {
int size = tabFrame.getTabSize();
bottomComp.setBounds(0, dim.height - size, dim.width, size);
bottomComp.setBounds(0, dim.height - bottomHeight, dim.width, bottomHeight);
if (ui.getDestIndex() >= -1) {
layoutHorizontalDrop(Alignment.SOUTH_WEST, leftSize, rightSize, size, 1);
layoutHorizontalDrop(Alignment.SOUTH_WEST, leftCount, rightCount, bottomHeight, 1);
}
} else {
bottomComp.setBounds(0, 0, 0, 0);
}
}
protected void layoutHorizontalDrop(final Alignment left, final int leftSize, final int rightSize,
protected void layoutHorizontalDrop(final Alignment left, final int leftCount, final int rightCount,
final int size, final int yOff) {
Alignment a = ui.getDestAlign();
Dimension dropSize = ui.getDropSize();
Component dropComp = ui.getDropComponent(left);
Component tabComp = ui.getTabContainer(left);
if (a == left) {
int x = leftSize > 0 ? leftHeight : 0;
int x = leftCount > 0 ? leftHeight : 0;
dropComp.setBounds(x, yOff, dropSize.width, size);
} else {
int x = rightSize > 0 ? tabComp.getWidth() - rightHeight : tabComp.getWidth();
int x = rightCount > 0 ? tabComp.getWidth() - rightHeight : tabComp.getWidth();
dropComp.setBounds(x - dropSize.width, yOff, dropSize.width, size);
}
}
protected void layoutHorizontal(final Dimension dim, final Alignment left, final Alignment right,
final int yOff, final int leftSize, final int rightSize, final int tabHeight) {
Point start = new Point(leftSize > 0 ? leftHeight : 0, yOff);
final int yOff, final int leftCount, final int rightCount, final int tabHeight) {
Point start = new Point(leftCount > 0 ? leftHeight : 0, yOff);
int leftEnd = layoutTabArea(start, left, true, tabHeight - 1);
start.x = rightSize > 0 ? dim.width - rightHeight : dim.width;
start.x = rightCount > 0 ? dim.width - rightHeight : dim.width;
int rightStart = layoutTabArea(start, right, false, tabHeight - 1);
if (rightStart < leftEnd) {
shift[getIndex(left)] = leftEnd - rightStart;
@ -179,18 +178,18 @@ public class TabFrameLayout implements LayoutManager {
}
}
protected void layoutLeftTab(final Dimension dim, final int leftSize) {
protected void layoutLeftTab(final Dimension dim, final int leftCount) {
Component leftPane = ui.getLeftContainer();
Component topPane = tabFrame.getTopTabContainer();
Component bottomPane = tabFrame.getBottomTabContainer();
if (leftSize > 0 || draggedOver[getIndex(Alignment.WEST)]) {
int size = leftSize > 0 ? leftHeight : tabFrame.getTabSize();
if (leftCount > 0 || draggedOver[getIndex(Alignment.WEST)]) {
int size = leftCount > 0 ? leftHeight : tabFrame.getTabSize();
int height = dim.height - topPane.getHeight() - bottomPane.getHeight();
leftPane.setBounds(0, topPane.getHeight(), size, height + (height % 2));
tabFrame.getLeftTabContainer().setPreferredSize(new Dimension(leftPane.getHeight(),
leftPane.getWidth()));
tabFrame.getLeftTabContainer().setSize(tabFrame.getLeftTabContainer().getPreferredSize());
if (leftSize > 0) {
if (leftCount > 0) {
Point start = new Point(leftPane.getHeight(), 0);
int topStart = layoutTabArea(start, Alignment.NORTH_WEST, false, size - 1);
start.x = 0;
@ -222,18 +221,18 @@ public class TabFrameLayout implements LayoutManager {
}
}
protected void layoutRightTab(final Dimension dim, final int rightSize) {
protected void layoutRightTab(final Dimension dim, final int rightCount) {
Component rightPane = ui.getRightContainer();
Component topPane = tabFrame.getTopTabContainer();
Component bottomPane = tabFrame.getBottomTabContainer();
if (rightSize > 0 || draggedOver[getIndex(Alignment.EAST)]) {
int size = rightSize > 0 ? rightHeight : tabFrame.getTabSize();
if (rightCount > 0 || draggedOver[getIndex(Alignment.EAST)]) {
int size = rightCount > 0 ? rightHeight : tabFrame.getTabSize();
int height = dim.height - topPane.getHeight() - bottomPane.getHeight();
rightPane.setBounds(dim.width - rightHeight, topPane.getHeight(), size, height + (height % 2));
tabFrame.getRightTabContainer().setPreferredSize(
new Dimension(rightPane.getHeight(), rightPane.getWidth()));
tabFrame.getRightTabContainer().setSize(tabFrame.getRightTabContainer().getPreferredSize());
if (rightSize > 0) {
if (rightCount > 0) {
Point start = new Point(0, 0);
int topEnd = layoutTabArea(start, Alignment.EAST, true, size - 1);
start.x = tabFrame.getRightTabContainer().getWidth();
@ -267,17 +266,6 @@ public class TabFrameLayout implements LayoutManager {
}
}
public int getShift(final Alignment a) {
switch (a) {
case NORTH:
case EAST:
case SOUTH_WEST:
case NORTH_WEST:
return 0;
}
return shift[getIndex(a)];
}
protected int getIndex(final Alignment a) {
switch (a) {
case NORTH:
@ -367,10 +355,10 @@ public class TabFrameLayout implements LayoutManager {
protected int calculateMaxTabSize(final Alignment a) {
int max = tabFrame.getTabSize();
for (TabFrameTab c : tabFrame.tabsForAlignment(a)) {
max = Math.max(max, c.getComponent().getMaximumSize().height + 1);
max = Math.max(max, c.getComponent().getPreferredSize().height + 1);
}
for (TabFrameTab c : tabFrame.tabsForAlignment(tabFrame.getPeer(a))) {
max = Math.max(max, c.getComponent().getMaximumSize().height + 1);
max = Math.max(max, c.getComponent().getPreferredSize().height + 1);
}
return max;
}

14
core/src/main/java/com/github/weisj/darklaf/ui/tabframe/TabFrameTransferHandler.java

@ -301,21 +301,9 @@ public class TabFrameTransferHandler extends TransferHandler implements DropTarg
protected void createDragImage(final TabFrameUI ui) {
Component comp = currentTransferable.transferData.tab.getComponent();
Image tabImage = ImageUtil.scaledImageFromComponent(comp, new Rectangle(0, 0, comp.getWidth(),
comp.getHeight()));
Image tabImage = ImageUtil.createDragImage(comp, 2, ui.getDragBorderColor());
int w = tabImage.getWidth(null);
int h = tabImage.getHeight(null);
Graphics g = tabImage.getGraphics();
g.setColor(ui.getDragBorderColor());
int lw = 2;
g.fillRect(0, 0, w, lw);
g.fillRect(0, 0, lw, h);
g.fillRect(w - lw, 0, lw, h);
g.fillRect(0, h - lw, w, lw);
g.dispose();
setDragImageOffset(new Point(w / 2, h / 2));
setDragImage(tabImage);
}

22
core/src/main/java/com/github/weisj/darklaf/util/ImageUtil.java

@ -32,10 +32,30 @@ import java.awt.image.BufferedImage;
*/
public final class ImageUtil {
private ImageUtil() {
}
public static Image createDragImage(final Component c, final int lw, final Color borderColor) {
return createDragImage(c, new Rectangle(0, 0, c.getWidth(), c.getHeight()), lw, borderColor);
}
public static Image createDragImage(final Component c, final Rectangle bounds,
final int lw, final Color borderColor) {
Image tabImage = ImageUtil.scaledImageFromComponent(c, bounds);
int w = tabImage.getWidth(null);
int h = tabImage.getHeight(null);
Graphics g = tabImage.getGraphics();
g.setColor(borderColor);
g.fillRect(0, 0, w, lw);
g.fillRect(0, 0, lw, h);
g.fillRect(w - lw, 0, lw, h);
g.fillRect(0, h - lw, w, lw);
g.dispose();
return tabImage;
}
/**
* Create image from component.
*

Loading…
Cancel
Save