Atoll/DynamicIsland/components/Tabs/TabSelectionView.swift
2025-08-23 23:50:04 +05:30

75 lines
2.6 KiB
Swift

//
// TabSelectionView.swift
// DynamicIsland
//
// Created by Hugo Persson on 2024-08-25.
// Modified by Hariharan Mudaliar
import SwiftUI
import Defaults
struct TabModel: Identifiable {
let id = UUID()
let label: String
let icon: String
let view: NotchViews
}
struct TabSelectionView: View {
@ObservedObject var coordinator = DynamicIslandViewCoordinator.shared
@Default(.enableTimerFeature) var enableTimerFeature
@Default(.enableStatsFeature) var enableStatsFeature
@Default(.enableColorPickerFeature) var enableColorPickerFeature
@Namespace var animation
var availableTabs: [TabModel] {
var tabs = [
TabModel(label: "Home", icon: "house.fill", view: .home),
TabModel(label: "Shelf", icon: "tray.fill", view: .shelf)
]
if enableTimerFeature {
tabs.append(TabModel(label: "Timer", icon: "timer", view: .timer))
}
if enableStatsFeature {
tabs.append(TabModel(label: "Stats", icon: "chart.line.uptrend.xyaxis", view: .stats))
}
if enableColorPickerFeature {
tabs.append(TabModel(label: "ColorPicker", icon: "eyedropper", view: .colorPicker))
}
return tabs
}
var body: some View {
HStack(spacing: 0) {
ForEach(availableTabs) { tab in
TabButton(label: tab.label, icon: tab.icon, selected: coordinator.currentView == tab.view) {
withAnimation(.smooth) {
coordinator.currentView = tab.view
}
}
.frame(height: 26)
.foregroundStyle(tab.view == coordinator.currentView ? .white : .gray)
.background {
if tab.view == coordinator.currentView {
Capsule()
.fill(coordinator.currentView == tab.view ? Color(nsColor: .secondarySystemFill) : Color.clear)
.matchedGeometryEffect(id: "capsule", in: animation)
} else {
Capsule()
.fill(coordinator.currentView == tab.view ? Color(nsColor: .secondarySystemFill) : Color.clear)
.matchedGeometryEffect(id: "capsule", in: animation)
.hidden()
}
}
}
}
.clipShape(Capsule())
}
}
#Preview {
DynamicIslandHeader().environmentObject(DynamicIslandViewModel())
}