Skip to content

Commit 5ad50d3

Browse files
committed
Apply code review suggestions to Lite module
Refactor logging and bundle loading in Lite components: - Replace print statements with os.log.Logger for structured logging - Use Bundle.module for resource bundle access (Swift 5.3+ standard) - Remove hardcoded magic numbers for cursor dimensions - Add proper log levels (debug, info, error) for different scenarios This improves code maintainability, follows Swift best practices, and provides better log management for production use.
1 parent 6b7322e commit 5ad50d3

File tree

2 files changed

+29
-45
lines changed

2 files changed

+29
-45
lines changed

Sources/ClickIt/Lite/SimpleClickEngine.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import Foundation
99
import CoreGraphics
10+
import os.log
1011

1112
/// Simple click engine for basic auto-clicking
1213
@MainActor
@@ -21,6 +22,7 @@ final class SimpleClickEngine {
2122

2223
// MARK: - Properties
2324

25+
private let logger = Logger(subsystem: "com.jsonify.clickit.lite", category: "SimpleClickEngine")
2426
private var isRunning = false
2527
private var clickTask: Task<Void, Never>?
2628
private var clickCount = 0
@@ -105,7 +107,7 @@ final class SimpleClickEngine {
105107
}
106108

107109
// Debug logging
108-
print("🖱️ Performing \(type) click at (\(Int(point.x)), \(Int(point.y)))")
110+
logger.debug("🖱️ Performing \(type) click at (\(Int(point.x)), \(Int(point.y)))")
109111

110112
// Create and post mouse down event
111113
if let mouseDown = CGEvent(

Sources/ClickIt/Lite/SimpleCursorManager.swift

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import AppKit
99
import SwiftUI
10+
import os.log
1011

1112
class SimpleCursorManager {
1213

@@ -16,6 +17,7 @@ class SimpleCursorManager {
1617

1718
// MARK: - Properties
1819

20+
private let logger = Logger(subsystem: "com.jsonify.clickit.lite", category: "SimpleCursorManager")
1921
private var customCursor: NSCursor?
2022
private var originalCursor: NSCursor?
2123
private var cursorUpdateTimer: Timer?
@@ -32,14 +34,14 @@ class SimpleCursorManager {
3234
/// Activates the custom target cursor system-wide
3335
func activateCustomCursor() {
3436
guard let customCursor = customCursor else {
35-
print("❌ Custom cursor not available")
37+
logger.error("❌ Custom cursor not available")
3638
showDebugAlert("Cursor Failed", "Custom cursor could not be loaded. Check console for details.")
3739
return
3840
}
3941

4042
// Prevent double activation
4143
if isCursorActive {
42-
print("ℹ️ Custom cursor already active")
44+
logger.info("ℹ️ Custom cursor already active")
4345
return
4446
}
4547

@@ -58,7 +60,7 @@ class SimpleCursorManager {
5860
self.customCursor?.set()
5961
}
6062

61-
print("✅ Custom target cursor activated with continuous refresh")
63+
logger.info("✅ Custom target cursor activated with continuous refresh")
6264
// Optional: Uncomment to show success alert
6365
// showDebugAlert("Cursor Active", "Custom target cursor has been activated")
6466
}
@@ -86,81 +88,61 @@ class SimpleCursorManager {
8688
// Restore arrow cursor
8789
NSCursor.arrow.set()
8890

89-
print("✅ Default cursor restored")
91+
logger.info("✅ Default cursor restored")
9092
}
9193

9294
// MARK: - Private Methods
9395

9496
/// Finds the correct resource bundle for Swift Package Manager
9597
private func findResourceBundle() -> Bundle {
96-
// Strategy 1: Look for SPM resource bundle (swift run / debug builds)
97-
if let bundleURL = Bundle.main.url(forResource: "ClickIt_ClickItLite", withExtension: "bundle"),
98-
let resourceBundle = Bundle(url: bundleURL) {
99-
print("✅ Found SPM resource bundle at: \(bundleURL.path)")
100-
return resourceBundle
101-
}
102-
103-
// Strategy 2: Look in Contents/Resources for packaged .app builds
104-
if let resourcePath = Bundle.main.resourcePath,
105-
let bundlePath = Bundle(path: resourcePath + "/ClickIt_ClickItLite.bundle") {
106-
print("✅ Found resource bundle in app Resources: \(resourcePath)/ClickIt_ClickItLite.bundle")
107-
return bundlePath
108-
}
109-
110-
// Strategy 3: Try Module.bundle (for Xcode builds)
111-
if let bundleURL = Bundle.main.url(forResource: "ClickItLite_ClickItLite", withExtension: "bundle"),
112-
let resourceBundle = Bundle(url: bundleURL) {
113-
print("✅ Found module resource bundle at: \(bundleURL.path)")
114-
return resourceBundle
115-
}
116-
117-
// Fallback to Bundle.main (resources might be directly in app bundle)
118-
print("⚠️ Using Bundle.main as fallback")
119-
return Bundle.main
98+
// For Swift 5.3+, Swift Package Manager automatically synthesizes a `Bundle.module` static property
99+
// for any target that includes resources. This is the modern and recommended way to access them.
100+
return Bundle.module
120101
}
121102

122103
private func setupCustomCursor() {
123-
// Debug: Print bundle path
124-
print("🔍 Bundle path: \(Bundle.main.bundlePath)")
125-
print("🔍 Resource path: \(Bundle.main.resourcePath ?? "nil")")
104+
// Debug: Log bundle path
105+
logger.debug("🔍 Bundle path: \(Bundle.main.bundlePath)")
106+
logger.debug("🔍 Resource path: \(Bundle.main.resourcePath ?? "nil")")
126107

127108
// Find the correct resource bundle for Swift Package Manager
128109
let bundle = findResourceBundle()
129-
print("🔍 Using bundle: \(bundle.bundlePath)")
110+
logger.debug("🔍 Using bundle: \(bundle.bundlePath)")
130111

131112
// Try to load the target image from resources
132113
guard let imageURL = bundle.url(forResource: "target-64", withExtension: "png") else {
133-
print("❌ Failed to find target-64.png in bundle")
134-
print("🔍 Searched in: \(bundle.bundleURL)")
114+
logger.error("❌ Failed to find target-64.png in bundle")
115+
logger.error("🔍 Searched in: \(bundle.bundleURL)")
135116

136117
// Try to list all resources
137118
if let resourcePath = bundle.resourcePath {
138119
do {
139120
let items = try FileManager.default.contentsOfDirectory(atPath: resourcePath)
140-
print("📁 Available resources in bundle: \(items)")
121+
logger.debug("📁 Available resources in bundle: \(items)")
141122
} catch {
142-
print("❌ Could not list resources: \(error)")
123+
logger.error("❌ Could not list resources: \(error)")
143124
}
144125
}
145126
return
146127
}
147128

148-
print("✅ Found image at: \(imageURL.path)")
129+
logger.debug("✅ Found image at: \(imageURL.path)")
149130

150131
guard let image = NSImage(contentsOf: imageURL) else {
151-
print("❌ Failed to load NSImage from: \(imageURL.path)")
132+
logger.error("❌ Failed to load NSImage from: \(imageURL.path)")
152133
return
153134
}
154135

155-
print("✅ NSImage loaded, original size: \(image.size)")
136+
logger.debug("✅ NSImage loaded, original size: \(image.size)")
156137

157-
// Set the cursor size (64x64 pixels)
158-
image.size = NSSize(width: 64, height: 64)
138+
// Set the cursor size
139+
let cursorDimension: CGFloat = 64
140+
image.size = NSSize(width: cursorDimension, height: cursorDimension)
159141

160-
// Create cursor with hotspot at center (32, 32)
161-
let hotspot = NSPoint(x: 32, y: 32)
142+
// Create cursor with hotspot at center
143+
let hotspot = NSPoint(x: cursorDimension / 2, y: cursorDimension / 2)
162144
customCursor = NSCursor(image: image, hotSpot: hotspot)
163145

164-
print("✅ Custom cursor created successfully")
146+
logger.info("✅ Custom cursor created successfully")
165147
}
166148
}

0 commit comments

Comments
 (0)