Skip to content
Open
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
1 change: 1 addition & 0 deletions resources/ios/NativeUIListRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ struct NativeUIListRenderer: View {
// Only this list draws the header, not one in its rows.
.environment(\.nativeUIPagerHeader, nil)
.modifier(ListAutoScrollModifier(request: autoScrollTo, targetID: autoScrollTarget, anchor: .top))
.modifier(NativeUIScrollEdgeExtension())
}
}

Expand Down
682 changes: 615 additions & 67 deletions resources/ios/NativeUIPagerRenderer.swift

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions resources/ios/NativeUIScrollViewRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ struct NativeUIScrollViewRenderer: View {
}
.scrollDismissesKeyboard(.interactively)
.modifier(ScrollViewBackgroundModifier(node: node))
.modifier(NativeUIScrollEdgeExtension())
// Whether the reader is at the bottom, from where the scroll view
// actually is. The anchor's onAppear / onDisappear can't tell on
// their own: a lazy stack keeps the anchor alive a little past the
Expand Down Expand Up @@ -530,3 +531,21 @@ struct ScrollViewBackgroundModifier: ViewModifier {
}
}
}

/// Lets a vertical scroll view run under the horizontal safe area beside it
/// (an iPhone Duo's vertical bar) and hands that area back to its content as
/// safe area. The indicator sits at the screen's edge, as in Apple's lists,
/// while rows stay where they were and only their backgrounds reach under.
struct NativeUIScrollEdgeExtension: ViewModifier {
@State private var insets = EdgeInsets()

func body(content: Content) -> some View {
content
.safeAreaPadding(.leading, insets.leading)
.safeAreaPadding(.trailing, insets.trailing)
.ignoresSafeArea(.container, edges: .horizontal)
.onGeometryChange(for: EdgeInsets.self) { proxy in
EdgeInsets(top: 0, leading: proxy.safeAreaInsets.leading, bottom: 0, trailing: proxy.safeAreaInsets.trailing)
} action: { insets = $0 }
}
}
1 change: 1 addition & 0 deletions resources/ios/NativeUIVirtualListRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct NativeUIVirtualListRenderer: View {
// Environment-propagating, so it reaches the List inside the body
// without threading another property through it.
.scrollIndicators(node.props.getBool("shows_indicators", default: true) ? .automatic : .hidden)
.modifier(NativeUIScrollEdgeExtension())
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/Elements/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use InvalidArgumentException;
use Native\Mobile\Edge\CallbackRegistry;
use Native\Mobile\Edge\Element;
use Native\Mobile\Edge\TailwindParser;
use Native\Mobile\UI\Concerns\ResolvesColorValues;
use Native\Mobile\UI\Theme;

Expand Down Expand Up @@ -83,6 +84,8 @@
* that scrolls up with the page showing while the tabs pin under the nav
* bar: see {@see PagerHeader}. A {@see PagerOverlay} hangs under the tabs
* over the pages. Neither counts as a page, and neither is windowed.
* `header-beside="expanded"` moves the header beside the pages once the
* window is that wide (see headerBeside()).
*
* The page index rides the tab-change transport, so a single feed is
* capped at 32,767 pages per screen.
Expand Down Expand Up @@ -138,6 +141,10 @@ public function applyAttributes(array $attrs): void
if ($settle !== null) {
$this->reportOnSettle(filter_var($settle, FILTER_VALIDATE_BOOLEAN));
}
$beside = $attrs['header_beside'] ?? $attrs['headerBeside'] ?? $attrs['header-beside'] ?? null;
if ($beside !== null) {
$this->headerBeside($beside);
}
$cb = $attrs['on_page_change'] ?? $attrs['onPageChange'] ?? $attrs['on-page-change'] ?? null;
if ($cb !== null) {
$this->onPageChange($cb);
Expand Down Expand Up @@ -212,6 +219,28 @@ public function reportOnSettle(bool $value = true): static
return $this;
}

/**
* From a breakpoint (`expanded`, `lg`, … as the responsive classes name
* them) or a width in points up, the header scrolls by itself in a
* leading column and the tabs and pages fill the trailing one, split at
* a foldable's fold or evenly elsewhere. Narrower, it is above the tabs
* as usual. Native decides against the live window width, as it does
* for the classes, so the header is sent once for either layout. iOS
* only so far: Android keeps the header above the tabs at any width.
*/
public function headerBeside(string|int|float $from): static
{
$width = is_numeric($from) ? (float) $from : TailwindParser::breakpointMinWidth($from);

if ($width === null) {
throw new InvalidArgumentException("Unknown breakpoint [{$from}] for header-beside.");
}

$this->pagerProps['header_beside_from'] = $width;

return $this;
}

public function onPageChange(string $method): static
{
$this->pageCallback = $method;
Expand Down
4 changes: 4 additions & 0 deletions src/Elements/PagerHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
* always finds the header at the height it left it. Once the header has
* gone, each page keeps its own place. A page too short to scroll the
* header away gets room at its end to do so.
*
* With `header-beside` on the pager, a wide enough window has the header in
* a column of its own beside the pages instead, and the pages' lists draw
* only their own rows (see {@see Pager::headerBeside()}).
*/
class PagerHeader extends Element
{
Expand Down
6 changes: 3 additions & 3 deletions tests/IosPagerStartPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
$source = file_get_contents(dirname(__DIR__).'/resources/ios/NativeUIPagerRenderer.swift');

expect($source)
->toContain('.onScrollGeometryChange(for: Int?.self)')
->toContain('guard length > 0, count > 0 else { return nil }')
->toContain('guard let page else { return }');
->toContain('.onScrollGeometryChange(for: PagerPosition?.self)')
->toContain('guard length > 0 else { return nil }')
->toContain('guard let value else { return }');
});
32 changes: 26 additions & 6 deletions tests/IosPagerTabBarTest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
<?php

it('runs a horizontal pager under a bar at the bottom of the screen', function () {
it('runs a horizontal pager under a bar at the bottom of the screen and at its sides', function () {
$source = file_get_contents(dirname(__DIR__).'/resources/ios/NativeUIPagerRenderer.swift');

expect($source)
->toContain('let bottomInset = horizontal ? geo.safeAreaInsets.bottom : 0')
->toContain('height: geo.size.height + bottomInset')
->toContain('.ignoresSafeArea(.container, edges: horizontal ? .bottom : [])');
->toContain('EdgeInsets(top: 0, leading: geo.safeAreaInsets.leading, bottom: geo.safeAreaInsets.bottom, trailing: geo.safeAreaInsets.trailing)')
->toContain('CGSize(width: geo.size.width + insets.leading + insets.trailing, height: geo.size.height + insets.bottom)')
->toContain('.ignoresSafeArea(.container, edges: horizontal ? [.bottom, .horizontal] : [])');
});

it('gives the bar inset back to the page content as safe area', function () {
it('gives the bar insets back to the page content as safe area', function () {
$source = file_get_contents(dirname(__DIR__).'/resources/ios/NativeUIPagerRenderer.swift');

expect($source)->toMatch('/\.safeAreaPadding\(\.bottom, bottomInset\)\s*\.frame\(width: size\.width, height: size\.height\)\s*\.clipped\(\)/');
expect($source)->toMatch('/\.safeAreaPadding\(insets\)\s*\.frame\(width: size\.width, height: size\.height\)\s*\.clipped\(\)/');
});

it('pages a horizontal pager by its own width and clips its sides beside a sideways bar', function () {
$source = file_get_contents(dirname(__DIR__).'/resources/ios/NativeUIPagerRenderer.swift');

expect($source)
->toContain('let length = horizontal ? geometry.bounds.width : visible.height')
->toContain('.clipShape(PagerSideClip(horizontal: horizontal))')
->toContain('.clipShape(PagerSideClip(horizontal: true))');
});

it('runs lists and scroll views under a bar at their sides and hands its inset back as safe area', function () {
$ios = dirname(__DIR__).'/resources/ios/';

expect(file_get_contents($ios.'NativeUIScrollViewRenderer.swift'))
->toMatch('/\.safeAreaPadding\(\.leading, insets\.leading\)\s*\.safeAreaPadding\(\.trailing, insets\.trailing\)\s*\.ignoresSafeArea\(\.container, edges: \.horizontal\)/');

foreach (['NativeUIScrollViewRenderer.swift', 'NativeUIListRenderer.swift', 'NativeUIVirtualListRenderer.swift', 'NativeUIPagerRenderer.swift'] as $file) {
expect(file_get_contents($ios.$file))->toContain('.modifier(NativeUIScrollEdgeExtension())');
}
});
24 changes: 23 additions & 1 deletion tests/PagerHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ function kotlinSource(string $file): string
->and(array_column($tree['children'], 'type'))->toBe(['pager_header', 'column', 'column', 'pager_overlay']);
});

it('sends the width from which the header moves beside the pages', function () {
NativeElementCollector::open('pager', ['horizontal' => true, 'header-beside' => 'expanded']);
NativeElementCollector::close();

$tree = NativeElementCollector::collect()->toArray(new CallbackRegistry);

expect($tree['props']['header_beside_from'])->toBe(840.0)
->and(Pager::make()->headerBeside(700)->toArray(new CallbackRegistry)['props']['header_beside_from'])->toBe(700.0)
->and(Pager::make()->toArray(new CallbackRegistry)['props'])->not->toHaveKey('header_beside_from');
});

it('rejects a header-beside breakpoint it does not know', function () {
Pager::make()->headerBeside('enormous');
})->throws(InvalidArgumentException::class, 'Unknown breakpoint [enormous] for header-beside.');

it('draws the header beside the pages on iOS once the window reaches that width', function () {
expect(swiftSource('NativeUIPagerRenderer.swift'))
->toContain('let beside = header != nil && besideFrom > 0 && width >= besideFrom')
->toContain('header: beside ? nil : header,')
->toContain('besideHeader: beside ? header : nil,');
});

it('registers the header and the overlay for the pager to draw', function () {
$components = collect(json_decode(file_get_contents(dirname(__DIR__).'/nativephp.json'), true)['components'])->keyBy('type');

Expand All @@ -85,7 +107,7 @@ function kotlinSource(string $file): string
it('keeps the header and the overlay out of the pages on both platforms', function () {
expect(swiftSource('NativeUIPagerRenderer.swift'))
->toContain('let pages = node.children.filter { $0.type != "pager_header" && $0.type != "pager_overlay" }')
->toContain('header: horizontal ? node.children.first { $0.type == "pager_header" } : nil');
->toContain('let header = horizontal ? node.children.first { $0.type == "pager_header" } : nil');

expect(kotlinSource('PagerRenderer.kt'))
->toContain('node.children.filter { it.type != "pager_header" && it.type != "pager_overlay" }')
Expand Down
7 changes: 4 additions & 3 deletions tests/PagerReportOnSettleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

expect($source)
->toContain('reportOnSettle: p.getBool("report_on_settle")')
->toMatch('/leading = page\s*(\/\/[^\n]*\s*)*if !reportOnSettle \{\s*report\(page\)\s*\}/')
->toMatch('/guard phase == \.idle else \{ return \}\s*report\(leading\)/');
->toMatch('/leading = page\s*(\/\/[^\n]*\s*)*if !reportOnSettle, [^{]*\{\s*report\(page\)\s*\}/')
->toMatch('/guard phase == \.idle else \{ return \}[\s\S]*?reconcile\(final\)/')
->toMatch('/private func reconcile\(_ position: PagerPosition\) \{[\s\S]*?report\(page\)\s*\}/');
});

it('counts a programmatic jump as known to PHP on iOS', function () {
$source = file_get_contents(dirname(__DIR__).'/resources/ios/NativeUIPagerRenderer.swift');

expect($source)->toMatch('/reported = \[page\]\s*lastReported = page\s*position = page/');
expect($source)->toMatch('/reported = \[page\]\s*lastReported = page\s*resume\.chosen\(page\)\s*position = page/');
});

it('reports the settled page on Android when reporting on settle', function () {
Expand Down
6 changes: 3 additions & 3 deletions tests/PagerTabsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ function pagerTabProps(array $attrs, ?CallbackRegistry $registry = null): array

expect($source)
// The unrounded position, written every frame into an observable only the row reads.
->toContain('.onScrollGeometryChange(for: CGFloat?.self)')
->toContain('progress.value = value')
->toContain('.onScrollGeometryChange(for: PagerPosition?.self)')
->toContain('progress.value = value.pages')
->toContain('@Observable')
// Highlight: the nearest page. Indicator: between the labels either side.
->toContain('Int(progress.value.rounded())')
Expand All @@ -156,7 +156,7 @@ function pagerTabProps(array $attrs, ?CallbackRegistry $registry = null): array
$source = file_get_contents(dirname(__DIR__).'/resources/ios/NativeUIPagerRenderer.swift');

expect($source)
->toMatch('/if index == leading, abs\(progress\.value - CGFloat\(index\)\) < 0\.01 \{\s*if let cb = tabs\?\.reselectCbId, cb != 0 \{\s*NativeElementBridge\.sendTabChangeEvent\(cb, nodeId: nodeId, index: index\)/')
->toMatch('/if index == leading, abs\(progress\.value - CGFloat\(index\)\) < 0\.01, resume\.position\?\.isOn\(index\) == true \{\s*resume\.settled\(\)\s*if let cb = tabs\?\.reselectCbId, cb != 0 \{\s*NativeElementBridge\.sendTabChangeEvent\(cb, nodeId: nodeId, index: index\)/')
->toContain('withAnimation { position = index }');
});

Expand Down