Refactor player controls and improve custom controls visibility

Restructured PlayerControls view hierarchy by extracting controls content into a separate computed property for better code organization. Added shouldShowCustomControls property to VideoPlayerView to properly determine when custom controls should be shown vs system controls. Updated hover logic to only show/hide custom controls when appropriate.
This commit is contained in:
Arkadiusz Fal
2025-11-14 18:58:28 +01:00
parent b8cde410c5
commit 8f97c40257
2 changed files with 182 additions and 172 deletions

View File

@@ -77,6 +77,10 @@ struct VideoPlayerView: View {
@ObservedObject var controlsOverlayModel = ControlOverlaysModel.shared // swiftlint:disable:this swiftui_state_private
var shouldShowCustomControls: Bool {
player.activeBackend == .mpv || !avPlayerUsesSystemControls || player.musicMode
}
var body: some View {
ZStack(alignment: overlayAlignment) {
videoPlayer
@@ -245,7 +249,7 @@ struct VideoPlayerView: View {
var content: some View {
Group {
ZStack(alignment: .bottomLeading) {
ZStack(alignment: .topLeading) {
#if os(tvOS)
ZStack {
player.playerBackendView
@@ -257,64 +261,66 @@ struct VideoPlayerView: View {
.ignoresSafeArea()
#else
GeometryReader { geometry in
player.playerBackendView
.modifier(
VideoPlayerSizeModifier(
geometry: geometry,
aspectRatio: player.aspectRatio,
fullScreen: fullScreenPlayer,
detailsHiddenInFullScreen: detailsHiddenInFullScreen
VStack(spacing: 0) {
player.playerBackendView
.modifier(
VideoPlayerSizeModifier(
geometry: geometry,
aspectRatio: player.aspectRatio,
fullScreen: fullScreenPlayer,
detailsHiddenInFullScreen: detailsHiddenInFullScreen
)
)
)
.onHover { hovering in
hoveringPlayer = hovering
if hovering {
player.controls.show()
} else {
player.controls.hide()
}
}
.gesture(player.controls.presentingOverlays ? nil : playerDragGesture)
#if os(macOS)
.onAppear {
NSEvent.addLocalMonitorForEvents(matching: [.mouseMoved]) {
hoverThrottle.execute {
if !player.currentItem.isNil, hoveringPlayer {
player.controls.resetTimer()
.onHover { hovering in
hoveringPlayer = hovering
// Only show/hide custom controls if they should be used
if shouldShowCustomControls {
if hovering {
player.controls.show()
} else {
player.controls.hide()
}
}
return $0
}
}
#endif
.background(Color.black)
if !detailsHiddenInFullScreen {
VideoDetails(
video: player.videoForDisplay,
fullScreen: $fullScreenDetails,
sidebarQueue: $sidebarQueue
)
.modifier(VideoDetailsPaddingModifier(
playerSize: player.playerSize,
fullScreen: fullScreenDetails
))
.gesture(player.controls.presentingOverlays ? nil : playerDragGesture)
#if os(macOS)
// TODO: Check whether this is needed on macOS.
.onDisappear {
if player.presentingPlayer {
player.setNeedsDrawing(true)
.onAppear {
NSEvent.addLocalMonitorForEvents(matching: [.mouseMoved]) {
hoverThrottle.execute {
if !player.currentItem.isNil, hoveringPlayer, shouldShowCustomControls {
player.controls.resetTimer()
}
}
return $0
}
}
}
#endif
.id(player.currentVideo?.cacheKey)
.transition(.opacity)
.offset(y: detailViewDragOffset)
.gesture(detailsDragGesture)
} else {
VStack {}
.background(Color.black)
if !detailsHiddenInFullScreen {
VideoDetails(
video: player.videoForDisplay,
fullScreen: $fullScreenDetails,
sidebarQueue: $sidebarQueue
)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
#if os(macOS)
// TODO: Check whether this is needed on macOS.
.onDisappear {
if player.presentingPlayer {
player.setNeedsDrawing(true)
}
}
#endif
.id(player.currentVideo?.cacheKey)
.transition(.opacity)
.offset(y: detailViewDragOffset)
.gesture(detailsDragGesture)
} else {
VStack {}
}
}
}
#endif