Skip to content
Draft
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
2 changes: 1 addition & 1 deletion docs/architecture/native-views.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ An app registers its own platform views by name, as NSH's embedding apps do (`ns
- The first `ItemNative` makes the component with a `NativeContext`, whose `Emit` reaches `OnEvent` and whose `SetSize` gives the element a natural size, as an image's; a width of 0 asks for its height alone, as a bar as wide as it is placed.
- A prop whose value is an `AnimatedNode` is bound rather than sent, as NSH 0.3.0's are: `NativeConfig.Bindings` names each bound prop's node and whether it outputs a color, and the backend's `Animator` (`BindNative`, `ChangedNatives`) hands the component each value that changed on the frame clock, through `driver.AnimatedPropSetter` or else `SetProp` with a `float64` or a `Color`. A plain value replacing a node is sent; a removed prop is sent as nil. Android binds them in Java as a canvas's arguments are and calls the view's setters, or a registered `NsgoComponent`'s `setAnimatedNumber` and `setAnimatedColor`, never an `AndroidPropMapper`; iOS hosts' components take the optional `setAnimatedNumber:value:` and `setAnimatedColor:argb:`.
- macOS and iOS register `driver.SwitchComponent` themselves, for `core.SystemSwitch`: NSSwitch and UISwitch, showing the user's choice at once and measuring themselves. Its parent centers it rather than stretching it, as a native view with a natural size keeps its aspect when stretched.
- iOS and Android register `driver.TabBarComponent` themselves, for `mobile.TabBar`; Android's measures itself 80 points tall plus the system navigation bar it reaches under.
- iOS and Android register `driver.TabBarComponent` themselves, for `mobile.TabBar`; Android's reports a natural height of 80 points plus the part of its frame overlapping the system navigation bar, updating when it or an ancestor moves. Only that overlap becomes bottom padding, so a bar elsewhere keeps all 80 points for its tabs; an explicit height still takes precedence.
- Factories run on the main thread and may return nil; an unknown name leaves an empty box and is logged once.
- Registering differs per platform only in what `View` returns, so an app registers a target's components in files built for it, as the gallery's `components_*.go` do.

Expand Down
1 change: 1 addition & 0 deletions docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ These items wait for community contributions, and a change to one backend record
| Item | NSH reference | Done when |
| --- | --- | --- |
| SVG icons in the iOS native tab bar | — | `UITabBar` shows `mobile.TabBarItem.SVG` as a template image where `Icon` names no SF Symbol, so a `ui.TabShell` whose tabs have only SVG icons uses the system's bar on iOS as it does on Android; today iOS shows only SF Symbols and such a shell draws its bar |
| iOS tab bar size after placement changes | `ios/tabbar.go` | A native tab bar reports its natural height again when its placement or safe area changes; today it measures once at creation, including the key window's bottom safe area before attachment, while Android updates the height for its actual navigation-bar overlap |
| Windows and Linux test-thread exits | [#410](https://github.com/nsgo-dev/nsgo/issues/410) | A native test's `onMain` (Windows) or `onLoop` (Linux) propagates `Fatal`, `FailNow` and `SkipNow` to the test goroutine while preserving the window's thread, as macOS's does; Windows' helper currently waits for its five-second timeout if the posted closure exits, and Linux's lets the test carry on past the exit while the GTK thread it shares with later windows stops |
| Test input isolation on other native targets | [#392](https://github.com/nsgo-dev/nsgo/issues/392) | Windows, Linux, iOS, tvOS, watchOS and Android test hosts ignore physical input while accepting their own scripted steps, as macOS does; web tests already use a dedicated headless browser |
| Precise Windows test scrolls | [#536](https://github.com/nsgo-dev/nsgo/issues/536), `windows/testhost.go` | The test host's scroll step carries precise point distances to both native scroll views and `OnWheel`; it currently emits wheel notches scaled for native scroll views, whose distance differs from the wheel handler's and has no touchpad phases |
Expand Down
58 changes: 47 additions & 11 deletions internal/platform/android/java/dev/oneo/nsgo/TabBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.util.TypedValue;
import android.util.Base64;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowInsets;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
Expand All @@ -36,11 +39,21 @@ public interface OnChangeListener {
void onChange(int index);
}

/** Reports the bar's natural height in points to the Go layout. */
public interface OnSizeListener {
void onSize(float height);
}

private OnChangeListener listener;
private OnSizeListener sizeListener;
private int selected;
/** The theme's accent as ARGB (0: none): the selected tab's icon, label and indicator. */
private int tint;
private final float density;
/** The pixels of this bar over the system's navigation bar, kept clear as bottom padding. */
private int bottomInset;
/** Runs after every layout pass of the window, so a move of the bar or of any ancestor updates the overlap; padding changed there takes effect on the next pass instead of re-entering this one. */
private final ViewTreeObserver.OnGlobalLayoutListener placement = this::updateNavigationInset;

public TabBar(Context context) {
super(context);
Expand All @@ -55,11 +68,7 @@ public TabBar(Context context) {
*/
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
// Under the system's navigation bar, as Material's bar reaches,
// its tabs above it.
int inset = navigationInset();
if (getPaddingBottom() != inset) setPadding(0, 0, 0, inset);
int height = MeasureSpec.getMode(heightSpec) == MeasureSpec.EXACTLY ? MeasureSpec.getSize(heightSpec) : dp(80) + inset;
int height = MeasureSpec.getMode(heightSpec) == MeasureSpec.EXACTLY ? MeasureSpec.getSize(heightSpec) : dp(80) + bottomInset;
int width = MeasureSpec.getMode(widthSpec) == MeasureSpec.UNSPECIFIED ? 0 : MeasureSpec.getSize(widthSpec);
super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
Expand All @@ -69,6 +78,22 @@ public void setOnChangeListener(OnChangeListener l) {
listener = l;
}

public void setOnSizeListener(OnSizeListener l) {
sizeListener = l;
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
getViewTreeObserver().addOnGlobalLayoutListener(placement);
}

@Override
protected void onDetachedFromWindow() {
getViewTreeObserver().removeOnGlobalLayoutListener(placement);
super.onDetachedFromWindow();
}

/** Makes the tabs from titles, resource icons and optional base64 canvas drawings. */
public void setItems(String json) {
removeAllViews();
Expand Down Expand Up @@ -189,14 +214,25 @@ private Drawable drawable(String name) {
}
}

/** The height the system's navigation bar covers, in pixels. */
private int navigationInset() {
/** Keeps only the part of this bar under navigation clear, never the IME. */
private void updateNavigationInset() {
if (!isAttachedToWindow()) return;
View root = getRootView();
if (getContext() instanceof android.app.Activity) {
root = ((android.app.Activity) getContext()).getWindow().getDecorView();
WindowInsets in = getRootWindowInsets();
int inset = 0;
if (in != null && root.getHeight() > 0) {
int nav = Build.VERSION.SDK_INT >= 30 ? in.getInsets(WindowInsets.Type.navigationBars()).bottom
: Math.min(in.getSystemWindowInsetBottom(), in.getStableInsetBottom());
int[] at = new int[2], window = new int[2];
getLocationInWindow(at);
root.getLocationInWindow(window);
int bottom = window[1] + root.getHeight();
inset = Math.max(0, Math.min(nav, at[1] + getHeight() - (bottom - nav)));
}
android.view.WindowInsets in = root.getRootWindowInsets();
return in != null ? in.getSystemWindowInsetBottom() : 0;
if (inset == bottomInset) return;
bottomInset = inset;
setPadding(0, 0, 0, inset);
if (sizeListener != null) sizeListener.onSize((dp(80) + inset) / density);
}

private boolean night() {
Expand Down
Binary file modified internal/platform/android/runtime/classes.dex
Binary file not shown.
Binary file modified internal/platform/android/runtime/classes.jar
Binary file not shown.
16 changes: 10 additions & 6 deletions internal/platform/android/tabbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import "github.com/nsgo-dev/nsgo/internal/driver"
// navigation bar, reporting the user's choice as "change".
type tabBar struct{ cx *driver.NativeContext }

// tabBarHeight is Material 3's navigation bar's height; the runtime's
// view measures itself taller by the system's navigation bar it reaches
// under.
// tabBarHeight is Material 3's navigation bar's content height. The runtime reports a taller natural size only where the bar overlaps system navigation.
const tabBarHeight driver.Points = 80

func newTabBar(cx *driver.NativeContext) driver.NativeComponent {
Expand Down Expand Up @@ -47,9 +45,15 @@ func (*tabBar) ViewProps(name string, value any) []driver.AndroidViewProp {

// ViewEvent implements driver.AndroidEventMapper.
func (t *tabBar) ViewEvent(name string, args []any) {
if name == "onChange" && len(args) == 1 {
if i, ok := args[0].(float64); ok {
t.cx.Emit("change", i)
if len(args) != 1 {
return
}
if value, ok := args[0].(float64); ok {
switch name {
case "onChange":
t.cx.Emit("change", value)
case "onSize":
t.cx.SetSize(driver.Size{Height: driver.Points(value)})
}
}
}
Expand Down
91 changes: 88 additions & 3 deletions internal/platform/android/tabbar_native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package android

import (
"image/color"
"math"
"sync/atomic"
"testing"
"time"

"github.com/nsgo-dev/nsgo/core"
"github.com/nsgo-dev/nsgo/nsgotest"
Expand All @@ -23,11 +26,11 @@ func TestTabBarSVGNativeTint(t *testing.T) {
theme.Light.Accent, theme.Dark.Accent = core.RGB(0xd02030), core.RGB(0xd02030)
cx.SetTheme(theme)
selected := core.NewSignal(cx, 0)
choose = selected.Set
return core.View{Style: core.Style{Width: core.Pct(100), Height: core.Pct(100)}, TestID: "root", Children: []core.Node{
choose = func(i int) { cx.Post(func() { selected.Set(i) }) }
return core.View{Style: core.Style{Width: core.Pct(100), Height: core.Pct(100), Padding: core.All(30)}, TestID: "root", Children: []core.Node{
core.TabBar{Items: []core.TabBarItem{{Title: "Home", SVG: svg}, {Title: "Search", Icon: "magnifyingglass", SVG: svg}, {Title: "Menu", Icon: "android:ic_menu_search", SVG: svg}},
Selected: selected.Get, OnChange: selected.Set, TestID: "bar",
Style: core.Style{Width: core.Pt(360), Height: core.Pt(80), Margin: core.All(60)}},
Style: core.Style{Width: core.Pct(100), Height: core.Pt(80)}},
}}
})
isIdle := func(c color.NRGBA) bool {
Expand Down Expand Up @@ -71,3 +74,85 @@ func TestTabBarSVGNativeTint(t *testing.T) {
app.Post(func() { choose(1) })
check(1)
}

// A bar only reserves the part of its own frame under the navigation bar. Moving an ancestor must update that overlap even if the bar's local frame stays unchanged, and moving back must restore its original natural height.
func TestTabBarNativeInsetsFollowPlacement(t *testing.T) {
if nsgotest.Target() != "android" {
t.Skip("requires Android pixels")
}
svg, err := core.ParseSVG([]byte(`<svg viewBox="0 0 24 24"><rect width="24" height="24" fill="currentColor"/></svg>`))
if err != nil {
t.Fatal(err)
}
var move func(int)
var safe atomic.Uint32
app := nsgotest.Start(t, func(cx *core.Scope) core.Node {
theme := core.DefaultTheme()
theme.Light.Accent, theme.Dark.Accent = core.RGB(0xd02030), core.RGB(0xd02030)
cx.SetTheme(theme)
placement := core.NewSignal(cx, 0)
move = func(at int) { cx.Post(func() { placement.Set(at) }) }
env := cx.Env()
return core.View{TestID: "root", Style: core.Style{Width: core.Pct(100), Height: core.Pct(100)}, Children: []core.Node{
core.View{StyleFunc: func() core.Style {
bottom := env.SafeArea().Bottom
safe.Store(math.Float32bits(float32(bottom)))
st := core.Style{Position: core.Absolute, Left: core.Pt(0), Right: core.Pt(0)}
switch placement.Get() {
case 1:
st.Bottom = core.Pt(0)
case 2:
st.Bottom = core.Pt(bottom)
case 3:
st.Bottom = core.Pt(bottom / 2)
default:
st.Top = core.Pt(60)
}
return st
}, Children: []core.Node{core.TabBar{TestID: "bar", Items: []core.TabBarItem{{Title: "Home", SVG: svg}, {Title: "Search", SVG: svg}}}}},
}}
})
for _, at := range []int{0, 1, 2, 3, 0} {
move(at)
app.Settle()
inset := core.Points(math.Float32frombits(safe.Load()))
if inset <= 0 {
t.Skip("requires a bottom navigation inset")
}
want := core.Points(80)
if at == 1 {
want += inset
}
if at == 3 {
want += inset / 2
}
app.WaitUntil(5*time.Second, func() bool { return math.Abs(float64(app.Find("id:bar").Bounds().Size.Height-want)) < .51 })
img := app.Capture()
scale := float64(img.Bounds().Dx()) / float64(app.Find("id:root").Frame.Size.Width)
bar := app.Find("id:bar").Bounds()
for tab := range 2 {
ink := func(c color.NRGBA) bool {
if tab == 0 {
return c == (color.NRGBA{R: 0xd0, G: 0x20, B: 0x30, A: 255})
}
return c == (color.NRGBA{R: 0x49, G: 0x45, B: 0x4f, A: 255}) || c == (color.NRGBA{R: 0xca, G: 0xc4, B: 0xd0, A: 255})
}
x := int((float64(bar.Origin.X) + (float64(tab)+.5)*float64(bar.Size.Width)/2) * scale)
y := int((float64(bar.Origin.Y) + 24) * scale)
if c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA); !ink(c) {
t.Fatalf("placement %d tab %d icon pixel %v", at, tab, c)
}
labels := 0
for y := int((float64(bar.Origin.Y) + 48) * scale); y < int((float64(bar.Origin.Y)+68)*scale); y++ {
for x := int((float64(bar.Origin.X) + float64(tab)*float64(bar.Size.Width)/2) * scale); x < int((float64(bar.Origin.X)+float64(tab+1)*float64(bar.Size.Width)/2)*scale); x++ {
if ink(color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA)) {
labels++
}
}
}
if labels < 5 {
t.Fatalf("placement %d tab %d label clipped (%d pixels)", at, tab, labels)
}
}
}
}