Scoop API

scoop:scoop.window:0.1.0

src/menu/model.scoop

Selected production source · src/menu/model.scoop

1package scoop.window
3internal 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.size
11 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 -> None
17 }
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.id
29internal 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.prepared
41 node.prepared = prepared
42 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 else
52 publishLive(node)
53 }
54 return applyChanges()
57internal 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.children
63 case None -> roots
64 }
65 if (index < 0L || index >= siblings.size)
66 throw IndexOutOfBoundsException()
67 var previous = 0L
68 while (siblings[previous].id != item)
69 previous += 1L
70 if (previous == index)
71 return RequestStatus.Applied
72 siblings.removeAt(previous)
73 siblings.add(index, node)
74 return applyChanges()
77internal 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.children
83 case None -> roots
84 }
85 if (busy())
86 deactivate(node)
87 var index = 0L
88 while (siblings[index].id != item)
89 index += 1L
90 siblings.removeAt(index)
91 retire(node)
92 return applyChanges()
95private fun MenuOwner.retire(node: MenuNode) {
96 for (child in node.children)
97 retire(child)
98 var index = 0L
99 while (nodes[index].id != node.id)
100 index += 1L
101 nodes.removeAt(index)
104private fun MenuOwner.deactivate(node: MenuNode) {
105 @Unsafe
106 {
107 nativeMenuLive(pointer!!, node.id.value, -1)
108 }
109 for (child in node.children)
110 deactivate(child)
113private fun MenuOwner.publishLive(node: MenuNode) {
114 @Unsafe
115 {
116 nativeMenuLive(pointer!!, node.id.value, flag(node.enabled()))
117 }
118 for (child in node.children)
119 publishLive(child)
122private fun MenuOwner.checkShortcut(item: PreparedMenuItem, replacing: MenuItemId?) {
123 if (item.key.key == 0)
124 return
125 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.modifiers
133 )
134 throw IllegalArgumentException(Some("duplicate native menu shortcut"))
135 }
138private fun MenuOwner.checkTextServices(item: PreparedMenuItem, replacing: MenuItemId?) {
139 if (item.kind != 5)
140 return
141 when (use) {
142 case Some(MenuUse.Bar(_)) -> throw WindowException(
143 "text services require a window popup",
144 WindowErrorKind.Unsupported
145 )
146 case Some(MenuUse.Native(_)) -> throw WindowException(
147 "text services require a window popup",
148 WindowErrorKind.Unsupported
149 )
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 )
159internal fun MenuOwner.publish() {
160 if (!dirty || phase != MenuState.Open)
161 return
162 try {
163 @Unsafe
164 {
165 checkNativeStatus(nativeMenuClear(pointer!!), "clear menu")
166 }
167 for (node in roots)
168 appendNative(node, 0L)
169 @Unsafe
170 {
171 checkNativeStatus(nativeMenuCommit(pointer!!), "commit menu")
172 }
173 dirty = false
174 } catch (error: Throwable) {
175 close(PopupCancelReason.MenuClosed)
176 throw error
177 }
180private fun MenuOwner.appendNative(node: MenuNode, parent: Long) {
181 val item = node.prepared
182 val description = item.description
183 @Unsafe
184 {
185 description.text.withUtf8Bytes {
186 bytes,
187 length -> @Unsafe
188 {
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.modifiers
202 ),
203 "append menu item"
204 )
205 }
206 }
207 when (description.kind) {
208 case TextServices(val text, val acceptReplacement) -> text.withUtf8Bytes {
209 bytes,
210 length -> @Unsafe
211 {
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 -> @Unsafe
231 {
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)