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 Settings window on its screen when a taller tab makes it grow

Țugui Dragoșb03377cf858a
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/GrowingWindow.swift b/Packages/PeelCore/Sources/PeelCore/Support/GrowingWindow.swift
new file mode 100644
index 00000000..a708a28f
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Support/GrowingWindow.swift
@@ -0,0 +1,12 @@
+public import CoreGraphics
+
+/// Where a window that grew has to stand to stay on its screen.
+public enum GrowingWindow {
+ /// `frame` moved up just as far as its bottom needs to be within `visible`, and never so far that its top passes
+ /// the top of `visible`. Both are in screen coordinates, which grow upward.
+ public static func frame(_ frame: CGRect, within visible: CGRect) -> CGRect {
+ let lowest = frame.height <= visible.height ? visible.minY : visible.maxY - frame.height
+ guard frame.minY < lowest else { return frame }
+ return CGRect(x: frame.minX, y: lowest, width: frame.width, height: frame.height)
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/GrowingWindowTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/GrowingWindowTests.swift
new file mode 100644
index 00000000..09dd709a
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/GrowingWindowTests.swift
@@ -0,0 +1,34 @@
+import CoreGraphics
+@testable import PeelCore
+import Testing
+
+struct GrowingWindowTests {
+ private let macBook = CGRect(x: 0, y: 0, width: 1470, height: 923)
+
+ @Test func aWindowThatGrewPastTheBottomMovesUpByWhatItPassed() {
+ let grown = CGRect(x: 355, y: -36, width: 760, height: 608)
+ #expect(GrowingWindow.frame(grown, within: macBook) == CGRect(x: 355, y: 0, width: 760, height: 608))
+ }
+
+ @Test func aWindowWithinTheScreenStaysWhereItIs() {
+ let window = CGRect(x: 355, y: 352, width: 760, height: 220)
+ #expect(GrowingWindow.frame(window, within: macBook) == window)
+ }
+
+ @Test func aWindowTallerThanTheScreenKeepsItsTopAtTheTopOfTheScreen() {
+ let small = CGRect(x: 0, y: 0, width: 1024, height: 633)
+ let grown = CGRect(x: 100, y: -200, width: 760, height: 700)
+ #expect(GrowingWindow.frame(grown, within: small) == CGRect(x: 100, y: -67, width: 760, height: 700))
+ }
+
+ @Test func aWindowIsNeverMovedDown() {
+ let high = CGRect(x: 355, y: 500, width: 760, height: 608)
+ #expect(GrowingWindow.frame(high, within: macBook) == high)
+ }
+
+ @Test func aScreenBesideTheMainOneIsMeasuredFromItsOwnBottom() {
+ let external = CGRect(x: 1470, y: -272, width: 1920, height: 1055)
+ let grown = CGRect(x: 2000, y: -300, width: 760, height: 608)
+ #expect(GrowingWindow.frame(grown, within: external) == CGRect(x: 2000, y: -272, width: 760, height: 608))
+ }
+}
diff --git a/Peel/Views/ContentView.swift b/Peel/Views/ContentView.swift
index ce290966..f2f3abe7 100644
--- a/Peel/Views/ContentView.swift
+++ b/Peel/Views/ContentView.swift
@@ -572,44 +572,3 @@ private struct Destination: Equatable {
private final class WindowWidth {
var value: CGFloat = 0
}
-
-/// The window the view is in, held the same way.
-private final class HostWindow {
- weak var value: NSWindow?
-}
-
-private struct HostWindowReader: NSViewRepresentable {
- let host: HostWindow
- /// Called once the view is in a window.
- let found: @MainActor () -> Void
-
- func makeNSView(context: Context) -> Reader {
- Reader(host: host, found: found)
- }
-
- func updateNSView(_ nsView: Reader, context: Context) {}
-
- final class Reader: NSView {
- let host: HostWindow
- let found: @MainActor () -> Void
-
- init(host: HostWindow, found: @escaping @MainActor () -> Void) {
- self.host = host
- self.found = found
- super.init(frame: .zero)
- }
-
- required init?(coder: NSCoder) {
- nil
- }
-
- override func viewDidMoveToWindow() {
- super.viewDidMoveToWindow()
- host.value = window
- // On a later turn, since what it changes is SwiftUI state and this can run inside SwiftUI's update.
- if window != nil {
- Task { found() }
- }
- }
- }
-}
diff --git a/Peel/Views/HostWindowReader.swift b/Peel/Views/HostWindowReader.swift
new file mode 100644
index 00000000..3ef6ad84
--- /dev/null
+++ b/Peel/Views/HostWindowReader.swift
@@ -0,0 +1,43 @@
+import AppKit
+import SwiftUI
+
+/// The window a view is in, outside SwiftUI's state, since it is set while SwiftUI updates the view.
+final class HostWindow {
+ weak var value: NSWindow?
+}
+
+struct HostWindowReader: NSViewRepresentable {
+ let host: HostWindow
+ /// Called once the view is in a window.
+ var found: @MainActor () -> Void = {}
+
+ func makeNSView(context: Context) -> Reader {
+ Reader(host: host, found: found)
+ }
+
+ func updateNSView(_ nsView: Reader, context: Context) {}
+
+ final class Reader: NSView {
+ let host: HostWindow
+ let found: @MainActor () -> Void
+
+ init(host: HostWindow, found: @escaping @MainActor () -> Void) {
+ self.host = host
+ self.found = found
+ super.init(frame: .zero)
+ }
+
+ required init?(coder: NSCoder) {
+ nil
+ }
+
+ override func viewDidMoveToWindow() {
+ super.viewDidMoveToWindow()
+ host.value = window
+ // On a later turn, since what it changes is SwiftUI state and this can run inside SwiftUI's update.
+ if window != nil {
+ Task { found() }
+ }
+ }
+ }
+}
diff --git a/Peel/Views/Settings/SettingsView.swift b/Peel/Views/Settings/SettingsView.swift
index 3c8b2ade..f63a6659 100644
--- a/Peel/Views/Settings/SettingsView.swift
+++ b/Peel/Views/Settings/SettingsView.swift
@@ -62,12 +62,25 @@ struct SettingsTabs: View {
/// The Settings window's content. The `Settings` scene sizes the window to fit it, so the window takes the
/// height of the tab shown and can't be resized, as a settings window on the Mac does.
struct SettingsView: View {
+ @State private var hostWindow = HostWindow()
+
var body: some View {
- if #available(macOS 27, *) {
- SettingsWindowTabs()
- } else {
- SettingsTabs(fitsEachTab: true)
+ Group {
+ if #available(macOS 27, *) {
+ SettingsWindowTabs()
+ } else {
+ SettingsTabs(fitsEachTab: true)
+ }
+ }
+ // AppKit keeps the window's top where it was as a taller tab makes it grow, which can carry its bottom past
+ // the bottom of the screen.
+ .onReceive(NotificationCenter.default.publisher(for: NSWindow.didResizeNotification)) { notice in
+ guard let window = hostWindow.value, notice.object as? NSWindow === window, let screen = window.screen
+ else { return }
+ let frame = GrowingWindow.frame(window.frame, within: screen.visibleFrame)
+ if frame.origin != window.frame.origin { window.setFrameOrigin(frame.origin) }
}
+ .background(HostWindowReader(host: hostWindow))
}
}