Scoop API

scoop:scoop.ui.component.desktop:0.1.0

src/controller.scoop

Selected production source · src/controller.scoop

1package scoop.ui.component.desktop
3import scoop.desktop.*
4import scoop.desktop.notification.*
5import scoop.ui.ApplicationScope
6import scoop.window.ActiveApplication
8/** Owns the application's desktop resources; the exposed resources are borrowed. */
9public class NativeDesktopController internal constructor(
10 active: ActiveApplication,
11 launch: NotificationLaunch?
12): Closeable {
13 public val application: DesktopApplication = active.openDesktop()
14 public val notifications: NotificationCenter? = connectNotifications(application, launch)
15 private var closed = false
16 private var shell: NativeDesktopAttachment? = None
18 public val isClosed: Boolean get() {
19 val nativeClosed = application.isClosed
20 return closed || nativeClosed
21 }
22 internal fun claim(owner: NativeDesktopAttachment) {
23 if (isClosed)
24 throw IllegalStateException(Some("desktop controller is closed"))
25 when (shell) {
26 case Some(val previous) -> if (!(previous === owner))
27 throw IllegalStateException(Some("desktop controller already has a shell element"))
28 case None -> shell = Some(owner)
29 }
30 }
31 internal fun release(owner: NativeDesktopAttachment) {
32 when (shell) {
33 case Some(val previous) -> if (previous === owner)
34 shell = None
35 case None -> {}
36 }
37 }
38 public override fun close() {
39 if (isClosed)
40 return
41 closed = true
42 application.close()
43 }
46private fun connectNotifications(
47 application: DesktopApplication,
48 launch: NotificationLaunch?
49): NotificationCenter? =
50 try {
51 when (launch) {
52 case Some(val value) -> Some(application.openNotifications(value))
53 case None -> None
54 }
55 } catch (error: Throwable) {
56 application.close()
57 throw error
58 }
60public fun ApplicationScope.desktopController(
61 launch: NotificationLaunch? = None
62): NativeDesktopController = resource { NativeDesktopController(application, launch) }