From a05ce5306ac326c5e492ecf962a4b83cd7149f9a Mon Sep 17 00:00:00 2001 From: isXander Date: Mon, 5 Jun 2023 16:43:44 +0100 Subject: Fix improve tab layout --- .../yacl3/gui/tab/ScrollableNavigationBar.java | 23 +++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'common/src/main/java/dev') diff --git a/common/src/main/java/dev/isxander/yacl3/gui/tab/ScrollableNavigationBar.java b/common/src/main/java/dev/isxander/yacl3/gui/tab/ScrollableNavigationBar.java index 199845d..da1965f 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/tab/ScrollableNavigationBar.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/tab/ScrollableNavigationBar.java @@ -36,26 +36,31 @@ public class ScrollableNavigationBar extends TabNavigationBar { public void arrangeElements() { int noScrollWidth = this.width - NAVBAR_MARGIN*2; + int allTabsWidth = 0; // first pass: set the width of each tab button for (TabButton tabButton : this.tabButtons) { - tabButton.setWidth(font.width(tabButton.getMessage()) + 10); + int buttonWidth = font.width(tabButton.getMessage()) + 20; + allTabsWidth += buttonWidth; + tabButton.setWidth(buttonWidth); } - // 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); + if (allTabsWidth < noScrollWidth) { + int equalWidth = noScrollWidth / this.tabButtons.size(); + var smallTabs = this.tabButtons.stream().filter(btn -> btn.getWidth() < equalWidth).toList(); + var bigTabs = this.tabButtons.stream().filter(btn -> btn.getWidth() >= equalWidth).toList(); + int leftoverWidth = noScrollWidth - bigTabs.stream().mapToInt(AbstractWidget::getWidth).sum(); + int equalWidthForSmallTabs = leftoverWidth / smallTabs.size(); + for (TabButton tabButton : smallTabs) { + tabButton.setWidth(equalWidthForSmallTabs); } + + allTabsWidth = noScrollWidth; } this.layout.arrangeElements(); this.layout.setY(0); this.scrollOffset = 0; - 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); } -- cgit