Scoop API

scoop:scoop.window:0.1.0

src/properties.scoop

Selected production source · src/properties.scoop

1package scoop.window
3/** Optional content-size limits; each present size is finite and nonnegative. */
4public struct WindowSizeConstraints(val min: LogicalSize? = None, val max: LogicalSize? = None) {
5 internal fun validate() {
6 when (min) {
7 case Some(val value) -> checkSize(value);
8 case None -> {}
9 }
10 when (max) {
11 case Some(val value) -> checkSize(value);
12 case None -> {}
13 }
14 when ((min, max)) {
15 case(Some(val low), Some(val high)) -> requireWindow(
16 low.width <= high.width && low.height <= high.height,
17 "inconsistent size constraints"
18 )
19 else -> {}
20 }
21 }
22 internal fun check(size: LogicalSize) {
23 when (min) {
24 case Some(val value) -> requireWindow(
25 size.width >= value.width && size.height >= value.height,
26 "size is below its minimum"
27 );
28 case None -> {}
29 }
30 when (max) {
31 case Some(val value) -> requireWindow(
32 size.width <= value.width && size.height <= value.height,
33 "size exceeds its maximum"
34 );
35 case None -> {}
36 }
37 }
40/** Requests desktop placement in global display-system coordinates. */
41public fun Window.requestPosition(position: DesktopPosition): RequestStatus {
42 owner.requireOpen()
43 requireWindow(position.x.isFinite() && position.y.isFinite(), "non-finite desktop position")
44 @Unsafe
45 {
46 return nativeRequestStatus(
47 nativeWindowPosition(owner.pointer!!, position.x, position.y),
48 "position",
49 RequestStatus.Requested
50 )
51 }
54/** Updates native limits and requests a clamped content size when necessary. */
55public fun Window.setSizeConstraints(constraints: WindowSizeConstraints): RequestStatus {
56 owner.requireOpen();
57 constraints.validate()
58 val minimum = constraints.min ?: LogicalSize(0.0, 0.0)
59 val maximum = constraints.max ?: LogicalSize(-1.0, -1.0)
60 @Unsafe
61 {
62 val status =
63 nativeRequestStatus(
64 nativeWindowConstraints(
65 owner.pointer!!,
66 minimum.width,
67 minimum.height,
68 maximum.width,
69 maximum.height
70 ),
71 "constraints",
72 RequestStatus.Requested
73 )
74 if (status == RequestStatus.Unsupported)
75 return status
76 }
77 owner.constraints = constraints
78 return RequestStatus.Requested
81/** Sets positive interactive resize steps, or restores the native default. */
82public fun Window.setResizeIncrements(increments: LogicalSize?): RequestStatus {
83 owner.requireOpen()
84 val value = increments ?: LogicalSize(1.0, 1.0)
85 checkIncrements(value)
86 @Unsafe
87 {
88 return nativeRequestStatus(
89 nativeWindowResizeIncrements(owner.pointer!!, value.width, value.height),
90 "resize_increments"
91 )
92 }
95/** Enables or disables interactive window resizing. */
96public fun Window.setResizable(value: Boolean): RequestStatus {
97 owner.requireOpen()
98 @Unsafe
99 {
100 return nativeRequestStatus(nativeWindowResizable(owner.pointer!!, flag(value)), "resizable")
101 }
104/** Sets the native close, minimize, and zoom button availability. */
105public fun Window.setEnabledButtons(buttons: WindowButtons): RequestStatus {
106 owner.requireOpen()
107 @Unsafe
108 {
109 val status =
110 nativeRequestStatus(
111 nativeWindowButtons(
112 owner.pointer!!,
113 flag(buttons.close),
114 flag(buttons.minimize),
115 flag(buttons.maximize)
116 ),
117 "buttons"
118 )
119 if (status == RequestStatus.Unsupported)
120 return status
121 }
122 owner.buttons = buttons
123 return RequestStatus.Applied
126/** Selects a native ordering level without claiming keyboard focus. */
127public fun Window.setWindowLevel(level: WindowLevel): RequestStatus {
128 owner.requireOpen()
129 if (
130 level ==
131 WindowLevel.AlwaysOnBottom &&
132 (owner.hasOwner || owner.application.hasOwnedWindow(owner))
133 )
134 return RequestStatus.Unsupported
135 val value =
136 when (level) {
137 case Normal -> 0;
138 case AlwaysOnTop -> 1;
139 case AlwaysOnBottom -> -1
140 }
141 @Unsafe
142 {
143 return nativeRequestStatus(nativeWindowLevel(owner.pointer!!, value), "level")
144 }
147/** Requests native miniaturization or restoration. */
148public fun Window.requestMinimized(value: Boolean): RequestStatus {
149 owner.requireOpen()
150 if (value) {
151 owner.application.cancelOwnedFileDialogs(owner, FileDialogCancelReason.OwnerHidden)
152 owner.application.cancelOwnedModals(owner, ModalCancelReason.OwnerHidden)
153 when (owner.modalSession) {
154 case Some(val session) -> session.end(
155 ModalResult.Cancelled(ModalCancelReason.WindowHidden)
156 )
157 case None -> {}
158 }
159 }
160 @Unsafe
161 {
162 return nativeRequestStatus(
163 nativeWindowMinimized(owner.pointer!!, flag(value)),
164 "minimized",
165 RequestStatus.Requested
166 )
167 }
170/** Requests native maximization or restoration; the OS determines the actual frame. */
171public fun Window.requestMaximized(value: Boolean): RequestStatus {
172 owner.requireOpen()
173 @Unsafe
174 {
175 return nativeRequestStatus(
176 nativeWindowMaximized(owner.pointer!!, flag(value)),
177 "maximized",
178 RequestStatus.Requested
179 )
180 }
183/** Requests entry into or exit from fullscreen on the window's current display. */
184public fun Window.requestFullscreen(value: Boolean): RequestStatus {
185 owner.requireOpen()
186 if (owner.hasOwner || owner.role != WindowRole.Normal) return RequestStatus.Unsupported
187 if (owner.inputBlocked || owner.hasModal)
188 throw WindowException("fullscreen", WindowErrorKind.InvalidState)
189 @Unsafe
190 {
191 return nativeRequestStatus(
192 nativeWindowFullscreen(owner.pointer!!, flag(value)),
193 "fullscreen",
194 RequestStatus.Requested
195 )
196 }
199/** Observes the actual fullscreen mode. Read again after a FrameChanged event. */
200public val Window.isFullscreen: Boolean get() = owner.snapshot().fullscreen != 0
202/** Observes native miniaturization. */
203public val Window.isMinimized: Boolean? get() {
204 val value = owner.snapshot().minimized
205 return if (value < 0)
206 None
207 else
208 Some(value != 0)
211/** Observes native zoom state. */
212public val Window.isMaximized: Boolean? get() {
213 val value = owner.snapshot().maximized
214 return if (value < 0)
215 None
216 else
217 Some(value != 0)
220/** Requests a preferred appearance; None follows the application/system. */
221public fun Window.requestTheme(theme: Theme?): RequestStatus {
222 owner.requireOpen()
223 val value =
224 when (theme) {
225 case Some(Theme.Light) -> 0;
226 case Some(Theme.Dark) -> 1;
227 case None -> -1
228 }
229 @Unsafe
230 {
231 return nativeRequestStatus(
232 nativeWindowTheme(owner.pointer!!, value),
233 "theme",
234 RequestStatus.Requested
235 )
236 }
239/** Controls the window background opacity independently of a blur request. */
240public fun Window.setTransparent(value: Boolean): RequestStatus {
241 owner.requireOpen()
242 @Unsafe
243 {
244 return nativeRequestStatus(
245 nativeWindowTransparent(owner.pointer!!, flag(value)),
246 "transparent"
247 )
248 }
251/** Places a native visual-effect background behind the renderer host view. */
252public fun Window.setBlur(value: Boolean): RequestStatus {
253 owner.requireOpen()
254 @Unsafe
255 {
256 return nativeRequestStatus(nativeWindowBlur(owner.pointer!!, flag(value)), "blur")
257 }
260/** Requests the system's content-sharing hint, without promising capture prevention. */
261public fun Window.setContentProtected(value: Boolean): RequestStatus {
262 owner.requireOpen()
263 @Unsafe
264 {
265 return nativeRequestStatus(
266 nativeWindowContentProtection(owner.pointer!!, flag(value)),
267 "content_protection"
268 ,
269 RequestStatus.Requested
270 )
271 }
274/** Requests or cancels native user attention independently of activation. */
275public fun Window.requestAttention(kind: AttentionKind): RequestStatus {
276 owner.requireOpen()
277 val value =
278 when (kind) {
279 case Informational -> 0;
280 case Critical -> 1;
281 case Cancel -> -1
282 }
283 @Unsafe
284 {
285 return nativeRequestStatus(
286 nativeWindowAttention(owner.pointer!!, value),
287 "attention",
288 RequestStatus.Requested
289 )
290 }
293internal fun checkIncrements(value: LogicalSize) {
294 checkSize(value)
295 requireWindow(value.width > 0.0 && value.height > 0.0, "resize increments must be positive")
298internal fun validateAttributes(attributes: WindowAttributes) {
299 checkSize(attributes.size)
300 attributes.constraints.validate()
301 attributes.constraints.check(attributes.size)
302 when (attributes.position) {
303 case Some(val value) -> requireWindow(
304 value.x.isFinite() && value.y.isFinite(),
305 "non-finite desktop position"
306 );
307 case None -> {}
308 }
309 when (attributes.resizeIncrements) {
310 case Some(val value) -> checkIncrements(value);
311 case None -> {}
312 }
315internal fun applyCreationProperties(owner: WindowOwner, attributes: WindowAttributes) {
316 val window = Window(owner)
317 @Unsafe
318 {
319 platformCreationProperties(owner.pointer!!, attributes.titlebarFlags)
320 }
321 requireCreationStatus(window.setFrame(attributes.frame), "frame")
322 requireCreationStatus(window.setShadow(attributes.shadow), "shadow")
323 requireCreationStatus(window.setSizeConstraints(attributes.constraints), "constraints")
324 requireCreationStatus(
325 window.setResizeIncrements(attributes.resizeIncrements),
326 "resize_increments"
327 )
328 requireCreationStatus(window.setEnabledButtons(attributes.buttons), "buttons")
329 requireCreationStatus(window.setWindowLevel(attributes.level), "level")
330 requireCreationStatus(window.requestTheme(attributes.theme), "theme")
331 requireCreationStatus(window.setBlur(attributes.blur), "blur")
332 requireCreationStatus(
333 window.setContentProtected(attributes.contentProtected),
334 "content_protection"
335 )
336 when (attributes.position) {
337 case Some(val value) -> requireCreationStatus(window.requestPosition(value), "position");
338 case None -> {}
339 }