this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

AssetsPanel: use a more sensible layout

+124 -100
+2 -2
src/main/java/app/pane/AssetsPanel.java
··· 34 34 import net.miginfocom.swing.MigLayout; 35 35 import util.Logger; 36 36 import util.ui.ThemedIcon; 37 - import util.ui.WrapLayout; 37 + import util.ui.UniformGridLayout; 38 38 39 39 public class AssetsPanel extends JPanel 40 40 { ··· 57 57 breadcrumbBar = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)); 58 58 add(breadcrumbBar, "growx, wrap"); 59 59 60 - resultsPanel = new JPanel(new WrapLayout(FlowLayout.LEFT, 0, 0)); 60 + resultsPanel = new JPanel(new UniformGridLayout(80, 80, 0, 0)); 61 61 scrollPane = new JScrollPane(resultsPanel); 62 62 scrollPane.setBorder(null); 63 63 add(scrollPane, "grow, push");
+122
src/main/java/util/ui/UniformGridLayout.java
··· 1 + package util.ui; 2 + 3 + import java.awt.*; 4 + import javax.swing.*; 5 + 6 + /** 7 + * A layout manager that arranges uniform-size cells in a responsive grid. 8 + * The number of columns adjusts automatically based on the container width. 9 + */ 10 + public class UniformGridLayout implements LayoutManager 11 + { 12 + private final int cellWidth; 13 + private final int cellHeight; 14 + private final int hgap; 15 + private final int vgap; 16 + 17 + public UniformGridLayout(int cellWidth, int cellHeight, int hgap, int vgap) 18 + { 19 + this.cellWidth = cellWidth; 20 + this.cellHeight = cellHeight; 21 + this.hgap = hgap; 22 + this.vgap = vgap; 23 + } 24 + 25 + @Override 26 + public void addLayoutComponent(String name, Component comp) {} 27 + 28 + @Override 29 + public void removeLayoutComponent(Component comp) {} 30 + 31 + @Override 32 + public Dimension preferredLayoutSize(Container target) 33 + { 34 + synchronized (target.getTreeLock()) { 35 + Insets insets = target.getInsets(); 36 + int count = getVisibleCount(target); 37 + 38 + if (count == 0) 39 + return new Dimension(insets.left + insets.right, insets.top + insets.bottom); 40 + 41 + int cols = getColumns(target); 42 + if (cols < 1) 43 + cols = 1; 44 + int rows = (count + cols - 1) / cols; 45 + 46 + int width = cols * cellWidth + (cols - 1) * hgap + insets.left + insets.right; 47 + int height = rows * cellHeight + (rows - 1) * vgap + insets.top + insets.bottom; 48 + 49 + return new Dimension(width, height); 50 + } 51 + } 52 + 53 + @Override 54 + public Dimension minimumLayoutSize(Container target) 55 + { 56 + Insets insets = target.getInsets(); 57 + return new Dimension(cellWidth + insets.left + insets.right, cellHeight + insets.top + insets.bottom); 58 + } 59 + 60 + @Override 61 + public void layoutContainer(Container target) 62 + { 63 + synchronized (target.getTreeLock()) { 64 + Insets insets = target.getInsets(); 65 + int cols = getColumns(target); 66 + if (cols < 1) 67 + cols = 1; 68 + 69 + int x = insets.left; 70 + int y = insets.top; 71 + int col = 0; 72 + 73 + for (int i = 0; i < target.getComponentCount(); i++) { 74 + Component c = target.getComponent(i); 75 + if (!c.isVisible()) 76 + continue; 77 + 78 + c.setBounds(x, y, cellWidth, cellHeight); 79 + 80 + col++; 81 + if (col >= cols) { 82 + col = 0; 83 + x = insets.left; 84 + y += cellHeight + vgap; 85 + } 86 + else { 87 + x += cellWidth + hgap; 88 + } 89 + } 90 + } 91 + } 92 + 93 + private int getColumns(Container target) 94 + { 95 + Insets insets = target.getInsets(); 96 + int availableWidth = target.getWidth() - insets.left - insets.right; 97 + 98 + // If inside a scroll pane, use the viewport width 99 + Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target); 100 + if (scrollPane != null) { 101 + JViewport viewport = ((JScrollPane) scrollPane).getViewport(); 102 + if (viewport != null) { 103 + availableWidth = viewport.getWidth() - insets.left - insets.right; 104 + } 105 + } 106 + 107 + if (availableWidth <= 0) 108 + return 1; 109 + 110 + return Math.max(1, (availableWidth + hgap) / (cellWidth + hgap)); 111 + } 112 + 113 + private int getVisibleCount(Container target) 114 + { 115 + int count = 0; 116 + for (int i = 0; i < target.getComponentCount(); i++) { 117 + if (target.getComponent(i).isVisible()) 118 + count++; 119 + } 120 + return count; 121 + } 122 + }
-98
src/main/java/util/ui/WrapLayout.java
··· 1 - package util.ui; 2 - 3 - import java.awt.*; 4 - import javax.swing.*; 5 - 6 - /** 7 - * A FlowLayout that supports wrapping of components to new lines. 8 - * TODO: make not terrible and slow 9 - */ 10 - public class WrapLayout extends FlowLayout { 11 - public WrapLayout() { 12 - super(); 13 - } 14 - 15 - public WrapLayout(int align) { 16 - super(align); 17 - } 18 - 19 - public WrapLayout(int align, int hgap, int vgap) { 20 - super(align, hgap, vgap); 21 - } 22 - 23 - @Override 24 - public Dimension preferredLayoutSize(Container target) { 25 - return layoutSize(target, true); 26 - } 27 - 28 - @Override 29 - public Dimension minimumLayoutSize(Container target) { 30 - Dimension minimum = layoutSize(target, false); 31 - minimum.width -= (getHgap() + 1); 32 - return minimum; 33 - } 34 - 35 - private Dimension layoutSize(Container target, boolean preferred) { 36 - synchronized (target.getTreeLock()) { 37 - int targetWidth = target.getSize().width; 38 - if (targetWidth == 0) { 39 - targetWidth = Integer.MAX_VALUE; 40 - } 41 - 42 - int hgap = getHgap(); 43 - int vgap = getVgap(); 44 - Insets insets = target.getInsets(); 45 - int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2); 46 - int maxWidth = targetWidth - horizontalInsetsAndGap; 47 - 48 - Dimension dim = new Dimension(0, 0); 49 - int rowWidth = 0; 50 - int rowHeight = 0; 51 - 52 - int nmembers = target.getComponentCount(); 53 - 54 - for (int i = 0; i < nmembers; i++) { 55 - Component m = target.getComponent(i); 56 - 57 - if (m.isVisible()) { 58 - Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize(); 59 - 60 - if (rowWidth + d.width > maxWidth && rowWidth > 0) { 61 - addRow(dim, rowWidth, rowHeight); 62 - rowWidth = 0; 63 - rowHeight = 0; 64 - } 65 - 66 - if (rowWidth != 0) { 67 - rowWidth += hgap; 68 - } 69 - 70 - rowWidth += d.width; 71 - rowHeight = Math.max(rowHeight, d.height); 72 - } 73 - } 74 - 75 - addRow(dim, rowWidth, rowHeight); 76 - 77 - dim.width += horizontalInsetsAndGap; 78 - dim.height += insets.top + insets.bottom + vgap * 2; 79 - 80 - Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target); 81 - if (scrollPane != null && target.isValid()) { 82 - dim.width -= (hgap + 1); 83 - } 84 - 85 - return dim; 86 - } 87 - } 88 - 89 - private void addRow(Dimension dim, int rowWidth, int rowHeight) { 90 - dim.width = Math.max(dim.width, rowWidth); 91 - 92 - if (dim.height > 0) { 93 - dim.height += getVgap(); 94 - } 95 - 96 - dim.height += rowHeight; 97 - } 98 - }