Skip to content

Commit 1d7f37d

Browse files
committed
refactor code and fix clicks interval
1 parent 6d72b96 commit 1d7f37d

File tree

6 files changed

+39
-23
lines changed

6 files changed

+39
-23
lines changed

AutoClicker.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
B173F39C260D5C6800717643 /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B173F39B260D5C6800717643 /* HomeView.swift */; };
2424
B173F39D260D5C6800717643 /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B173F39B260D5C6800717643 /* HomeView.swift */; };
2525
B1FD016B261A54C000690375 /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1FD016A261A54C000690375 /* AppState.swift */; };
26+
B1FD0184261DDB7A00690375 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1FD0183261DDB7900690375 /* Utilities.swift */; };
2627
/* End PBXBuildFile section */
2728

2829
/* Begin PBXContainerItemProxy section */
@@ -63,6 +64,7 @@
6364
B173F37B260CAEE100717643 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
6465
B173F39B260D5C6800717643 /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = "<group>"; };
6566
B1FD016A261A54C000690375 /* AppState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppState.swift; sourceTree = "<group>"; };
67+
B1FD0183261DDB7900690375 /* Utilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utilities.swift; sourceTree = "<group>"; };
6668
/* End PBXFileReference section */
6769

6870
/* Begin PBXFrameworksBuildPhase section */
@@ -121,6 +123,7 @@
121123
B1FD016A261A54C000690375 /* AppState.swift */,
122124
B173F39B260D5C6800717643 /* HomeView.swift */,
123125
B1080205261A3521003E098E /* Constants.swift */,
126+
B1FD0183261DDB7900690375 /* Utilities.swift */,
124127
);
125128
path = Shared;
126129
sourceTree = "<group>";
@@ -351,6 +354,7 @@
351354
B1FD016B261A54C000690375 /* AppState.swift in Sources */,
352355
B173F37F260CAEE100717643 /* ContentView.swift in Sources */,
353356
B173F37D260CAEE100717643 /* AutoClickerApp.swift in Sources */,
357+
B1FD0184261DDB7A00690375 /* Utilities.swift in Sources */,
354358
B1080212261A3957003E098E /* GeneralSettingsView.swift in Sources */,
355359
B1080206261A3521003E098E /* Constants.swift in Sources */,
356360
);
@@ -561,6 +565,7 @@
561565
"@executable_path/../Frameworks",
562566
);
563567
MACOSX_DEPLOYMENT_TARGET = 11.0;
568+
MARKETING_VERSION = 1.0.1;
564569
PRODUCT_BUNDLE_IDENTIFIER = focux.AutoClicker;
565570
PRODUCT_NAME = AutoClicker;
566571
SDKROOT = macosx;
@@ -585,6 +590,7 @@
585590
"@executable_path/../Frameworks",
586591
);
587592
MACOSX_DEPLOYMENT_TARGET = 11.0;
593+
MARKETING_VERSION = 1.0.1;
588594
PRODUCT_BUNDLE_IDENTIFIER = focux.AutoClicker;
589595
PRODUCT_NAME = AutoClicker;
590596
SDKROOT = macosx;

AutoClicker.xcodeproj/xcuserdata/leonardodominguez.xcuserdatad/xcschemes/xcschememanagement.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
<key>AutoClicker (iOS).xcscheme_^#shared#^_</key>
88
<dict>
99
<key>orderHint</key>
10-
<integer>1</integer>
10+
<integer>0</integer>
1111
</dict>
1212
<key>AutoClicker (macOS).xcscheme_^#shared#^_</key>
1313
<dict>
1414
<key>orderHint</key>
15-
<integer>0</integer>
15+
<integer>1</integer>
1616
</dict>
1717
</dict>
1818
</dict>

Shared/GeneralSettingsView.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import KeyboardShortcuts
1010

1111
struct GeneralSettingsView: View {
1212
@AppStorage("showPreview") private var showPreview = true
13-
@AppStorage("secondsInterval") private var secondsInterval = 1.0
1413
@AppStorage("playSound") private var playSound = true
1514
@AppStorage("clicksPerSecond") private var clicksPerSecond = 1.0
1615
@EnvironmentObject var appState: AppState
@@ -25,10 +24,6 @@ struct GeneralSettingsView: View {
2524
})
2625
}
2726
Toggle("Play sound when clicking", isOn: $playSound)
28-
Slider(value: $secondsInterval, in: 1...30, step: 1) {
29-
Text("Seconds Interval (\(secondsInterval, specifier: "%.0f")s)")
30-
.frame(width: 145, alignment: .leading)
31-
}
3227
Slider(value: $clicksPerSecond, in: 1...20, step: 1) {
3328
Text("Clicks per second (\(clicksPerSecond, specifier: "%.0f"))")
3429
.frame(width: 145, alignment: .leading)

Shared/HomeView.swift

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,27 @@ struct BlueButtonStyle: ButtonStyle {
2626

2727
struct HomeView: View {
2828
@State private var enabled = false
29-
@AppStorage("secondsInterval") private var secondsInterval = 1.0
3029
@AppStorage("playSound") private var playSound = true
3130
@AppStorage("clicksPerSecond") private var clicksPerSecond = 1.0
3231
@State private var timer: Publishers.Autoconnect<Timer.TimerPublisher>? = nil
3332
@EnvironmentObject var appState: AppState
3433

3534
private var shortcut = KeyboardShortcuts.getShortcut(for: .toggleAutoClick)?.description
3635

36+
var secondsBetweenClicks: Double { 1/clicksPerSecond }
37+
3738
func handleClick() {
3839
enabled.toggle()
3940

4041
if enabled {
41-
timer = Timer.publish(every: secondsInterval, on: .main, in: .common).autoconnect()
42+
timer = Timer.publish(every: secondsBetweenClicks, on: .main, in: .common).autoconnect()
4243

4344
timer = timer!.upstream.autoconnect()
4445
} else {
4546
timer!.upstream.connect().cancel()
4647
}
4748
}
4849

49-
func click() {
50-
let dummy = CGEvent.init(source: nil)
51-
let loc = dummy!.location
52-
53-
let mouseDown = CGEvent(mouseEventSource: nil, mouseType: CGEventType.leftMouseDown, mouseCursorPosition: loc, mouseButton: CGMouseButton.left)
54-
let mouseUp = CGEvent(mouseEventSource: nil, mouseType: CGEventType.leftMouseUp, mouseCursorPosition: loc, mouseButton: CGMouseButton.left)
55-
mouseDown?.post(tap: CGEventTapLocation.cghidEventTap)
56-
mouseUp?.post(tap: CGEventTapLocation.cghidEventTap)
57-
}
58-
5950
var content: some View {
6051
VStack {
6152
Button(action: handleClick) {
@@ -95,9 +86,10 @@ struct HomeView: View {
9586
for i in 1...Int(self.clicksPerSecond) {
9687
print("clickkk #\(i) \(self.clicksPerSecond)")
9788
if self.playSound {
98-
NSSound.beep()
89+
simulateClickWithBeep()
90+
} else {
91+
simulateClick()
9992
}
100-
click()
10193
}
10294

10395
}
@@ -117,6 +109,6 @@ struct HomeView: View {
117109

118110
struct HomeView_Previews: PreviewProvider {
119111
static var previews: some View {
120-
HomeView()
112+
HomeView().environmentObject(AppState())
121113
}
122114
}

Shared/Utilities.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// Utilities.swift
3+
// AutoClicker (macOS)
4+
//
5+
// Created by Leonardo Dominguez on 7/4/21.
6+
//
7+
import AVFoundation
8+
import SwiftUI
9+
10+
func simulateClick() {
11+
let dummy = CGEvent.init(source: nil)
12+
let loc = dummy!.location
13+
14+
let mouseDown = CGEvent(mouseEventSource: nil, mouseType: CGEventType.leftMouseDown, mouseCursorPosition: loc, mouseButton: CGMouseButton.left)
15+
let mouseUp = CGEvent(mouseEventSource: nil, mouseType: CGEventType.leftMouseUp, mouseCursorPosition: loc, mouseButton: CGMouseButton.left)
16+
mouseDown?.post(tap: CGEventTapLocation.cghidEventTap)
17+
mouseUp?.post(tap: CGEventTapLocation.cghidEventTap)
18+
}
19+
20+
func simulateClickWithBeep() {
21+
simulateClick()
22+
NSSound.beep()
23+
}

macOS/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<key>CFBundlePackageType</key>
1818
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>1.0</string>
20+
<string>$(MARKETING_VERSION)</string>
2121
<key>CFBundleVersion</key>
2222
<string>1</string>
2323
<key>LSMinimumSystemVersion</key>

0 commit comments

Comments
 (0)