Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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 -> {
Expand Down Expand Up @@ -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);
Expand All @@ -339,6 +367,7 @@ protected void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
appearanceChanged();
for (Item item : items) item.textSize();
fit();
}

@Override
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down
29 changes: 29 additions & 0 deletions crates/nsh-platform-android/tests/TabBarLargeText.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<View class="bg-background" style={{ flex: 1, paddingTop: 64 }}>
<Text class="text-foreground" style={{ alignSelf: "center" }}>Tab bar</Text>
<Text class="text-foreground" style={{ alignSelf: "center" }}>
{fontScale() === 1 ? "Default text" : fontScale() >= 1.5 ? "Larger text" : "Other text"}
</Text>
{/* As tall as the navigation bar the tab bar reaches under. */}
<View testID="inset" style={{ position: "absolute", left: 0, top: 0, width: 1, height: safeArea().bottom }} />
<View testID="tab-bar" style={{ position: "absolute", left: 0, right: 0, bottom: 0 }}>
<TabBar
items={[
{ title: "Home", icon: <Icon symbol="android:ic_menu_view" /> },
{ title: "Search", icon: <Icon symbol="android:ic_menu_search" /> },
{ title: "Settings", icon: <Icon symbol="android:ic_menu_preferences" /> },
]}
selectedIndex={selected()}
onChange={({ index }) => selected.set(index)}
/>
</View>
</View>
);
}

mount(App);
18 changes: 18 additions & 0 deletions crates/nsh-platform-android/tests/tab-bar-large-text.test.tsx
Original file line number Diff line number Diff line change
@@ -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);
}
});
Loading