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
1 change: 1 addition & 0 deletions lib/papyrus_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ export 'src/engine/pdf/pdf_facade.dart'
PdfViewportConfiguration;
export 'src/engine/pdf/pdf_reader_engine.dart';
export 'src/presentation/papyrus_reader.dart';
export 'src/presentation/reader_controls.dart' show ReaderOpeningView;
export 'src/presentation/reader_theme_data.dart';
export 'src/presentation/reader_ui_builders.dart';
20 changes: 4 additions & 16 deletions lib/src/presentation/papyrus_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -547,22 +547,10 @@ final class _PapyrusReaderState extends State<PapyrusReader> {
),
);

return Stack(
fit: StackFit.expand,
children: [
content,
if (widget.onBack != null)
PositionedDirectional(
top: 0,
start: 0,
child: ReaderIconButton(
icon: Icons.arrow_back_rounded,
tooltip: 'Back',
onPressed: widget.onBack,
theme: theme,
),
),
],
return ReaderOpeningView(
onBack: widget.onBack,
theme: theme,
child: content,
);
}

Expand Down
67 changes: 65 additions & 2 deletions lib/src/presentation/reader_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,65 @@ double readerToolbarHeight(
);
}

/// Shows loading content with Back in the default reader toolbar's leading slot.
final class ReaderOpeningView extends StatelessWidget {
const ReaderOpeningView({
super.key,
required this.child,
this.onBack,
this.theme,
});

final Widget child;
final VoidCallback? onBack;
final ReaderThemeData? theme;

@override
Widget build(BuildContext context) {
final back = onBack;
if (back == null) return child;

final readerTheme = theme ?? ReaderThemeData.fromTheme(Theme.of(context));
return LayoutBuilder(
builder: (context, constraints) => Stack(
fit: StackFit.expand,
children: [
child,
PositionedDirectional(
top: 0,
start: 0,
child: _ReaderToolbarBackButton(
onPressed: back,
height: readerToolbarHeight(
context,
readerTheme,
constraints.maxWidth >= readerTheme.compactBreakpoint,
),
),
),
],
),
);
}
}

final class _ReaderToolbarBackButton extends StatelessWidget {
const _ReaderToolbarBackButton({
required this.onPressed,
required this.height,
});

final VoidCallback onPressed;
final double height;

@override
Widget build(BuildContext context) => SizedBox(
width: AppBarTheme.of(context).leadingWidth ?? kToolbarHeight,
height: height,
child: BackButton(onPressed: onPressed),
);
}

final class ReaderToolbar extends StatelessWidget {
const ReaderToolbar({
super.key,
Expand Down Expand Up @@ -101,13 +160,17 @@ final class ReaderToolbar extends StatelessWidget {
@override
Widget build(BuildContext context) {
final height = readerToolbarHeight(context, theme, isWide);
final back = onBack;
final leading = back == null
? null
: _ReaderToolbarBackButton(onPressed: back, height: height);
if (pdfTools case final tools?) {
return SizedBox(
height: height,
child: ReaderPdfControls(
tools: tools,
compact: compactPdf,
onBack: onBack,
leading: leading,
onSearch: onSearch!,
actions: [
...actions,
Expand Down Expand Up @@ -153,7 +216,7 @@ final class ReaderToolbar extends StatelessWidget {
foregroundColor: theme.onChromeColor,
elevation: 0,
scrolledUnderElevation: 0,
leading: onBack == null ? null : BackButton(onPressed: onBack),
leading: leading,
actions: [
...actions,
ReaderIconButton(
Expand Down
6 changes: 3 additions & 3 deletions lib/src/presentation/reader_pdf_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ final class ReaderPdfControls extends StatefulWidget {
const ReaderPdfControls({
required this.tools,
required this.onSearch,
this.onBack,
this.leading,
this.actions = const [],
this.compact = false,
super.key,
});

final PdfReaderTools tools;
final VoidCallback onSearch;
final VoidCallback? onBack;
final Widget? leading;
final List<Widget> actions;
final bool compact;

Expand Down Expand Up @@ -87,7 +87,7 @@ final class _ReaderPdfControlsState extends State<ReaderPdfControls> {

return Row(
children: [
if (widget.onBack != null) BackButton(onPressed: widget.onBack),
?widget.leading,
const Spacer(),
if (!compact) ...[
IconButton(
Expand Down
10 changes: 9 additions & 1 deletion test/presentation/papyrus_reader_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -771,21 +771,27 @@ void main() {
var backCalls = 0;
addTearDown(controller.dispose);

tester.view.padding = const FakeViewPadding(top: 30, left: 12);
addTearDown(tester.view.resetPadding);
await pumpReader(
tester,
document: document('Loading'),
textScale: 2,
controller: controller,
onBack: () => backCalls++,
);

expect(find.byType(CircularProgressIndicator), findsOneWidget);
expect(find.byTooltip('Back'), findsOneWidget);
final backIcon = find.byType(BackButtonIcon);
final openingIcon = tester.getRect(backIcon);
final openingTarget = tester.getRect(find.byType(BackButton));
await tester.tap(find.byTooltip('Back'));
expect(backCalls, 1);
expect(find.text('Opening The Loading Reader…'), findsNothing);
expect(
tester.getCenter(find.byType(CircularProgressIndicator)),
const Offset(300, 400),
const Offset(306, 415),
);

gate.complete();
Expand All @@ -796,6 +802,8 @@ void main() {
expect(find.byTooltip('Reading settings'), findsNothing);
expect(find.byTooltip('Table of contents'), findsNothing);
await toggleReaderControls(tester);
expect(tester.getRect(backIcon), openingIcon);
expect(tester.getRect(find.byType(BackButton)), openingTarget);
expect(find.byTooltip('Reading settings'), findsOneWidget);
});

Expand Down
10 changes: 9 additions & 1 deletion test/presentation/pdf_tools_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1240,11 +1240,12 @@ void main() {
return createPdfrxFacade(bytes!, sourceName: 'shell-tools');
});
final tools = facade! as PdfReaderTools;
final opening = Completer<PdfFacade>();
final controller = ReaderController(
registry: ReaderEngineRegistry([
ReaderEngineRegistration(
formats: const {ReaderFormat.pdf},
factory: () => PdfReaderEngine(facadeFactory: (_) async => facade),
factory: () => PdfReaderEngine(facadeFactory: (_) => opening.future),
),
]),
);
Expand All @@ -1261,9 +1262,14 @@ void main() {
loadBytes: () async => bytes!,
),
onActivity: events.add,
onBack: () {},
),
),
);
final backIcon = find.byType(BackButtonIcon);
final openingIcon = tester.getRect(backIcon);
final openingTarget = tester.getRect(find.byType(BackButton));
opening.complete(facade);
await _ready(tester, tools);
await tools.setZoom(1.37);
await tester.pumpAndSettle();
Expand Down Expand Up @@ -1312,6 +1318,8 @@ void main() {

final toolbar = find.byKey(const ValueKey('reader-toolbar'));
final progress = find.byKey(const ValueKey('reader-progress-controls'));
expect(tester.getRect(backIcon), openingIcon);
expect(tester.getRect(find.byType(BackButton)), openingTarget);
final toolbarRect = tester.getRect(toolbar);
final progressRect = tester.getRect(progress);
final viewportRect = tester.getRect(find.byType(PdfViewer));
Expand Down
Loading