scoop:scoop.ui.component:0.1.0
platform/desktop/native/controllers/tool.scoop
Selected production source · platform/desktop/native/controllers/tool.scoop
1
package scoop.ui.component3
import scoop.graphics.text.FontCollection4
import scoop.ui.*5
import scoop.window.*6
import scoop.window.copy as copyWindowAttributes8
public enum NativeToolAppearance {9
RequireTool, PreferTool, Normal10
}12
public class NativeToolWindowController internal constructor(13
private val scope: NativeControllerScope,14
private val owner: Window15
): Closeable {16
private var current: NativeOwnedWindow? = None17
private val open = state(false)18
private var closed = false19
public val isOpen: Boolean get() = open.value20
public val window: Window? get() {21
open.value;22
return current?.window23
}25
public fun <C: Component> open(26
create: (ComponentScope) -> C,27
onClosed: () -> Unit = {},28
attributes: WindowAttributes = windowAttributes(),29
appearance: NativeToolAppearance = NativeToolAppearance.RequireTool,30
fonts: FontCollection? = None31
): Boolean =32
scope.request {33
if (closed)34
return@request false35
when (current) {36
case Some(_) -> return@request false;37
case None -> {}38
}39
val supported = scope.application.desktopCapabilities().windows.toolWindow40
val role =41
when (appearance) {42
case RequireTool -> {43
if (!supported)44
throw WindowException("native tool window", WindowErrorKind.Unsupported)45
WindowRole.Tool46
}47
case PreferTool -> if (supported)48
WindowRole.Tool49
else50
WindowRole.Normal51
case Normal -> WindowRole.Normal52
}53
val parent = owner54
val created =55
scope.application.createWindow(56
attributes.copyWindowAttributes {57
this.relation = WindowRelation.Owned(parent)58
this.visible = false59
this.requestActivation = false60
this.role = role61
}62
)63
val owned = NativeOwnedWindow(created)64
current = Some(owned)65
try {66
owned.events =67
Some(68
scope.observeWindow(created) {69
event -> when (event) {70
case CloseRequested -> closeWindow()71
case Closing -> open.value = false72
case Destroyed -> {73
current = None74
open.value = false75
owned.close()76
if (!closed)77
onClosed()78
}79
else -> {}80
}81
}82
)83
owned.ui = scope.host.attach(created, create, fonts)84
when (owned.ui) {85
case None -> {86
current = None87
owned.close()88
return@request false89
}90
case Some(_) -> {}91
}92
if (closed || !scope.mounted) {93
owned.close()94
return@request false95
}96
created.setVisible(true)97
if (attributes.requestActivation)98
created.requestFocus()99
open.value = true100
true101
} catch (error: Throwable) {102
current = None103
open.value = false104
throwNative(cleanupNative(Some(error)) { owned.close() })105
false106
}107
}108
public fun closeWindow() {109
if (!closed)110
scope.dispatch {111
open.value = false112
current?.closeContent()113
}114
}115
public override fun close() {116
scope.mounted117
if (closed)118
return119
closed = true120
val owned = current121
current = None122
open.value = false123
owned?.close()124
}125
}127
public fun ComponentScope.nativeToolWindowController(): NativeToolWindowController =128
resource { NativeToolWindowController(NativeControllerScope.Window(this), nativeWindow) }