Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Make the share sheet actually appear on iPad

Barnabas6c5be06bb48a
diff --git a/lib/screens/admin_users_screen.dart b/lib/screens/admin_users_screen.dart
index 5600ecca..b11dba9f 100644
--- a/lib/screens/admin_users_screen.dart
+++ b/lib/screens/admin_users_screen.dart
@@ -18,6 +18,7 @@ import '../widgets/setup_link_share_sheet.dart';
import '../l10n/app_localizations.dart';
import '../utils/duration_format.dart';
import '../utils/app_platform.dart';
+import '../utils/share_origin.dart';
class AdminUsersScreen extends StatefulWidget {
final List<dynamic> users;
@@ -871,14 +872,10 @@ class _UserDetailScreenState extends State<_UserDetailScreen> {
username: username,
setupLink: setupLink,
onShare: () async {
- final box = sheetContext.findRenderObject() as RenderBox?;
- final origin = box == null
- ? null
- : box.localToGlobal(Offset.zero) & box.size;
await Share.share(
setupLink.toString(),
subject: l.setupLinkShareSubject(username),
- sharePositionOrigin: origin,
+ sharePositionOrigin: shareOriginFor(sheetContext),
);
},
onCopy: () async {
diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart
index 9e8b61b3..b2621316 100644
--- a/lib/screens/settings_screen.dart
+++ b/lib/screens/settings_screen.dart
@@ -34,6 +34,7 @@ import '../screens/auth_sessions_screen.dart';
import '../screens/transcription_settings_screen.dart';
import '../main.dart' show applyThemeMode, applyTrustAllCerts, applyFlatBackground, applyColorSource, applyManualSeed, applyGradientIntensity, applyUseColorEverywhere, applyEinkModeTheme, applyOrientationLock, localeNotifier, flatNotifier, gradientIntensityNotifier, snappyTransitionsNotifier;
import '../services/wording.dart';
+import '../utils/share_origin.dart';
import '../widgets/absorb_page_header.dart';
import '../widgets/theme_presets.dart';
import '../widgets/color_wheel_picker.dart';
@@ -3817,13 +3818,9 @@ class _SettingsScreenState extends State<SettingsScreen> {
trailing: const Icon(Icons.chevron_right_rounded),
onTap: () async {
try {
- final box = context.findRenderObject() as RenderBox?;
- final origin = box != null
- ? box.localToGlobal(Offset.zero) & box.size
- : null;
await LogService().shareLogs(
serverVersion: auth.serverVersion,
- sharePositionOrigin: origin,
+ sharePositionOrigin: shareOriginFor(context),
);
} catch (e) {
if (mounted) {
diff --git a/lib/utils/share_origin.dart b/lib/utils/share_origin.dart
new file mode 100644
index 00000000..18ae7d7d
--- /dev/null
+++ b/lib/utils/share_origin.dart
@@ -0,0 +1,28 @@
+import 'package:flutter/widgets.dart';
+
+/// Anchor rect for share_plus on iPad.
+///
+/// iPad presents the share sheet as a popover pointed at
+/// [sharePositionOrigin]. When the rect passed in covers (nearly) the whole
+/// window - which happens when it's computed from a screen- or sheet-level
+/// context instead of the tapped button - UIKit has no room to place the
+/// popover and silently never shows it. The same call works in compact width
+/// because iOS uses a regular sheet there and ignores the anchor entirely.
+///
+/// Returns the context's own rect when it's a reasonable anchor, otherwise a
+/// point in the middle of the window so the popover always has room.
+Rect shareOriginFor(BuildContext context) {
+ final windowSize = MediaQuery.sizeOf(context);
+ final center = Rect.fromCenter(
+ center: Offset(windowSize.width / 2, windowSize.height / 2),
+ width: 1,
+ height: 1,
+ );
+ final box = context.findRenderObject() as RenderBox?;
+ if (box == null || !box.hasSize) return center;
+ final rect = box.localToGlobal(Offset.zero) & box.size;
+ final tooBig = rect.width >= windowSize.width * 0.8 &&
+ rect.height >= windowSize.height * 0.8;
+ if (tooBig) return center;
+ return rect;
+}
diff --git a/lib/widgets/book_detail_sheet.dart b/lib/widgets/book_detail_sheet.dart
index 84c233f1..99174951 100644
--- a/lib/widgets/book_detail_sheet.dart
+++ b/lib/widgets/book_detail_sheet.dart
@@ -49,6 +49,7 @@ import 'absorb_wave_icon.dart';
import 'stackable_sheet.dart';
import 'ebook_router.dart';
import '../utils/duration_format.dart';
+import '../utils/share_origin.dart';
// ─── BOOK DETAIL BOTTOM SHEET ───────────────────────────────
@@ -2518,11 +2519,10 @@ class _FullCoverViewerState extends State<_FullCoverViewer> {
final file = File('${dir.path}/$safeTitle$ext');
await file.writeAsBytes(response.bodyBytes);
if (!mounted) return;
- final box = context.findRenderObject() as RenderBox?;
- final origin = box != null
- ? box.localToGlobal(Offset.zero) & box.size
- : null;
- await Share.shareXFiles([XFile(file.path)], sharePositionOrigin: origin);
+ await Share.shareXFiles(
+ [XFile(file.path)],
+ sharePositionOrigin: shareOriginFor(context),
+ );
} catch (e) {
if (mounted) {
final l = AppLocalizations.of(context)!;
diff --git a/lib/widgets/notes_sheet.dart b/lib/widgets/notes_sheet.dart
index 83d3cfa5..af489a68 100644
--- a/lib/widgets/notes_sheet.dart
+++ b/lib/widgets/notes_sheet.dart
@@ -6,6 +6,7 @@ import 'package:share_plus/share_plus.dart';
import 'package:url_launcher/url_launcher.dart';
import '../l10n/app_localizations.dart';
import '../services/notes_service.dart';
+import '../utils/share_origin.dart';
/// Bottom sheet displaying notes for a library item.
class NotesSheet extends StatefulWidget {
@@ -161,11 +162,10 @@ class _NotesSheetState extends State<NotesSheet> {
final safe = widget.itemTitle.replaceAll(RegExp(r'[<>:"/\\|?*]'), '_');
final file = File('${dir.path}/${safe}_notes.$format');
await file.writeAsString(buffer.toString());
- final box = context.findRenderObject() as RenderBox?;
- final origin = box != null
- ? box.localToGlobal(Offset.zero) & box.size
- : null;
- await Share.shareXFiles([XFile(file.path)], sharePositionOrigin: origin);
+ await Share.shareXFiles(
+ [XFile(file.path)],
+ sharePositionOrigin: shareOriginFor(context),
+ );
}
Future<_NoteEditResult?> _showEditor(BuildContext context, {String title = '', String body = ''}) {
diff --git a/lib/widgets/quote_share_sheet.dart b/lib/widgets/quote_share_sheet.dart
index bc705c18..41ee3a94 100644
--- a/lib/widgets/quote_share_sheet.dart
+++ b/lib/widgets/quote_share_sheet.dart
@@ -14,6 +14,7 @@ import '../l10n/app_localizations.dart';
import '../providers/library_provider.dart';
import 'adaptive_modal.dart';
import 'overlay_toast.dart';
+import '../utils/share_origin.dart';
/// Card shapes offered for a shared quote, as width/height.
enum _QuoteShape {
@@ -182,12 +183,9 @@ class _QuoteShareSheetState extends State<_QuoteShareSheet> {
await file.writeAsBytes(data.buffer.asUint8List(), flush: true);
if (!mounted) return;
- final box = context.findRenderObject() as RenderBox?;
- final origin =
- box != null ? box.localToGlobal(Offset.zero) & box.size : null;
await Share.shareXFiles(
[XFile(file.path, mimeType: 'image/png')],
- sharePositionOrigin: origin,
+ sharePositionOrigin: shareOriginFor(context),
);
} catch (e) {
debugPrint('[QuoteShare] Failed: $e');