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.

Hold the window's floor with constraints on macOS 27

Țugui Dragoșe0a583481a0b
diff --git a/Peel/Navigation/FloorConstraints.swift b/Peel/Navigation/FloorConstraints.swift
new file mode 100644
index 00000000..8bb61470
--- /dev/null
+++ b/Peel/Navigation/FloorConstraints.swift
@@ -0,0 +1,23 @@
+import AppKit
+
+/// Holds the main window at its floor on macOS 27, through required constraints on its content view, which AppKit
+/// keeps on every resize. A SwiftUI minimum around the columns makes SwiftUI 27 lay them out wider than the window
+/// while the sidebar slides back: the page runs past the window's edge and toolbar items move into the overflow menu.
+@MainActor
+final class FloorConstraints {
+ private var constraints: [NSLayoutConstraint] = []
+
+ func hold(_ floor: CGSize, in window: NSWindow) {
+ guard let content = window.contentView else { return }
+ if constraints.first?.firstItem !== content {
+ NSLayoutConstraint.deactivate(constraints)
+ constraints = [
+ content.widthAnchor.constraint(greaterThanOrEqualToConstant: floor.width),
+ content.heightAnchor.constraint(greaterThanOrEqualToConstant: floor.height),
+ ]
+ NSLayoutConstraint.activate(constraints)
+ }
+ if constraints[0].constant != floor.width { constraints[0].constant = floor.width }
+ if constraints[1].constant != floor.height { constraints[1].constant = floor.height }
+ }
+}
diff --git a/Peel/Views/ContentView.swift b/Peel/Views/ContentView.swift
index a6479217..5c46bef7 100644
--- a/Peel/Views/ContentView.swift
+++ b/Peel/Views/ContentView.swift
@@ -37,8 +37,10 @@ struct ContentView: View {
@State private var windowWidth = WindowWidth()
@State private var listColumn = ListColumn()
@State private var hostWindow = HostWindow()
- /// The content's share of `WindowFloor` on the window's screen, once the window is known (`fitFloor`).
+ /// The content's share of `WindowFloor` on the window's screen, once the window is known (`fitFloor`). On macOS
+ /// 27 the window's content view holds the floor instead (`FloorConstraints`).
@State private var floor: CGSize?
+ @State private var floorConstraints = FloorConstraints()
@State private var searchText = ""
private var navigator: Navigator { .shared }
@@ -106,7 +108,7 @@ struct ContentView: View {
// The floor is `WindowFloor`, as far as the window's screen has room for it. On a screen too narrow for the
// sidebar beside what the page needs, the sidebar steps aside (`fitSidebar`), and Home puts its halves one
// above the other. Growing is unrestricted.
- .frame(minWidth: floor?.width, minHeight: floor?.height)
+ .floorFrame(floor)
.onGeometryChange(for: CGFloat.self) { $0.size.width } action: { width in
windowWidth.value = width
fitWidth()
@@ -190,10 +192,15 @@ struct ContentView: View {
}
/// `WindowFloor` is the window's whole frame, while a minimum height set here sizes the content under the
- /// title bar, so the title bar's height comes off it.
+ /// title bar, so the title bar's height comes off it. On macOS 27 the window's content view, which spans the
+ /// whole frame, takes it as it is.
private func fitFloor() {
guard let window = hostWindow.value, let screen = window.screen else { return }
let frame = WindowFloor.size(within: screen.visibleFrame.size)
+ if #available(macOS 27, *) {
+ floorConstraints.hold(frame, in: window)
+ return
+ }
let titleBar = window.frame.height - window.contentLayoutRect.height
let fitting = CGSize(width: frame.width, height: frame.height - titleBar)
if fitting != floor { floor = fitting }
@@ -580,3 +587,15 @@ private struct Destination: Equatable {
private final class WindowWidth {
var value: CGFloat = 0
}
+
+private extension View {
+ /// The floor as a minimum on the content. On macOS 27 the window's content view holds it (`FloorConstraints`).
+ @ViewBuilder
+ func floorFrame(_ floor: CGSize?) -> some View {
+ if #available(macOS 27, *) {
+ self
+ } else {
+ frame(minWidth: floor?.width, minHeight: floor?.height)
+ }
+ }
+}
diff --git a/PeelTests/FloorConstraintsTests.swift b/PeelTests/FloorConstraintsTests.swift
new file mode 100644
index 00000000..afc91fe6
--- /dev/null
+++ b/PeelTests/FloorConstraintsTests.swift
@@ -0,0 +1,48 @@
+import AppKit
+@testable import Peel
+import Testing
+
+@MainActor
+struct FloorConstraintsTests {
+ private func window() -> NSWindow {
+ let window = NSWindow(
+ contentRect: NSRect(x: 0, y: 0, width: 1400, height: 900),
+ styleMask: [.titled, .resizable, .fullSizeContentView], backing: .buffered, defer: false
+ )
+ window.contentView = NSView()
+ return window
+ }
+
+ @Test func aWindowIsNeverMadeSmallerThanItsFloor() throws {
+ let window = window()
+ FloorConstraints().hold(CGSize(width: 1307, height: 756), in: window)
+ window.setContentSize(NSSize(width: 900, height: 500))
+ window.layoutIfNeeded()
+ let content = try #require(window.contentView)
+ #expect(content.frame.width >= 1307)
+ #expect(content.frame.height >= 756)
+ }
+
+ @Test func aNewFloorTakesThePlaceOfTheOldOne() throws {
+ let window = window()
+ let floor = FloorConstraints()
+ floor.hold(CGSize(width: 1307, height: 756), in: window)
+ floor.hold(CGSize(width: 1000, height: 600), in: window)
+ window.setContentSize(NSSize(width: 900, height: 500))
+ window.layoutIfNeeded()
+ let content = try #require(window.contentView)
+ #expect(content.frame.size == CGSize(width: 1000, height: 600))
+ }
+
+ @Test func aNewContentViewTakesTheFloorWithIt() throws {
+ let window = window()
+ let floor = FloorConstraints()
+ floor.hold(CGSize(width: 1307, height: 756), in: window)
+ window.contentView = NSView()
+ floor.hold(CGSize(width: 1307, height: 756), in: window)
+ window.setContentSize(NSSize(width: 900, height: 500))
+ window.layoutIfNeeded()
+ let content = try #require(window.contentView)
+ #expect(content.frame.size == CGSize(width: 1307, height: 756))
+ }
+}