Skip to content

Commit

Permalink
Comment cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
JPKribs committed Mar 2, 2025
1 parent a261c4f commit fb2e0ad
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 161 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
//
// Swiftfin is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2025 Jellyfin & Jellyfin Contributors
//

import Defaults
import SwiftUI

extension FilterPickerBar {

struct FilterPickerButton: View {

// MARK: - Defaults

@Default(.accentColor)
private var accentColor

// MARK: - Environment Variables

@Environment(\.isSelected)
private var isSelected

// MARK: - Focus State

@FocusState
private var isFocused: Bool

// MARK: - Button Variables

private let systemName: String?
private let title: String
private let role: ButtonRole?
private var onSelect: () -> Void

// MARK: - Button Dimensions

private let collapsedWidth: CGFloat = 75

private var expandedWidth: CGFloat {
textWidth(for: title) + 20 + collapsedWidth
}

// MARK: - Button Styles

private var buttonColor: Color {
isSelected ? ((role == .destructive && isFocused) ? Color.red.opacity(0.2) : accentColor) : Color.secondarySystemFill
}

private var textColor: Color {
isFocused ? ((role == .destructive) ? .red : .black) : .primary
}

// MARK: - Initializer

init(
systemName: String?,
title: String,
role: ButtonRole?,
onSelect: @escaping () -> Void
) {
self.systemName = systemName
self.title = title
self.role = role
self.onSelect = onSelect
}

// MARK: - Text Width

private func textWidth(for text: String) -> CGFloat {
let textSize = String().height(
withConstrainedWidth: CGFloat.greatestFiniteMagnitude,
font: UIFont.preferredFont(
forTextStyle: .footnote
)
)
let font = UIFont.systemFont(ofSize: textSize, weight: .semibold)
let attributes = [NSAttributedString.Key.font: font]
let size = (text as NSString).size(withAttributes: attributes)
return size.width
}

// MARK: - Body

var body: some View {
ZStack(alignment: .leading) {
Capsule()
.foregroundColor(buttonColor)
.brightness(isFocused ? 0.25 : 0)
.opacity(isFocused ? 1 : 0.5)
.frame(width: isFocused ? expandedWidth : collapsedWidth, height: collapsedWidth)
.overlay {
Capsule()
.stroke(buttonColor, lineWidth: 1)
.brightness(isFocused ? 0.25 : 0)
}
.allowsHitTesting(false)

HStack(spacing: 10) {
if let systemName = systemName {
Image(systemName: systemName)
.hoverEffectDisabled()
.focusEffectDisabled()
.foregroundColor(textColor)
.frame(width: collapsedWidth, alignment: .center)
}

if isFocused {
Text(title)
.foregroundColor(textColor)
.transition(.move(edge: .leading).combined(with: .opacity))

Spacer(minLength: 0)
}
}
.font(.footnote.weight(.semibold))
.frame(height: collapsedWidth)
.allowsHitTesting(false)

Button {
onSelect()
} label: {
Color.clear
.frame(width: collapsedWidth, height: collapsedWidth)
}
.padding(0)
.buttonStyle(.borderless)
.focused($isFocused)
}
.frame(width: collapsedWidth, height: collapsedWidth, alignment: .leading)
.animation(.easeIn(duration: 0.2), value: isFocused)
}
}
}

extension FilterPickerBar.FilterPickerButton {
init(systemName: String, title: String, role: ButtonRole? = nil) {
self.init(
systemName: systemName,
title: title,
role: role,
onSelect: {}
)
}

func onSelect(_ action: @escaping () -> Void) -> Self {
copy(modifying: \.onSelect, with: action)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Defaults
import JellyfinAPI
import SwiftUI

struct FilterBar: View {
struct FilterPickerBar: View {

// MARK: - Observed Object

Expand All @@ -27,7 +27,7 @@ struct FilterBar: View {
var body: some View {
VStack(spacing: 20) {
if viewModel.currentFilters.hasFilters {
FilterButton(
FilterPickerButton(
systemName: "line.3.horizontal.decrease.circle.fill",
title: L10n.reset,
role: .destructive
Expand All @@ -37,12 +37,13 @@ struct FilterBar: View {
}
.environment(\.isSelected, true)
} else {
// Leave space for the Reset Button
Spacer()
.frame(width: 75, height: 75)
}

ForEach(filterTypes, id: \.self) { type in
FilterButton(
FilterPickerButton(
systemName: type.systemImage,
title: type.displayTitle
)
Expand All @@ -60,7 +61,7 @@ struct FilterBar: View {
}
}

extension FilterBar {
extension FilterPickerBar {
init(viewModel: FilterViewModel, types: [ItemFilterType]) {
self.init(
viewModel: viewModel,
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion Swiftfin tvOS/Extensions/View/View-tvOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extension View {
} else {
libraryFilterBars(
filters: {
FilterBar(
FilterPickerBar(
viewModel: viewModel,
types: types
)
Expand Down
Loading

0 comments on commit fb2e0ad

Please sign in to comment.