Skip to content

Commit

Permalink
πŸ”€ Merge pull request #333 from MrKai77/Swiftlint
Browse files Browse the repository at this point in the history
🎨 Swiftlint updates + Update README
  • Loading branch information
MrKai77 authored Apr 18, 2024
2 parents 80b00c3 + 6fa744b commit 8eb3171
Show file tree
Hide file tree
Showing 45 changed files with 415 additions and 270 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: SwiftLint

on:
pull_request:
paths:
- '.github/workflows/swiftlint.yml'
- '.swiftlint.yml'
- '**/*.swift'

jobs:
SwiftLint:
runs-on: ubuntu-latest
container:
image: ghcr.io/realm/swiftlint:latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: SwiftLint
run: |
swiftlint --reporter github-actions-logging
12 changes: 12 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
opt_in_rules:
- modifier_order
- multiline_function_chains
- multiline_arguments_brackets
- superfluous_else
- switch_case_on_newline
- trailing_closure
- unneeded_parentheses_in_closure_argument
- unowned_variable_capture
- vertical_parameter_alignment_on_call
- vertical_whitespace_opening_braces

excluded:
- Loop/Extensions/CGKeyCode+Extensions.swift

Expand Down
2 changes: 2 additions & 0 deletions Loop.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
A84497CA2B395D8C003D4CF3 /* AnchorPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnchorPicker.swift; sourceTree = "<group>"; };
A84497CC2B3A52C7003D4CF3 /* PreviewWindowButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreviewWindowButton.swift; sourceTree = "<group>"; };
A84497D12B3B88A6003D4CF3 /* Optional+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Optional+Extensions.swift"; sourceTree = "<group>"; };
A848130C2BD1A8D100B02E93 /* .github */ = {isa = PBXFileReference; lastKnownFileType = folder; path = .github; sourceTree = "<group>"; };
A848D8A62A8C2F3F00060834 /* LoopManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoopManager.swift; sourceTree = "<group>"; };
A8504D2C2A85832F00C2EFDA /* SoftwareUpdater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoftwareUpdater.swift; sourceTree = "<group>"; };
A859799A2B55FE94009FB067 /* UNNotification+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UNNotification+Extensions.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -403,6 +404,7 @@
A8E59C2C297F5E9A0064D4BA = {
isa = PBXGroup;
children = (
A848130C2BD1A8D100B02E93 /* .github */,
A8291D6D2A4513D200C5CB69 /* .swiftlint.yml */,
A8E6D1FC2A4155DC005751D4 /* .gitignore */,
A86AFD7529888B29008F4892 /* README.md */,
Expand Down
1 change: 0 additions & 1 deletion Loop/About Window/AboutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ struct AboutView: View {
var body: some View {
VStack {
VStack(spacing: 5) {

Image(nsImage: NSImage(named: IconManager.currentAppIcon.iconName)!)
.resizable()
.frame(width: 120, height: 120)
Expand Down
8 changes: 4 additions & 4 deletions Loop/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Defaults
import UserNotifications

class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate {

private let loopManager = LoopManager()
private let windowDragManager = WindowDragManager()

Expand Down Expand Up @@ -56,12 +55,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
func openSettings() {
// Settings window is already open
guard !NSApp.windows.contains(where: { $0.toolbar?.items != nil }) else {
NSApp.windows.first(where: { $0.toolbar?.items != nil })?.orderFrontRegardless()
NSApp.windows.first { $0.toolbar?.items != nil }?.orderFrontRegardless()

return
}

let eventSource = CGEventSource(stateID: .hidSystemState)
let keyCommand = CGEvent(keyboardEventSource: eventSource, virtualKey: 0x2B, keyDown: true)
let keyCommand = CGEvent(keyboardEventSource: eventSource, virtualKey: CGKeyCode.kVK_ANSI_Comma, keyDown: true)
guard let keyCommand else { return }

keyCommand.flags = .maskCommand
Expand Down Expand Up @@ -105,7 +105,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
static func requestNotificationAuthorization() {
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert]
) { (accepted, error) in
) { accepted, error in
if !accepted {
print("User Notification access denied.")
}
Expand Down
6 changes: 4 additions & 2 deletions Loop/Extensions/CGGeometry+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,17 @@ extension CGRect {
x: self.minX,
y: screen.frame.maxY - self.maxY,
width: self.width,
height: self.height)
height: self.height
)
}

func flipY(maxY: CGFloat) -> CGRect {
return CGRect(
x: self.minX,
y: maxY - self.maxY,
width: self.width,
height: self.height)
height: self.height
)
}

func padding(_ sides: Edge.Set, _ amount: CGFloat) -> CGRect {
Expand Down
6 changes: 2 additions & 4 deletions Loop/Extensions/Color+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ extension Color {
case .normal:
if Defaults[.useSystemAccentColor] {
return Color.accentColor
} else {
return Defaults[.customAccentColor]
}
return Defaults[.customAccentColor]
case .darker:
if Defaults[.useSystemAccentColor] {
return Color(nsColor: NSColor.controlAccentColor.blended(withFraction: 0.5, of: .black)!)
} else {
return Defaults[.gradientColor]
}
return Defaults[.gradientColor]
}
}
}
10 changes: 6 additions & 4 deletions Loop/Extensions/NSImage+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import AppKit
extension NSImage {
func resized(to newSize: NSSize) -> NSImage {
return NSImage(size: newSize, flipped: false) { rect in
self.draw(in: rect,
from: NSRect(origin: CGPoint.zero, size: self.size),
operation: NSCompositingOperation.copy,
fraction: 1.0)
self.draw(
in: rect,
from: NSRect(origin: CGPoint.zero, size: self.size),
operation: NSCompositingOperation.copy,
fraction: 1.0
)
return true
}
}
Expand Down
1 change: 0 additions & 1 deletion Loop/Extensions/NSScreen+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import SwiftUI
import Defaults

extension NSScreen {

// Return the CGDirectDisplayID
// Used in to help calculate the size a window needs to be resized to
var displayID: CGDirectDisplayID? {
Expand Down
5 changes: 2 additions & 3 deletions Loop/Managers/AppListManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import SwiftUI
import Algorithms

class AppListManager: ObservableObject {

struct App: Identifiable {
var id: String { bundleID }
var bundleID: String
Expand Down Expand Up @@ -47,7 +46,7 @@ class AppListManager: ObservableObject {

private func queryDidFinishGathering(notification: Notification) {
if let items = qry.results as? [NSMetadataItem] {
self.installedApps = items.compactMap({ item in
self.installedApps = items.compactMap { item in
guard
let bundleId = item.value(forAttribute: NSMetadataItemCFBundleIdentifierKey) as? String,
let displayName = item.value(forAttribute: NSMetadataItemDisplayNameKey) as? String,
Expand All @@ -63,7 +62,7 @@ class AppListManager: ObservableObject {
displayName: displayName,
installationFolder: installationFolder
)
})
}
}
}
}
11 changes: 6 additions & 5 deletions Loop/Managers/IconManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Defaults
import UserNotifications

class IconManager {

struct Icon: Hashable {
var name: String?
var iconName: String
Expand All @@ -21,10 +20,10 @@ class IconManager {
func getName() -> String {
if let name = self.name {
return name
} else {
let prefix = "AppIcon-"
return iconName.replacingOccurrences(of: prefix, with: "")
}

let prefix = "AppIcon-"
return iconName.replacingOccurrences(of: prefix, with: "")
}

static var gregLassaleFooter = String(
Expand Down Expand Up @@ -176,6 +175,8 @@ You've already looped 25 times! As a reward, here's new icon: \(.init(localized:
}

static var currentAppIcon: Icon {
return icons.first(where: { $0.iconName == Defaults[.currentIcon] }) ?? icons.first!
return icons.first {
$0.iconName == Defaults[.currentIcon]
} ?? icons.first!
}
}
22 changes: 9 additions & 13 deletions Loop/Managers/KeybindMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class KeybindMonitor {
self.eventMonitor = CGEventMonitor(eventMask: [.keyDown, .keyUp]) { cgEvent in
if cgEvent.type == .keyDown || cgEvent.type == .keyUp,
let event = NSEvent(cgEvent: cgEvent) {

if event.type == .keyUp {
KeybindMonitor.shared.pressedKeys.remove(event.keyCode.baseKey)
} else if event.type == .keyDown {
Expand All @@ -44,22 +43,21 @@ class KeybindMonitor {
if self.specialEvents.contains(event.keyCode.baseKey) {
if self.canPassthroughSpecialEvents {
return Unmanaged.passRetained(cgEvent)
} else {
return nil
}
return nil
}

// If this is a valid event, don't passthrough
if self.performKeybind(event: event) {
return nil
} else {
// If this wasn't, check if it was a system keybind (ex. screenshot), and
// in that case, passthrough and foce-close Loop
if CGKeyCode.systemKeybinds.contains(self.pressedKeys) {
Notification.Name.forceCloseLoop.post()
print("Detected system keybind, closing!")
return Unmanaged.passRetained(cgEvent)
}
}

// If this wasn't, check if it was a system keybind (ex. screenshot), and
// in that case, passthrough and foce-close Loop
if CGKeyCode.systemKeybinds.contains(self.pressedKeys) {
Notification.Name.forceCloseLoop.post()
print("Detected system keybind, closing!")
return Unmanaged.passRetained(cgEvent)
}
}

Expand All @@ -70,7 +68,6 @@ class KeybindMonitor {
if cgEvent.type == .flagsChanged,
let event = NSEvent(cgEvent: cgEvent),
!Defaults[.triggerKey].contains(where: { $0.baseModifier == event.keyCode.baseModifier }) {

self.checkForModifier(event, .kVK_Shift, .shift)
self.checkForModifier(event, .kVK_Command, .command)
self.checkForModifier(event, .kVK_Option, .option)
Expand Down Expand Up @@ -124,7 +121,6 @@ class KeybindMonitor {

if let newAction = WindowAction.getAction(for: pressedKeys) {
if !event.isARepeat || newAction.willManipulateCurrentWindowSize {

Notification.Name.updateBackendDirection.post(userInfo: ["action": newAction])
print("performKeybind: returning true due to valid event: \(newAction.direction)")
}
Expand Down
35 changes: 20 additions & 15 deletions Loop/Managers/LoopManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import SwiftUI
import Defaults

class LoopManager: ObservableObject {

// Size Adjustment
static var sidesToAdjust: Edge.Set?
static var lastTargetFrame: CGRect = .zero
Expand Down Expand Up @@ -110,15 +109,24 @@ class LoopManager: ObservableObject {
// If mouse over 50 points away, select half or quarter positions
if distanceToMouse > pow(50 - Defaults[.radialMenuThickness], 2) {
switch Int((angleToMouse.normalized().degrees + 22.5) / 45) {
case 0, 8: resizeDirection = .rightHalf
case 1: resizeDirection = .bottomRightQuarter
case 2: resizeDirection = .bottomHalf
case 3: resizeDirection = .bottomLeftQuarter
case 4: resizeDirection = .leftHalf
case 5: resizeDirection = .topLeftQuarter
case 6: resizeDirection = .topHalf
case 7: resizeDirection = .topRightQuarter
default: resizeDirection = .noAction
case 0, 8:
resizeDirection = .rightHalf
case 1:
resizeDirection = .bottomRightQuarter
case 2:
resizeDirection = .bottomHalf
case 3:
resizeDirection = .bottomLeftQuarter
case 4:
resizeDirection = .leftHalf
case 5:
resizeDirection = .topLeftQuarter
case 6:
resizeDirection = .topHalf
case 7:
resizeDirection = .topRightQuarter
default:
resizeDirection = .noAction
}
} else if distanceToMouse < pow(noActionDistance, 2) {
resizeDirection = .noAction
Expand Down Expand Up @@ -159,9 +167,9 @@ class LoopManager: ObservableObject {
var nextIndex = (cycle.firstIndex(of: self.currentAction) ?? -1) + 1

if self.currentAction.direction != .custom {
nextIndex = (cycle.firstIndex(where: {
nextIndex = (cycle.firstIndex {
$0.direction == self.currentAction.direction
}) ?? -1) + 1
} ?? -1) + 1
}

if nextIndex >= cycle.count {
Expand Down Expand Up @@ -220,7 +228,6 @@ class LoopManager: ObservableObject {
performHapticFeedback()

if newAction != currentAction || newAction.willManipulateCurrentWindowSize {

self.currentAction = newAction

if Defaults[.hideUntilDirectionIsChosen] {
Expand Down Expand Up @@ -368,10 +375,8 @@ class LoopManager: ObservableObject {
forceClose == false,
self.currentAction.direction != .noAction,
self.isLoopActive {

if let screenToResizeOn = self.screenToResizeOn,
Defaults[.previewVisibility] {

LoopManager.canAdjustSize = false
WindowEngine.resize(
self.targetWindow!,
Expand Down
8 changes: 6 additions & 2 deletions Loop/Managers/ScreenManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ class ScreenManager {

private static func getScreensInOrder() -> [NSScreen] {
NSScreen.screens
.sorted(by: { $0.frame.origin.y < $1.frame.origin.y })
.sorted(by: { $0.frame.origin.x < $1.frame.origin.x })
.sorted {
$0.frame.origin.y < $1.frame.origin.y
}
.sorted {
$0.frame.origin.x < $1.frame.origin.x
}
}

private static func findScreen(with window: Window, _ screens: [NSScreen]) -> NSScreen? {
Expand Down
1 change: 0 additions & 1 deletion Loop/Managers/WindowDragManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Cocoa
import Defaults

class WindowDragManager {

private var draggingWindow: Window?
private var initialWindowFrame: CGRect?
private var direction: WindowDirection = .noAction
Expand Down
8 changes: 4 additions & 4 deletions Loop/Preview Window/PreviewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import SwiftUI
import Defaults

class PreviewController {

private var previewWindowController: NSWindowController?
private var screen: NSScreen?
private var window: Window?
Expand Down Expand Up @@ -91,9 +90,10 @@ class PreviewController {
}

let targetWindowFrame = action.getFrame(
window: self.window,
bounds: screen.safeScreenFrame
).flipY(maxY: NSScreen.screens[0].frame.maxY)
window: self.window,
bounds: screen.safeScreenFrame
)
.flipY(maxY: NSScreen.screens[0].frame.maxY)

let shouldBeTransparent = targetWindowFrame.size.area == 0

Expand Down
1 change: 0 additions & 1 deletion Loop/Preview Window/PreviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import SwiftUI
import Defaults

struct PreviewView: View {

let previewMode: Bool
@State private var scale: CGFloat = 1

Expand Down
Loading

0 comments on commit 8eb3171

Please sign in to comment.