diff options
author | isXander <xandersmith2008@gmail.com> | 2023-06-03 16:50:20 +0100 |
---|---|---|
committer | isXander <xandersmith2008@gmail.com> | 2023-06-03 16:50:20 +0100 |
commit | 65ed43c30dbc65b1e4fb3b74a8a4ec42807d9c6d (patch) | |
tree | 3922e2998fce163ad3fbf6f5585db697f6e31237 /common/src/main/java/dev | |
parent | d32297fd0b851c3ca700a965e31cf19cee0d65c8 (diff) | |
download | YetAnotherConfigLib-65ed43c30dbc65b1e4fb3b74a8a4ec42807d9c6d.tar.gz YetAnotherConfigLib-65ed43c30dbc65b1e4fb3b74a8a4ec42807d9c6d.tar.bz2 YetAnotherConfigLib-65ed43c30dbc65b1e4fb3b74a8a4ec42807d9c6d.zip |
Better distribution of tabs (thanks MattiDragon)
Diffstat (limited to 'common/src/main/java/dev')
-rw-r--r-- | common/src/main/java/dev/isxander/yacl/gui/tab/ScrollableNavigationBar.java | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/common/src/main/java/dev/isxander/yacl/gui/tab/ScrollableNavigationBar.java b/common/src/main/java/dev/isxander/yacl/gui/tab/ScrollableNavigationBar.java index 8132ccc..b452495 100644 --- a/common/src/main/java/dev/isxander/yacl/gui/tab/ScrollableNavigationBar.java +++ b/common/src/main/java/dev/isxander/yacl/gui/tab/ScrollableNavigationBar.java @@ -35,20 +35,27 @@ public class ScrollableNavigationBar extends TabNavigationBar { @Override public void arrangeElements() { int noScrollWidth = this.width - NAVBAR_MARGIN*2; - int minimumSize = tabButtons.stream() - .map(AbstractWidget::getMessage) - .mapToInt(label -> font.width(label) + 3) - .min().orElse(0); - int singleTabWidth = Math.max(noScrollWidth / Math.min(this.tabButtons.size(), 3), minimumSize); + + // first pass: set the width of each tab button for (TabButton tabButton : this.tabButtons) { - tabButton.setWidth(singleTabWidth); + tabButton.setWidth(font.width(tabButton.getMessage()) + 10); + } + + // second pass: redistribute remaining width evenly + for (TabButton tabButton : tabButtons) { + int totalWidth = tabButtons.stream().mapToInt(AbstractWidget::getWidth).sum(); + int remainingWidth = noScrollWidth - totalWidth; + if (remainingWidth > 0) { + int extra = remainingWidth / tabButtons.size(); + tabButton.setWidth(tabButton.getWidth() + extra); + } } this.layout.arrangeElements(); this.layout.setY(0); this.scrollOffset = 0; - int allTabsWidth = singleTabWidth * this.tabButtons.size(); + int allTabsWidth = this.tabButtons.stream().mapToInt(AbstractWidget::getWidth).sum(); this.layout.setX(Math.max((this.width - allTabsWidth) / 2, NAVBAR_MARGIN)); this.maxScrollOffset = Math.max(0, allTabsWidth - noScrollWidth); } |