Scoop API

scoop:scoop.desktop:0.1.0

src/window.scoop

Selected production source · src/window.scoop

1package scoop.desktop
3import scoop.window.*
4import scoop.window.native.*
6public class DesktopWindow internal constructor(private val owner: DesktopWindowOwner): Closeable {
7 public val isClosed: Boolean get() {
8 owner.application.checkThread();
9 return owner.closed
10 }
11 public fun capabilities(): DesktopWindowCapabilities {
12 owner.requireOpen()
13 @Unsafe
14 {
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) != 0
22 )
23 }
24 }
25 public fun setIcon(image: DesktopImage?): RequestStatus {
26 owner.requireOpen()
27 if (!capabilities().windowIcon)
28 return RequestStatus.Unsupported
29 if (sameDesktopImage(owner.icon, image) && owner.iconStatus != None)
30 return owner.iconStatus!!
31 val result =
32 withDesktopImage(image) {
33 native -> @Unsafe
34 {
35 return@withDesktopImage desktopStatus(
36 nativeWindowIcon(owner.pointer!!, native),
37 "window icon"
38 )
39 }
40 }
41 owner.icon = image
42 owner.iconStatus = Some(result)
43 return result
44 }
45 public fun setOverlayIcon(image: DesktopImage?, description: String = ""): RequestStatus {
46 owner.requireOpen()
47 checkDesktopText(description)
48 if (!capabilities().overlayIcon)
49 return RequestStatus.Unsupported
50 if (
51 sameDesktopImage(owner.overlay, image) &&
52 owner.overlayDescription ==
53 description &&
54 owner.overlayStatus !=
55 None
56 )
57 return owner.overlayStatus!!
58 val result =
59 withDesktopImage(image) {
60 native -> @Unsafe
61 {
62 return@withDesktopImage description.withUtf8Bytes {
63 text,
64 length -> @Unsafe
65 {
66 return@withUtf8Bytes desktopStatus(
67 nativeWindowOverlay(owner.pointer!!, native, text, length),
68 "taskbar overlay"
69 )
70 }
71 }
72 }
73 }
74 owner.overlay = image
75 owner.overlayDescription = description
76 owner.overlayStatus = Some(result)
77 return result
78 }
79 public fun setProgress(progress: DesktopProgress): RequestStatus {
80 owner.requireOpen()
81 var state = 0
82 var completed = 0UL
83 var total = 0UL
84 when (progress) {
85 case None -> {}
86 case Indeterminate -> state = 1
87 case Normal(val value, val limit) -> {
88 state = 2;
89 completed = value;
90 total = limit
91 }
92 case Paused(val value, val limit) -> {
93 state = 3;
94 completed = value;
95 total = limit
96 }
97 case Error(val value, val limit) -> {
98 state = 4;
99 completed = value;
100 total = limit
101 }
102 }
103 if (state >= 2 && (total == 0UL || completed > total))
104 throw IllegalArgumentException(Some("invalid taskbar progress range"))
105 if (!capabilities().progress)
106 return RequestStatus.Unsupported
107 @Unsafe
108 {
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 }
122internal class DesktopWindowOwner(val application: DesktopOwner, val window: Window) {
123 var pointer: Ptr<Unit>? = None
124 var lease: NativeWindowLease? = None
125 var closed = false
126 var icon: DesktopImage? = None
127 var iconStatus: RequestStatus? = None
128 var overlay: DesktopImage? = None
129 var overlayDescription = ""
130 var overlayStatus: RequestStatus? = None
131 fun requireOpen() {
132 application.requireOpen()
133 if (closed)
134 throw WindowException("desktop window", WindowErrorKind.InvalidState)
135 }
136 fun initialize() {
137 @Unsafe
138 {
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 = None
151 throw error
152 }
153 }
154 }
155 fun caption(value: String?, thumbnail: Boolean): RequestStatus {
156 requireOpen()
157 checkDesktopText(value ?: "")
158 @Unsafe
159 {
160 return (value ?: "").withUtf8Bytes {
161 text,
162 length -> @Unsafe
163 {
164 return@withUtf8Bytes desktopStatus(
165 nativeWindowCaption(
166 pointer!!,
167 if (thumbnail)
168 1
169 else
170 0,
171 text,
172 length
173 ),
174 "desktop caption"
175 )
176 }
177 }
178 }
179 }
180 fun close() {
181 application.checkThread()
182 if (closed)
183 return
184 closed = true
185 @Unsafe
186 {
187 if (pointer != None)
188 nativeWindowDestroy(pointer!!)
189 }
190 pointer = None
191 icon = None
192 overlay = None
193 lease?.close()
194 lease = None
195 for (index in 0L ..< application.windows.size)
196 if (application.windows[index] === this) {
197 application.windows.removeAt(index)
198 break
199 }
200 }