145 lines
6.5 KiB
Swift
145 lines
6.5 KiB
Swift
// Whether the cursor just left the screen, which way, and where it should land
|
|
// on the machine it is going to. See docs/protocol.md §4.3-§4.4.
|
|
//
|
|
// Ported from App/Core/MachineStuff.cs MoveToMyNeighbourIfNeeded (203-283) and
|
|
// the landing-point arms of MoveRight/Left/Up/Down (385-421, 524-530, 593-599,
|
|
// 663-669), plus the monitor corners collected in App/Core/WinAPI.cs (104-109).
|
|
|
|
import Foundation
|
|
|
|
/// The rectangles and options edge detection needs, in Windows screen
|
|
/// coordinates: top-left origin, y down, whole pixels.
|
|
public struct EdgeConfig: Sendable {
|
|
/// The union of every monitor (`MachineStuff.desktopBounds`).
|
|
public var desktop: CGRect
|
|
/// The monitor whose origin is (0, 0) (`MachineStuff.primaryScreenBounds`).
|
|
public var primary: CGRect
|
|
/// Every monitor's four corners (`WinAPI.SensitivePoints`, `WinAPI.cs:104-109`).
|
|
public var corners: [CGPoint]
|
|
/// `BlockMouseAtScreenCorners`: suppress switching near a corner.
|
|
public var blockCorners: Bool
|
|
/// `MoveMouseRelatively`: the controlled machine reports its own deltas, so
|
|
/// landing points are computed against desktop bounds on both sides.
|
|
public var moveRelatively: Bool
|
|
|
|
/// An edge configuration.
|
|
public init(
|
|
desktop: CGRect = Coordinates.emptyRect, primary: CGRect = Coordinates.emptyRect,
|
|
corners: [CGPoint] = [], blockCorners: Bool = false, moveRelatively: Bool = false
|
|
) {
|
|
self.desktop = desktop
|
|
self.primary = primary
|
|
self.corners = corners
|
|
self.blockCorners = blockCorners
|
|
self.moveRelatively = moveRelatively
|
|
}
|
|
}
|
|
|
|
/// Edge detection: pure geometry, no matrix and no machines.
|
|
///
|
|
/// `crossing(...)` answers "did this cursor position leave the screen, and which
|
|
/// way"; `landing(...)` answers "where does the cursor appear on the machine we
|
|
/// are switching to". Which machine that is belongs to `Matrix`; putting the two
|
|
/// together is `Switcher`'s job.
|
|
public struct Edge: Sendable {
|
|
/// How close to an edge counts as leaving it (`SKIP_PIXELS`,
|
|
/// `MachineStuff.cs:33`).
|
|
public static let skipPixels = 1
|
|
|
|
/// How far inside the far edge the cursor reappears (`JUMP_PIXELS`,
|
|
/// `MachineStuff.cs:34`).
|
|
public static let jumpPixels = 2
|
|
|
|
/// How near a monitor corner blocks a switch when `blockCorners` is on
|
|
/// (`MachineStuff.cs:228`).
|
|
public static let cornerRadius = 100
|
|
|
|
/// The quiet period after a switch, 100 ms (`MachineStuff.cs:217`).
|
|
public static let jumpDebounce: TimeInterval = 0.1
|
|
|
|
/// The rectangles and options.
|
|
public var config: EdgeConfig
|
|
|
|
/// The last cursor position worth remembering, persisted by the app as the
|
|
/// `LastX`/`LastY` settings (`MachineStuff.cs:207-215`).
|
|
public private(set) var lastX: Int32 = 0
|
|
/// See `lastX`.
|
|
public private(set) var lastY: Int32 = 0
|
|
|
|
/// Edge detection over these rectangles.
|
|
public init(config: EdgeConfig) {
|
|
self.config = config
|
|
}
|
|
|
|
/// Which way the cursor left the screen, or `nil` for "stay put"
|
|
/// (`MoveToMyNeighbourIfNeeded`, `MachineStuff.cs:203-283`).
|
|
///
|
|
/// - `cursorIsLocal` is `desMachineID == MachineID`: the cursor is on this
|
|
/// machine, so it roams the whole desktop. Otherwise it is being scaled
|
|
/// from the controller's primary monitor and that is the rectangle to test.
|
|
/// - `broadcasting` is ALL mode, in which switching is off.
|
|
/// - `lastJump` is when the last switch happened, in the caller's time base.
|
|
public mutating func crossing(
|
|
at point: CGPoint, cursorIsLocal: Bool, broadcasting: Bool, lastJump: TimeInterval,
|
|
now: TimeInterval
|
|
) -> Direction? {
|
|
let x = Int(point.x)
|
|
let y = Int(point.y)
|
|
if abs(x) > 10 { lastX = Int32(truncatingIfNeeded: x) } // MachineStuff.cs:207-210
|
|
if abs(y) > 10 { lastY = Int32(truncatingIfNeeded: y) } // MachineStuff.cs:212-215
|
|
|
|
guard now - lastJump >= Edge.jumpDebounce, !broadcasting else { return nil }
|
|
if config.blockCorners && isNearCorner(x: x, y: y) { return nil }
|
|
|
|
let r = Coordinates.bounds(cursorIsLocal ? config.desktop : config.primary)
|
|
if x < r.left + Edge.skipPixels { return .left }
|
|
if x >= r.right - Edge.skipPixels { return .right }
|
|
if y < r.top + Edge.skipPixels { return .up }
|
|
if y >= r.bottom - Edge.skipPixels { return .down }
|
|
return nil
|
|
}
|
|
|
|
/// Where the cursor lands on the machine being switched to, as a universal
|
|
/// value (`MachineStuff.cs:385-421` and the identical arms in the other
|
|
/// three `Move*` methods).
|
|
///
|
|
/// The rectangle is chosen by `landingRect`; the cursor arrives
|
|
/// `jumpPixels` inside the **opposite** edge, keeping the other axis where
|
|
/// it was.
|
|
public func landing(
|
|
direction: Direction, from point: CGPoint, targetIsLocal: Bool, cursorIsLocal: Bool
|
|
) -> UniversalPoint {
|
|
let rect = landingRect(targetIsLocal: targetIsLocal, cursorIsLocal: cursorIsLocal)
|
|
let r = Coordinates.bounds(rect)
|
|
let jump = Edge.jumpPixels
|
|
let landed: CGPoint
|
|
switch direction {
|
|
case .right: landed = Coordinates.point(r.left + jump, Int(point.y))
|
|
case .left: landed = Coordinates.point(r.right - jump, Int(point.y))
|
|
case .up: landed = Coordinates.point(Int(point.x), r.bottom - jump)
|
|
case .down: landed = Coordinates.point(Int(point.x), r.top + jump)
|
|
}
|
|
return Coordinates.toUniversal(landed, in: rect)
|
|
}
|
|
|
|
/// The four-case rule of docs/protocol.md §4.4 (`MachineStuff.cs:385-421`).
|
|
///
|
|
/// In relative mode both machines work in desktop bounds. Otherwise the
|
|
/// cursor is bounded by the controller's primary monitor whenever the
|
|
/// controller is not the one holding it, so only the "switching away from
|
|
/// the controller" case gets to use desktop bounds.
|
|
func landingRect(targetIsLocal: Bool, cursorIsLocal: Bool) -> CGRect {
|
|
if config.moveRelatively { return config.desktop } // MachineStuff.cs:417-420
|
|
if targetIsLocal { return config.primary } // MachineStuff.cs:387-392
|
|
return cursorIsLocal ? config.desktop : config.primary // MachineStuff.cs:396-411
|
|
}
|
|
|
|
/// `BlockMouseAtCorners`: within 100 px of any monitor corner, on both axes
|
|
/// (`MachineStuff.cs:222-234`).
|
|
private func isNearCorner(x: Int, y: Int) -> Bool {
|
|
config.corners.contains { corner in
|
|
abs(Int(corner.x) - x) < Edge.cornerRadius && abs(Int(corner.y) - y) < Edge.cornerRadius
|
|
}
|
|
}
|
|
}
|