scoop:scoop.window:0.1.0
src/monitor.scoop
Selected production source · src/monitor.scoop
1
package scoop.window3
/** An owned display snapshot; desktop coordinates do not use a window's DPI. */4
public struct Monitor(5
val id: MonitorId,6
val name: String?,7
val desktopBounds: DesktopRect?,8
val workArea: DesktopRect?,9
val physicalSize: PhysicalSize,10
val scaleFactor: Double,11
val refreshRateMilliHertz: Long?12
)14
/** Enumerates the displays currently reported by the native window system. */15
public fun ActiveApplication.monitors(): Array<Monitor> = owner.refreshMonitors()17
/** Returns the display owning the desktop origin, when one is available. */18
public fun ActiveApplication.primaryMonitor(): Monitor? {19
owner.refreshMonitors()20
for (record in owner.monitorRecords)21
if (record.primary)22
return Some(record.monitor)23
return None24
}26
/** Returns the display currently associated with this native window. */27
public fun Window.currentMonitor(): Monitor? {28
owner.requireOpen()29
owner.application.refreshMonitors()30
@Unsafe31
{32
val key = nativeCurrentMonitor(owner.pointer!!)33
for (record in owner.application.monitorRecords)34
if (record.key == key)35
return Some(record.monitor)36
}37
return None38
}40
internal class MonitorRecord(val key: Long, val monitor: Monitor, val primary: Boolean)42
internal fun ApplicationOwner.refreshMonitors(): Array<Monitor> {43
checkThread()44
if (native == None)45
throw WindowException("enumerate monitors", WindowErrorKind.InvalidState)46
val records = ArrayList<MonitorRecord>()47
val result = ArrayList<Monitor>()48
@Unsafe49
{50
val pointer =51
nativeMonitors() ?:52
throw WindowException("enumerate monitors", WindowErrorKind.NativeFailure)53
try {54
val count = nativeMonitorsCount(pointer)55
var index = 0L56
while (index < count) {57
val value = nativeMonitorData(pointer, index)58
val name =59
when (value.name) {60
case Some(val bytes) -> Some(String.fromUtf8(bytes, value.nameLength))61
case None -> None62
}63
val monitor =64
Monitor(65
monitorIdentity(value.key),66
name,67
if (value.x.isFinite() && value.y.isFinite())68
Some(69
DesktopRect(70
DesktopPosition(value.x, value.y),71
value.width,72
value.height73
)74
)75
else76
None,77
if (78
value.workX.isFinite() &&79
value.workY.isFinite() &&80
value.workWidth >81
0.0 &&82
value.workHeight >83
0.084
)85
Some(86
DesktopRect(87
DesktopPosition(value.workX, value.workY),88
value.workWidth,89
value.workHeight90
)91
)92
else93
None,94
PhysicalSize(value.pixelWidth.toInt(), value.pixelHeight.toInt()),95
value.scale,96
if (value.refreshMilliHertz > 0L)97
Some(value.refreshMilliHertz)98
else99
None100
)101
records.add(MonitorRecord(value.key, monitor, value.primary != 0L))102
result.add(monitor)103
index += 1L104
}105
} finally {106
nativeMonitorsRelease(pointer)107
}108
}109
monitorRecords = records110
return result.toArray()111
}113
private fun ApplicationOwner.monitorIdentity(key: Long): MonitorId {114
for (record in monitorRecords)115
if (record.key == key)116
return record.monitor.id117
if (nextMonitor == 9223372036854775807L)118
throw WindowException("monitor identity", WindowErrorKind.NativeFailure)119
val identity = MonitorId(nextMonitor)120
nextMonitor += 1L121
return identity122
}