Files
plezy/lib/utils/desktop_window_padding.dart
edde746 c68ffe9ed0 refactor: share the toolbar scrim and dedupe playback and download paths
Extracts the repeated toolbar fade into a single ToolbarScrim widget, folds
duplicated request/retry handling in the media server HTTP client, and
collapses the parallel playback-source, download-manager and live TV helper
paths into shared implementations.
2026-07-26 06:09:49 +02:00

170 lines
5.7 KiB
Dart

import 'dart:io' show Platform;
import 'package:flutter/material.dart';
import '../services/fullscreen_state_manager.dart';
/// InheritedWidget to indicate that a side navigation is present in the widget tree.
/// When present, app bars should skip their left padding since the side nav
/// already handles the macOS traffic lights area.
class SideNavigationScope extends InheritedWidget {
const SideNavigationScope({super.key, required super.child});
static bool isPresent(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<SideNavigationScope>() != null;
}
@override
bool updateShouldNotify(SideNavigationScope oldWidget) => false;
}
/// Padding values for desktop window controls
class DesktopWindowPadding {
/// Left padding for macOS traffic lights (normal window mode)
static const double macOSLeft = 80.0;
/// Left padding for macOS in fullscreen (reduced since traffic lights auto-hide)
static const double macOSLeftFullscreen = 0.0;
/// Right padding for macOS to prevent actions from being too close to edge
static const double macOSRight = 16.0;
/// Right padding for mobile devices to prevent actions from being too close to edge
static const double mobileRight = 6.0;
/// Left padding for macOS reflecting the current fullscreen state
static double get macOSLeftCurrent => FullscreenStateManager().isFullscreen ? macOSLeftFullscreen : macOSLeft;
}
/// Helper class for adjusting app bar widgets to account for desktop window controls
class DesktopAppBarHelper {
/// Builds actions list with appropriate right padding for macOS and mobile
static List<Widget>? buildAdjustedActions(List<Widget>? actions) {
double? rightPadding;
if (Platform.isMacOS) {
rightPadding = DesktopWindowPadding.macOSRight;
} else if (Platform.isIOS || Platform.isAndroid) {
rightPadding = DesktopWindowPadding.mobileRight;
}
if (rightPadding == null) {
return actions;
}
return actions != null ? [...actions, SizedBox(width: rightPadding)] : [SizedBox(width: rightPadding)];
}
/// Builds leading widget with appropriate left padding for macOS traffic lights
///
/// [includeGestureDetector] - If true, wraps in GestureDetector to prevent window dragging
/// [context] - Required to check if side navigation is visible
static Widget? buildAdjustedLeading(Widget? leading, {bool includeGestureDetector = false, BuildContext? context}) {
if (!Platform.isMacOS || leading == null) {
return leading;
}
if (context != null && SideNavigationScope.isPresent(context)) {
return includeGestureDetector ? wrapWithGestureDetector(leading, opaque: true) : leading;
}
return ListenableBuilder(
listenable: FullscreenStateManager(),
builder: (context, _) {
final paddedWidget = Padding(
padding: .only(left: DesktopWindowPadding.macOSLeftCurrent),
child: leading,
);
return includeGestureDetector ? wrapWithGestureDetector(paddedWidget, opaque: true) : paddedWidget;
},
);
}
/// Builds flexible space with gesture detector on macOS to prevent window dragging
static Widget? buildAdjustedFlexibleSpace(Widget? flexibleSpace) {
if (!Platform.isMacOS || flexibleSpace == null) {
return flexibleSpace;
}
return wrapWithGestureDetector(flexibleSpace);
}
/// Calculates the leading width for SliverAppBar to account for macOS traffic lights
/// [context] - Required to check if side navigation is visible
static double? calculateLeadingWidth(Widget? leading, {BuildContext? context}) {
if (!Platform.isMacOS || leading == null) {
return null;
}
if (context != null && SideNavigationScope.isPresent(context)) {
return null;
}
return DesktopWindowPadding.macOSLeftCurrent + kToolbarHeight;
}
/// Wraps a widget with GestureDetector on macOS to prevent window dragging
///
/// [opaque] - If true, uses HitTestBehavior.opaque to fully consume gestures.
/// If false (default), uses HitTestBehavior.translucent.
static Widget wrapWithGestureDetector(Widget child, {bool opaque = false}) {
if (!Platform.isMacOS) {
return child;
}
return GestureDetector(
behavior: opaque ? HitTestBehavior.opaque : HitTestBehavior.translucent,
// ignore: no-empty-block - consumes gesture to prevent macOS window dragging
onPanDown: (_) {},
child: child,
);
}
}
/// A widget that adds padding to account for desktop window controls.
/// On macOS, adds left padding for traffic lights (reduced in fullscreen).
/// When side navigation is visible, left padding is skipped as the side nav
/// already occupies the traffic lights area.
class DesktopTitleBarPadding extends StatelessWidget {
final Widget child;
final double? leftPadding;
final double? rightPadding;
const DesktopTitleBarPadding({super.key, required this.child, this.leftPadding, this.rightPadding});
@override
Widget build(BuildContext context) {
if (!Platform.isMacOS) {
return child;
}
if (SideNavigationScope.isPresent(context)) {
final right = rightPadding ?? 0.0;
if (right == 0.0) {
return child;
}
return Padding(
padding: .only(right: right),
child: child,
);
}
return ListenableBuilder(
listenable: FullscreenStateManager(),
builder: (context, _) {
// In fullscreen, use minimal padding since traffic lights auto-hide
final left = leftPadding ?? DesktopWindowPadding.macOSLeftCurrent;
final right = rightPadding ?? 0.0;
if (left == 0.0 && right == 0.0) {
return child;
}
return Padding(
padding: .only(left: left, right: right),
child: child,
);
},
);
}
}