Files
downterm/CommandNotch/CommandNotch/Components/MouseAwareTerminalView.swift

50 lines
1.4 KiB
Swift

import AppKit
import SwiftTerm
struct TerminalScrollWheelRouter {
static func shouldSendMouseWheel(
allowMouseReporting: Bool,
mouseMode: Terminal.MouseMode,
deltaY: Double
) -> Bool {
allowMouseReporting && mouseMode != .off && deltaY != 0
}
static func velocity(for deltaY: Double) -> Int {
let magnitude = Int(abs(deltaY))
if magnitude > 9 {
return 20
}
if magnitude > 5 {
return 10
}
if magnitude > 1 {
return 3
}
return 1
}
static func gridPosition(
point: CGPoint,
bounds: CGRect,
cols: Int,
rows: Int
) -> (x: Int, y: Int, pixelX: Int, pixelY: Int) {
let safeCols = max(cols, 1)
let safeRows = max(rows, 1)
let width = max(bounds.width, 1)
let height = max(bounds.height, 1)
let clampedX = min(max(point.x, 0), width)
let clampedY = min(max(point.y, 0), height)
let cellWidth = width / CGFloat(safeCols)
let cellHeight = height / CGFloat(safeRows)
let column = min(max(Int(clampedX / cellWidth), 0), safeCols - 1)
let row = min(max(Int((height - clampedY) / cellHeight), 0), safeRows - 1)
let pixelX = min(max(Int(clampedX), 0), Int(width))
let pixelY = min(max(Int(height - clampedY), 0), Int(height))
return (column, row, pixelX, pixelY)
}
}