|
| 1 | +package org.quiltmc.enigma.gui.util; |
| 2 | + |
| 3 | +import javax.swing.JScrollBar; |
| 4 | +import javax.swing.SwingUtilities; |
| 5 | +import java.awt.Component; |
| 6 | +import java.awt.Container; |
| 7 | +import java.awt.Dimension; |
| 8 | +import java.awt.FlowLayout; |
| 9 | +import java.awt.Insets; |
| 10 | + |
| 11 | +public class WrapLayout extends FlowLayout { |
| 12 | + public WrapLayout() { |
| 13 | + super(); |
| 14 | + } |
| 15 | + |
| 16 | + public WrapLayout(int align) { |
| 17 | + super(align); |
| 18 | + } |
| 19 | + |
| 20 | + public WrapLayout(int align, int hgap, int vgap) { |
| 21 | + super(align, hgap, vgap); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public Dimension preferredLayoutSize(Container target) { |
| 26 | + return this.computeLayoutSize(target, true); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public Dimension minimumLayoutSize(Container target) { |
| 31 | + Dimension minimum = this.computeLayoutSize(target, false); |
| 32 | + minimum.width -= (this.getHgap() + 1); |
| 33 | + |
| 34 | + return minimum; |
| 35 | + } |
| 36 | + |
| 37 | + private Dimension computeLayoutSize(Container target, boolean preferred) { |
| 38 | + int targetWidth = target.getSize().width; |
| 39 | + |
| 40 | + if (targetWidth == 0) { |
| 41 | + targetWidth = Integer.MAX_VALUE; |
| 42 | + } |
| 43 | + |
| 44 | + int horizontalGap = this.getHgap(); |
| 45 | + Insets insets = target.getInsets(); |
| 46 | + int horizontalSpacing = insets.left + insets.right + horizontalGap * 2; |
| 47 | + int maxWidth = targetWidth - horizontalSpacing; |
| 48 | + |
| 49 | + Dimension layoutSize = new Dimension(0, 0); |
| 50 | + int rowWidth = 0; |
| 51 | + int rowHeight = 0; |
| 52 | + |
| 53 | + int componentCount = target.getComponentCount(); |
| 54 | + |
| 55 | + for (int i = 0; i < componentCount; i++) { |
| 56 | + Component component = target.getComponent(i); |
| 57 | + |
| 58 | + if (component.isVisible()) { |
| 59 | + Dimension componentSize = preferred ? component.getPreferredSize() : component.getMinimumSize(); |
| 60 | + |
| 61 | + if (rowWidth + componentSize.width > maxWidth) { |
| 62 | + this.addRow(layoutSize, rowWidth, rowHeight); |
| 63 | + rowWidth = 0; |
| 64 | + rowHeight = 0; |
| 65 | + } else if (rowWidth != 0) { |
| 66 | + rowWidth += horizontalGap; |
| 67 | + } |
| 68 | + |
| 69 | + rowWidth += componentSize.width; |
| 70 | + rowHeight = Math.max(rowHeight, componentSize.height); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + this.addRow(layoutSize, rowWidth, rowHeight); |
| 75 | + |
| 76 | + layoutSize.width += horizontalSpacing; |
| 77 | + layoutSize.height += insets.top + insets.bottom + this.getVgap() * 2; |
| 78 | + |
| 79 | + if (SwingUtilities.getAncestorOfClass(JScrollBar.class, target) != null) { |
| 80 | + layoutSize.width -= (horizontalGap + 1); |
| 81 | + } |
| 82 | + |
| 83 | + return layoutSize; |
| 84 | + } |
| 85 | + |
| 86 | + private void addRow(Dimension layoutSize, int width, int height) { |
| 87 | + layoutSize.width = Math.max(layoutSize.width, width); |
| 88 | + |
| 89 | + if (layoutSize.height > 0) { |
| 90 | + layoutSize.height += this.getVgap(); |
| 91 | + } |
| 92 | + |
| 93 | + layoutSize.height += height; |
| 94 | + } |
| 95 | +} |
0 commit comments