Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Keep the menu bar panel within the screen

Țugui Dragoșf22132b4c040
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/MenuBarPanelRoom.swift b/Packages/PeelCore/Sources/PeelCore/Support/MenuBarPanelRoom.swift
new file mode 100644
index 00000000..c5f09f62
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Support/MenuBarPanelRoom.swift
@@ -0,0 +1,12 @@
+public import Foundation
+
+/// How tall the menu bar panel may be. It opens under the menu bar of whichever display was clicked, and macOS
+/// neither shortens nor scrolls a panel taller than its screen, so it fits the least room any display has, with a
+/// little space kept below it.
+public enum MenuBarPanelRoom {
+ static let spaceBelow: CGFloat = 8
+
+ public static func height(within visibleHeights: [CGFloat]) -> CGFloat? {
+ visibleHeights.min().map { max($0 - spaceBelow, 0) }
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/MenuBarPanelRoomTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/MenuBarPanelRoomTests.swift
new file mode 100644
index 00000000..e7e83588
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/MenuBarPanelRoomTests.swift
@@ -0,0 +1,14 @@
+import Foundation
+@testable import PeelCore
+import Testing
+
+struct MenuBarPanelRoomTests {
+ @Test func fitsTheDisplayWithTheLeastRoom() {
+ #expect(MenuBarPanelRoom.height(within: [959, 629]) == 621)
+ #expect(MenuBarPanelRoom.height(within: [923]) == 915)
+ }
+
+ @Test func setsNoBoundWithoutADisplay() {
+ #expect(MenuBarPanelRoom.height(within: []) == nil)
+ }
+}
diff --git a/Peel/Views/MenuBarPanel.swift b/Peel/Views/MenuBarPanel.swift
index 831d63d0..ac62605a 100644
--- a/Peel/Views/MenuBarPanel.swift
+++ b/Peel/Views/MenuBarPanel.swift
@@ -8,6 +8,7 @@ struct MenuBarPanel: View {
@Environment(AppLibrary.self) private var library
@Environment(LifetimeStats.self) private var stats
@State private var isPointingAtUpdates = false
+ @State private var room = Self.room
static let rowPadding: CGFloat = 18
static let iconWidth: CGFloat = 20
@@ -15,9 +16,35 @@ struct MenuBarPanel: View {
/// Where a row's words start, so a divider between rows begins under them.
static let wordsInset = rowPadding + iconWidth + iconSpacing
+ private static var room: CGFloat {
+ MenuBarPanelRoom.height(within: NSScreen.screens.map(\.visibleFrame.height)) ?? .infinity
+ }
+
var body: some View {
+ UpToHeight(maximum: room) {
+ VStack(spacing: 0) {
+ header
+ // Scrolls only when the panel is taller than its room, so Open Peel stays in view.
+ ViewThatFits(in: .vertical) {
+ middle
+ ScrollView { middle }
+ .scrollBounceBehavior(.basedOnSize)
+ }
+ Divider()
+ footer
+ }
+ .frame(width: 320)
+ .background(Album.sheet)
+ }
+ .onReceive(
+ NotificationCenter.default.publisher(for: NSApplication.didChangeScreenParametersNotification)
+ ) { _ in
+ room = Self.room
+ }
+ }
+
+ private var middle: some View {
VStack(spacing: 0) {
- header
hero
Text("Since \(stats.installedOn, format: .dateTime.day().month(.wide).year())")
.font(.callout.weight(.semibold))
@@ -27,11 +54,7 @@ struct MenuBarPanel: View {
Divider()
rows
MenuBarFound { open(at: $0) }
- Divider()
- footer
}
- .frame(width: 320)
- .background(Album.sheet)
}
private var header: some View {
diff --git a/Peel/Views/UpToHeight.swift b/Peel/Views/UpToHeight.swift
new file mode 100644
index 00000000..83919602
--- /dev/null
+++ b/Peel/Views/UpToHeight.swift
@@ -0,0 +1,18 @@
+import SwiftUI
+
+/// Lays its content out at its ideal height, or at `maximum` when that is less, so a list inside can scroll rather
+/// than run past the screen.
+struct UpToHeight: Layout {
+ let maximum: CGFloat
+
+ func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
+ guard let content = subviews.first else { return .zero }
+ let ideal = content.sizeThatFits(ProposedViewSize(width: proposal.width, height: nil))
+ guard ideal.height > maximum else { return ideal }
+ return content.sizeThatFits(ProposedViewSize(width: proposal.width, height: maximum))
+ }
+
+ func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
+ subviews.first?.place(at: bounds.origin, proposal: ProposedViewSize(bounds.size))
+ }
+}
diff --git a/PeelTests/UpToHeightTests.swift b/PeelTests/UpToHeightTests.swift
new file mode 100644
index 00000000..69673f21
--- /dev/null
+++ b/PeelTests/UpToHeightTests.swift
@@ -0,0 +1,32 @@
+@testable import Peel
+import SwiftUI
+import Testing
+
+@MainActor
+struct UpToHeightTests {
+ private func panelHeight(rows: Int, maximum: CGFloat) -> CGFloat {
+ let list = VStack(spacing: 0) {
+ ForEach(0..<rows, id: \.self) { _ in Color.clear.frame(height: 40) }
+ }
+ let panel = UpToHeight(maximum: maximum) {
+ VStack(spacing: 0) {
+ Color.clear.frame(height: 50)
+ ViewThatFits(in: .vertical) {
+ list
+ ScrollView { list }
+ }
+ Color.clear.frame(height: 50)
+ }
+ .frame(width: 320)
+ }
+ return NSHostingView(rootView: panel).fittingSize.height
+ }
+
+ @Test func aPanelTallerThanItsRoomTakesTheRoom() {
+ #expect(panelHeight(rows: 60, maximum: 600) == 600)
+ }
+
+ @Test func aPanelThatFitsKeepsItsHeight() {
+ #expect(panelHeight(rows: 5, maximum: 600) == 300)
+ }
+}