Skip to content

Commit

Permalink
Merge pull request #19 from ther0n/fixes
Browse files Browse the repository at this point in the history
A bunch of minor fixes and other polish
  • Loading branch information
ther0n authored Feb 4, 2021
2 parents 377670b + 6e23c05 commit 91cde77
Show file tree
Hide file tree
Showing 11 changed files with 601 additions and 61 deletions.
37 changes: 31 additions & 6 deletions UnnaturalScrollWheels.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@
objects = {

/* Begin PBXBuildFile section */
914D7D7825C3108C00503565 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 914D7D7A25C3108C00503565 /* Main.storyboard */; };
914D7D8225C32FD800503565 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 914D7D8425C32FD800503565 /* Localizable.strings */; };
91727DB224CCBDDA00432163 /* PreferencesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91727DB124CCBDDA00432163 /* PreferencesViewController.swift */; };
918546AB25C0758700B2AD21 /* MenuBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 918546AA25C0758700B2AD21 /* MenuBarItem.swift */; };
91C7ECE424CC86B6007E2D4C /* Options.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91C7ECE324CC86B6007E2D4C /* Options.swift */; };
91C7ECE624CC8789007E2D4C /* ScrollInterceptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91C7ECE524CC8789007E2D4C /* ScrollInterceptor.swift */; };
91C8ABFB24CCA471007E85A9 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 91C8ABFA24CCA471007E85A9 /* Main.storyboard */; };
91D8069E24CB37B80092C286 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91D8069D24CB37B80092C286 /* AppDelegate.swift */; };
91D806A024CB37BD0092C286 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 91D8069F24CB37BD0092C286 /* Assets.xcassets */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
9138880425B0CB2700A3A633 /* UnnaturalScrollWheels-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UnnaturalScrollWheels-Bridging-Header.h"; sourceTree = "<group>"; };
914D7D7925C3108C00503565 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = en; path = en.lproj/Main.storyboard; sourceTree = "<group>"; };
914D7D8325C32FD800503565 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
914D7D8925C342A900503565 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
91727DB124CCBDDA00432163 /* PreferencesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesViewController.swift; sourceTree = "<group>"; };
918546AA25C0758700B2AD21 /* MenuBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuBarItem.swift; sourceTree = "<group>"; };
91C7ECE324CC86B6007E2D4C /* Options.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Options.swift; sourceTree = "<group>"; };
91C7ECE524CC8789007E2D4C /* ScrollInterceptor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrollInterceptor.swift; sourceTree = "<group>"; };
91C8ABFA24CCA471007E85A9 /* Main.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = "<group>"; };
91D8069A24CB37B80092C286 /* UnnaturalScrollWheels.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = UnnaturalScrollWheels.app; sourceTree = BUILT_PRODUCTS_DIR; };
91D8069D24CB37B80092C286 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
91D8069F24CB37BD0092C286 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -60,12 +63,13 @@
91D8069C24CB37B80092C286 /* UnnaturalScrollWheels */ = {
isa = PBXGroup;
children = (
914D7D8425C32FD800503565 /* Localizable.strings */,
91D8069D24CB37B80092C286 /* AppDelegate.swift */,
91C7ECE324CC86B6007E2D4C /* Options.swift */,
91727DB124CCBDDA00432163 /* PreferencesViewController.swift */,
91C7ECE524CC8789007E2D4C /* ScrollInterceptor.swift */,
9138880425B0CB2700A3A633 /* UnnaturalScrollWheels-Bridging-Header.h */,
91C8ABFA24CCA471007E85A9 /* Main.storyboard */,
914D7D7A25C3108C00503565 /* Main.storyboard */,
91D8069F24CB37BD0092C286 /* Assets.xcassets */,
91D806A424CB37BD0092C286 /* Info.plist */,
91D806A524CB37BD0092C286 /* UnnaturalScrollWheels.entitlements */,
Expand Down Expand Up @@ -134,7 +138,8 @@
buildActionMask = 2147483647;
files = (
91D806A024CB37BD0092C286 /* Assets.xcassets in Resources */,
91C8ABFB24CCA471007E85A9 /* Main.storyboard in Resources */,
914D7D8225C32FD800503565 /* Localizable.strings in Resources */,
914D7D7825C3108C00503565 /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -155,6 +160,26 @@
};
/* End PBXSourcesBuildPhase section */

/* Begin PBXVariantGroup section */
914D7D7A25C3108C00503565 /* Main.storyboard */ = {
isa = PBXVariantGroup;
children = (
914D7D7925C3108C00503565 /* en */,
914D7D8925C342A900503565 /* Base */,
);
name = Main.storyboard;
sourceTree = "<group>";
};
914D7D8425C32FD800503565 /* Localizable.strings */ = {
isa = PBXVariantGroup;
children = (
914D7D8325C32FD800503565 /* en */,
);
name = Localizable.strings;
sourceTree = "<group>";
};
/* End PBXVariantGroup section */

/* Begin XCBuildConfiguration section */
91D806A624CB37BD0092C286 /* Debug */ = {
isa = XCBuildConfiguration;
Expand Down Expand Up @@ -288,7 +313,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 1.2.0;
MARKETING_VERSION = 1.2.1;
PRODUCT_BUNDLE_IDENTIFIER = com.theron.UnnaturalScrollWheels;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "UnnaturalScrollWheels/UnnaturalScrollWheels-Bridging-Header.h";
Expand All @@ -314,7 +339,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 1.2.0;
MARKETING_VERSION = 1.2.1;
PRODUCT_BUNDLE_IDENTIFIER = com.theron.UnnaturalScrollWheels;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "UnnaturalScrollWheels/UnnaturalScrollWheels-Bridging-Header.h";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
filePath = "UnnaturalScrollWheels/PreferencesViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "20"
endingLineNumber = "20"
startingLineNumber = "21"
endingLineNumber = "21"
landmarkName = "PreferencesViewController"
landmarkType = "3">
<Locations>
Expand Down
79 changes: 66 additions & 13 deletions UnnaturalScrollWheels/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,76 @@

import Cocoa
import Foundation
import IOKit

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {

private let statusItem = MenuBarItem.shared
var prefsWindow: NSWindow?
@IBOutlet weak var menu: NSMenu?
//@IBOutlet weak var showMenuBarItem: NSMenuItem?
@IBOutlet weak var openAtLoginItem: NSMenuItem?
@IBOutlet weak var preferencesMenuItem: NSMenuItem?
@IBOutlet weak var quitMenuItem: NSMenuItem?

func applicationDidFinishLaunching(_ aNotification: Notification) {
Options.shared.loadOptions()
if Options.shared.firstLaunch {
UserDefaults.standard.set(false, forKey: "FirstLaunch")
}
self.statusItem.menu = self.menu
refresh()
if AXIsProcessTrusted() {
ScrollInterceptor.shared.interceptScroll()
} else {
if Options.shared.firstLaunch{
let alert = NSAlert()
alert.messageText = NSLocalizedString("PermissionsTitle", comment: "")
alert.informativeText = NSLocalizedString("PermissionsMessage", comment: "")
} else {
accessibilityAlert()
}
pollAccessibility()
ScrollInterceptor.shared.interceptScroll()
}
}

func pollAccessibility() {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
if AXIsProcessTrusted() {
ScrollInterceptor.shared.interceptScroll()
} else {
self.pollAccessibility()
}
}
}

func accessibilityAlert() {
let alert = NSAlert()
alert.messageText = NSLocalizedString("PermissionsTitle", comment: "")
alert.informativeText = NSLocalizedString("PermissionsMessage", comment: "")
alert.addButton(withTitle: NSLocalizedString("OpenPreferences", comment: ""))
alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
if alert.runModal() == NSApplication.ModalResponse.alertFirstButtonReturn {
NSWorkspace.shared.open(URL(string:"x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility")!)
}
}

func refresh() {
Options.shared.loadOptions()
statusItem.refreshVisibility()
ScrollInterceptor.shared.interceptScroll()
disableMouseAccel()
}

@IBAction func openAtLoginClicked(_ sender: Any) {
let url = URL(string: "https://github.com/ther0n/UnnaturalScrollWheels/blob/master/RunAtLogin.md")!
NSWorkspace.shared.open(url)
}

@IBAction func preferencesClicked(_ sender: Any) {
showPreferences()
if AXIsProcessTrusted() {
showPreferences()
} else{
accessibilityAlert()
}
}

@IBAction func showAbout(_ sender: Any) {
Expand All @@ -51,7 +93,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
let controllerId = NSStoryboard.SceneIdentifier("Preferences")
guard let controller = storyboard.instantiateController(withIdentifier: controllerId) as? NSWindowController else { return }
guard let window = controller.window else { return }

window.delegate = self
window.center()
prefsWindow = window
Expand All @@ -74,12 +116,23 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
}

func disableMouseAccel() {
if Options.shared.disableMouseAccel {
// Based on https://github.com/apsun/NoMouseAccel
let accelNum: CFNumber = CFNumberCreate(kCFAllocatorDefault, CFNumberType.sInt32Type, &Options.shared.accel)
let client: IOHIDEventSystemClient = IOHIDEventSystemClientCreateSimpleClient(kCFAllocatorDefault)
let mouseAccelerationType: CFString = kIOHIDMouseAccelerationType as NSString
IOHIDEventSystemClientSetProperty(client, mouseAccelerationType, accelNum)
// Based on https://github.com/apsun/NoMouseAccel

let client: IOHIDEventSystemClient = IOHIDEventSystemClientCreateSimpleClient(kCFAllocatorDefault)
let mouseAccelerationType: CFString = kIOHIDMouseAccelerationType as NSString

// Get the starting acceleration value
var origAccel: Int32 = 0
let originalAccelRef: CFTypeRef = IOHIDEventSystemClientCopyProperty(client, mouseAccelerationType)!
CFNumberGetValue((originalAccelRef as! CFNumber), CFNumberType.sInt32Type, &origAccel)
// Only save it if it's not -1 (acceleration off)
if origAccel != -1 {
Options.shared.origAccel = origAccel
UserDefaults.standard.set(origAccel, forKey: "OriginalAccel")
}
// Set the mouse acceleration
let accelNum: CFNumber = CFNumberCreate(kCFAllocatorDefault, CFNumberType.sInt32Type, &Options.shared.accel)
IOHIDEventSystemClientSetProperty(client, mouseAccelerationType, accelNum)
}
}

Loading

0 comments on commit 91cde77

Please sign in to comment.