|
| 1 | +import Foundation |
| 2 | +import SwiftUI |
| 3 | + |
| 4 | +enum Tab: Int, CaseIterable, Equatable { |
| 5 | + case general |
| 6 | + case account |
| 7 | + case feature |
| 8 | + case customCommand |
| 9 | +// case app |
| 10 | + case debug |
| 11 | + case about |
| 12 | +} |
| 13 | + |
| 14 | +struct TabContainer: View { |
| 15 | + @State var tab = Tab.customCommand |
| 16 | + |
| 17 | + var body: some View { |
| 18 | + VStack(spacing: 0) { |
| 19 | + TabBar(tab: $tab) |
| 20 | + .padding(.bottom, 8) |
| 21 | + |
| 22 | + Divider() |
| 23 | + |
| 24 | + Group { |
| 25 | + switch tab { |
| 26 | + case .general: |
| 27 | + GeneralView() |
| 28 | + case .account: |
| 29 | + AccountView() |
| 30 | + case .feature: |
| 31 | + EmptyView() |
| 32 | + case .customCommand: |
| 33 | + EmptyView() |
| 34 | +// CustomCommandView() |
| 35 | +// case .app: |
| 36 | +// EmptyView() |
| 37 | + case .debug: |
| 38 | + DebugSettingsView() |
| 39 | + case .about: |
| 40 | + EmptyView() |
| 41 | + } |
| 42 | + } |
| 43 | + .frame(minHeight: 400) |
| 44 | + } |
| 45 | + .padding(.top, 8) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +struct TabBar: View { |
| 50 | + @Binding var tab: Tab |
| 51 | + |
| 52 | + var body: some View { |
| 53 | + HStack { |
| 54 | + ForEach(Tab.allCases, id: \.self) { tab in |
| 55 | + switch tab { |
| 56 | + case .general: |
| 57 | + TabBarButton( |
| 58 | + currentTab: $tab, |
| 59 | + title: "General", |
| 60 | + image: "app.gift", |
| 61 | + tab: tab |
| 62 | + ) |
| 63 | + case .account: |
| 64 | + TabBarButton(currentTab: $tab, title: "Account", image: "person", tab: tab) |
| 65 | + case .feature: |
| 66 | + TabBarButton( |
| 67 | + currentTab: $tab, |
| 68 | + title: "Feature", |
| 69 | + image: "star.square.on.square", |
| 70 | + tab: tab |
| 71 | + ) |
| 72 | + case .customCommand: |
| 73 | + TabBarButton( |
| 74 | + currentTab: $tab, |
| 75 | + |
| 76 | + title: "Custom Command", |
| 77 | + image: "puzzlepiece.extension", |
| 78 | + tab: tab |
| 79 | + ) |
| 80 | +// case .app: |
| 81 | +// TabBarButton(currentTab: $tab, title: "App", image: "app", tab: tab) |
| 82 | + case .debug: |
| 83 | + TabBarButton(currentTab: $tab, title: "Advanced", image: "gearshape.2", tab: tab) |
| 84 | + case .about: |
| 85 | + TabBarButton( |
| 86 | + currentTab: $tab, |
| 87 | + title: "About", |
| 88 | + image: "info.circle", |
| 89 | + tab: tab |
| 90 | + ) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +struct TabBarButton: View { |
| 98 | + @Binding var currentTab: Tab |
| 99 | + @State var isHovered = false |
| 100 | + var title: String |
| 101 | + var image: String |
| 102 | + var tab: Tab |
| 103 | + |
| 104 | + var body: some View { |
| 105 | + Button(action: { |
| 106 | + self.currentTab = tab |
| 107 | + }) { |
| 108 | + VStack(spacing: 2) { |
| 109 | + Image(systemName: image) |
| 110 | + .resizable() |
| 111 | + .scaledToFit() |
| 112 | + .frame(height: 18) |
| 113 | + Text(title) |
| 114 | + } |
| 115 | + .font(.body) |
| 116 | + .padding(.horizontal, 12) |
| 117 | + .padding(.vertical, 4) |
| 118 | + .padding(.top, 4) |
| 119 | + .background( |
| 120 | + tab == currentTab |
| 121 | + ? Color(nsColor: .textColor).opacity(0.1) |
| 122 | + : Color.clear, |
| 123 | + in: RoundedRectangle(cornerRadius: 8) |
| 124 | + ) |
| 125 | + .background( |
| 126 | + isHovered |
| 127 | + ? Color(nsColor: .textColor).opacity(0.05) |
| 128 | + : Color.clear, |
| 129 | + in: RoundedRectangle(cornerRadius: 8) |
| 130 | + ) |
| 131 | + } |
| 132 | + .onHover(perform: { yes in |
| 133 | + isHovered = yes |
| 134 | + }) |
| 135 | + .buttonStyle(.borderless) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +struct TabContainer_Previews: PreviewProvider { |
| 140 | + static var previews: some View { |
| 141 | + TabContainer() |
| 142 | + .frame(width: 800) |
| 143 | + } |
| 144 | +} |
| 145 | + |
0 commit comments