Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
20 changes: 15 additions & 5 deletions Rectangle/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
Notification.Name.configImported.onPost(using: { _ in
self.checkAutoCheckForUpdates()
self.statusItem.refreshVisibility()
self.applicationToggle.reloadFromDefaults()
self.shortcutManager.reloadFromDefaults()
self.snappingManager.reloadFromDefaults()
self.initializeTodo(false)
self.reloadWindowManagementAfterImport()
})

Notification.Name.todoMenuToggled.onPost(using: { _ in
Expand Down Expand Up @@ -140,10 +137,23 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func checkAutoCheckForUpdates() {
updaterController.updater.automaticallyChecksForUpdates = Defaults.SUEnableAutomaticChecks.enabled
}

func reloadWindowManagementAfterImport() {
// Preferences can import before Accessibility is granted. The managers
// will read those settings when accessibilityTrusted() creates them.
guard let applicationToggle, let shortcutManager, let snappingManager else { return }
applicationToggle.reloadFromDefaults()
shortcutManager.reloadFromDefaults()
snappingManager.reloadFromDefaults()
initializeTodo(false)
}

func accessibilityTrusted() {
self.windowCalculationFactory = WindowCalculationFactory()
self.windowManager = WindowManager()
self.windowManager = WindowManager(delegateProviders: [
SnappedWindowFitProvider(),
CooperativeResizeDelegateProvider(windowHistory: Self.windowHistory)
])
self.shortcutManager = ShortcutManager(windowManager: windowManager)
self.applicationToggle = ApplicationToggle(shortcutManager: shortcutManager)
self.snappingManager = SnappingManager()
Expand Down
47 changes: 47 additions & 0 deletions Rectangle/Config/BlurMaterialPreference.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Foundation

enum BlurMaterial: Int {
case standard = 0
case liquidGlass = 1
}

struct BlurMaterialPreference: Default {
let key = "blurMaterial"
private let defaults: UserDefaults

init(defaults: UserDefaults = .standard) {
self.defaults = defaults
}

private var selection: BlurMaterial? {
guard let number = defaults.object(forKey: key) as? NSNumber,
CFGetTypeID(number) != CFBooleanGetTypeID() else { return nil }
let raw = number.intValue
guard number.compare(NSNumber(value: raw)) == .orderedSame else { return nil }
return BlurMaterial(rawValue: raw)
}

var hasSelection: Bool { selection != nil }

// A saved standard choice and an absent choice render alike, but only the latter
// should select Glass the first time the user enables footprint blur.
var value: BlurMaterial {
get { selection ?? .standard }
nonmutating set {
// Preferences can treat Boolean true and integer 1 as equal during a write.
// Remove the malformed value first so the replacement keeps its integer type.
if selection == nil, defaults.object(forKey: key) != nil { defaults.removeObject(forKey: key) }
defaults.set(newValue.rawValue, forKey: key)
}
}

func load(from codable: CodableDefault) {
if let raw = codable.int, let material = BlurMaterial(rawValue: raw) {
value = material
}
}

func toCodable() -> CodableDefault {
CodableDefault(int: value.rawValue)
}
}
9 changes: 8 additions & 1 deletion Rectangle/Config/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ extension Defaults {
for exportableDefault in Defaults.array {
codableDefaults[exportableDefault.key] = exportableDefault.toCodable()
}
codableDefaults[footprintAlpha.key] = CodableDefault(double: effectiveFootprintAlpha)
// An untouched material preference must still allow Glass on the first explicit blur enable.
if !blurMaterial.hasSelection {
codableDefaults.removeValue(forKey: blurMaterial.key)
}
// An unset opacity follows the active preview style after import.
if UserDefaults.standard.object(forKey: footprintAlpha.key) == nil {
codableDefaults.removeValue(forKey: footprintAlpha.key)
}

let config = Config(bundleId: "com.knollsoft.Rectangle",
version: version,
Expand Down
17 changes: 10 additions & 7 deletions Rectangle/Config/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Defaults {
static let cycleSizesIsChanged = BoolDefault(key: "cycleSizesIsChanged")
static let cornerCycleExpansionAxis = IntEnumDefault<CornerCycleExpansionMode>(key: "cornerCycleExpansionAxis", defaultValue: .horizontal)
static let cooperativeCornerResize = BoolDefault(key: "cooperativeCornerResize")
static let fitBesideSnappedWindows = BoolDefault(key: "fitBesideSnappedWindows")
static let experimentalWindowAnimations = BoolDefault(key: "experimentalWindowAnimations")
static let showMinimumWindowSizeWarning = OptionalBoolDefault(key: "showMinimumWindowSizeWarning")
static let allowAnyShortcut = BoolDefault(key: "allowAnyShortcut")
Expand Down Expand Up @@ -74,6 +75,7 @@ class Defaults {
static let footprintFade = OptionalBoolDefault(key: "footprintFade")
static let footprintColor = JSONDefault<CodableColor>(key: "footprintColor")
static let footprintBlur = BoolDefault(key: "footprintBlur")
static let blurMaterial = BlurMaterialPreference()
static let blurAppearance = IntEnumDefault<BlurAppearance>(key: "blurAppearance", defaultValue: .system)

static let SUEnableAutomaticChecks = BoolDefault(key: "SUEnableAutomaticChecks")
Expand Down Expand Up @@ -151,6 +153,7 @@ class Defaults {
cycleSizesIsChanged,
cornerCycleExpansionAxis,
cooperativeCornerResize,
fitBesideSnappedWindows,
experimentalWindowAnimations,
allowAnyShortcut,
windowSnapping,
Expand Down Expand Up @@ -195,6 +198,7 @@ class Defaults {
footprintFade,
footprintColor,
footprintBlur,
blurMaterial,
blurAppearance,
SUEnableAutomaticChecks,
todo,
Expand Down Expand Up @@ -532,6 +536,7 @@ class IntDefault: Default {
class JSONDefault<T: Codable>: StringDefault {

private var typeInitialized = false
private let fallbackValue: T?

var typedValue: T? {
didSet {
Expand All @@ -542,17 +547,16 @@ class JSONDefault<T: Codable>: StringDefault {
}

override init(key: String) {
fallbackValue = nil
super.init(key: key)
loadFromJSON()
typeInitialized = true
}

init(key: String, defaultValue: T) {
fallbackValue = defaultValue
super.init(key: key)
loadFromJSON()
if typedValue == nil {
typedValue = defaultValue
}
}

override func load(from codable: CodableDefault) {
Expand All @@ -565,10 +569,9 @@ class JSONDefault<T: Codable>: StringDefault {
}

private func loadFromJSON() {
guard let jsonString = value else { return }
let decoder = JSONDecoder()
guard let jsonData = jsonString.data(using: .utf8) else { return }
typedValue = try? decoder.decode(T.self, from: jsonData)
let decodedValue = value.flatMap { $0.data(using: .utf8) }
.flatMap { try? JSONDecoder().decode(T.self, from: $0) }
typedValue = decodedValue ?? fallbackValue
}

private func saveToJSON(_ obj: T?) {
Expand Down
19 changes: 11 additions & 8 deletions Rectangle/General/ScreenSelector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct ScreenSelection {
let source: UsableScreens
/// Whether the window's last action no longer counts as the one being repeated.
var forgetsLastAction = false
var cursorScreenFrame: CGRect?
}

struct ScreenSelector {
Expand All @@ -41,7 +42,7 @@ struct ScreenSelector {
var history: CursorScreenHistory = .shared

/// The screens a move is calculated against, or nil when they can't be found.
/// Records the press with cursor screen detection, so it must be called once per move.
/// The cursor press is recorded when the move is admitted.
func screens(for windowElement: AccessibilityElement, windowId: CGWindowID?,
parameters: ExecutionParameters) -> ScreenSelection? {
guard let sourceScreens = screenDetection.detectScreens(using: windowElement) else { return nil }
Expand Down Expand Up @@ -72,24 +73,26 @@ struct ScreenSelector {
let cursorChangedDisplay = history.changedDisplay(for: windowId, cursorScreenFrame: cursorScreenFrame)
let isSameCommandAgain = windowId.map { history.actions[$0] == action } ?? false

if let windowId {
history.screenFrames[windowId] = cursorScreenFrame
history.actions[windowId] = action
}

if !cursorChangedDisplay && isSameCommandAgain {
// Until the cursor points elsewhere, pressing the same command again continues
// from wherever the last press left the window, so repeats that cross displays
// keep working. This mustn't depend on the last action, which is forgotten
// whenever the app adjusts the frame Rectangle set.
return ScreenSelection(usable: sourceScreens, source: sourceScreens)
return ScreenSelection(usable: sourceScreens, source: sourceScreens, cursorScreenFrame: cursorScreenFrame)
}

// Pointing at another display, or pressing another command, is a first press
// on the cursor's display. The last action there doesn't count as a repeat if
// the cursor moved or repeats walked the window off that display.
let forgetsLastAction = cursorChangedDisplay || sourceScreens.currentScreen != cursorScreens.currentScreen
return ScreenSelection(usable: cursorScreens, source: sourceScreens, forgetsLastAction: forgetsLastAction)
return ScreenSelection(usable: cursorScreens, source: sourceScreens, forgetsLastAction: forgetsLastAction,
cursorScreenFrame: cursorScreenFrame)
}

func recordCursorPress(_ selection: ScreenSelection, action: WindowAction, windowId: CGWindowID) {
guard let frame = selection.cursorScreenFrame else { return }
history.screenFrames[windowId] = frame
history.actions[windowId] = action
}

/// A window moved to the cursor's display was sent there by the cursor, so moving
Expand Down
10 changes: 10 additions & 0 deletions Rectangle/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -23022,6 +23022,16 @@
}
}
},
"Liquid Glass for Blur footprint" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Liquid Glass for Blur footprint"
}
}
}
},
"Lower Middle Center Left Sixteenth" : {
"localizations" : {
"en" : {
Expand Down
7 changes: 4 additions & 3 deletions Rectangle/Managers/ShortcutManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,10 @@ class ShortcutManager {
!screenSelector.cursorChangedDisplay(for: windowId),
RepeatedMaximizeRestore.restoreRect(for: parameters.action, windowId: windowId, windowRect: windowElement.logicalFrame) == nil {
if let screen = screenDetection.detectScreens(using: windowElement)?.adjacentScreens?.next{
parameters = ExecutionParameters(parameters.action, updateRestoreRect: parameters.updateRestoreRect, screen: screen, windowElement: windowElement, windowId: windowId, source: parameters.source, preservesFocus: parameters.preservesFocus, completion: parameters.completion, cancellation: parameters.cancellation)
// Bypass any other subsequent action by removing the last action
AppDelegate.windowHistory.lastRectangleActions.removeValue(forKey: windowId)
parameters = ExecutionParameters(parameters.action, updateRestoreRect: parameters.updateRestoreRect, screen: screen,
windowElement: windowElement, windowId: windowId, source: parameters.source,
preservesFocus: parameters.preservesFocus, completion: parameters.completion,
cancellation: parameters.cancellation, isDisplayCycle: true, identity: parameters.identity)
}
}
}
Expand Down
Loading
Loading