diff --git a/crates/nsh-platform-android/java/com/longbridge/nsh/TabBarComponent.java b/crates/nsh-platform-android/java/com/longbridge/nsh/TabBarComponent.java index d443b0506f..6fc0c4a0d8 100644 --- a/crates/nsh-platform-android/java/com/longbridge/nsh/TabBarComponent.java +++ b/crates/nsh-platform-android/java/com/longbridge/nsh/TabBarComponent.java @@ -43,10 +43,15 @@ final class TabBarComponent implements NshComponent { /** Its name ({@code nsh_core::platform::TAB_BAR_COMPONENT}). */ static final String NAME = "nsh/tab-bar"; - /** Material 3: the bar's height, the indicator's size, the icon's and the label's. */ + /** + * Material 3: the bar's height (its least: a label scaled up by the font size makes it taller), the indicator's + * size, the icon's and the label's. + */ private static final float BAR_DP = 80, INDICATOR_W_DP = 64, INDICATOR_H_DP = 32, ICON_DP = 24, LABEL_SP = 12; /** Material 3's badges: the small one's size, the large one's height and label size. */ private static final float DOT_DP = 6, BADGE_DP = 16, BADGE_SP = 11; + /** A tab's space above its indicator, between the indicator and the label, and below the label. */ + private static final float TOP_DP = 12, LABEL_GAP_DP = 4, BOTTOM_DP = 16; private final Context context; private final NshComponentContext events; @@ -68,6 +73,8 @@ final class TabBarComponent implements NshComponent { private Integer tint; /** The bottom inset the bar reaches under, in px. */ private int inset; + /** The tabs' height, in px: {@link #BAR_DP}, or more for the labels at the font size. */ + private int content; private final BottomBarVisibility visibility = new BottomBarVisibility(); private boolean appliedHidden; @@ -78,9 +85,29 @@ final class TabBarComponent implements NshComponent { this.images = images; this.density = context.getResources().getDisplayMetrics().density; this.bar = new Bar(context); + this.content = px(BAR_DP); events.setSize(0, BAR_DP); } + /** + * Sizes the bar for its labels: with a larger font size (the system's font scale), a label is taller than + * Material's 80 dp bar leaves it, and the bar grows to keep it whole (80 dp is Material's least height). + */ + private void fit() { + // A label down to its text's descent: the font's padding below that is empty, and may reach into the space + // under the label (at the default size the label then fits Material's 80 dp). + int labels = 0; + for (Item item : items) { + item.label.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); + android.graphics.Paint.FontMetricsInt font = item.label.getPaint().getFontMetricsInt(); + labels = Math.max(labels, item.label.getMeasuredHeight() - (font.bottom - font.descent)); + } + int next = Math.max(px(BAR_DP), px(TOP_DP) + px(INDICATOR_H_DP) + px(LABEL_GAP_DP) + labels + px(BOTTOM_DP)); + if (next == content) return; + content = next; + events.setSize(0, (content + inset) / density); + } + @Override public View view() { return bar; @@ -224,6 +251,7 @@ private void setItems(List tabs) { svgIcons = drawn; paint(); select(); + fit(); if (!documents.isEmpty()) { int generation = iconGeneration; iconLoad = images.loadSvgIcons(documents.toArray(new String[0]), px(ICON_DP), icons -> { @@ -325,7 +353,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) { } if (bottom != inset) { inset = bottom; - events.setSize(0, BAR_DP + inset / density); + events.setSize(0, (content + inset) / density); } if (left != getPaddingLeft() || right != getPaddingRight() || inset != getPaddingBottom()) { setPadding(left, 0, right, inset); @@ -339,6 +367,7 @@ protected void onConfigurationChanged(Configuration config) { super.onConfigurationChanged(config); appearanceChanged(); for (Item item : items) item.textSize(); + fit(); } @Override @@ -411,7 +440,7 @@ private final class Item extends LinearLayout { this.iconName = iconName; setOrientation(VERTICAL); setGravity(Gravity.CENTER_HORIZONTAL); - setPadding(0, px(12), 0, px(16)); + setPadding(0, px(TOP_DP), 0, px(BOTTOM_DP)); setClickable(true); setFocusable(true); // TalkBack says the badge with the title, as VoiceOver reads iOS's @@ -480,7 +509,7 @@ private final class Item extends LinearLayout { label.setTextSize(TypedValue.COMPLEX_UNIT_SP, LABEL_SP); label.setTypeface(TextLayouts.weighted(Typeface.DEFAULT, 400, false, 500, false)); LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); - lp.topMargin = px(4); + lp.topMargin = px(LABEL_GAP_DP); addView(label, lp); } diff --git a/crates/nsh-platform-android/tests/TabBarLargeText.tsx b/crates/nsh-platform-android/tests/TabBarLargeText.tsx new file mode 100644 index 0000000000..dbe7b62749 --- /dev/null +++ b/crates/nsh-platform-android/tests/TabBarLargeText.tsx @@ -0,0 +1,29 @@ +import { fontScale, mount, safeArea, signal, Text, View } from "nsh"; +import { Icon, TabBar } from "nsh/mobile"; + +function App() { + const selected = signal(0); + return ( + + Tab bar + + {fontScale() === 1 ? "Default text" : fontScale() >= 1.5 ? "Larger text" : "Other text"} + + {/* As tall as the navigation bar the tab bar reaches under. */} + + + }, + { title: "Search", icon: }, + { title: "Settings", icon: }, + ]} + selectedIndex={selected()} + onChange={({ index }) => selected.set(index)} + /> + + + ); +} + +mount(App); diff --git a/crates/nsh-platform-android/tests/tab-bar-large-text.test.tsx b/crates/nsh-platform-android/tests/tab-bar-large-text.test.tsx new file mode 100644 index 0000000000..72eb2aca4e --- /dev/null +++ b/crates/nsh-platform-android/tests/tab-bar-large-text.test.tsx @@ -0,0 +1,18 @@ +// Run with --target android at the default text size and again after +// `adb shell settings put system font_scale 2.0` (then put back 1.0): the bar +// grows with its labels, so a larger text size does not cut them off. +import { expect, open, test } from "nsh/test"; + +test("the tab bar is tall enough for its labels at the system's text size", async () => { + const app = await open("./TabBarLargeText.tsx"); + await app.waitFor("Tab bar"); + // The tabs, above the navigation bar the bar reaches under. + const tabs = app.layout("id:tab-bar")!.height - app.layout("id:inset")!.height; + if (app.shows("Default text")) { + // Material 3's 80 dp. + expect(Math.round(tabs)).toBe(80); + } else if (app.shows("Larger text")) { + // 12 sp labels at 1.5× and more need more than the 16 dp 80 dp leaves them. + expect(tabs).toBeGreaterThan(80); + } +});