Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix focusing a screen when the screen has no windows. #749

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
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
16 changes: 14 additions & 2 deletions Amethyst/Categories/NSScreen+Amethyst.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,21 @@ extension NSScreen {
return String(managedDisplay.takeRetainedValue())
}

/// Returns the screen's frame translated to Quartz' coordinate system: The screen's frame (and bounds and so on)
/// are in cocoa coordinates which are used for views. This coordinate space differs from from Quartz coordinate
/// space which is used for events.
/// See https://stackoverflow.com/a/19887161 for details.
func cgRect() -> CGRect {
guard let primaryScreen = NSScreen.screens.first else {
return CGRect(origin: frame.origin, size: frame.size)
}
return CGRect(x: frame.origin.x, y: primaryScreen.frame.maxY - self.frame.maxY,
width: frame.width, height: frame.height)
}

func focusScreen() {
let screenFrame = self.frame
let mouseCursorPoint = NSPoint(x: screenFrame.midX, y: screenFrame.midY)
let frame = cgRect()
let mouseCursorPoint = CGPoint(x: frame.midX, y: frame.midY)
let mouseMoveEvent = CGEvent(mouseEventSource: nil, mouseType: .mouseMoved, mouseCursorPosition: mouseCursorPoint, mouseButton: .left)
mouseMoveEvent?.flags = CGEventFlags(rawValue: 0)
mouseMoveEvent?.post(tap: .cghidEventTap)
Expand Down