Scoop API

scoop:scoop.window:0.1.0

src/application.scoop

Selected production source · src/application.scoop

1package scoop.window
3import scoop.platform.time.MonotonicInstant
4import scoop.window.native.ApplicationAttachment
5import scoop.window.native.ApplicationHost
7/** Controls when the native loop wakes. WaitUntil uses the system monotonic clock. */
8public enum ControlFlow {
9 Wait, Poll, WaitUntil(MonotonicInstant)
12/** Application lifecycle and window notifications have owned payloads. */
13public enum ApplicationEvent {
14 NewEvents(StartCause), Resumed, Suspended, MemoryWarning,
15 Menu(MenuActivation), MenuBarDetached(MenuBarBindingId), PopupCompleted(
16 PopupSessionId,
17 PopupResult
18 ),
19 ModalCompleted(ModalSessionId, ModalResult), FileDialogCompleted(FileDialogSessionId),
20 Window(WindowId, WindowEvent), MonitorsChanged,
21 InteractionPreferencesChanged(InteractionPreferences), AboutToWait, ExitRequested(
22 ExitReason
23 ), Exiting
26/** Events whose final notification is Destroyed. */
27public enum WindowEvent {
28 Contact(ContactEvent),
29 CloseRequested, Closing, Destroyed, RedrawRequested, FrameChanged,
30 FrameButtonChanged(WindowSystemButton, Boolean, Boolean),
31 InputBlockedChanged(Boolean),
32 SoftwareKeyboardChanged(SoftwareKeyboardState),
33 GeometryChanged(WindowMetrics), Moved(DesktopPosition), Focused(Boolean), Occluded(
34 Boolean
35 ), ThemeChanged(Theme?),
36 Keyboard(KeyEvent), TextCommand(TextEditCommand), ModifiersChanged(Modifiers), Pointer(
37 PointerEvent
38 ), Gesture(GestureEvent), TextInput(TextInputEvent)
41/** Mutable construction options; Application copies their values. */
42public class ApplicationOptions internal constructor(
43 public val lastWindowPolicy: LastWindowPolicy = LastWindowPolicy.KeepRunning,
44 public val applicationId: String? = None,
45 internal val activationPolicy: Int = 0
48/** The one application and one native main-loop run owned by this process. */
49public class Application public constructor(options: ApplicationOptions = applicationOptions()) {
50 internal val owner: ApplicationOwner = createApplicationOwner(options)
51 /** Runs the native loop until explicit, fully drained shutdown. */
52 public fun run(handler: ApplicationHandler) {
53 owner.run(handler, None)
54 }
55 /** Returns the thread-safe business mailbox, available before run begins. */
56 public fun proxy(): ApplicationProxy = ApplicationProxy(owner)
59/** Owner-thread operations available while the native application is active. */
60public class ActiveApplication internal constructor(internal val owner: ApplicationOwner) {
61 public val applicationId: String? get() = owner.applicationId
62 /** Creates a native top-level window from a snapshot of these attributes. */
63 public fun createWindow(attributes: WindowAttributes = windowAttributes()): Window = owner
64 .createWindow(attributes)
65 /** Selects native waiting behavior without introducing a periodic tick. */
66 public fun setControlFlow(flow: ControlFlow) {
67 owner.setControlFlow(flow)
68 }
69 /** Starts ordered window and attached-I/O shutdown. */
70 public fun requestExit() {
71 owner.checkThread();
72 owner.beginExit()
73 }
74 /** Returns a mailbox usable by workers and foreign callbacks. */
75 public fun proxy(): ApplicationProxy = ApplicationProxy(owner)
78/** Cross-thread FIFO business delivery, separate from shutdown control work. */
79public class ApplicationProxy internal constructor(private val owner: ApplicationOwner) {
80 /** Accepts asynchronous work; shutdown may discard work that has not run. */
81 public fun post(block: (ActiveApplication) -> Unit): Boolean = owner
82 .mailbox
83 .post { block(owner.active) }
84 /** Requests shutdown without calling GUI operations on this thread. */
85 public fun requestExit(): Boolean = owner.mailbox.postExit { owner.beginExit() }
88private object ApplicationSlot {
89 val claimed: AtomicBoolean = AtomicBoolean(false)
92private fun createApplicationOwner(options: ApplicationOptions): ApplicationOwner {
93 if (!ApplicationSlot.claimed.compareAndSet(false, true))
94 throw IllegalStateException(Some("one Application per process"))
95 return ApplicationOwner(
96 options.lastWindowPolicy,
97 options.activationPolicy,
98 options.applicationId
99 )