// // 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 @Default(.timerDisplayMode) var timerDisplayMode @Namespace var animation private var tabs: [TabModel] { var tabsArray: [TabModel] = [] tabsArray.append(TabModel(label: "Home", icon: "house.fill", view: .home)) if Defaults[.dynamicShelf] { tabsArray.append(TabModel(label: "Shelf", icon: "tray.fill", view: .shelf)) } if enableTimerFeature && timerDisplayMode == .tab { tabsArray.append(TabModel(label: "Timer", icon: "timer", view: .timer)) } // Stats tab only shown when stats feature is enabled if Defaults[.enableStatsFeature] { tabsArray.append(TabModel(label: "Stats", icon: "chart.xyaxis.line", view: .stats)) } if Defaults[.enableNotes] || (Defaults[.enableClipboardManager] && Defaults[.clipboardDisplayMode] == .separateTab) { let label = Defaults[.enableNotes] ? "Notes" : "Clipboard" let icon = Defaults[.enableNotes] ? "note.text" : "doc.on.clipboard" tabsArray.append(TabModel(label: label, icon: icon, view: .notes)) } return tabsArray } var body: some View { HStack(spacing: 0) { ForEach(tabs) { 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()) }