refactor: Promote constants for subtitle positioning to improve readability and flexibility

This commit is contained in:
Kirill Boychenko 2025-07-28 00:50:16 +02:00
parent b9f87bbc5e
commit 480766f75f
2 changed files with 145 additions and 155 deletions

View file

@ -288,12 +288,8 @@ class _DesktopControlsState extends ConsumerState<DesktopControls> {
); );
} }
final GlobalKey _bottomControlsKey = GlobalKey();
Widget bottomButtons(BuildContext context) { Widget bottomButtons(BuildContext context) {
return Container( return Container(child: Consumer(builder: (context, ref, child) {
key: _bottomControlsKey,
child: Consumer(builder: (context, ref, child) {
final mediaPlayback = ref.watch(mediaPlaybackProvider); final mediaPlayback = ref.watch(mediaPlaybackProvider);
final bitRateOptions = ref.watch(playBackModel.select((value) => value?.bitRateOptions)); final bitRateOptions = ref.watch(playBackModel.select((value) => value?.bitRateOptions));
return Container( return Container(
@ -394,8 +390,7 @@ class _DesktopControlsState extends ConsumerState<DesktopControls> {
Tooltip( Tooltip(
message: context.localized.stop, message: context.localized.stop,
child: IconButton( child: IconButton(
onPressed: () => closePlayer(), onPressed: () => closePlayer(), icon: const Icon(IconsaxPlusLinear.close_square))),
icon: const Icon(IconsaxPlusLinear.close_square))),
const Spacer(), const Spacer(),
if (AdaptiveLayout.viewSizeOf(context) >= ViewSize.tablet && if (AdaptiveLayout.viewSizeOf(context) >= ViewSize.tablet &&
ref.read(videoPlayerProvider).hasPlayer) ...{ ref.read(videoPlayerProvider).hasPlayer) ...{
@ -438,12 +433,6 @@ class _DesktopControlsState extends ConsumerState<DesktopControls> {
})); }));
} }
// Method to get height
double? getMenuHeight() {
final RenderBox? renderBox = _bottomControlsKey.currentContext?.findRenderObject() as RenderBox?;
return renderBox?.size.height;
}
Widget progressBar(MediaPlaybackModel mediaPlayback) { Widget progressBar(MediaPlaybackModel mediaPlayback) {
return Consumer( return Consumer(
builder: (context, ref, child) { builder: (context, ref, child) {

View file

@ -207,12 +207,17 @@ class _VideoSubtitles extends ConsumerStatefulWidget {
} }
class _VideoSubtitlesState extends ConsumerState<_VideoSubtitles> { class _VideoSubtitlesState extends ConsumerState<_VideoSubtitles> {
// Promote constants to static for better readability and flexibility
static const double _menuAreaThreshold = 0.15; // Bottom 15% typically contains controls
static const double _menuAvoidanceOffset = 0.1; // Move up by 10% when needed
static const double _maxSubtitleOffset = 0.85; // Max 85% up from bottom
late List<String> subtitle = widget.controller.player.state.subtitle; late List<String> subtitle = widget.controller.player.state.subtitle;
StreamSubscription<List<String>>? subscription; StreamSubscription<List<String>>? subscription;
@override @override
void initState() { void initState() {
super.initState(); super.initState(); // Move to very start as per best practices
subscription = widget.controller.player.stream.subtitle.listen((value) { subscription = widget.controller.player.stream.subtitle.listen((value) {
if (mounted) { if (mounted) {
setState(() { setState(() {
@ -234,21 +239,17 @@ class _VideoSubtitlesState extends ConsumerState<_VideoSubtitles> {
return settings.verticalOffset; return settings.verticalOffset;
} }
// Estimate the menu area (bottom ~15% of screen typically contains controls)
const menuAreaThreshold = 0.15;
// If subtitles are already positioned above the menu area, leave them alone // If subtitles are already positioned above the menu area, leave them alone
if (settings.verticalOffset >= menuAreaThreshold) { if (settings.verticalOffset >= _menuAreaThreshold) {
return settings.verticalOffset; return settings.verticalOffset;
} }
// When menu is visible and subtitles are in the menu area, // When menu is visible and subtitles are in the menu area,
// move them up slightly to avoid overlap // move them up slightly to avoid overlap
const menuAvoidanceOffset = 0.1; final adjustedOffset = settings.verticalOffset + _menuAvoidanceOffset;
final adjustedOffset = settings.verticalOffset + menuAvoidanceOffset;
// Clamp to reasonable bounds (don't go too high or too low) // Clamp to reasonable bounds (don't go too high or too low)
return math.min(adjustedOffset, 0.85); // Max 85% up from bottom return math.max(0.0, math.min(adjustedOffset, _maxSubtitleOffset));
} }
@override @override
@ -261,12 +262,12 @@ class _VideoSubtitlesState extends ConsumerState<_VideoSubtitles> {
// Return empty widget if libass is enabled (native subtitle rendering) // Return empty widget if libass is enabled (native subtitle rendering)
if (widget.controller.player.platform?.configuration.libass ?? false) { if (widget.controller.player.platform?.configuration.libass ?? false) {
return const IgnorePointer(child: SizedBox.shrink()); return const SizedBox.shrink();
} }
// Return empty widget if no subtitle text // Return empty widget if no subtitle text
if (text.isEmpty) { if (text.isEmpty) {
return const IgnorePointer(child: SizedBox.shrink()); return const SizedBox.shrink();
} }
return SubtitleText( return SubtitleText(