From 7adba6052e163756d3a3d60cbcb68008d12150a0 Mon Sep 17 00:00:00 2001 From: Eskil Gjerde Sviggum Date: Thu, 8 Aug 2024 18:31:15 +0200 Subject: [PATCH] Rename enum `CycleBetweenDivision` to CycleSize Refactor Change CycleSizeDefault key to `selectedCycleSizes` Change cycleSizesIsChanges key to `cycleSizesIsChanged` --- Rectangle.xcodeproj/project.pbxproj | 8 +-- Rectangle/Base.lproj/Main.storyboard | 4 +- ...BetweenDivisions.swift => CycleSize.swift} | 32 +++++------ Rectangle/Defaults.swift | 8 +-- .../PrefsWindow/SettingsViewController.swift | 54 +++++++++---------- .../RepeatedExecutionsCalculation.swift | 8 +-- ...epeatedExecutionsInThirdsCalculation.swift | 2 +- 7 files changed, 58 insertions(+), 58 deletions(-) rename Rectangle/{CycleBetweenDivisions.swift => CycleSize.swift} (73%) diff --git a/Rectangle.xcodeproj/project.pbxproj b/Rectangle.xcodeproj/project.pbxproj index e72e6212..ad05e4bf 100644 --- a/Rectangle.xcodeproj/project.pbxproj +++ b/Rectangle.xcodeproj/project.pbxproj @@ -19,7 +19,7 @@ 6490B39F27BF98840056C220 /* BottomCenterRightEighthCalculation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6490B39E27BF98840056C220 /* BottomCenterRightEighthCalculation.swift */; }; 6490B3A127BF98C70056C220 /* BottomRightEighthCalculation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6490B3A027BF98C70056C220 /* BottomRightEighthCalculation.swift */; }; 729E0A982AFF76B1006E2F48 /* CenterProminentlyCalculation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 729E0A972AFF76B1006E2F48 /* CenterProminentlyCalculation.swift */; }; - 7BE578EF2C5BF4EE0083DAE3 /* CycleBetweenDivisions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BE578EE2C5BF4ED0083DAE3 /* CycleBetweenDivisions.swift */; }; + 7BE578EF2C5BF4EE0083DAE3 /* CycleSize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BE578EE2C5BF4ED0083DAE3 /* CycleSize.swift */; }; 866661F2257D248A00A9CD2D /* RepeatedExecutionsInThirdsCalculation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 866661F1257D248A00A9CD2D /* RepeatedExecutionsInThirdsCalculation.swift */; }; 94E9B08E2C3B8D97004C7F41 /* MacTilingDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94E9B08D2C3B8D97004C7F41 /* MacTilingDefaults.swift */; }; 94E9B0902C3E4578004C7F41 /* StringExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94E9B08F2C3E4578004C7F41 /* StringExtension.swift */; }; @@ -187,7 +187,7 @@ 6490B39E27BF98840056C220 /* BottomCenterRightEighthCalculation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BottomCenterRightEighthCalculation.swift; sourceTree = ""; }; 6490B3A027BF98C70056C220 /* BottomRightEighthCalculation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BottomRightEighthCalculation.swift; sourceTree = ""; }; 729E0A972AFF76B1006E2F48 /* CenterProminentlyCalculation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CenterProminentlyCalculation.swift; sourceTree = ""; }; - 7BE578EE2C5BF4ED0083DAE3 /* CycleBetweenDivisions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CycleBetweenDivisions.swift; sourceTree = ""; }; + 7BE578EE2C5BF4ED0083DAE3 /* CycleSize.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CycleSize.swift; sourceTree = ""; }; 866661F1257D248A00A9CD2D /* RepeatedExecutionsInThirdsCalculation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RepeatedExecutionsInThirdsCalculation.swift; sourceTree = ""; }; 94E9B08D2C3B8D97004C7F41 /* MacTilingDefaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacTilingDefaults.swift; sourceTree = ""; }; 94E9B08F2C3E4578004C7F41 /* StringExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringExtension.swift; sourceTree = ""; }; @@ -554,7 +554,7 @@ 9821405F22B3EFB200ABFB3F /* Defaults.swift */, 984EDB0E29A42ED200D119D2 /* LaunchOnLogin.swift */, 98C1008B2305F1FA006E5344 /* SubsequentExecutionMode.swift */, - 7BE578EE2C5BF4ED0083DAE3 /* CycleBetweenDivisions.swift */, + 7BE578EE2C5BF4ED0083DAE3 /* CycleSize.swift */, 985B9BF422B93EEC00A2E8F0 /* ApplicationToggle.swift */, 9824703022AFA8470037B409 /* RectangleStatusItem.swift */, 9824703622B0F3200037B409 /* WindowAction.swift */, @@ -925,7 +925,7 @@ 9824703722B0F3200037B409 /* WindowAction.swift in Sources */, B4521F932BD7CEFB00FD43CC /* ChangeWindowDimensionCalculation.swift in Sources */, 9821402922B3889100ABFB3F /* LowerLeftCalculation.swift in Sources */, - 7BE578EF2C5BF4EE0083DAE3 /* CycleBetweenDivisions.swift in Sources */, + 7BE578EF2C5BF4EE0083DAE3 /* CycleSize.swift in Sources */, 9821402122B3884600ABFB3F /* BottomHalfCalculation.swift in Sources */, 98910B42231476B30066EC23 /* PrefsViewController.swift in Sources */, 9851A5C3251BEBA300ECF78C /* OrientationAware.swift in Sources */, diff --git a/Rectangle/Base.lproj/Main.storyboard b/Rectangle/Base.lproj/Main.storyboard index b9d37271..4ae20109 100644 --- a/Rectangle/Base.lproj/Main.storyboard +++ b/Rectangle/Base.lproj/Main.storyboard @@ -3216,8 +3216,8 @@ - - + + diff --git a/Rectangle/CycleBetweenDivisions.swift b/Rectangle/CycleSize.swift similarity index 73% rename from Rectangle/CycleBetweenDivisions.swift rename to Rectangle/CycleSize.swift index 51be292b..ee4e065c 100644 --- a/Rectangle/CycleBetweenDivisions.swift +++ b/Rectangle/CycleSize.swift @@ -1,5 +1,5 @@ // -// CycleBetweenDivisions.swift +// CycleSize.swift // Rectangle // // Created by Eskil Gjerde Sviggum on 01/08/2024. @@ -8,14 +8,14 @@ import Foundation -enum CycleBetweenDivision: Int, CaseIterable { +enum CycleSize: Int, CaseIterable { case twoThirds = 0 case oneHalf = 1 case oneThird = 2 case oneQuarter = 3 case threeQuarters = 4 - static func fromBits(bits: Int) -> Set { + static func fromBits(bits: Int) -> Set { Set( Self.allCases.filter { (bits >> $0.rawValue) & 1 == 1 @@ -23,8 +23,8 @@ enum CycleBetweenDivision: Int, CaseIterable { ) } - static var firstDivision = CycleBetweenDivision.oneHalf - static var defaultCycleSizes: Set = [.oneHalf, .oneThird, .twoThirds] + static var firstSize = CycleSize.oneHalf + static var defaultSizes: Set = [.oneHalf, .twoThirds, .oneThird] // The expected order of the cycle sizes is to start with the // first division, then go gradually upwards in size and wrap @@ -32,21 +32,21 @@ enum CycleBetweenDivision: Int, CaseIterable { // // For example if all cycles are used, the order should be: // 1/2, 2/3, 3/4, 1/4, 1/3 - static var sortedCycleDivisions: [CycleBetweenDivision] = { + static var sortedSizes: [CycleSize] = { let sortedDivisions = Self.allCases.sorted(by: { $0.fraction < $1.fraction }) - guard let firstDivisionIndex = sortedDivisions.firstIndex(of: firstDivision) else { + guard let firstDivisionIndex = sortedDivisions.firstIndex(of: firstSize) else { return sortedDivisions } let lessThanFistDivision = sortedDivisions[0.. Int { var bits = 0 self.forEach { @@ -98,11 +98,11 @@ extension Set where Element == CycleBetweenDivision { } } -class CycleBetweenDivisionsDefault: Default { - public private(set) var key: String = "cycleBetweenDivisions" +class CycleSizesDefault: Default { + public private(set) var key: String = "selectedCycleSizes" private var initialized = false - var value: Set { + var value: Set { didSet { if initialized { UserDefaults.standard.set(value.toBits(), forKey: key) @@ -112,13 +112,13 @@ class CycleBetweenDivisionsDefault: Default { init() { let bits = UserDefaults.standard.integer(forKey: key) - value = CycleBetweenDivision.fromBits(bits: bits) + value = CycleSize.fromBits(bits: bits) initialized = true } func load(from codable: CodableDefault) { if let bits = codable.int { - let divisions = CycleBetweenDivision.fromBits(bits: bits) + let divisions = CycleSize.fromBits(bits: bits) value = divisions } } diff --git a/Rectangle/Defaults.swift b/Rectangle/Defaults.swift index 7228569d..13bd2954 100644 --- a/Rectangle/Defaults.swift +++ b/Rectangle/Defaults.swift @@ -14,8 +14,8 @@ class Defaults { static let hideMenuBarIcon = BoolDefault(key: "hideMenubarIcon") static let alternateDefaultShortcuts = BoolDefault(key: "alternateDefaultShortcuts") // switch to magnet defaults static let subsequentExecutionMode = SubsequentExecutionDefault() - static let cycleBetweenDivisions = CycleBetweenDivisionsDefault() - static let cycleBetweenDivisionsIsChanged = BoolDefault(key: "cycleBetweenDivisionsIsChanged") + static let selectedCycleSizes = CycleSizesDefault() + static let cycleSizesIsChanged = BoolDefault(key: "cycleSizesIsChanged") static let allowAnyShortcut = BoolDefault(key: "allowAnyShortcut") static let windowSnapping = OptionalBoolDefault(key: "windowSnapping") static let almostMaximizeHeight = FloatDefault(key: "almostMaximizeHeight") @@ -97,8 +97,8 @@ class Defaults { hideMenuBarIcon, alternateDefaultShortcuts, subsequentExecutionMode, - cycleBetweenDivisions, - cycleBetweenDivisionsIsChanged, + selectedCycleSizes, + cycleSizesIsChanged, allowAnyShortcut, windowSnapping, almostMaximizeHeight, diff --git a/Rectangle/PrefsWindow/SettingsViewController.swift b/Rectangle/PrefsWindow/SettingsViewController.swift index 029a2029..d0d8f943 100644 --- a/Rectangle/PrefsWindow/SettingsViewController.swift +++ b/Rectangle/PrefsWindow/SettingsViewController.swift @@ -34,16 +34,16 @@ class SettingsViewController: NSViewController { @IBOutlet weak var stageSlider: NSSlider! @IBOutlet weak var stageLabel: NSTextField! - @IBOutlet weak var cycleBetweenOptionsView: NSStackView! + @IBOutlet weak var cycleSizesView: NSStackView! - @IBOutlet var cycleBetweenOptionsViewHeightConstraint: NSLayoutConstraint! + @IBOutlet var cycleSizesViewHeightConstraint: NSLayoutConstraint! @IBOutlet var todoViewHeightConstraint: NSLayoutConstraint! private var aboutTodoWindowController: NSWindowController? - private var cycleBetweenSizeCheckboxes = [NSButton]() + private var cycleSizeCheckboxes = [NSButton]() @IBAction func toggleLaunchOnLogin(_ sender: NSButton) { let newSetting: Bool = sender.state == .on @@ -236,15 +236,15 @@ class SettingsViewController: NSViewController { initializeTodoModeSettings() - self.cycleBetweenSizeCheckboxes.forEach { + self.cycleSizeCheckboxes.forEach { $0.removeFromSuperview() } - let cycleBetweenSizesCheckboxes = makeCycleBetweenSizesCheckboxes() - cycleBetweenSizesCheckboxes.forEach { checkbox in - cycleBetweenOptionsView.addArrangedSubview(checkbox) + let cycleSizeCheckboxes = makeCycleSizeCheckboxes() + cycleSizeCheckboxes.forEach { checkbox in + cycleSizesView.addArrangedSubview(checkbox) } - self.cycleBetweenSizeCheckboxes = cycleBetweenSizesCheckboxes + self.cycleSizeCheckboxes = cycleSizeCheckboxes initializeCycleBetweenOptionsView(animated: false) @@ -322,8 +322,8 @@ class SettingsViewController: NSViewController { } animateChanges(animated: animated) { - cycleBetweenOptionsView.isHidden = !showOptionsView - cycleBetweenOptionsViewHeightConstraint.isActive = !showOptionsView + cycleSizesView.isHidden = !showOptionsView + cycleSizesViewHeightConstraint.isActive = !showOptionsView } } @@ -341,16 +341,16 @@ class SettingsViewController: NSViewController { } } - private func makeCycleBetweenSizesCheckboxes() -> [NSButton] { - CycleBetweenDivision.sortedCycleDivisions.map { division in - let button = NSButton(checkboxWithTitle: division.title, target: self, action: #selector(didCheckCycleBetweenCheckbox(sender:))) + private func makeCycleSizeCheckboxes() -> [NSButton] { + CycleSize.sortedSizes.map { division in + let button = NSButton(checkboxWithTitle: division.title, target: self, action: #selector(didCheckCycleSizeCheckbox(sender:))) button.tag = division.rawValue button.setContentCompressionResistancePriority(.required, for: .vertical) return button } } - @objc private func didCheckCycleBetweenCheckbox(sender: Any?) { + @objc private func didCheckCycleSizeCheckbox(sender: Any?) { guard let checkbox = sender as? NSButton else { Logger.log("Expected action to be sent from NSButton. Instead, sender is: \(String(describing: sender))") return @@ -358,36 +358,36 @@ class SettingsViewController: NSViewController { let rawValue = checkbox.tag - guard let cycleDivision = CycleBetweenDivision(rawValue: rawValue) else { + guard let cycleSize = CycleSize(rawValue: rawValue) else { Logger.log("Expected tag of cycle between checkbox to match a value of CycleBetweenDivision. Got: \(String(describing: rawValue))") return } - // If cycle between divisions has not been changed, write the defaults. - if !Defaults.cycleBetweenDivisionsIsChanged.enabled { - Defaults.cycleBetweenDivisions.value = CycleBetweenDivision.defaultCycleSizes + // If selected cycle sizes has not been changed, write the defaults. + if !Defaults.cycleSizesIsChanged.enabled { + Defaults.selectedCycleSizes.value = CycleSize.defaultSizes } - Defaults.cycleBetweenDivisionsIsChanged.enabled = true + Defaults.cycleSizesIsChanged.enabled = true if checkbox.state == .on { - Defaults.cycleBetweenDivisions.value.insert(cycleDivision) + Defaults.selectedCycleSizes.value.insert(cycleSize) } else { - Defaults.cycleBetweenDivisions.value.remove(cycleDivision) + Defaults.selectedCycleSizes.value.remove(cycleSize) } } private func setToggleStatesForCycleSizeCheckboxes() { - let useDefaultCycleSizes = !Defaults.cycleBetweenDivisionsIsChanged.enabled - let cycleBetweenSizes = useDefaultCycleSizes ? CycleBetweenDivision.defaultCycleSizes : Defaults.cycleBetweenDivisions.value + let useDefaultCycleSizes = !Defaults.cycleSizesIsChanged.enabled + let cycleSizes = useDefaultCycleSizes ? CycleSize.defaultSizes : Defaults.selectedCycleSizes.value - cycleBetweenSizeCheckboxes.forEach { checkbox in - guard let cycleBetweenSizeForCheckbox = CycleBetweenDivision(rawValue: checkbox.tag) else { + cycleSizeCheckboxes.forEach { checkbox in + guard let cycleSizeForCheckbox = CycleSize(rawValue: checkbox.tag) else { return } - let isAlwaysEnabled = cycleBetweenSizeForCheckbox.isAlwaysEnabled - let isChecked = isAlwaysEnabled || cycleBetweenSizes.contains(cycleBetweenSizeForCheckbox) + let isAlwaysEnabled = cycleSizeForCheckbox.isAlwaysEnabled + let isChecked = isAlwaysEnabled || cycleSizes.contains(cycleSizeForCheckbox) checkbox.state = isChecked ? .on : .off // Show that the box cannot be unchecked. diff --git a/Rectangle/WindowCalculation/RepeatedExecutionsCalculation.swift b/Rectangle/WindowCalculation/RepeatedExecutionsCalculation.swift index bf32fe18..160f5b52 100644 --- a/Rectangle/WindowCalculation/RepeatedExecutionsCalculation.swift +++ b/Rectangle/WindowCalculation/RepeatedExecutionsCalculation.swift @@ -12,7 +12,7 @@ protocol RepeatedExecutionsCalculation { func calculateFirstRect(_ params: RectCalculationParameters) -> RectResult - func calculateRect(for cycleDivision: CycleBetweenDivision, params: RectCalculationParameters) -> RectResult + func calculateRect(for cycleDivision: CycleSize, params: RectCalculationParameters) -> RectResult } @@ -26,10 +26,10 @@ extension RepeatedExecutionsCalculation { return calculateFirstRect(params) } - let useDefaultPositions = !Defaults.cycleBetweenDivisionsIsChanged.enabled - let positions = useDefaultPositions ? CycleBetweenDivision.defaultCycleSizes : Defaults.cycleBetweenDivisions.value + let useDefaultPositions = !Defaults.cycleSizesIsChanged.enabled + let positions = useDefaultPositions ? CycleSize.defaultSizes : Defaults.selectedCycleSizes.value - let sortedPositions = CycleBetweenDivision.sortedCycleDivisions + let sortedPositions = CycleSize.sortedSizes .filter { positions.contains($0) } let position = count % sortedPositions.count diff --git a/Rectangle/WindowCalculation/RepeatedExecutionsInThirdsCalculation.swift b/Rectangle/WindowCalculation/RepeatedExecutionsInThirdsCalculation.swift index c36b52f6..5e6590db 100644 --- a/Rectangle/WindowCalculation/RepeatedExecutionsInThirdsCalculation.swift +++ b/Rectangle/WindowCalculation/RepeatedExecutionsInThirdsCalculation.swift @@ -20,7 +20,7 @@ extension RepeatedExecutionsInThirdsCalculation { return calculateFractionalRect(params, fraction: 1 / 2.0) } - func calculateRect(for cycleDivision: CycleBetweenDivision, params: RectCalculationParameters) -> RectResult { + func calculateRect(for cycleDivision: CycleSize, params: RectCalculationParameters) -> RectResult { let fraction = cycleDivision.fraction return calculateFractionalRect(params, fraction: fraction) }