scoop:scoop.window:0.1.0
src/geometry.scoop
Selected production source · src/geometry.scoop
1
package scoop.window3
/** A position in a window's logical, top-left coordinate system. */4
public struct LogicalPosition(val x: Double, val y: Double)6
/** A nonnegative logical content size, validated when used by a window operation. */7
public struct LogicalSize(val width: Double, val height: Double) {8
/** Computes allocation pixels by rounding each dimension upward. */9
public fun toPhysical(scaleFactor: Double): PhysicalSize {10
checkSize(this)11
checkScale(scaleFactor)12
return PhysicalSize(pixelExtent(width, scaleFactor), pixelExtent(height, scaleFactor))13
}14
}16
/** A logical rectangle in content coordinates. */17
public struct LogicalRect(val origin: LogicalPosition, val size: LogicalSize)19
/** A physical pixel position; subpixel coordinates are preserved. */20
public struct PhysicalPosition(val x: Double, val y: Double)22
/** A drawable allocation size in pixels. */23
public struct PhysicalSize(val width: Int, val height: Int) {24
/** Whether both dimensions allow a render target. */25
public val isDrawable: Boolean get() = width > 0 && height > 026
/** Converts pixels using the scale belonging to the same window snapshot. */27
public fun toLogical(scaleFactor: Double): LogicalSize {28
requireWindow(width >= 0 && height >= 0, "negative physical size")29
checkScale(scaleFactor)30
return LogicalSize(width.toDouble() / scaleFactor, height.toDouble() / scaleFactor)31
}32
}34
/** Insets from the view edges in logical points. */35
public struct WindowInsets(36
val top: Double = 0.0,37
val left: Double = 0.0,38
val bottom: Double = 0.0,39
val right: Double = 0.040
)42
/** One complete observed content geometry and backing-scale snapshot. */43
public struct WindowMetrics(44
val logicalSize: LogicalSize,45
val physicalSize: PhysicalSize,46
val scaleFactor: Double,47
val safeAreaInsets: WindowInsets = WindowInsets()48
) {49
/** Converts a point within this window using this snapshot's scale. */50
public fun toPhysical(position: LogicalPosition): PhysicalPosition {51
checkPosition(position)52
checkScale(scaleFactor)53
val result = PhysicalPosition(position.x * scaleFactor, position.y * scaleFactor)54
requireWindow(result.x.isFinite() && result.y.isFinite(), "physical position overflow")55
return result56
}57
/** Converts a pixel point within this window back to logical coordinates. */58
public fun toLogical(position: PhysicalPosition): LogicalPosition {59
requireWindow(60
position.x.isFinite() && position.y.isFinite(),61
"non-finite physical position"62
)63
checkScale(scaleFactor)64
return LogicalPosition(position.x / scaleFactor, position.y / scaleFactor)65
}66
}68
internal fun requireWindow(condition: Boolean, message: String) {69
if (!condition) throw IllegalArgumentException(Some(message))70
}72
internal fun checkScale(scale: Double) {73
requireWindow(scale.isFinite() && scale > 0.0, "invalid scale factor")74
}76
internal fun checkSize(size: LogicalSize) {77
requireWindow(78
size.width.isFinite() && size.height.isFinite() && size.width >= 0.0 && size.height >= 0.0,79
"invalid logical size"80
)81
}83
internal fun checkPosition(position: LogicalPosition) {84
requireWindow(position.x.isFinite() && position.y.isFinite(), "non-finite logical position")85
}87
internal fun checkRect(rect: LogicalRect) {88
checkPosition(rect.origin);89
checkSize(rect.size)90
}92
private fun pixelExtent(value: Double, scale: Double): Int {93
val pixels = value * scale94
requireWindow(pixels.isFinite() && pixels <= 2147483647.0, "physical size overflow")95
val floor = pixels.toInt()96
return if (floor.toDouble() < pixels) floor + 1 else floor97
}