scoop:scoop.ui.component.desktop:0.1.0
src/controller.scoop
Selected production source · src/controller.scoop
1
package scoop.ui.component.desktop3
import scoop.desktop.*4
import scoop.desktop.notification.*5
import scoop.ui.ApplicationScope6
import scoop.window.ActiveApplication8
/** Owns the application's desktop resources; the exposed resources are borrowed. */9
public 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 = false16
private var shell: NativeDesktopAttachment? = None18
public val isClosed: Boolean get() {19
val nativeClosed = application.isClosed20
return closed || nativeClosed21
}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 = None35
case None -> {}36
}37
}38
public override fun close() {39
if (isClosed)40
return41
closed = true42
application.close()43
}44
}46
private 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 -> None54
}55
} catch (error: Throwable) {56
application.close()57
throw error58
}60
public fun ApplicationScope.desktopController(61
launch: NotificationLaunch? = None62
): NativeDesktopController = resource { NativeDesktopController(application, launch) }