scoop:scoop.window:0.1.0
src/application.scoop
Selected production source · src/application.scoop
1
package scoop.window3
import scoop.platform.time.MonotonicInstant4
import scoop.window.native.ApplicationAttachment5
import scoop.window.native.ApplicationHost7
/** Controls when the native loop wakes. WaitUntil uses the system monotonic clock. */8
public enum ControlFlow {9
Wait, Poll, WaitUntil(MonotonicInstant)10
}12
/** Application lifecycle and window notifications have owned payloads. */13
public enum ApplicationEvent {14
NewEvents(StartCause), Resumed, Suspended, MemoryWarning,15
Menu(MenuActivation), MenuBarDetached(MenuBarBindingId), PopupCompleted(16
PopupSessionId,17
PopupResult18
),19
ModalCompleted(ModalSessionId, ModalResult), FileDialogCompleted(FileDialogSessionId),20
Window(WindowId, WindowEvent), MonitorsChanged,21
InteractionPreferencesChanged(InteractionPreferences), AboutToWait, ExitRequested(22
ExitReason23
), Exiting24
}26
/** Events whose final notification is Destroyed. */27
public 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
Boolean35
), ThemeChanged(Theme?),36
Keyboard(KeyEvent), TextCommand(TextEditCommand), ModifiersChanged(Modifiers), Pointer(37
PointerEvent38
), Gesture(GestureEvent), TextInput(TextInputEvent)39
}41
/** Mutable construction options; Application copies their values. */42
public class ApplicationOptions internal constructor(43
public val lastWindowPolicy: LastWindowPolicy = LastWindowPolicy.KeepRunning,44
public val applicationId: String? = None,45
internal val activationPolicy: Int = 046
)48
/** The one application and one native main-loop run owned by this process. */49
public 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)57
}59
/** Owner-thread operations available while the native application is active. */60
public class ActiveApplication internal constructor(internal val owner: ApplicationOwner) {61
public val applicationId: String? get() = owner.applicationId62
/** Creates a native top-level window from a snapshot of these attributes. */63
public fun createWindow(attributes: WindowAttributes = windowAttributes()): Window = owner64
.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)76
}78
/** Cross-thread FIFO business delivery, separate from shutdown control work. */79
public 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 = owner82
.mailbox83
.post { block(owner.active) }84
/** Requests shutdown without calling GUI operations on this thread. */85
public fun requestExit(): Boolean = owner.mailbox.postExit { owner.beginExit() }86
}88
private object ApplicationSlot {89
val claimed: AtomicBoolean = AtomicBoolean(false)90
}92
private 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.applicationId99
)100
}