Skip to content
Closed
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
56 changes: 56 additions & 0 deletions Rectangle.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@
B28F5289FADB9F07DCADCB0A /* WindowFrameAnimation.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA5AF55AD6A9C0F2BE0B6DDD /* WindowFrameAnimation.swift */; };
196F08301B312AD386ACC153 /* WindowKeyboardMotion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40F9A124A18C13EEEFE2E2DD /* WindowKeyboardMotion.swift */; };
212A5B7088F376DAAB80D83D /* ImmediateWindowPlacement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86951EE306D7872356ECEBE9 /* ImmediateWindowPlacement.swift */; };
D618B80A9D85B6065FA46EC4 /* WindowSizeConstraintStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A91570EA76EBDFD318F17A9 /* WindowSizeConstraintStore.swift */; };
2518225E437B7B41809FB6EB /* WindowSizeLimitArchive.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4072980F81B85711259AA380 /* WindowSizeLimitArchive.swift */; };
916ECF86DD46B749C11DA709 /* WindowSizeResizeObservation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 357C78BB785690712E274508 /* WindowSizeResizeObservation.swift */; };
37421F0217CF812957F2D7B7 /* WindowSizeConstraints.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0F6945BC513A54896386AB75 /* WindowSizeConstraints.swift */; };
610CF9AAA1B849E7D4251F68 /* AccessibilityElement+WindowSizeLimits.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3938A4C57358A71DCEEADE /* AccessibilityElement+WindowSizeLimits.swift */; };
3A696116C7C1A960D0C2DB32 /* WindowGeometry.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8A609694C9F97FF7B9F2507 /* WindowGeometry.swift */; };
C5834D5B85E111B1542FCD61 /* AccessibilityReadCancellation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 080E8082F159D7F7E40DA33D /* AccessibilityReadCancellation.swift */; };
57D557BAFB47E23D017CE1B6 /* WindowSizeLimitsWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 517CC9DD512EA8133C239BC9 /* WindowSizeLimitsWindowController.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -485,6 +493,14 @@
EA5AF55AD6A9C0F2BE0B6DDD /* WindowFrameAnimation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowFrameAnimation.swift; sourceTree = "<group>"; };
40F9A124A18C13EEEFE2E2DD /* WindowKeyboardMotion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowKeyboardMotion.swift; sourceTree = "<group>"; };
86951EE306D7872356ECEBE9 /* ImmediateWindowPlacement.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImmediateWindowPlacement.swift; sourceTree = "<group>"; };
4A91570EA76EBDFD318F17A9 /* WindowSizeConstraintStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WindowSizeConstraintStore.swift"; sourceTree = "<group>"; };
4072980F81B85711259AA380 /* WindowSizeLimitArchive.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WindowSizeLimitArchive.swift"; sourceTree = "<group>"; };
357C78BB785690712E274508 /* WindowSizeResizeObservation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WindowSizeResizeObservation.swift"; sourceTree = "<group>"; };
0F6945BC513A54896386AB75 /* WindowSizeConstraints.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WindowSizeConstraints.swift"; sourceTree = "<group>"; };
BB3938A4C57358A71DCEEADE /* AccessibilityElement+WindowSizeLimits.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AccessibilityElement+WindowSizeLimits.swift"; sourceTree = "<group>"; };
E8A609694C9F97FF7B9F2507 /* WindowGeometry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WindowGeometry.swift"; sourceTree = "<group>"; };
080E8082F159D7F7E40DA33D /* AccessibilityReadCancellation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AccessibilityReadCancellation.swift"; sourceTree = "<group>"; };
517CC9DD512EA8133C239BC9 /* WindowSizeLimitsWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WindowSizeLimitsWindowController.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -725,6 +741,9 @@
9824700B22AF9B7D0037B409 /* Rectangle */ = {
isa = PBXGroup;
children = (
AD98157F2789FD0D20D9ADB5 /* Accessibility */,
AF34C11266D4F3B51536C801 /* WindowPlacement */,
4D49FD46832389AADBAE5A9D /* WindowSizeLimits */,
9824700C22AF9B7D0037B409 /* AppDelegate.swift */,
9824701022AF9B7E0037B409 /* Assets.xcassets */,
482E9E412EB1528D00D7928A /* AppIcon.icon */,
Expand Down Expand Up @@ -834,6 +853,7 @@
98ED704D306179160077BD10 /* SettingsWindow */ = {
isa = PBXGroup;
children = (
517CC9DD512EA8133C239BC9 /* WindowSizeLimitsWindowController.swift */,
98985838305C1B6400459AFF /* SettingsWindowController.swift */,
9898583A305C1B6400459AFF /* ShortcutsViewController.swift */,
98ED70523062D3810077BD10 /* ExtraSettings.swift */,
Expand Down Expand Up @@ -886,6 +906,34 @@
path = WindowAnimation;
sourceTree = "<group>";
};
4D49FD46832389AADBAE5A9D /* WindowSizeLimits */ = {
isa = PBXGroup;
children = (
4A91570EA76EBDFD318F17A9 /* WindowSizeConstraintStore.swift */,
4072980F81B85711259AA380 /* WindowSizeLimitArchive.swift */,
357C78BB785690712E274508 /* WindowSizeResizeObservation.swift */,
0F6945BC513A54896386AB75 /* WindowSizeConstraints.swift */,
BB3938A4C57358A71DCEEADE /* AccessibilityElement+WindowSizeLimits.swift */,
);
path = "WindowSizeLimits";
sourceTree = "<group>";
};
AF34C11266D4F3B51536C801 /* WindowPlacement */ = {
isa = PBXGroup;
children = (
E8A609694C9F97FF7B9F2507 /* WindowGeometry.swift */,
);
path = "WindowPlacement";
sourceTree = "<group>";
};
AD98157F2789FD0D20D9ADB5 /* Accessibility */ = {
isa = PBXGroup;
children = (
080E8082F159D7F7E40DA33D /* AccessibilityReadCancellation.swift */,
);
path = "Accessibility";
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -1040,6 +1088,14 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
57D557BAFB47E23D017CE1B6 /* WindowSizeLimitsWindowController.swift in Sources */,
C5834D5B85E111B1542FCD61 /* AccessibilityReadCancellation.swift in Sources */,
3A696116C7C1A960D0C2DB32 /* WindowGeometry.swift in Sources */,
610CF9AAA1B849E7D4251F68 /* AccessibilityElement+WindowSizeLimits.swift in Sources */,
37421F0217CF812957F2D7B7 /* WindowSizeConstraints.swift in Sources */,
916ECF86DD46B749C11DA709 /* WindowSizeResizeObservation.swift in Sources */,
2518225E437B7B41809FB6EB /* WindowSizeLimitArchive.swift in Sources */,
D618B80A9D85B6065FA46EC4 /* WindowSizeConstraintStore.swift in Sources */,
289A31409B0E53587282CECF /* DirectWindowAnimator.swift in Sources */,
6CC120F4A5FAD525090190EF /* WindowAnimationCurve.swift in Sources */,
42BCD7CED5BEC19A6A3433C6 /* WindowAnimationDiagnostics.swift in Sources */,
Expand Down
10 changes: 10 additions & 0 deletions Rectangle/Accessibility/AccessibilityReadCancellation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Cocoa

/// Cancellation never holds its lock while an external app handles AX work.
final class AccessibilityReadCancellation {
private let lock = NSLock()
private var cancelled = false

func cancel() { lock.lock(); cancelled = true; lock.unlock() }
var isCurrent: Bool { lock.lock(); defer { lock.unlock() }; return !cancelled }
}
9 changes: 9 additions & 0 deletions Rectangle/AccessibilityElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ class AccessibilityElement {
}

var minimumSize: CGSize? {
WindowSizeConstraints.shared.minimum(for: self, reported: reportedMinimumSize)
}

var reportedMinimumSize: CGSize? {
wrappedElement.getWrappedValue(.minSize, type: .cgSize)
?? wrappedElement.getWrappedValue(.minimumSize, type: .cgSize)
}
Expand All @@ -155,11 +159,13 @@ class AccessibilityElement {
/// To handle moving to different displays, we have to adjust the size then the position, then the size again since macOS will enforce sizes that fit on the current display.
/// When windows take a long time to adjust size & position, there is some visual stutter with doing each of these actions. The stutter can be slightly reduced by removing the initial size adjustment, which can make unsnap restore appear smoother.
func setFrame(_ frame: CGRect, adjustSizeFirst: Bool = true, adjustPosition: Bool = true) {
let before = WindowSizeConstraints.frameBeforeResize(for: self)
performFrameAdjustment {
if adjustSizeFirst { size = frame.size }
if adjustPosition { position = frame.origin }
size = frame.size
}
if let before { WindowSizeConstraints.shared.observeResize(self, before: before, requested: frame) }
}

/// A move can release a size restriction imposed by the old screen or Dock edge.
Expand Down Expand Up @@ -207,6 +213,9 @@ class AccessibilityElement {
if before.size != target.size { size = target.size }
}
}
if before.size != target.size {
WindowSizeConstraints.shared.observeResize(self, before: before, requested: target)
}
}

private func performFrameAdjustment(_ adjustment: () -> Void) {
Expand Down
7 changes: 5 additions & 2 deletions Rectangle/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Defaults {
static let cornerCycleExpansionAxis = IntEnumDefault<CornerCycleExpansionAxis>(key: "cornerCycleExpansionAxis", defaultValue: .horizontal)
static let cooperativeCornerResize = BoolDefault(key: "cooperativeCornerResize")
static let experimentalWindowAnimations = BoolDefault(key: "experimentalWindowAnimations")
static let rememberWindowSizeLimits = BoolDefault(key: "rememberWindowSizeLimits", defaultValue: true)
static let showMinimumWindowSizeWarning = OptionalBoolDefault(key: "showMinimumWindowSizeWarning")
static let allowAnyShortcut = BoolDefault(key: "allowAnyShortcut")
static let windowSnapping = OptionalBoolDefault(key: "windowSnapping")
Expand Down Expand Up @@ -144,6 +145,8 @@ class Defaults {
cornerCycleExpansionAxis,
cooperativeCornerResize,
experimentalWindowAnimations,
rememberWindowSizeLimits,
showMinimumWindowSizeWarning,
allowAnyShortcut,
windowSnapping,
almostMaximizeHeight,
Expand Down Expand Up @@ -274,9 +277,9 @@ class BoolDefault: Default {
}
}

init(key: String) {
init(key: String, defaultValue: Bool = false) {
self.key = key
enabled = UserDefaults.standard.bool(forKey: key)
enabled = UserDefaults.standard.object(forKey: key) == nil ? defaultValue : UserDefaults.standard.bool(forKey: key)
initialized = true
}

Expand Down
81 changes: 29 additions & 52 deletions Rectangle/MultiWindow/MultiWindowManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,37 @@ class MultiWindowManager {

static func execute(parameters: ExecutionParameters) -> Bool {
// TODO: Protocol and factory for all multi-window positioning algorithms
let operation: () -> Void
switch parameters.action {
case .reverseAll:
ReverseAllManager.reverseAll(windowElement: parameters.windowElement)
return true
operation = { ReverseAllManager.reverseAll(windowElement: parameters.windowElement) }
case .tileAll:
tileAllWindowsOnScreen()
return true
operation = { tileAllWindowsOnScreen() }
case .tileRows:
tileWindowsInBands(.rows)
return true
operation = { tileWindowsInBands(.rows) }
case .tileColumns:
tileWindowsInBands(.columns)
return true
operation = { tileWindowsInBands(.columns) }
case .cascadeAll:
cascadeAllWindowsOnScreen(windowElement: parameters.windowElement)
return true
operation = { cascadeAllWindowsOnScreen(windowElement: parameters.windowElement) }
case .cascadeActiveApp:
cascadeActiveAppWindowsOnScreen(windowElement: parameters.windowElement)
return true
operation = { cascadeActiveAppWindowsOnScreen(windowElement: parameters.windowElement) }
case .tileActiveApp:
tileActiveAppWindowsOnScreen(windowElement: parameters.windowElement)
return true
operation = { tileActiveAppWindowsOnScreen(windowElement: parameters.windowElement) }
case .cycleStackedWindows:
StackCycleManager.cycle(forward: true, windowElement: parameters.windowElement)
return true
operation = { StackCycleManager.cycle(forward: true, windowElement: parameters.windowElement) }
case .cycleStackedWindowsBackward:
StackCycleManager.cycle(forward: false, windowElement: parameters.windowElement)
return true
operation = { StackCycleManager.cycle(forward: false, windowElement: parameters.windowElement) }
default:
return false
}
WindowSizeConstraints.shared.cancelPendingObservations()
let generation = WindowSizeConstraints.shared.observationGeneration
WindowAnimator.shared.afterPendingWrites(isCurrent: {
WindowSizeConstraints.shared.observationGeneration == generation
}) {
operation()
}
return true
}

private static func allWindowsOnScreen(windowElement: AccessibilityElement? = nil, sortByPID: Bool = false) -> (screens: UsableScreens, windows: [AccessibilityElement])? {
Expand Down Expand Up @@ -323,30 +323,17 @@ class MultiWindowManager {
static func tileAllWindowsOnScreen(windows: [AccessibilityElement], screen: NSScreen) {
guard !windows.isEmpty else { return }

let screenFrame = screen.adjustedVisibleFrame().screenFlipped
let count = windows.count

let columns = Int(ceil(sqrt(CGFloat(count))))
let rows = Int(ceil(CGFloat(count) / CGFloat(columns)))
let size = CGSize(width: (screenFrame.maxX - screenFrame.minX) / CGFloat(columns), height: (screenFrame.maxY - screenFrame.minY) / CGFloat(rows))

for (ind, w) in windows.enumerated() {
let column = ind % Int(columns)
let row = ind / Int(columns)
tileWindow(w, screenFrame: screenFrame, size: size, column: column, row: row)
}
tileWindows(windows, in: screen.adjustedVisibleFrame().screenFlipped)
}

private static func tileWindow(_ w: AccessibilityElement, screenFrame: CGRect, size: CGSize, column: Int, row: Int) {
var rect = w.frame

// TODO: save previous position in history

rect.origin.x = screenFrame.origin.x + size.width * CGFloat(column)
rect.origin.y = screenFrame.origin.y + size.height * CGFloat(row)
rect.size = size

w.setFrame(rect)
private static func tileWindows(_ windows: [AccessibilityElement], in bounds: CGRect) {
guard !windows.isEmpty else { return }
WindowSizeConstraints.shared.cancelPendingObservations()
let minima = windows.map { $0.isResizable() ? $0.minimumSize : $0.frame.size }
guard let frames = WindowSizeConstraints.tileFrames(in: bounds, minimumSizes: minima) else {
NSSound.beep(); Logger.log("Known window minimum sizes do not fit the tile area"); return
}
for (window, frame) in zip(windows, frames) { window.setFrame(frame) }
}

static func cascadeAllWindowsOnScreen(windowElement: AccessibilityElement? = nil) {
Expand Down Expand Up @@ -433,7 +420,7 @@ class MultiWindowManager {
}
}

w.setFrame(rect)
guard WindowSizeConstraints.shared.place(w, target: rect, in: screenFrame) else { return }
w.bringToFront()
}

Expand All @@ -449,16 +436,6 @@ class MultiWindowManager {
// keep windows with a pid equal to the front window's pid
let filtered = windows.filter { $0.pid == frontWindowElement.pid }

let count = filtered.count

let columns = Int(ceil(sqrt(CGFloat(count))))
let rows = Int(ceil(CGFloat(count) / CGFloat(columns)))
let size = CGSize(width: (screenFrame.maxX - screenFrame.minX) / CGFloat(columns), height: (screenFrame.maxY - screenFrame.minY) / CGFloat(rows))

for (ind, w) in filtered.enumerated() {
let column = ind % Int(columns)
let row = ind / Int(columns)
tileWindow(w, screenFrame: screenFrame, size: size, column: column, row: row)
}
tileWindows(filtered, in: screenFrame)
}
}
31 changes: 30 additions & 1 deletion Rectangle/SettingsWindow/BehaviorSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,15 @@ struct BehaviorSettingsView: View {
}
}

windowSizeLimitsSection

// MARK: - Extras
Section {
CustomDisclosureGroup {
VStack(alignment: .leading, spacing: 12) {
Divider()
Toggle("Animate windows", isOn: $viewModel.experimentalAnimations)
Toggle("Preserve side axis size for half actions, similar to Windows", isOn: $viewModel.halvesPreserveOtherAxisSize)
Toggle("Show warning when windows cannot be resized small enough", isOn: $viewModel.showMinimumWindowSizeWarning)
Toggle("Show *Extra* shortcuts in menu", isOn: $viewModel.showAdditionalSizesInMenu)
if viewModel.showCombinedDisplayMode {
VStack(alignment: .leading, spacing: 2) {
Expand All @@ -412,6 +413,34 @@ struct BehaviorSettingsView: View {
.animation(.easeInOut(duration: 0.2), value: viewModel.subsequentExecutionMode)
}

private var windowSizeLimitsSection: some View {
Section {
CustomDisclosureGroup {
VStack(alignment: .leading, spacing: 12) {
Divider()
VStack(alignment: .leading, spacing: 4) {
HStack {
Toggle("Remember learned window size limits", isOn: $viewModel.rememberWindowSizeLimits)
.accessibilityIdentifier("rememberWindowSizeLimits")
Spacer()
Button("Manage memory") { viewModel.showWindowSizeLimits() }
.disabled(!viewModel.rememberWindowSizeLimits)
.accessibilityIdentifier("showWindowSizeLimits")
}
Text("Remembers window size limits and updates them as windows change.")
.font(.caption)
.foregroundColor(.secondary)
.accessibilityIdentifier("rememberWindowSizeLimitsDescription")
}
Toggle("Show warning when windows cannot be resized small enough", isOn: $viewModel.showMinimumWindowSizeWarning)
}
.padding(.leading, 12)
} label: {
Label("Window Size Limits", systemImage: "ruler")
}
}
}

/// A recorder for a window action's shortcut, validated like the ones on
/// the Shortcuts tab.
private func windowActionShortcutView(_ action: WindowAction) -> some View {
Expand Down
Loading
Loading