scoop:scoop.desktop:0.1.0
src/window.scoop
Selected production source · src/window.scoop
1
package scoop.desktop3
import scoop.window.*4
import scoop.window.native.*6
public class DesktopWindow internal constructor(private val owner: DesktopWindowOwner): Closeable {7
public val isClosed: Boolean get() {8
owner.application.checkThread();9
return owner.closed10
}11
public fun capabilities(): DesktopWindowCapabilities {12
owner.requireOpen()13
@Unsafe14
{15
val flags = nativeWindowCapabilities(owner.pointer!!)16
return DesktopWindowCapabilities(17
flags.and(1) != 0,18
flags.and(2) != 0,19
flags.and(4) != 0,20
flags.and(8) != 0,21
flags.and(16) != 022
)23
}24
}25
public fun setIcon(image: DesktopImage?): RequestStatus {26
owner.requireOpen()27
if (!capabilities().windowIcon)28
return RequestStatus.Unsupported29
if (sameDesktopImage(owner.icon, image) && owner.iconStatus != None)30
return owner.iconStatus!!31
val result =32
withDesktopImage(image) {33
native -> @Unsafe34
{35
return@withDesktopImage desktopStatus(36
nativeWindowIcon(owner.pointer!!, native),37
"window icon"38
)39
}40
}41
owner.icon = image42
owner.iconStatus = Some(result)43
return result44
}45
public fun setOverlayIcon(image: DesktopImage?, description: String = ""): RequestStatus {46
owner.requireOpen()47
checkDesktopText(description)48
if (!capabilities().overlayIcon)49
return RequestStatus.Unsupported50
if (51
sameDesktopImage(owner.overlay, image) &&52
owner.overlayDescription ==53
description &&54
owner.overlayStatus !=55
None56
)57
return owner.overlayStatus!!58
val result =59
withDesktopImage(image) {60
native -> @Unsafe61
{62
return@withDesktopImage description.withUtf8Bytes {63
text,64
length -> @Unsafe65
{66
return@withUtf8Bytes desktopStatus(67
nativeWindowOverlay(owner.pointer!!, native, text, length),68
"taskbar overlay"69
)70
}71
}72
}73
}74
owner.overlay = image75
owner.overlayDescription = description76
owner.overlayStatus = Some(result)77
return result78
}79
public fun setProgress(progress: DesktopProgress): RequestStatus {80
owner.requireOpen()81
var state = 082
var completed = 0UL83
var total = 0UL84
when (progress) {85
case None -> {}86
case Indeterminate -> state = 187
case Normal(val value, val limit) -> {88
state = 2;89
completed = value;90
total = limit91
}92
case Paused(val value, val limit) -> {93
state = 3;94
completed = value;95
total = limit96
}97
case Error(val value, val limit) -> {98
state = 4;99
completed = value;100
total = limit101
}102
}103
if (state >= 2 && (total == 0UL || completed > total))104
throw IllegalArgumentException(Some("invalid taskbar progress range"))105
if (!capabilities().progress)106
return RequestStatus.Unsupported107
@Unsafe108
{109
return desktopStatus(110
nativeWindowProgress(owner.pointer!!, state, completed, total),111
"taskbar progress"112
)113
}114
}115
public fun setMiniwindowCaption(value: String?): RequestStatus = owner.caption(value, false)116
public fun setThumbnailTooltip(value: String?): RequestStatus = owner.caption(value, true)117
public override fun close() {118
owner.close()119
}120
}122
internal class DesktopWindowOwner(val application: DesktopOwner, val window: Window) {123
var pointer: Ptr<Unit>? = None124
var lease: NativeWindowLease? = None125
var closed = false126
var icon: DesktopImage? = None127
var iconStatus: RequestStatus? = None128
var overlay: DesktopImage? = None129
var overlayDescription = ""130
var overlayStatus: RequestStatus? = None131
fun requireOpen() {132
application.requireOpen()133
if (closed)134
throw WindowException("desktop window", WindowErrorKind.InvalidState)135
}136
fun initialize() {137
@Unsafe138
{139
lease = Some(application.resource!!.acquireWindow(window, Some({ close() })))140
try {141
pointer =142
Some(143
desktopCreated(144
nativeWindowCreate(application.pointer!!, platformWindow(lease!!)),145
"attach desktop window"146
)147
)148
} catch (error: Throwable) {149
lease!!.close()150
lease = None151
throw error152
}153
}154
}155
fun caption(value: String?, thumbnail: Boolean): RequestStatus {156
requireOpen()157
checkDesktopText(value ?: "")158
@Unsafe159
{160
return (value ?: "").withUtf8Bytes {161
text,162
length -> @Unsafe163
{164
return@withUtf8Bytes desktopStatus(165
nativeWindowCaption(166
pointer!!,167
if (thumbnail)168
1169
else170
0,171
text,172
length173
),174
"desktop caption"175
)176
}177
}178
}179
}180
fun close() {181
application.checkThread()182
if (closed)183
return184
closed = true185
@Unsafe186
{187
if (pointer != None)188
nativeWindowDestroy(pointer!!)189
}190
pointer = None191
icon = None192
overlay = None193
lease?.close()194
lease = None195
for (index in 0L ..< application.windows.size)196
if (application.windows[index] === this) {197
application.windows.removeAt(index)198
break199
}200
}201
}