scoop:scoop.window:0.1.0
src/menu/model.scoop
Selected production source · src/menu/model.scoop
1
package scoop.window3
internal fun MenuOwner.insert(4
description: MenuItemDescription,5
parent: MenuItemId?,6
index: Long?7
): MenuItemId {8
requireOpen()9
val target = children(parent)10
val position = index ?: target.size11
if (position < 0L || position > target.size)12
throw IndexOutOfBoundsException()13
val parentNode =14
when (parent) {15
case Some(val identity) -> Some(requireItem(identity))16
case None -> None17
}18
checkRoot(description.kind, parentNode)19
val prepared = prepareMenuItem(description)20
checkTextServices(prepared, None)21
checkShortcut(prepared, None)22
val node = MenuNode(application.takeMenuItemId(), parentNode, prepared)23
target.add(position, node)24
nodes.add(node)25
applyChanges()26
return node.id27
}29
internal fun MenuOwner.update(item: MenuItemId, description: MenuItemDescription): RequestStatus {30
requireOpen()31
val node = requireItem(item)32
requireWindow(33
node.children.size == 0L || description.kind == MenuItemKind.Submenu,34
"cannot replace a nonempty submenu"35
)36
checkRoot(description.kind, node.parent)37
val prepared = prepareMenuItem(description)38
checkTextServices(prepared, Some(item))39
checkShortcut(prepared, Some(item))40
val previous = node.prepared41
node.prepared = prepared42
if (busy()) {43
if (44
previous.description.command !=45
description.command ||46
previous.kind !=47
prepared.kind ||48
(previous.kind == 5 && previous.description.kind != description.kind)49
)50
deactivate(node)51
else52
publishLive(node)53
}54
return applyChanges()55
}57
internal fun MenuOwner.move(item: MenuItemId, index: Long): RequestStatus {58
requireOpen()59
val node = requireItem(item)60
val siblings =61
when (node.parent) {62
case Some(val parent) -> parent.children63
case None -> roots64
}65
if (index < 0L || index >= siblings.size)66
throw IndexOutOfBoundsException()67
var previous = 0L68
while (siblings[previous].id != item)69
previous += 1L70
if (previous == index)71
return RequestStatus.Applied72
siblings.removeAt(previous)73
siblings.add(index, node)74
return applyChanges()75
}77
internal fun MenuOwner.remove(item: MenuItemId): RequestStatus {78
requireOpen()79
val node = requireItem(item)80
val siblings =81
when (node.parent) {82
case Some(val parent) -> parent.children83
case None -> roots84
}85
if (busy())86
deactivate(node)87
var index = 0L88
while (siblings[index].id != item)89
index += 1L90
siblings.removeAt(index)91
retire(node)92
return applyChanges()93
}95
private fun MenuOwner.retire(node: MenuNode) {96
for (child in node.children)97
retire(child)98
var index = 0L99
while (nodes[index].id != node.id)100
index += 1L101
nodes.removeAt(index)102
}104
private fun MenuOwner.deactivate(node: MenuNode) {105
@Unsafe106
{107
nativeMenuLive(pointer!!, node.id.value, -1)108
}109
for (child in node.children)110
deactivate(child)111
}113
private fun MenuOwner.publishLive(node: MenuNode) {114
@Unsafe115
{116
nativeMenuLive(pointer!!, node.id.value, flag(node.enabled()))117
}118
for (child in node.children)119
publishLive(child)120
}122
private fun MenuOwner.checkShortcut(item: PreparedMenuItem, replacing: MenuItemId?) {123
if (item.key.key == 0)124
return125
for (node in nodes) {126
if (127
Some(node.id) !=128
replacing &&129
node.prepared.key.key ==130
item.key.key &&131
node.prepared.key.modifiers ==132
item.key.modifiers133
)134
throw IllegalArgumentException(Some("duplicate native menu shortcut"))135
}136
}138
private fun MenuOwner.checkTextServices(item: PreparedMenuItem, replacing: MenuItemId?) {139
if (item.kind != 5)140
return141
when (use) {142
case Some(MenuUse.Bar(_)) -> throw WindowException(143
"text services require a window popup",144
WindowErrorKind.Unsupported145
)146
case Some(MenuUse.Native(_)) -> throw WindowException(147
"text services require a window popup",148
WindowErrorKind.Unsupported149
)150
else -> {}151
}152
for (node in nodes)153
requireWindow(154
Some(node.id) == replacing || node.prepared.kind != 5,155
"menu already contains text services"156
)157
}159
internal fun MenuOwner.publish() {160
if (!dirty || phase != MenuState.Open)161
return162
try {163
@Unsafe164
{165
checkNativeStatus(nativeMenuClear(pointer!!), "clear menu")166
}167
for (node in roots)168
appendNative(node, 0L)169
@Unsafe170
{171
checkNativeStatus(nativeMenuCommit(pointer!!), "commit menu")172
}173
dirty = false174
} catch (error: Throwable) {175
close(PopupCancelReason.MenuClosed)176
throw error177
}178
}180
private fun MenuOwner.appendNative(node: MenuNode, parent: Long) {181
val item = node.prepared182
val description = item.description183
@Unsafe184
{185
description.text.withUtf8Bytes {186
bytes,187
length -> @Unsafe188
{189
checkNativeStatus(190
nativeMenuAppend(191
pointer!!,192
node.id.value,193
parent,194
item.kind,195
item.checked,196
flag(node.enabled()),197
item.role,198
bytes,199
length,200
item.key.key,201
item.key.modifiers202
),203
"append menu item"204
)205
}206
}207
when (description.kind) {208
case TextServices(val text, val acceptReplacement) -> text.withUtf8Bytes {209
bytes,210
length -> @Unsafe211
{212
checkNativeStatus(213
nativeMenuTextServices(214
pointer!!,215
node.id.value,216
bytes,217
length,218
flag(acceptReplacement)219
),220
"configure text services menu"221
)222
}223
}224
else -> {}225
}226
when (description.image) {227
case Some(val image) -> for (representation in image.representations) {228
representation.pixels.withDataPointer {229
bytes,230
count -> @Unsafe231
{232
checkNativeStatus(233
nativeMenuImage(234
pointer!!,235
node.id.value,236
representation.size.width,237
representation.size.height,238
bytes,239
image.size.width,240
image.size.height,241
flag(image.template)242
),243
"menu image"244
)245
}246
}247
}248
case None -> {}249
}250
}251
for (child in node.children)252
appendNative(child, node.id.value)253
}