|
9 | 9 | import SwiftUI |
10 | 10 |
|
11 | 11 | struct TopBar: View { |
12 | | - @EnvironmentObject private var store: Store |
13 | | - var selectedTab : TabId |
14 | | - |
15 | | - private var isHomePage: Bool { |
16 | | - return selectedTab == .feed |
| 12 | + @EnvironmentObject private var store: Store |
| 13 | + var selectedTab : TabId |
| 14 | + @State private var rotationAngle: Double = 0 |
| 15 | + |
| 16 | + private var isHomePage: Bool { |
| 17 | + return selectedTab == .feed |
| 18 | + } |
| 19 | + |
| 20 | + private var title: String { |
| 21 | + switch selectedTab { |
| 22 | + case .feed: |
| 23 | + let selectedTab = store.appState.feedState.selectedTab |
| 24 | + return selectedTab == .all ? "V2EX" : selectedTab.displayName() |
| 25 | + case .explore: |
| 26 | + return "发现" |
| 27 | + case .message: |
| 28 | + return "通知" |
| 29 | + case .me: |
| 30 | + return "我" |
| 31 | + case .none: |
| 32 | + return .empty |
17 | 33 | } |
18 | | - |
19 | | - private var title: String { |
20 | | - switch selectedTab { |
21 | | - case .feed: |
22 | | - let selectedTab = store.appState.feedState.selectedTab |
23 | | - return selectedTab == .all ? "V2EX" : selectedTab.displayName() |
24 | | - case .explore: |
25 | | - return "发现" |
26 | | - case .message: |
27 | | - return "通知" |
28 | | - case .me: |
29 | | - return "我" |
30 | | - case .none: |
31 | | - return .empty |
| 34 | + } |
| 35 | + |
| 36 | + var body: some View { |
| 37 | + VStack(spacing: 0) { |
| 38 | + ZStack { |
| 39 | + HStack { |
| 40 | + Spacer() |
| 41 | + Image(systemName: "magnifyingglass") |
| 42 | + .foregroundColor(.primary) |
| 43 | + .font(.system(size: 22)) |
| 44 | + .padding(6) |
| 45 | + .forceClickable() |
| 46 | + .to { SearchPage() } |
32 | 47 | } |
33 | | - } |
34 | | - |
35 | | - var body: some View { |
36 | | - VStack(spacing: 0) { |
37 | | - ZStack { |
38 | | - HStack { |
39 | | - Image(systemName: "square.grid.2x2") |
40 | | - .foregroundColor(.primary) |
41 | | - .font(.system(size: 22)) |
42 | | - .padding(6) |
43 | | - .forceClickable() |
44 | | - .hide() |
45 | | - Spacer() |
46 | | - Image(systemName: "magnifyingglass") |
47 | | - .foregroundColor(.primary) |
48 | | - .font(.system(size: 22)) |
49 | | - .padding(6) |
50 | | - .forceClickable() |
51 | | - .to { SearchPage() } |
52 | | - } |
53 | | - .padding(.horizontal, 10) |
54 | | - .padding(.vertical, 8) |
55 | | - |
56 | | - // Centered title |
57 | | - HStack { |
58 | | - Spacer() |
59 | | - if isHomePage { |
60 | | - HStack(spacing: 4) { |
61 | | - Text(title) |
62 | | - .font(.title2) |
63 | | - .foregroundColor(.primary) |
64 | | - .fontWeight(.heavy) |
65 | | - Image(systemName: store.appState.feedState.showFilterMenu ? "chevron.up" : "chevron.down") |
66 | | - .font(.system(size: 14, weight: .semibold)) |
67 | | - .foregroundColor(.primary) |
68 | | - } |
69 | | - .padding(.horizontal, 20) // Expand tap area horizontally |
70 | | - .padding(.vertical, 8) // Expand tap area vertically |
71 | | - .contentShape(Rectangle()) // Make entire padded area tappable |
72 | | - .onTapGesture { |
73 | | - // Soft haptic feedback |
74 | | - let impactFeedback = UIImpactFeedbackGenerator(style: .light) |
75 | | - impactFeedback.impactOccurred() |
76 | | - |
77 | | - dispatch(FeedActions.ToggleFilterMenu()) |
78 | | - } |
79 | | - } else { |
80 | | - Text(title) |
81 | | - .font(.headline) |
82 | | - .foregroundColor(.primary) |
83 | | - .fontWeight(.bold) |
84 | | - } |
85 | | - Spacer() |
86 | | - } |
87 | | - .allowsHitTesting(isHomePage) |
| 48 | + .padding(.horizontal, 10) |
| 49 | + .padding(.vertical, 8) |
| 50 | + |
| 51 | + // Centered title |
| 52 | + HStack { |
| 53 | + Spacer() |
| 54 | + if isHomePage { |
| 55 | + HStack(spacing: 4) { |
| 56 | + Text(title) |
| 57 | + .font(.title2) |
| 58 | + .foregroundColor(.primary) |
| 59 | + .fontWeight(.heavy) |
| 60 | + // Rotate chevron when filter menu is open |
| 61 | + Image(systemName: "chevron.down") |
| 62 | + .font(.system(size: 14, weight: .semibold)) |
| 63 | + .foregroundColor(.primary) |
| 64 | + .rotationEffect(.degrees(rotationAngle)) |
| 65 | + .animation(.spring(response: 0.3, dampingFraction: 0.8), value: store.appState.feedState.showFilterMenu) |
| 66 | + .accessibilityHidden(true) |
88 | 67 | } |
89 | | - .padding(.top, topSafeAreaInset().top) |
90 | | - .background(VEBlur()) |
91 | | - |
92 | | - Divider() |
93 | | - .light() |
94 | | - } |
95 | | - .readSize { |
96 | | - print("size: \($0))") |
| 68 | + .padding(.horizontal, 26) // Expand tap area horizontally |
| 69 | + .padding(.vertical, 8) // Expand tap area vertically |
| 70 | + .forceClickable() |
| 71 | + .onChange(of: store.appState.feedState.showFilterMenu) { newValue in |
| 72 | + withAnimation { |
| 73 | + rotationAngle += 180 |
| 74 | + } |
| 75 | + } |
| 76 | + .onTapGesture { |
| 77 | + // Soft haptic feedback |
| 78 | + let impactFeedback = UIImpactFeedbackGenerator(style: .soft) |
| 79 | + impactFeedback.impactOccurred() |
| 80 | + dispatch(FeedActions.ToggleFilterMenu(), .default) |
| 81 | + } |
| 82 | + } else { |
| 83 | + Text(title) |
| 84 | + .font(.headline) |
| 85 | + .foregroundColor(.primary) |
| 86 | + .fontWeight(.bold) |
| 87 | + } |
| 88 | + Spacer() |
97 | 89 | } |
| 90 | + .allowsHitTesting(isHomePage) |
| 91 | + } |
| 92 | + .padding(.top, topSafeAreaInset().top) |
| 93 | + .background(VEBlur()) |
| 94 | + |
| 95 | + Divider() |
| 96 | + .light() |
98 | 97 | } |
| 98 | + .readSize { |
| 99 | + print("size: \($0))") |
| 100 | + } |
| 101 | + } |
99 | 102 | } |
100 | 103 |
|
101 | 104 | struct TopBar_Previews: PreviewProvider { |
102 | | -// @State static var selecedTab = TabId.feed |
103 | | - static var selecedTab = TabId.explore |
104 | | - |
105 | | - static var previews: some View { |
106 | | - VStack { |
107 | | - TopBar(selectedTab: selecedTab) |
108 | | - Spacer() |
109 | | - } |
110 | | - .ignoresSafeArea(.container) |
| 105 | + // @State static var selecedTab = TabId.feed |
| 106 | + static var selecedTab = TabId.explore |
| 107 | + |
| 108 | + static var previews: some View { |
| 109 | + VStack { |
| 110 | + TopBar(selectedTab: selecedTab) |
| 111 | + Spacer() |
111 | 112 | } |
| 113 | + .ignoresSafeArea(.container) |
| 114 | + } |
112 | 115 | } |
0 commit comments