scoop:scoop.window:0.1.0
src/modal/session.scoop
Selected production source · src/modal/session.scoop
1
package scoop.window3
/** Borrows a native window until the native presentation and its callbacks retire. */4
public class ModalSession internal constructor(5
public val id: ModalSessionId,6
public val window: Window,7
public val options: ModalOptions,8
public val presentation: ModalPresentation,9
internal var pointer: Ptr<Unit>?10
) {11
internal val application: ApplicationOwner = window.owner.application12
internal var phase: ModalState = ModalState.Pending13
internal var completion: ModalResult? = None14
internal var requestedEnd: ModalResult? = None15
internal var commandPending: Boolean = true16
public val state: ModalState get() {17
application.checkThread();18
return phase19
}20
public val result: ModalResult? get() {21
application.checkThread();22
return completion23
}25
public fun dismiss() {26
application.checkThread();27
end(ModalResult.Completed)28
}29
public fun cancel() {30
application.checkThread();31
end(ModalResult.Cancelled(ModalCancelReason.Cancelled))32
}34
internal fun end(result: ModalResult) {35
if (phase == ModalState.Ending || phase == ModalState.Completed)36
return37
requestedEnd = Some(result)38
phase = ModalState.Ending39
application.cancelOwnedFileDialogs(window.owner, FileDialogCancelReason.OwnerHidden)40
application.cancelOwnedModals(window.owner, ModalCancelReason.OwnerHidden)41
@Unsafe42
{43
nativeModalEnd(pointer!!)44
}45
application.mailbox.wake()46
}48
internal fun start() {49
try {50
if (phase == ModalState.Ending || phase == ModalState.Completed)51
return52
val owner = window.owner53
if (application.exiting) {54
end(ModalResult.Cancelled(ModalCancelReason.ApplicationExit));55
return56
}57
if (owner.phase == WindowState.Closing || owner.phase == WindowState.Destroyed) {58
end(ModalResult.Cancelled(ModalCancelReason.WindowClosed));59
return60
}61
phase = ModalState.Showing62
owner.requestedVisible = true63
@Unsafe64
{65
checkNativeStatus(nativeModalStart(pointer!!), "show modal")66
}67
} catch (error: WindowException) {68
end(ModalResult.Failed(error))69
} finally {70
commandPending = false71
application.mailbox.wake()72
}73
}74
}76
public fun Window.showModal(options: ModalOptions = ModalOptions()): ModalSession {77
owner.requireOpen()78
val application = owner.application79
application.requireRunning("show modal")80
if (owner.hasModal)81
throw WindowException("show modal", WindowErrorKind.ResourceBusy)82
if (owner.requestedVisible || isVisible)83
throw WindowException("show hidden modal window", WindowErrorKind.InvalidState)84
val parent = owner.owningWindow85
if (options.scope == ModalScope.Owner && !owner.hasOwner)86
throw WindowException("owner modal requires an owner", WindowErrorKind.InvalidState)87
for (session in application.modals) {88
if (owner.hasOwner && session.window.owner.isOwnedBy(parent!!))89
throw WindowException("modal owner is busy", WindowErrorKind.ResourceBusy)90
if (91
session.options.scope ==92
ModalScope.Application &&93
(!owner.hasOwner || !application.modalBranch(parent!!, session.window.owner))94
)95
throw WindowException("application modal branch", WindowErrorKind.ResourceBusy)96
}97
if (options.scope == ModalScope.Application && application.fileDialogs.size != 0L)98
throw WindowException("application file dialog is active", WindowErrorKind.ResourceBusy)99
when (parent) {100
case Some(val value) -> {101
application.requireDialogOwnerAvailable(value)102
value.requireInteractive()103
val snapshot = value.snapshot()104
if (snapshot.visible == 0 || snapshot.minimized != 0)105
throw WindowException("modal owner visibility", WindowErrorKind.InvalidState)106
}107
case None -> {}108
}109
val capabilities = managementCapabilities().modal110
if (!capabilities.supports(options.scope, options.presentation))111
throw WindowException("modal presentation", WindowErrorKind.Unsupported)112
val presentation =113
if (options.presentation == ModalPresentation.Automatic)114
if (options.scope == ModalScope.Owner && capabilities.ownerSheet)115
ModalPresentation.Sheet116
else117
ModalPresentation.SeparateWindow118
else119
options.presentation120
if (121
presentation ==122
ModalPresentation.Sheet &&123
owner.hasOwner &&124
parent!!.hasModal &&125
parent!!.modalSession!!.presentation ==126
ModalPresentation.Sheet &&127
!capabilities.nestedSheet128
)129
throw WindowException("nested sheet", WindowErrorKind.Unsupported)130
if (application.nextModal == 9223372036854775807L)131
throw WindowException("modal identity", WindowErrorKind.NativeFailure)132
val id = ModalSessionId(application.nextModal)133
application.nextModal += 1L134
@Unsafe135
{136
val native =137
created(138
nativeModalCreate(139
owner.pointer!!,140
if (options.scope == ModalScope.Owner)141
0142
else143
1,144
if (presentation == ModalPresentation.Sheet)145
1146
else147
0148
),149
"prepare modal"150
)151
val session = ModalSession(id, this, options, presentation, Some(native))152
owner.leases += 1L153
when (parent) {154
case Some(val value) -> value.leases += 1L;155
case None -> {}156
}157
owner.modalSession = Some(session)158
application.modals.add(session)159
application.updateModalInput()160
application.deferNativeOperation { session.start() }161
return session162
}163
}