1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 22:30:32 +05:30
yattee/Shared/Player/Video Details/VideoDetailsToolbar.swift

160 lines
5.8 KiB
Swift
Raw Normal View History

2022-11-14 03:20:42 +05:30
import Defaults
2022-11-13 23:22:15 +05:30
import SwiftUI
struct VideoDetailsToolbar: View {
static let lowOpacity = 0.5
2022-11-13 23:22:15 +05:30
var video: Video?
@Binding var page: VideoDetails.DetailsPage
var sidebarQueue: Bool
@State private var tools: [VideoDetailsTool] = [
.init(icon: "info.circle", name: "Info", page: .info),
.init(icon: "wand.and.stars", name: "Inspector", page: .inspector),
.init(icon: "bookmark", name: "Chapters", page: .chapters),
.init(icon: "text.bubble", name: "Comments", page: .comments),
.init(icon: "rectangle.stack.fill", name: "Related", page: .related),
.init(icon: "list.number", name: "Queue", page: .queue)
]
@State private var activeTool: VideoDetailsTool?
@State private var startedToolPosition: CGRect = .zero
@State private var opacity = 1.0
2022-11-14 03:20:42 +05:30
@EnvironmentObject<PlayerModel> private var player
@Default(.playerDetailsPageButtonLabelStyle) private var playerDetailsPageButtonLabelStyle
2022-11-13 23:22:15 +05:30
var body: some View {
2022-11-14 03:20:42 +05:30
VStack {
HStack(spacing: 12) {
ForEach($tools) { $tool in
if $tool.wrappedValue.isAvailable(for: video, sidebarQueue: sidebarQueue) {
ToolView(tool: $tool)
.padding(.vertical, 10)
2022-11-13 23:22:15 +05:30
}
}
2022-11-14 03:20:42 +05:30
}
2022-11-14 23:37:16 +05:30
.id(video?.id)
2022-11-14 03:20:42 +05:30
.onChange(of: page) { newValue in
activeTool = tools.first { $0.id == newValue.rawValue }
}
.coordinateSpace(name: "toolbarArea")
2022-11-14 04:10:18 +05:30
#if !os(tvOS)
.gesture(
DragGesture(minimumDistance: 0)
.onChanged { value in
withAnimation(.linear(duration: 0.2)) {
opacity = 1
}
2022-11-14 03:20:42 +05:30
2022-11-14 04:10:18 +05:30
guard let firstTool = tools.first else { return }
if startedToolPosition == .zero {
startedToolPosition = firstTool.toolPostion
2022-11-13 23:22:15 +05:30
}
2022-11-14 04:10:18 +05:30
let location = CGPoint(x: value.location.x, y: value.location.y)
if let index = tools.firstIndex(where: { $0.toolPostion.contains(location) }),
activeTool?.id != tools[index].id,
tools[index].isAvailable(for: video, sidebarQueue: sidebarQueue)
{
withAnimation(.interpolatingSpring(stiffness: 230, damping: 22)) {
activeTool = tools[index]
}
withAnimation(.linear(duration: 0.25)) {
page = activeTool?.page ?? .info
}
2022-11-13 23:22:15 +05:30
}
}
2022-11-14 04:10:18 +05:30
.onEnded { _ in
withAnimation(.interactiveSpring(response: 0.5, dampingFraction: 1, blendDuration: 1)) {
startedToolPosition = .zero
}
Delay.by(2) {
lowerOpacity()
}
2022-11-14 03:20:42 +05:30
}
2022-11-14 04:10:18 +05:30
)
#endif
}
#if !os(tvOS)
.onHover { hovering in
hovering ? resetOpacity(0.2) : lowerOpacity(0.2)
2022-11-14 03:20:42 +05:30
}
2022-11-14 04:10:18 +05:30
#endif
2022-11-14 03:20:42 +05:30
.onAppear {
Delay.by(2) { lowerOpacity() }
2022-11-13 23:22:15 +05:30
}
2022-11-14 03:20:42 +05:30
.opacity(opacity)
2022-11-13 23:22:15 +05:30
.background(
Rectangle()
.contentShape(Rectangle())
.foregroundColor(.clear)
)
2022-11-14 03:20:42 +05:30
}
func lowerOpacity(_ duration: Double = 1.0) {
withAnimation(.linear(duration: duration)) {
opacity = Self.lowOpacity
}
}
func resetOpacity(_ duration: Double = 1.0) {
withAnimation(.linear(duration: duration)) {
opacity = 1
2022-11-13 23:22:15 +05:30
}
}
@ViewBuilder func ToolView(tool: Binding<VideoDetailsTool>) -> some View {
HStack(spacing: 0) {
Image(systemName: tool.wrappedValue.icon)
.font(.title2)
.foregroundColor(.white)
.frame(width: 30, height: 30)
.layoutPriority(1)
.background(
GeometryReader { proxy in
let frame = proxy.frame(in: .named("toolbarArea"))
Color.clear
.preference(key: RectKey.self, value: frame)
.onPreferenceChange(RectKey.self) { rect in
tool.wrappedValue.toolPostion = rect
}
}
)
2022-11-14 03:20:42 +05:30
if activeToolID == tool.wrappedValue.id,
playerDetailsPageButtonLabelStyle.text,
player.playerSize.width > 450
{
2022-11-13 23:22:15 +05:30
Text(tool.wrappedValue.name)
.font(.system(size: 14).bold())
.padding(.trailing, 4)
2022-11-13 23:22:15 +05:30
.foregroundColor(.white)
.allowsTightening(true)
.lineLimit(1)
}
}
.padding(.horizontal, 10)
.padding(.vertical, 6)
.background(
RoundedRectangle(cornerRadius: 10, style: .continuous)
.fill(activeToolID == tool.wrappedValue.id ? Color.accentColor : Color.secondary)
)
}
var visibleToolsCount: Int {
tools.filter { $0.isAvailable(for: video, sidebarQueue: sidebarQueue) }.count
}
2022-11-13 23:22:15 +05:30
var activeToolID: VideoDetailsTool.ID {
activeTool?.id ?? "queue"
}
}
struct VideoDetailsToolbar_Previews: PreviewProvider {
static var previews: some View {
VideoDetailsToolbar(page: .constant(.queue), sidebarQueue: false)
.injectFixtureEnvironmentObjects()
}
}