Scoop API

scoop:scoop.window:0.1.0

src/monitor.scoop

Selected production source · src/monitor.scoop

1package scoop.window
3/** An owned display snapshot; desktop coordinates do not use a window's DPI. */
4public 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?
14/** Enumerates the displays currently reported by the native window system. */
15public fun ActiveApplication.monitors(): Array<Monitor> = owner.refreshMonitors()
17/** Returns the display owning the desktop origin, when one is available. */
18public fun ActiveApplication.primaryMonitor(): Monitor? {
19 owner.refreshMonitors()
20 for (record in owner.monitorRecords)
21 if (record.primary)
22 return Some(record.monitor)
23 return None
26/** Returns the display currently associated with this native window. */
27public fun Window.currentMonitor(): Monitor? {
28 owner.requireOpen()
29 owner.application.refreshMonitors()
30 @Unsafe
31 {
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 None
40internal class MonitorRecord(val key: Long, val monitor: Monitor, val primary: Boolean)
42internal 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 @Unsafe
49 {
50 val pointer =
51 nativeMonitors() ?:
52 throw WindowException("enumerate monitors", WindowErrorKind.NativeFailure)
53 try {
54 val count = nativeMonitorsCount(pointer)
55 var index = 0L
56 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 -> None
62 }
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.height
73 )
74 )
75 else
76 None,
77 if (
78 value.workX.isFinite() &&
79 value.workY.isFinite() &&
80 value.workWidth >
81 0.0 &&
82 value.workHeight >
83 0.0
84 )
85 Some(
86 DesktopRect(
87 DesktopPosition(value.workX, value.workY),
88 value.workWidth,
89 value.workHeight
90 )
91 )
92 else
93 None,
94 PhysicalSize(value.pixelWidth.toInt(), value.pixelHeight.toInt()),
95 value.scale,
96 if (value.refreshMilliHertz > 0L)
97 Some(value.refreshMilliHertz)
98 else
99 None
100 )
101 records.add(MonitorRecord(value.key, monitor, value.primary != 0L))
102 result.add(monitor)
103 index += 1L
104 }
105 } finally {
106 nativeMonitorsRelease(pointer)
107 }
108 }
109 monitorRecords = records
110 return result.toArray()
113private fun ApplicationOwner.monitorIdentity(key: Long): MonitorId {
114 for (record in monitorRecords)
115 if (record.key == key)
116 return record.monitor.id
117 if (nextMonitor == 9223372036854775807L)
118 throw WindowException("monitor identity", WindowErrorKind.NativeFailure)
119 val identity = MonitorId(nextMonitor)
120 nextMonitor += 1L
121 return identity