Skip to content

Commit

Permalink
Default to use system wide mouse down on certain apps
Browse files Browse the repository at this point in the history
  • Loading branch information
rxhanson committed Feb 21, 2024
1 parent 164bdf5 commit 78cdb8f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
7 changes: 5 additions & 2 deletions Rectangle/AccessibilityElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,11 @@ extension AccessibilityElement {

static func getWindowElementUnderCursor() -> AccessibilityElement? {
let position = NSEvent.mouseLocation.screenFlipped

let systemWideFirst = Defaults.systemWideMouseDown.userEnabled

var systemWideFirst = Defaults.systemWideMouseDown.userEnabled
if Defaults.systemWideMouseDown.notSet, let frontAppId = ApplicationToggle.frontAppId {
systemWideFirst = Defaults.systemWideMouseDownApps.typedValue?.contains(frontAppId) == true
}

if systemWideFirst,
let element = AccessibilityElement(position),
Expand Down
10 changes: 5 additions & 5 deletions Rectangle/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
self.windowManager = WindowManager()
self.shortcutManager = ShortcutManager(windowManager: windowManager)
self.applicationToggle = ApplicationToggle(shortcutManager: shortcutManager)
self.snappingManager = SnappingManager(applicationToggle: applicationToggle)
self.snappingManager = SnappingManager()
self.titleBarManager = TitleBarManager()
self.initializeTodo()
checkForProblematicApps()
Expand Down Expand Up @@ -281,10 +281,10 @@ extension AppDelegate: NSMenuDelegate {
return
}

if let frontAppName = applicationToggle.frontAppName {
if let frontAppName = ApplicationToggle.frontAppName {
let ignoreString = NSLocalizedString("D99-0O-MB6.title", tableName: "Main", value: "Ignore frontmost.app", comment: "")
ignoreMenuItem.title = ignoreString.replacingOccurrences(of: "frontmost.app", with: frontAppName)
ignoreMenuItem.state = applicationToggle.shortcutsDisabled ? .on : .off
ignoreMenuItem.state = ApplicationToggle.shortcutsDisabled ? .on : .off
ignoreMenuItem.isHidden = false
} else {
ignoreMenuItem.isHidden = true
Expand Down Expand Up @@ -314,7 +314,7 @@ extension AppDelegate: NSMenuDelegate {
menuItem.image?.isTemplate = true
}

if !applicationToggle.shortcutsDisabled {
if !ApplicationToggle.shortcutsDisabled {
if let fullKeyEquivalent = shortcutManager.getKeyEquivalent(action: windowAction),
let keyEquivalent = fullKeyEquivalent.0?.lowercased() {
menuItem.keyEquivalent = keyEquivalent
Expand Down Expand Up @@ -495,7 +495,7 @@ extension AppDelegate {
return
}

if let frontAppName = applicationToggle.frontAppName {
if let frontAppName = ApplicationToggle.frontAppName {
let appString = NSLocalizedString("Use frontmost.app as Todo App", tableName: "Main", value: "", comment: "")
todoAppMenuItem.title = appString.replacingOccurrences(
of: "frontmost.app", with: frontAppName)
Expand Down
26 changes: 13 additions & 13 deletions Rectangle/ApplicationToggle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import Cocoa
class ApplicationToggle: NSObject {

private var disabledApps = Set<String>()
public private(set) var frontAppId: String? = "com.knollsoft.Rectangle"
public private(set) var frontAppName: String? = "Rectangle"
public private(set) var shortcutsDisabled: Bool = false
public private(set) static var frontAppId: String? = "com.knollsoft.Rectangle"
public private(set) static var frontAppName: String? = "Rectangle"
public private(set) static var shortcutsDisabled: Bool = false

private let shortcutManager: ShortcutManager

Expand Down Expand Up @@ -54,8 +54,8 @@ class ApplicationToggle: NSObject {
}

private func disableShortcuts() {
if !self.shortcutsDisabled {
self.shortcutsDisabled = true
if !Self.shortcutsDisabled {
Self.shortcutsDisabled = true
self.shortcutManager.unbindShortcuts()
if !Defaults.ignoreDragSnapToo.userDisabled {
Notification.Name.windowSnapping.post(object: false)
Expand All @@ -64,8 +64,8 @@ class ApplicationToggle: NSObject {
}

private func enableShortcuts() {
if self.shortcutsDisabled {
self.shortcutsDisabled = false
if Self.shortcutsDisabled {
Self.shortcutsDisabled = false
self.shortcutManager.bindShortcuts()
if !Defaults.ignoreDragSnapToo.userDisabled {
Notification.Name.windowSnapping.post(object: true)
Expand All @@ -74,15 +74,15 @@ class ApplicationToggle: NSObject {
}

public func disableFrontApp() {
if let frontAppId = self.frontAppId {
if let frontAppId = Self.frontAppId {
disabledApps.insert(frontAppId)
saveDisabledApps()
disableShortcuts()
}
}

public func enableFrontApp() {
if let frontAppId = self.frontAppId {
if let frontAppId = Self.frontAppId {
disabledApps.remove(frontAppId)
saveDisabledApps()
enableShortcuts()
Expand All @@ -99,8 +99,8 @@ class ApplicationToggle: NSObject {

@objc func receiveFrontAppChangeNote(_ notification: Notification) {
if let application = notification.userInfo?["NSWorkspaceApplicationKey"] as? NSRunningApplication {
self.frontAppId = application.bundleIdentifier
self.frontAppName = application.localizedName
Self.frontAppId = application.bundleIdentifier
Self.frontAppName = application.localizedName
if let frontAppId = application.bundleIdentifier {
if isDisabled(bundleId: frontAppId) {
disableShortcuts()
Expand All @@ -124,10 +124,10 @@ class ApplicationToggle: NSObject {
// todo mode
extension ApplicationToggle {
public func setTodoApp() {
Defaults.todoApplication.value = self.frontAppId
Defaults.todoApplication.value = Self.frontAppId
}

public func todoAppIsActive() -> Bool {
return Defaults.todoApplication.value == self.frontAppId
return Defaults.todoApplication.value == Self.frontAppId
}
}
12 changes: 11 additions & 1 deletion Rectangle/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Defaults {
static let doubleClickTitleBarIgnoredApps = JSONDefault<[String]>(key: "doubleClickTitleBarIgnoredApps")
static let ignoreDragSnapToo = OptionalBoolDefault(key: "ignoreDragSnapToo")
static let systemWideMouseDown = OptionalBoolDefault(key: "systemWideMouseDown")
static let systemWideMouseDownApps = JSONDefault<Set<String>>(key:"systemWideMouseDownApps", defaultValue: Set<String>(["org.languagetool.desktop", "com.microsoft.teams2"]))

static var array: [Default] = [
launchOnLogin,
Expand Down Expand Up @@ -158,7 +159,8 @@ class Defaults {
doubleClickTitleBarRestore,
doubleClickTitleBarIgnoredApps,
ignoreDragSnapToo,
systemWideMouseDown
systemWideMouseDown,
systemWideMouseDownApps
]
}

Expand Down Expand Up @@ -231,6 +233,7 @@ class OptionalBoolDefault: Default {

var userDisabled: Bool { enabled == false }
var userEnabled: Bool { enabled == true }
var notSet: Bool { enabled == nil }

init(key: String) {
self.key = key
Expand Down Expand Up @@ -372,6 +375,13 @@ class JSONDefault<T: Codable>: StringDefault {
typeInitialized = true
}

init(key: String, defaultValue: T) {
if typedValue == nil {
typedValue = defaultValue
}
super.init(key: key)
}

override func load(from codable: CodableDefault) {
if value != codable.string {
value = codable.string
Expand Down
9 changes: 3 additions & 6 deletions Rectangle/Snapping/SnappingManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,13 @@ class SnappingManager {
var box: FootprintWindow?

let screenDetection = ScreenDetection()
let applicationToggle: ApplicationToggle

private let marginTop = Defaults.snapEdgeMarginTop.cgFloat
private let marginBottom = Defaults.snapEdgeMarginBottom.cgFloat
private let marginLeft = Defaults.snapEdgeMarginLeft.cgFloat
private let marginRight = Defaults.snapEdgeMarginRight.cgFloat

init(applicationToggle: ApplicationToggle) {
self.applicationToggle = applicationToggle

init() {
if Defaults.windowSnapping.enabled != false {
enableSnapping()
}
Expand All @@ -65,14 +62,14 @@ class SnappingManager {
}

func frontAppChanged(notification: Notification) {
if applicationToggle.shortcutsDisabled {
if ApplicationToggle.shortcutsDisabled {
DispatchQueue.main.async {
if !Defaults.ignoreDragSnapToo.userDisabled {
self.allowListening = false
self.toggleListening()
} else {
for id in self.fullIgnoreIds {
if self.applicationToggle.frontAppId?.starts(with: id) == true {
if ApplicationToggle.frontAppId?.starts(with: id) == true {
self.allowListening = false
self.toggleListening()
break
Expand Down

0 comments on commit 78cdb8f

Please sign in to comment.