Scoop API

scoop:scoop.desktop:0.1.0

src/application.scoop

Selected production source · src/application.scoop

1package scoop.desktop
3import scoop.window.*
4import scoop.window.native.*
6/** Native application state; closing it does not close business windows. */
7public class DesktopApplication internal constructor(internal val owner: DesktopOwner): Closeable {
8 public val isClosed: Boolean get() {
9 owner.checkThread();
10 return owner.closed
11 }
12 public fun capabilities(): DesktopApplicationCapabilities {
13 owner.requireOpen()
14 @Unsafe
15 {
16 val flags = nativeApplicationCapabilities(owner.pointer!!)
17 return DesktopApplicationCapabilities(
18 flags.and(1) != 0,
19 flags.and(2) != 0,
20 flags.and(4) != 0,
21 flags.and(8) != 0
22 )
23 }
24 }
25 public fun setIcon(image: DesktopImage?): RequestStatus {
26 owner.requireOpen()
27 if (!capabilities().applicationIcon)
28 return RequestStatus.Unsupported
29 if (sameDesktopImage(owner.icon, image) && owner.iconStatus != None)
30 return owner.iconStatus!!
31 val result =
32 withDesktopImage(image) {
33 native -> @Unsafe
34 {
35 return@withDesktopImage desktopStatus(
36 nativeApplicationIcon(owner.pointer!!, native),
37 "application icon"
38 )
39 }
40 }
41 owner.icon = image
42 owner.iconStatus = Some(result)
43 return result
44 }
45 public fun setBadgeText(value: String?): RequestStatus {
46 owner.requireOpen()
47 checkDesktopText(value ?: "")
48 if (!capabilities().badgeText)
49 return RequestStatus.Unsupported
50 if (owner.badge == value && owner.badgeStatus != None)
51 return owner.badgeStatus!!
52 @Unsafe
53 {
54 val result =
55 (value ?: "").withUtf8Bytes {
56 text,
57 length -> @Unsafe
58 {
59 return@withUtf8Bytes desktopStatus(
60 nativeApplicationBadge(owner.pointer!!, text, length),
61 "Dock badge"
62 )
63 }
64 }
65 owner.badge = value
66 owner.badgeStatus = Some(result)
67 return result
68 }
69 }
70 public fun setDockMenu(menu: Menu?): RequestStatus {
71 owner.requireOpen()
72 if (!capabilities().dockMenu)
73 return RequestStatus.Unsupported
74 return owner.setMenu(menu)
75 }
76 public val menuUseId: NativeMenuUseId? get() {
77 owner.requireOpen();
78 return owner.menuLease?.id
79 }
80 public fun observeMenuBinding(callback: (NativeMenuUseId?) -> Unit): Closeable {
81 owner.requireOpen()
82 val observer = DesktopMenuObserver(owner, Some(callback))
83 owner.menuObservers.add(observer)
84 return observer
85 }
86 public fun setJumpListTasks(tasks: Array<JumpListTask>): RequestStatus {
87 owner.requireOpen()
88 if (!capabilities().jumpListTasks)
89 return RequestStatus.Unsupported
90 if (owner.application.applicationId == None)
91 throw WindowException(
92 "Jump List requires an application id",
93 WindowErrorKind.InvalidState
94 )
95 return publishJumpList(owner, tasks)
96 }
97 public fun attachWindow(window: Window): DesktopWindow {
98 owner.requireOpen()
99 for (binding in owner.windows)
100 if (binding.window === window)
101 throw WindowException(
102 "desktop window is already attached",
103 WindowErrorKind.ResourceBusy
104 )
105 val binding = DesktopWindowOwner(owner, window)
106 binding.initialize()
107 owner.windows.add(binding)
108 return DesktopWindow(binding)
109 }
110 public override fun close() {
111 owner.close()
112 }
115private object DesktopSlot {
116 var owner: DesktopOwner? = None
119/** Opens the one live native desktop resource for this application. */
120public fun ActiveApplication.openDesktop(): DesktopApplication {
121 val previous = DesktopSlot.owner
122 when (previous) {
123 case Some(val value) -> {
124 value.checkThread()
125 if (!value.closed)
126 throw WindowException(
127 "desktop application is already open",
128 WindowErrorKind.ResourceBusy
129 )
130 }
131 case None -> {}
132 }
133 val owner = DesktopOwner(this)
134 owner.initialize()
135 DesktopSlot.owner = Some(owner)
136 return DesktopApplication(owner)