Scoop API

scoop:scoop.surface:0.1.0

src/shared/host/host.scoop

Selected production source · src/shared/host/host.scoop

1package scoop.surface
3import scoop.platform.concurrent.currentThreadId
4import scoop.window.*
6/** ApplicationHandler wrapper shared by Application.run and runWithAsyncio. */
7public class SurfaceHost public constructor(
8 private val onWindowError: (WindowId, Throwable) -> Unit = { _, error -> throw error },
9 private val handler: (SurfaceHost, ActiveApplication, ApplicationEvent) -> Unit
10): ApplicationHandler, Closeable {
11 private val thread = currentThreadId()
12 private var closing = false
13 private var callbackDepth = 0
14 private var pendingRetirement = false
15 private var proxy: ApplicationProxy? = None
16 private var renderer: RendererHost? = None
17 private val windows = ArrayList<SurfaceWindow>()
18 public val isClosed: Boolean get() = closing && windows.size == 0L
19 public val isClosing: Boolean get() {
20 checkThread()
21 return closing
22 }
23 internal val callbackActive: Boolean get() = callbackDepth != 0
24 internal val acceptsShortcuts: Boolean get() = !closing
25 internal var shortcutAction: HostInputAction? = None
27 /** Attaches one painter to an open window without replacing its input view. */
28 public fun attach(window: Window, painter: (FrameContext) -> Unit): SurfaceWindow {
29 return attach(window, PainterContent(painter))
30 }
32 /** Takes ownership after the thread check, including on attachment failure. */
33 public fun attach(window: Window, content: SurfaceContent): SurfaceWindow {
34 checkThread()
35 var target: WindowRenderer? = None
36 var attached: SurfaceWindow? = None
37 var sharedFailure = false
38 try {
39 if (
40 closing ||
41 (
42 window.state !=
43 WindowState.Open &&
44 window.state !=
45 WindowState.PendingConfigure
46 )
47 )
48 throw IllegalStateException(Some("Surface host or window is not open"))
49 when (find(window.id)) {
50 case Some(_) -> throw IllegalStateException(Some("window is already attached"))
51 case None -> {}
52 }
53 val mailbox =
54 proxy ?: throw IllegalStateException(Some("attach requires a running Surface host"))
55 val device =
56 when (renderer) {
57 case Some(val value) -> value
58 case None -> {
59 sharedFailure = true
60 val value = createRendererHost()
61 sharedFailure = false
62 renderer = Some(value)
63 value
64 }
65 }
66 val created = device.create(window, mailbox)
67 target = Some(created)
68 val result = SurfaceWindow(this, window, content, created)
69 attached = Some(result)
70 windows.add(result)
71 result.initialize()
72 return result
73 } catch (error: Throwable) {
74 var failure: Throwable? = Some(error)
75 when (attached) {
76 case Some(val value) -> failure = cleanupFailure(failure) { value.rollbackAttach() }
77 case None -> {
78 failure = cleanupFailure(failure) { content.close() }
79 when (target) {
80 case Some(val value) -> failure = cleanupFailure(failure) { value.close() }
81 case None -> {}
82 }
83 }
84 }
85 failure = cleanupFailure(failure) { releaseUnusedRenderer() }
86 if (sharedFailure)
87 failure = cleanupFailure(failure) { close() }
88 throwFailure(failure)
89 throw error
90 }
91 }
93 public override fun onEvent(application: ActiveApplication, event: ApplicationEvent) {
94 checkThread()
95 when (proxy) {
96 case None -> proxy = Some(application.proxy())
97 case Some(_) -> {}
98 }
99 callbackDepth += 1
100 var failure: Throwable? = None
101 try {
102 when (event) {
103 case Window(val id, val _event) -> {
104 when (find(id)) {
105 case Some(val window) -> window.event(_event);
106 case None -> {}
107 }
108 }
109 case AboutToWait -> {
110 for (window in windows.toArray())
111 window.refresh()
112 }
113 case Exiting -> close()
114 else -> {}
115 }
116 handler(this, application, event)
117 } catch (error: Throwable) {
118 failure = Some(error)
119 }
120 callbackDepth -= 1
121 when (failure) {
122 case Some(_) -> failure = cleanupFailure(failure) { close() }
123 case None -> {}
124 }
125 if (callbackDepth == 0 && pendingRetirement) {
126 pendingRetirement = false
127 for (window in windows.toArray())
128 failure = cleanupFailure(failure) { window.finishClosing() }
129 when (failure) {
130 case Some(_) -> failure = cleanupFailure(failure) { close() }
131 case None -> {}
132 }
133 }
134 throwFailure(failure)
135 }
136 internal fun requestRetirement() {
137 if (callbackActive)
138 pendingRetirement = true
139 }
140 internal fun reportWindowError(id: WindowId, error: Throwable) {
141 if (closing)
142 throw error
143 try {
144 onWindowError(id, error)
145 } catch (reported: Throwable) {
146 val failure: Throwable? =
147 if (
148 reported ===
149 error ||
150 (reported is ResourceUseException && reported.primaryFailure === error)
151 )
152 Some(reported)
153 else
154 Some(ResourceUseException(error, reported))
155 throwFailure(cleanupFailure(failure) { close() })
156 }
157 }
158 internal fun checkThread() {
159 if (thread != currentThreadId())
160 throw IllegalStateException(Some("Surface requires its owner thread"))
161 }
162 private fun find(id: WindowId): SurfaceWindow? {
163 for (window in windows)
164 if (window.nativeWindow.id == id)
165 return Some(window)
166 return None
167 }
168 internal fun detached(window: SurfaceWindow) {
169 var index = 0L
170 while (index < windows.size) {
171 if (windows[index] === window) {
172 windows.removeAt(index);
173 break
174 }
175 index += 1L
176 }
177 releaseUnusedRenderer()
178 }
179 private fun releaseUnusedRenderer() {
180 if (windows.size != 0L)
181 return
182 when (renderer) {
183 case Some(val value) -> {
184 renderer = None
185 try {
186 value.close()
187 } catch (error: Throwable) {
188 if (closing)
189 throw error
190 throwFailure(cleanupFailure(Some(error)) { close() })
191 }
192 }
193 case None -> {}
194 }
195 }
196 public override fun close() {
197 checkThread()
198 closing = true
199 var failure: Throwable? = None
200 when (shortcutAction) {
201 case Some(val action) -> failure = cleanupFailure(failure) { action.close() }
202 case None -> {}
203 }
204 for (window in windows.toArray())
205 failure = cleanupFailure(failure) { window.close() }
206 failure = cleanupFailure(failure) { releaseUnusedRenderer() }
207 throwFailure(failure)
208 }