Scoop API

scoop:scoop.window:0.1.0

src/modal/session.scoop

Selected production source · src/modal/session.scoop

1package scoop.window
3/** Borrows a native window until the native presentation and its callbacks retire. */
4public 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.application
12 internal var phase: ModalState = ModalState.Pending
13 internal var completion: ModalResult? = None
14 internal var requestedEnd: ModalResult? = None
15 internal var commandPending: Boolean = true
16 public val state: ModalState get() {
17 application.checkThread();
18 return phase
19 }
20 public val result: ModalResult? get() {
21 application.checkThread();
22 return completion
23 }
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 return
37 requestedEnd = Some(result)
38 phase = ModalState.Ending
39 application.cancelOwnedFileDialogs(window.owner, FileDialogCancelReason.OwnerHidden)
40 application.cancelOwnedModals(window.owner, ModalCancelReason.OwnerHidden)
41 @Unsafe
42 {
43 nativeModalEnd(pointer!!)
44 }
45 application.mailbox.wake()
46 }
48 internal fun start() {
49 try {
50 if (phase == ModalState.Ending || phase == ModalState.Completed)
51 return
52 val owner = window.owner
53 if (application.exiting) {
54 end(ModalResult.Cancelled(ModalCancelReason.ApplicationExit));
55 return
56 }
57 if (owner.phase == WindowState.Closing || owner.phase == WindowState.Destroyed) {
58 end(ModalResult.Cancelled(ModalCancelReason.WindowClosed));
59 return
60 }
61 phase = ModalState.Showing
62 owner.requestedVisible = true
63 @Unsafe
64 {
65 checkNativeStatus(nativeModalStart(pointer!!), "show modal")
66 }
67 } catch (error: WindowException) {
68 end(ModalResult.Failed(error))
69 } finally {
70 commandPending = false
71 application.mailbox.wake()
72 }
73 }
76public fun Window.showModal(options: ModalOptions = ModalOptions()): ModalSession {
77 owner.requireOpen()
78 val application = owner.application
79 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.owningWindow
85 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().modal
110 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.Sheet
116 else
117 ModalPresentation.SeparateWindow
118 else
119 options.presentation
120 if (
121 presentation ==
122 ModalPresentation.Sheet &&
123 owner.hasOwner &&
124 parent!!.hasModal &&
125 parent!!.modalSession!!.presentation ==
126 ModalPresentation.Sheet &&
127 !capabilities.nestedSheet
128 )
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 += 1L
134 @Unsafe
135 {
136 val native =
137 created(
138 nativeModalCreate(
139 owner.pointer!!,
140 if (options.scope == ModalScope.Owner)
141 0
142 else
143 1,
144 if (presentation == ModalPresentation.Sheet)
145 1
146 else
147 0
148 ),
149 "prepare modal"
150 )
151 val session = ModalSession(id, this, options, presentation, Some(native))
152 owner.leases += 1L
153 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 session
162 }