Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions airsync-mac/Core/Util/Remote/MacRemoteManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class MacRemoteManager: ObservableObject {

@Published var lastVolumeLevel: Int = 0
private var volumeCheckTimer: Timer?
private var cachedScreenHeight: CGFloat = 1080
private var isMonitoring = false

// Key codes
Expand Down Expand Up @@ -48,7 +47,6 @@ class MacRemoteManager: ObservableObject {
private init() {
// Initialize last known volume
self.lastVolumeLevel = getVolume()
self.cachedScreenHeight = NSScreen.main?.frame.height ?? 1080
}

deinit {
Expand Down Expand Up @@ -89,11 +87,9 @@ class MacRemoteManager: ObservableObject {
// MARK: - Input Simulation

func simulateMouseRelativeMove(dx: CGFloat, dy: CGFloat) {
let mouseLoc = NSEvent.mouseLocation

// Convert Cocoa coordinates (bottom-left) to CoreGraphics (top-left)
// Use cached screen height to avoid querying screen frame 100 times/sec
let currentPos = CGPoint(x: mouseLoc.x, y: cachedScreenHeight - mouseLoc.y)
// CGEvent's location is already in global CoreGraphics coordinates (top-left of the primary display),
// so it stays correct with multiple displays, unlike flipping NSEvent.mouseLocation with one screen's height.
let currentPos = CGEvent(source: nil)?.location ?? .zero
let newPos = CGPoint(x: currentPos.x + dx, y: currentPos.y + dy)

if let event = CGEvent(mouseEventSource: nil, mouseType: .mouseMoved, mouseCursorPosition: newPos, mouseButton: .left) {
Expand All @@ -102,9 +98,7 @@ class MacRemoteManager: ObservableObject {
}

func simulateMouseClick(button: CGMouseButton, isDown: Bool) {
let mouseLoc = NSEvent.mouseLocation
let screenFrame = NSScreen.main?.frame ?? .zero
let currentPos = CGPoint(x: mouseLoc.x, y: screenFrame.height - mouseLoc.y)
let currentPos = CGEvent(source: nil)?.location ?? .zero

let mouseType: CGEventType
switch button {
Expand Down