8000 Remove empty space above popover footer by EClinick · Pull Request #23 · EClinick/juice · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions Sources/Juice/PopoverView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -569,12 +569,13 @@ struct PopoverView: View {

/// Gives MenuBarExtra dashboards a finite viewport while leaving action
/// controls outside the scrollable region. ScrollView has no useful intrinsic
/// height in a menu-bar panel, so the explicit height prevents both clipping
/// and collapse.
/// height in a menu-bar panel, so measure its content and cap the viewport to
/// prevent clipping without leaving empty space above the action footer.
struct PopoverDashboardViewport<Content: View>: View {
let height: CGFloat
private let content: Content
@Environment(\.accessibilityReduceMotion) private var reduceMotion
@State private var contentHeight: CGFloat?
@State private var showsScrollHint = true
@State private var scrollHintHovered = false

Expand All @@ -599,9 +600,20 @@ struct PopoverDashboardViewport<Content: View>: View {
.frame(height: 1)
.id(PopoverDashboardScrollTarget.bottom)
}
.background {
GeometryReader { geometry in
Color.clear.preference(
key: PopoverDashboardContentHeightKey.self,
value: geometry.size.height)
}
}
}
.scrollIndicators(.never)
.frame(height: height)
.frame(height: min(contentHeight ?? height, height))
.onPreferenceChange(PopoverDashboardContentHeightKey.self) { measuredHeight in
guard measuredHeight > 0 else { return }
contentHeight = measuredHeight
}
.overlay(alignment: .bottom) {
if showsScrollHint {
ZStack(alignment: .bottom) {
Expand Down Expand Up @@ -653,6 +665,14 @@ struct PopoverDashboardViewport<Content: View>: View {
}
}

private struct PopoverDashboardContentHeightKey: PreferenceKey {
static let defaultValue: CGFloat = 0

static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = max(value, nextValue())
}
}

private enum PopoverDashboardScrollTarget: Hashable {
case bottom
}
Expand Down
28 changes: 28 additions & 0 deletions Tests/JuiceTests/PopoverLayoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,34 @@ struct PopoverLayoutTests {
#expect(controller.view.bounds.contains(footerFrame))
}

@Test("short dashboard shrinks instead of leaving space above actions")
func shortDashboardShrinksToContent() async throws {
let controller = NSHostingController(
rootView: VStack(alignment: .leading, spacing: 10) {
PopoverDashboardViewport(height: 120) {
Color.clear.frame(height: 40)
}

Divider()
FooterMarker()
.frame(height: 20)
}
.frame(width: 320)
)
controller.view.frame = NSRect(
origin: .zero,
size: controller.view.fittingSize)
controller.view.layoutSubtreeIfNeeded()
try await Task.sleep(for: .milliseconds(50))
controller.view.layoutSubtreeIfNeeded()

let scrollView = try #require(firstSubview(of: NSScrollView.self, in: controller.view))
let scrollFrame = scrollView.convert(scrollView.bounds, to: controller.view)

// The one-point bottom scroll target is part of the measured document.
#expect(abs(scrollFrame.height - 41) < 1)
}

@Test("release updater scrolls with the dashboard while actions stay fixed")
func releaseUpdaterStaysInsideViewport() throws {
let controller = NSHostingController(
Expand Down
0