Gitea CI / Check code and tests (pull_request) Failing after 8s
Gitea CI / Build Android APK (pull_request) Skipped
Gitea CI / Build Windows x64 app (pull_request) Skipped
Gitea CI / Audit and package Windows x64 (pull_request) Skipped
Gitea CI / Assemble and validate CI artifacts (pull_request) Skipped
Gitea CI / Gitea CI passed (pull_request) Failing after 1s
173 lines
5.0 KiB
Dart
173 lines
5.0 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:cutdex_ui/theme.dart';
|
|
import 'package:cutdex_native/cutdex_native.dart';
|
|
import 'package:cutdex_native_flutter/cutdex_native_flutter.dart';
|
|
|
|
/// Moves the existing preview into the root overlay, retaining its native view.
|
|
class PreviewFullscreenHost extends StatefulWidget {
|
|
const PreviewFullscreenHost({super.key, required this.child});
|
|
final Widget child;
|
|
|
|
@override
|
|
State<PreviewFullscreenHost> createState() => _PreviewFullscreenHostState();
|
|
}
|
|
|
|
class _PreviewFullscreenHostState extends State<PreviewFullscreenHost> {
|
|
late final _nativeSession = NativeFullscreenSession(
|
|
onExit: () {
|
|
if (!mounted) return;
|
|
_overlay.hide();
|
|
setState(() => _fullscreen = false);
|
|
},
|
|
);
|
|
final _overlay = OverlayPortalController();
|
|
final _contentKey = GlobalKey();
|
|
bool _fullscreen = false;
|
|
bool _busy = false;
|
|
bool get _native => !kIsWeb && defaultTargetPlatform == TargetPlatform.macOS;
|
|
|
|
Future<void> _toggle() async {
|
|
if (_busy) return;
|
|
final entering = !_fullscreen;
|
|
setState(() {
|
|
_busy = true;
|
|
_fullscreen = entering;
|
|
});
|
|
if (entering) {
|
|
_overlay.show();
|
|
if (_native) {
|
|
_nativeSession.attach();
|
|
}
|
|
} else {
|
|
_overlay.hide();
|
|
}
|
|
try {
|
|
if (_native) await _nativeSession.setFullscreen(entering);
|
|
} catch (error) {
|
|
if (mounted) {
|
|
_overlay.hide();
|
|
setState(() => _fullscreen = false);
|
|
debugPrint('Could not change preview fullscreen: $error');
|
|
}
|
|
} finally {
|
|
if (mounted) setState(() => _busy = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_nativeSession.dispose(exitFullscreen: _native && _fullscreen);
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final fullscreenTheme = AppTheme.previewFullscreenTheme;
|
|
final previewBackground = fullscreenTheme
|
|
.extension<AppSurfaces>()!
|
|
.previewBackground;
|
|
final content = PreviewFullscreenScope(
|
|
key: _contentKey,
|
|
fullscreen: _fullscreen,
|
|
toggle: _busy ? null : _toggle,
|
|
child: widget.child,
|
|
);
|
|
return OverlayPortal(
|
|
overlayLocation: OverlayChildLocation.rootOverlay,
|
|
controller: _overlay,
|
|
overlayChildBuilder: (context) => Positioned.fill(
|
|
child: Theme(
|
|
data: fullscreenTheme,
|
|
child: Material(
|
|
color: previewBackground,
|
|
child: CallbackShortcuts(
|
|
bindings: {
|
|
const SingleActivator(LogicalKeyboardKey.escape): () {
|
|
if (_fullscreen && !_busy) unawaited(_toggle());
|
|
},
|
|
},
|
|
child: Focus(autofocus: true, child: content),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
child: _fullscreen ? const SizedBox.expand() : content,
|
|
);
|
|
}
|
|
}
|
|
|
|
class PreviewFullscreenScope extends InheritedWidget {
|
|
const PreviewFullscreenScope({
|
|
super.key,
|
|
required this.fullscreen,
|
|
required this.toggle,
|
|
required super.child,
|
|
});
|
|
final bool fullscreen;
|
|
final VoidCallback? toggle;
|
|
|
|
static PreviewFullscreenScope? of(BuildContext context) =>
|
|
context.dependOnInheritedWidgetOfExactType<PreviewFullscreenScope>();
|
|
|
|
@override
|
|
bool updateShouldNotify(PreviewFullscreenScope oldWidget) =>
|
|
fullscreen != oldWidget.fullscreen || toggle != oldWidget.toggle;
|
|
}
|
|
|
|
/// Keeps the canvas mounted while the toolbar floats over fullscreen playback.
|
|
class PreviewFullscreenLayout extends StatelessWidget {
|
|
const PreviewFullscreenLayout({
|
|
super.key,
|
|
required this.canvas,
|
|
required this.toolbar,
|
|
});
|
|
final Widget canvas;
|
|
final Widget toolbar;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final fullscreen = PreviewFullscreenScope.of(context)?.fullscreen == true;
|
|
return Stack(
|
|
fit: StackFit.expand,
|
|
children: [
|
|
Positioned.fill(
|
|
bottom: fullscreen
|
|
? 0
|
|
: AppTheme.isDesktop(context)
|
|
? AppDesktopMetrics.playbackBar
|
|
: AppControlSize.panelHeader,
|
|
child: canvas,
|
|
),
|
|
Align(
|
|
alignment: Alignment.bottomCenter,
|
|
child: Container(
|
|
margin: fullscreen
|
|
? const EdgeInsets.only(
|
|
left: AppSpacing.extraLarge,
|
|
right: AppSpacing.extraLarge,
|
|
bottom: AppSpacing.emptyState,
|
|
)
|
|
: EdgeInsets.zero,
|
|
constraints: BoxConstraints(
|
|
maxWidth: fullscreen ? 560 : double.infinity,
|
|
),
|
|
decoration: fullscreen
|
|
? BoxDecoration(
|
|
color: Theme.of(context).colorScheme.surfaceContainer
|
|
.withValues(alpha: .93),
|
|
borderRadius: BorderRadius.circular(AppRadius.large),
|
|
)
|
|
: null,
|
|
child: NativePreviewOcclusion(child: toolbar),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|