Skip to content

Commit

Permalink
🚨 Add SwiftLint, fix all warnings/errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKai77 committed Jun 23, 2023
1 parent d41cee6 commit 8405561
Show file tree
Hide file tree
Showing 29 changed files with 537 additions and 539 deletions.
9 changes: 9 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
excluded:
- Loop/Helpers/KeyCode.swift

cyclomatic_complexity:
ignores_case_statements: true

force_cast: warning

line_length: 140
22 changes: 22 additions & 0 deletions Loop.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
/* Begin PBXFileReference section */
A82521EB29E234EB00139654 /* AboutViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutViewController.swift; sourceTree = "<group>"; };
A82521ED29E235AC00139654 /* AccessibilityAccessManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccessibilityAccessManager.swift; sourceTree = "<group>"; };
A8291D6D2A4513D200C5CB69 /* .swiftlint.yml */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = text.yaml; path = .swiftlint.yml; sourceTree = "<group>"; tabWidth = 2; };
A8330ABC2A3AC0CA00673C8D /* Bundle+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Bundle+Extensions.swift"; sourceTree = "<group>"; };
A8330AC02A3AC13100673C8D /* Defaults+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Defaults+Extensions.swift"; sourceTree = "<group>"; };
A8330AC42A3AC15900673C8D /* Notification+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Notification+Extensions.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -191,6 +192,7 @@
A8E59C2C297F5E9A0064D4BA = {
isa = PBXGroup;
children = (
A8291D6D2A4513D200C5CB69 /* .swiftlint.yml */,
A8E6D1FC2A4155DC005751D4 /* .gitignore */,
A86AFD7529888B29008F4892 /* README.md */,
A8E59C37297F5E9A0064D4BA /* Loop */,
Expand Down Expand Up @@ -240,6 +242,7 @@
isa = PBXNativeTarget;
buildConfigurationList = A8E59C44297F5E9B0064D4BA /* Build configuration list for PBXNativeTarget "Loop" */;
buildPhases = (
A8291D6C2A450C2700C5CB69 /* Run SwiftLint */,
A8E59C31297F5E9A0064D4BA /* Sources */,
A8E59C32297F5E9A0064D4BA /* Frameworks */,
A8E59C33297F5E9A0064D4BA /* Resources */,
Expand Down Expand Up @@ -310,6 +313,25 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
A8291D6C2A450C2700C5CB69 /* Run SwiftLint */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
name = "Run SwiftLint";
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "if [[ \"$(uname -m)\" == arm64 ]]; then\n export PATH=\"/opt/homebrew/bin:$PATH\"\nfi\n\nif which swiftlint > /dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi\n";
};
A83F608129874F18005796CE /* Set Build Number */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
Expand Down
72 changes: 44 additions & 28 deletions Loop/About Window/AboutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,58 @@

import SwiftUI

struct packageDescription {
struct PackageDescription {
var name: String
var url: URL
var license: URL
}

struct AboutView: View {

@Environment(\.openURL) private var openURL

@State private var isShowingAcknowledgements = false
@State private var isHoveringOverIcon = false

let PACKAGES: [packageDescription] = [
packageDescription(name: "AppMover", url: URL(string: "https://github.com/iamcalledrob/AppMover")!, license: URL(string: "https://github.com/iamcalledrob/AppMover#license")!),
packageDescription(name: "Defaults", url: URL(string: "https://github.com/sindresorhus/Defaults")!, license: URL(string: "https://github.com/sindresorhus/Defaults/blob/main/license")!),
packageDescription(name: "Sparkle", url: URL(string: "https://sparkle-project.org")!, license: URL(string: "https://github.com/sparkle-project/Sparkle/blob/2.x/LICENSE")!),

let packages: [PackageDescription] = [
PackageDescription(
name: "AppMover",
url: URL(string: "https://github.com/iamcalledrob/AppMover")!,
license: URL(string: "https://github.com/iamcalledrob/AppMover#license")!
),
PackageDescription(
name: "Defaults",
url: URL(
string: "https://github.com/sindresorhus/Defaults"
)!,
license: URL(string: "https://github.com/sindresorhus/Defaults/blob/main/license")!
),
PackageDescription(
name: "Sparkle",
url: URL(string: "https://sparkle-project.org")!,
license: URL(
string: "https://github.com/sparkle-project/Sparkle/blob/2.x/LICENSE"
)!
)
]

var iconAnimation: Animation {
Animation.snappy
.speed(0.5)
}

var body: some View {
VStack {
VStack(spacing: 5) {

// When user puts their cursor at the center of the icon, the icon will spin
ZStack {
Image(nsImage: NSApplication.shared.applicationIconImage)
.resizable()
.frame(width: 120, height: 120)
.rotationEffect(Angle.degrees(isHoveringOverIcon ? 360 : 0))
.animation(iconAnimation, value: isHoveringOverIcon)

// This is what the user needs to hover over
Circle()
.foregroundColor(.clear)
Expand All @@ -51,24 +67,24 @@ struct AboutView: View {
self.isHoveringOverIcon = hover
}
}

Text("\(Bundle.main.appName)")
.font(.title)
.fontWeight(.bold)

Text("Version \(Bundle.main.appVersion) (\(Bundle.main.appBuild))")
.font(.caption2)
.textSelection(.enabled)
.foregroundColor(.secondary)
}

Spacer()

Text("The elegant, mouse-oriented window manager")
.multilineTextAlignment(.center)

Spacer()

Button {
openURL(URL(string: "https://github.com/MrKai77/Loop")!)
} label: {
Expand All @@ -77,7 +93,7 @@ struct AboutView: View {
.frame(maxWidth: .infinity)
}
.controlSize(.large)

Button {
self.isShowingAcknowledgements = true
} label: {
Expand All @@ -88,42 +104,42 @@ struct AboutView: View {
.controlSize(.large)
.popover(isPresented: $isShowingAcknowledgements) {
VStack {
ForEach(0..<PACKAGES.count, id: \.self) { i in
ForEach(0..<packages.count, id: \.self) { idx in
HStack {
Text(PACKAGES[i].name)
Text(packages[idx].name)
Spacer()

Button(action: {
openURL(PACKAGES[i].url)
openURL(packages[idx].url)
}, label: {
Image(systemName: "safari")
})
.help("link")

Button(action: {
openURL(PACKAGES[i].license)
openURL(packages[idx].license)
}, label: {
Image(systemName: "info.circle")
})
.help("license")
}
.frame(width: 180)
.buttonStyle(.link)
.tag(i)
.tag(idx)
.padding(.vertical, 2)
}
}
.padding(10)
}

Link(destination: URL(string: "https://github.com/MrKai77/Loop/blob/release/LICENSE")!) {
Text("MIT License")
.underline()
.font(.caption)
.textSelection(.disabled)
.foregroundColor(.secondary)
}

Text(Bundle.main.copyright)
.textSelection(.disabled)
.foregroundColor(.secondary)
Expand Down
4 changes: 2 additions & 2 deletions Loop/About Window/AboutViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SwiftUI

class AboutViewController {
var aboutWindowController: NSWindowController?

func showAboutWindow() {
if aboutWindowController == nil {
let window = NSWindow()
Expand All @@ -23,7 +23,7 @@ class AboutViewController {
window.center()
aboutWindowController = .init(window: window)
}

aboutWindowController?.showWindow(aboutWindowController?.window)
NSApp.activate(ignoringOtherApps: true)
}
Expand Down
8 changes: 4 additions & 4 deletions Loop/Extensions/AXUIElement+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ extension AXUIElement {
let error = AXUIElementSetAttributeValue(self, attribute as CFString, value)
return error == .success
}

func copyAttributeValue(attribute: String) -> CFTypeRef? {
var ref: CFTypeRef? = nil
var ref: CFTypeRef?
let error = AXUIElementCopyAttributeValue(self, attribute as CFString, &ref)
if error == .success {
return ref
}
return .none
}

func getAttributeNames() -> [String]? {
var ref: CFArray? = nil
var ref: CFArray?
let error = AXUIElementCopyAttributeNames(self, &ref)
if error == .success {
return ref! as [AnyObject] as? [String]
Expand Down
4 changes: 2 additions & 2 deletions Loop/Extensions/Bundle+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ extension Bundle {
var displayName: String { getInfo("CFBundleDisplayName") }
var bundleID: String { getInfo("CFBundleIdentifier") }
var copyright: String { getInfo("NSHumanReadableCopyright") }

var appBuild: String { getInfo("CFBundleVersion") }
var appVersion: String { getInfo("CFBundleShortVersionString") }

func getInfo(_ str: String) -> String {
infoDictionary?[str] as? String ?? "⚠️"
}
Expand Down
9 changes: 5 additions & 4 deletions Loop/Extensions/CGPoint+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ extension CGPoint {

return CGFloat(bearingRadians)
}

func distanceSquared(to comparisonPoint: CGPoint) -> CGFloat {
let from = CGPoint(x: x, y: y)
let to = comparisonPoint

return (from.x - to.x) * (from.x - to.x) + (from.y - to.y) * (from.y - to.y)
return (from.x - comparisonPoint.x)
* (from.x - comparisonPoint.x)
+ (from.y - comparisonPoint.y)
* (from.y - comparisonPoint.y)
}
}
6 changes: 3 additions & 3 deletions Loop/Extensions/Defaults+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ extension Defaults.Keys {
static let currentIcon = Key<String>("currentIcon", default: "AppIcon-Default")
static let timesLooped = Key<Int>("timesLooped", default: 0)
static let isAccessibilityAccessGranted = Key<Bool>("isAccessibilityAccessGranted", default: false)

static let useSystemAccentColor = Key<Bool>("useSystemAccentColor", default: false)
static let accentColor = Key<Color>("accentColor", default: Color(.white))
static let useGradientAccentColor = Key<Bool>("useGradientAccentColor", default: false)
static let gradientAccentColor = Key<Color>("gradientAccentColor", default: Color(.black))

static let triggerKey = Key<UInt16>("triggerKey", default: KeyCode.function)
static let radialMenuCornerRadius = Key<CGFloat>("radialMenuCornerRadius", default: 50)
static let radialMenuThickness = Key<CGFloat>("radialMenuThickness", default: 22)

static let previewVisibility = Key<Bool>("previewVisibility", default: true)
static let previewCornerRadius = Key<CGFloat>("previewCornerRadius", default: 15)
static let previewPadding = Key<CGFloat>("previewPadding", default: 10)
Expand Down
6 changes: 3 additions & 3 deletions Loop/Extensions/NSScreen+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import SwiftUI
// Return the CGDirectDisplayID
// Used in to help calculate the size a window needs to be resized to
extension NSScreen {
var displayID: CGDirectDisplayID {
var displayID: CGDirectDisplayID? {
let key = NSDeviceDescriptionKey("NSScreenNumber")
return self.deviceDescription[key] as! CGDirectDisplayID
return self.deviceDescription[key] as? CGDirectDisplayID
}

func screenWithMouse() -> NSScreen? {
let mouseLocation = NSEvent.mouseLocation
let screens = NSScreen.screens
Expand Down
6 changes: 3 additions & 3 deletions Loop/Helpers/AccessibilityAccessManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ class AccessibilityAccessManager {
// Get current state for accessibility access
let options: NSDictionary = [kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: ask]
let status = AXIsProcessTrustedWithOptions(options)

Defaults[.isAccessibilityAccessGranted] = status
return status
}

func accessibilityAccessAlert() {
let alert = NSAlert()
alert.messageText = "\(Bundle.main.appName) Needs Accessibility Permissions"
alert.informativeText = "Welcome to \(Bundle.main.appName)! Please grant accessibility access to be able to resize windows."
alert.runModal()

checkAccessibilityAccess(ask: true)
}
}
Loading

0 comments on commit 8405561

Please sign in to comment.