Repository navigation
Unify tab bar button lane layout #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
19255c6
a6ba1db
0f608a4
be79a65
68ec77f
c1e7c7f
e73e43f
cbd73c4
4f72a3e
d0de5d1
625c370
e022718
77a6f8c
f462bf5
e625083
7aef42c
bfaad67
4730ebb
e48b4ae
9fffbdd
d212b31
a9ea9bd
5dd5b92
3db51a7
eaae2e9
bb53b34
b68e2c4
c5b2449
125625a
7c4439c
8106a1a
6dac95f
819688a
6635168
cc0f548
eeb04dd
9d082c0
90f4981
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,25 @@ private struct TabFramePreferenceKey: PreferenceKey { | |
| } | ||
| } | ||
|
|
||
| private struct SplitButtonLaneWidthPreferenceKey: PreferenceKey { | ||
| static let defaultValue: CGFloat = 0 | ||
|
|
||
| static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { | ||
| value = max(value, nextValue()) | ||
| } | ||
| } | ||
|
|
||
| private struct SplitButtonLaneWidthReader: View { | ||
| var body: some View { | ||
| GeometryReader { geometry in | ||
| Color.clear.preference( | ||
| key: SplitButtonLaneWidthPreferenceKey.self, | ||
| value: geometry.size.width | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @MainActor | ||
| private final class TabBarScrollViewBridge: ObservableObject { | ||
| private struct ScrollMetrics { | ||
|
|
@@ -276,6 +295,80 @@ enum TabBarStyling { | |
| } | ||
| } | ||
|
|
||
| struct TabBarLayout: Equatable { | ||
| let barHeight: CGFloat | ||
| let splitButtonCount: Int | ||
| let splitButtonLaneVisible: Bool | ||
| let reservesSplitButtonLane: Bool | ||
| let measuredSplitButtonLaneWidth: CGFloat | ||
|
|
||
| init( | ||
| tabBarHeight: CGFloat, | ||
| splitButtonCount: Int, | ||
| splitButtonLaneVisible: Bool, | ||
| reservesSplitButtonLane: Bool, | ||
| measuredSplitButtonLaneWidth: CGFloat = 0 | ||
| ) { | ||
| self.barHeight = max(1, tabBarHeight) | ||
| self.splitButtonCount = max(0, splitButtonCount) | ||
| self.splitButtonLaneVisible = splitButtonLaneVisible | ||
| self.reservesSplitButtonLane = reservesSplitButtonLane | ||
| self.measuredSplitButtonLaneWidth = self.splitButtonCount > 0 | ||
| ? max(0, measuredSplitButtonLaneWidth) | ||
| : 0 | ||
| } | ||
|
|
||
| var minimumSplitButtonLaneWidth: CGFloat { | ||
| TabBarStyling.splitButtonsBackdropWidth(buttonCount: splitButtonCount) | ||
| } | ||
|
|
||
| var fullSplitButtonLaneWidth: CGFloat { | ||
| max(minimumSplitButtonLaneWidth, measuredSplitButtonLaneWidth) | ||
| } | ||
|
|
||
| var trailingTabContentInset: CGFloat { | ||
| reservesSplitButtonLane ? fullSplitButtonLaneWidth : 0 | ||
| } | ||
|
|
||
| var splitActionButtonHeight: CGFloat { | ||
| barHeight | ||
| } | ||
|
|
||
| func selectedSeparatorGap( | ||
| selectedTabFrame: CGRect?, | ||
| totalWidth: CGFloat | ||
| ) -> ClosedRange<CGFloat>? { | ||
| guard let selectedTabFrame, totalWidth > 0 else { return nil } | ||
|
|
||
| let minX = min(max(selectedTabFrame.minX, 0), totalWidth) | ||
| let maxX = min(max(selectedTabFrame.maxX, 0), totalWidth) | ||
| guard maxX > minX else { return nil } | ||
| return minX...maxX | ||
| } | ||
|
|
||
| func selectedIndicatorFrame( | ||
| selectedTabFrame: CGRect?, | ||
| totalWidth: CGFloat | ||
| ) -> CGRect? { | ||
| guard let gap = selectedSeparatorGap( | ||
| selectedTabFrame: selectedTabFrame, | ||
| totalWidth: totalWidth | ||
| ) else { return nil } | ||
|
|
||
| let minX = gap.lowerBound | ||
| let maxX = gap.upperBound | ||
| let width = max(0, maxX - minX - TabBarMetrics.activeIndicatorTrailingInset) | ||
| guard width > 0 else { return nil } | ||
|
|
||
| return CGRect( | ||
| x: minX, | ||
| y: 0, | ||
| width: width, | ||
| height: TabBarMetrics.activeIndicatorHeight | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| struct TabContextMenuState { | ||
| let isPinned: Bool | ||
| let isUnread: Bool | ||
|
|
@@ -321,6 +414,7 @@ struct TabBarView: View { | |
| @State private var containerWidth: CGFloat = 0 | ||
| @State private var selectedTabFrameInBar: CGRect? | ||
| @State private var tabFramesInBar: [UUID: CGRect] = [:] | ||
| @State private var measuredSplitButtonLaneWidth: CGFloat = 0 | ||
| @StateObject private var controlKeyMonitor = TabControlShortcutKeyMonitor() | ||
| @StateObject private var scrollViewBridge = TabBarScrollViewBridge() | ||
|
|
||
|
|
@@ -349,7 +443,17 @@ struct TabBarView: View { | |
| } | ||
|
|
||
| private var tabBarHeight: CGFloat { | ||
| max(1, appearance.tabBarHeight) | ||
| tabBarLayout.barHeight | ||
| } | ||
|
|
||
| private var tabBarLayout: TabBarLayout { | ||
| TabBarLayout( | ||
| tabBarHeight: appearance.tabBarHeight, | ||
| splitButtonCount: visibleSplitButtons.count, | ||
| splitButtonLaneVisible: shouldShowSplitButtons, | ||
| reservesSplitButtonLane: showSplitButtons && !isMinimalMode, | ||
| measuredSplitButtonLaneWidth: measuredSplitButtonLaneWidth | ||
| ) | ||
| } | ||
|
|
||
| private var visibleSplitButtons: [BonsplitConfiguration.SplitActionButton] { | ||
|
|
@@ -397,7 +501,7 @@ struct TabBarView: View { | |
| } | ||
|
|
||
| private var splitButtonsBackdropWidth: CGFloat { | ||
| TabBarStyling.splitButtonsBackdropWidth(buttonCount: visibleSplitButtons.count) | ||
| tabBarLayout.fullSplitButtonLaneWidth | ||
| } | ||
|
|
||
| private var splitButtonBackdropFadeWidth: CGFloat { | ||
|
|
@@ -429,11 +533,7 @@ struct TabBarView: View { | |
| } | ||
|
|
||
| private var trailingTabContentInset: CGFloat { | ||
| TabBarStyling.trailingTabContentInset( | ||
| showSplitButtons: showSplitButtons, | ||
| isMinimalMode: isMinimalMode, | ||
| buttonCount: visibleSplitButtons.count | ||
| ) | ||
| tabBarLayout.trailingTabContentInset | ||
| } | ||
|
|
||
| private var leadingScrollAnchorId: String { | ||
|
|
@@ -584,14 +684,14 @@ struct TabBarView: View { | |
| .frame(height: tabBarHeight) | ||
| .mask(combinedMask) | ||
| // Split buttons sit on top of the tab strip. Their backing surface is | ||
| // painted by `tabBarBackground` so translucent colors are composited once, | ||
| // painted by `tabBarSurface` so translucent colors are composited once, | ||
| // while `combinedMask` fades overflowing tab content out below them. | ||
| .overlay(alignment: .trailing) { | ||
| if shouldRenderSplitButtons { | ||
| splitButtons | ||
| .saturation(tabBarSaturation) | ||
| .padding(.bottom, 1) | ||
| .frame(width: splitButtonsBackdropWidth, alignment: .trailing) | ||
| .background(SplitButtonLaneWidthReader()) | ||
| .frame(width: splitButtonsBackdropWidth, height: tabBarHeight, alignment: .trailing) | ||
| .opacity(shouldShowSplitButtons ? 1 : 0) | ||
| .allowsHitTesting(shouldShowSplitButtons) | ||
| .animation(.easeInOut(duration: 0.14), value: shouldShowSplitButtons) | ||
|
|
@@ -602,7 +702,8 @@ struct TabBarView: View { | |
| .frame(maxWidth: .infinity, alignment: .leading) | ||
| .frame(height: tabBarHeight) | ||
| .coordinateSpace(name: "tabBar") | ||
| .background(tabBarBackground) | ||
| .background(tabBarSurface) | ||
| .overlay(maskedTabBarChrome) | ||
| .background(TabBarDragAndHoverView( | ||
|
Comment on lines
+1016
to
1034
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Render order changed so Useful? React with 👍 / 👎. |
||
| isMinimalMode: isMinimalMode, | ||
| onDoubleClick: { | ||
|
|
@@ -652,6 +753,9 @@ struct TabBarView: View { | |
| .onPreferenceChange(TabFramePreferenceKey.self) { frames in | ||
| tabFramesInBar = frames | ||
| } | ||
| .onPreferenceChange(SplitButtonLaneWidthPreferenceKey.self) { width in | ||
| measuredSplitButtonLaneWidth = width | ||
| } | ||
| .onDisappear { | ||
| controlKeyMonitor.stop() | ||
| } | ||
|
|
@@ -920,13 +1024,14 @@ struct TabBarView: View { | |
| } label: { | ||
| splitActionButtonIcon(button.icon) | ||
| } | ||
| .buttonStyle(SplitActionButtonStyle(appearance: appearance)) | ||
| .buttonStyle(SplitActionButtonStyle(appearance: appearance, layout: tabBarLayout)) | ||
| .accessibilityIdentifier(splitActionButtonAccessibilityIdentifier(button)) | ||
| .safeHelp(splitActionButtonTooltip(button, tooltips: tooltips)) | ||
| } | ||
| } | ||
| .padding(.leading, TabBarStyling.splitButtonsLeadingPadding) | ||
| .padding(.trailing, TabBarStyling.splitButtonsTrailingPadding) | ||
| .frame(height: tabBarHeight, alignment: .center) | ||
| } | ||
|
|
||
| private func splitActionButtonAccessibilityIdentifier(_ button: BonsplitConfiguration.SplitActionButton) -> String { | ||
|
|
@@ -1143,56 +1248,40 @@ struct TabBarView: View { | |
| HStack(spacing: 0) { | ||
| // Left scroll fade | ||
| LinearGradient(colors: [.clear, .black], startPoint: .leading, endPoint: .trailing) | ||
| .frame(width: canScrollLeft ? fadeWidth : 0) | ||
| .frame(width: canScrollLeft ? fadeWidth : 0, height: tabBarHeight) | ||
|
|
||
| // Visible content area (always opaque so hit testing reaches the tabs) | ||
| Rectangle().fill(Color.black) | ||
| .frame(height: tabBarHeight) | ||
|
|
||
| if shouldMaskTabsUnderSplitButtonBackdrop { | ||
| LinearGradient(colors: [.black, .clear], startPoint: .leading, endPoint: .trailing) | ||
| .frame(width: splitButtonContentFadeWidth) | ||
| .frame(width: splitButtonContentFadeWidth, height: tabBarHeight) | ||
| // Content is already fully faded before the backdrop ramp starts. This keeps the | ||
| // beginning of a transparent backdrop fade from blending over bright tab text. | ||
| Color.clear | ||
| .frame(width: splitButtonContentOcclusionWidth) | ||
| .frame(width: splitButtonContentOcclusionWidth, height: tabBarHeight) | ||
| } else { | ||
| // Right scroll fade only when scroll content actually overflows. | ||
| LinearGradient(colors: [.black, .clear], startPoint: .leading, endPoint: .trailing) | ||
| .frame(width: canScrollRight ? fadeWidth : 0) | ||
| .frame(width: canScrollRight ? fadeWidth : 0, height: tabBarHeight) | ||
| } | ||
| } | ||
| .frame(height: tabBarHeight) | ||
| } | ||
|
|
||
| // MARK: - Fade Overlays | ||
|
|
||
| /// Mask that fades scroll content at the edges instead of overlaying | ||
| /// a colored gradient. The mask uses black (visible) → clear (hidden), | ||
| /// so the tab bar background shows through naturally with no compositing. | ||
| @ViewBuilder | ||
| private var fadeOverlays: some View { | ||
| let fadeWidth: CGFloat = 24 | ||
| HStack(spacing: 0) { | ||
| LinearGradient(colors: [.clear, .black], startPoint: .leading, endPoint: .trailing) | ||
| .frame(width: canScrollLeft ? fadeWidth : 0) | ||
|
|
||
| Rectangle().fill(Color.black) | ||
|
|
||
| LinearGradient(colors: [.black, .clear], startPoint: .leading, endPoint: .trailing) | ||
| .frame(width: canScrollRight ? fadeWidth : 0) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Background | ||
| // MARK: - Chrome | ||
|
|
||
| @ViewBuilder | ||
| private var tabBarBackground: some View { | ||
| private var tabBarSurface: some View { | ||
| let baseBarColor = TabBarColors.nsColorBarBackground(for: appearance) | ||
| let barColor = appearance.usesSharedBackdrop || isFocused | ||
| ? baseBarColor | ||
| : baseBarColor.withAlphaComponent(baseBarColor.alphaComponent * 0.95) | ||
| HStack(spacing: 0) { | ||
| TabBarLayerBackedColor(color: barColor) | ||
| .frame(maxWidth: .infinity) | ||
| .frame(height: tabBarHeight) | ||
| if shouldPaintSplitButtonBackdrop { | ||
| let effect = splitButtonBackdropEffect | ||
| let targetColor = Self.buttonBackdropColor( | ||
|
|
@@ -1218,23 +1307,33 @@ struct TabBarView: View { | |
| startPoint: .leading, | ||
| endPoint: .trailing | ||
| ) | ||
| .frame(width: splitButtonBackdropFadeWidth) | ||
| .frame(width: splitButtonBackdropFadeWidth, height: tabBarHeight) | ||
| } | ||
| TabBarLayerBackedColor(color: colors.trailing) | ||
| .frame(width: splitButtonBackdropSolidWidth) | ||
| .frame(width: splitButtonBackdropSolidWidth, height: tabBarHeight) | ||
| } | ||
| } | ||
| .overlay { | ||
| GeometryReader { geometry in | ||
| // Bar-edge chrome stays in tab-bar coordinates so item content | ||
| // offsets and padding cannot move it. | ||
| ZStack(alignment: .topLeading) { | ||
| selectedTabIndicator(totalWidth: geometry.size.width) | ||
| tabBarBottomSeparator(totalWidth: geometry.size.width) | ||
| } | ||
| } | ||
| .allowsHitTesting(false) | ||
| .frame(height: tabBarHeight) | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| private var maskedTabBarChrome: some View { | ||
| GeometryReader { geometry in | ||
| tabBarChrome(totalWidth: geometry.size.width) | ||
| .mask(combinedMask) | ||
| } | ||
| .allowsHitTesting(false) | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| private func tabBarChrome(totalWidth: CGFloat) -> some View { | ||
| // Bar-edge chrome uses the same mask as scroll content, so selected | ||
| // indicators, separators, and tab titles fade together under controls. | ||
| ZStack(alignment: .topLeading) { | ||
| selectedTabIndicator(totalWidth: totalWidth) | ||
| tabBarBottomSeparator(totalWidth: totalWidth) | ||
| } | ||
| .frame(height: tabBarHeight) | ||
| } | ||
|
|
||
| @ViewBuilder | ||
|
|
@@ -1253,9 +1352,10 @@ struct TabBarView: View { | |
| Spacer(minLength: 0) | ||
| HStack(spacing: 0) { | ||
| let separator = TabBarColors.separator(for: appearance) | ||
| let gapRange: ClosedRange<CGFloat>? = selectedTabFrameInBar.map { frame in | ||
| frame.minX...frame.maxX | ||
| } | ||
| let gapRange = tabBarLayout.selectedSeparatorGap( | ||
| selectedTabFrame: selectedTabFrameInBar, | ||
| totalWidth: totalWidth | ||
| ) | ||
| let segments = TabBarStyling.separatorSegments( | ||
| totalWidth: totalWidth, | ||
| gap: gapRange | ||
|
|
@@ -1272,16 +1372,9 @@ struct TabBarView: View { | |
| } | ||
|
|
||
| private func selectedIndicatorFrame(totalWidth: CGFloat) -> CGRect? { | ||
| guard let selectedTabFrameInBar, totalWidth > 0 else { return nil } | ||
| let minX = min(max(selectedTabFrameInBar.minX, 0), totalWidth) | ||
| let maxX = min(max(selectedTabFrameInBar.maxX, 0), totalWidth) | ||
| let width = max(0, maxX - minX - TabBarMetrics.activeIndicatorTrailingInset) | ||
| guard width > 0 else { return nil } | ||
| return CGRect( | ||
| x: minX, | ||
| y: 0, | ||
| width: width, | ||
| height: TabBarMetrics.activeIndicatorHeight | ||
| tabBarLayout.selectedIndicatorFrame( | ||
| selectedTabFrame: selectedTabFrameInBar, | ||
| totalWidth: totalWidth | ||
| ) | ||
| } | ||
| } | ||
|
|
@@ -1371,9 +1464,12 @@ private final class SplitActionButtonImageCache { | |
|
|
||
| private struct SplitActionButtonStyle: ButtonStyle { | ||
| let appearance: BonsplitConfiguration.Appearance | ||
| let layout: TabBarLayout | ||
|
|
||
| func makeBody(configuration: Configuration) -> some View { | ||
| configuration.label | ||
| .frame(height: layout.splitActionButtonHeight) | ||
| .contentShape(Rectangle()) | ||
| .foregroundStyle(TabBarColors.splitActionIcon(for: appearance, isPressed: configuration.isPressed)) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| .opacity(configuration.isPressed ? 0.72 : 1.0) | ||
| .animation(.easeOut(duration: 0.08), value: configuration.isPressed) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
fullSplitButtonLaneWidthgrows beyond the default reserved width (for example, custom split buttons with wider emoji/text icons), the lane now reserves and masks that larger width, buttabBarSurfacestill paints the backdrop using onlyfadeWidth + solidWidth. This creates a visible strip where buttons sit over the base tab-bar color instead of the split-button backdrop, which can reduce contrast and break the intended lane styling for wide button configurations. Consider deriving backdrop paint width from the same expanded lane width.Useful? React with 👍 / 👎.