Scoop API

scoop:scoop.window:0.1.0

src/dialogs/session.scoop

Selected production source · src/dialogs/session.scoop

1package scoop.window
3import scoop.platform.fs.Path
5/** A native file presentation with an owned, typed completion result. */
6public class FileDialogSession<T> internal constructor(
7 internal val owner: FileDialogOwner,
8 private val decode: (Array<Path>) -> T
9) {
10 private var cached: FileDialogResult<T>? = None
11 public val id: FileDialogSessionId get() = owner.id
12 public val state: FileDialogState get() {
13 owner.application.checkThread()
14 return owner.phase
15 }
16 public val result: FileDialogResult<T>? get() {
17 owner.application.checkThread()
18 when (cached) {
19 case Some(_) -> return cached
20 case None -> {}
21 }
22 val value = owner.completion ?: return None
23 cached =
24 Some(
25 when (value) {
26 case Accepted(val paths) -> FileDialogResult.Accepted<T>(decode(paths))
27 case Cancelled(val reason) -> FileDialogResult.Cancelled<T>(reason)
28 case Failed(val error) -> FileDialogResult.Failed<T>(error)
29 }
30 )
31 return cached
32 }
33 public fun cancel() {
34 owner.application.checkThread()
35 owner.cancel(FileDialogCancelReason.Cancelled)
36 }
39internal class FileDialogOwner(
40 val application: ApplicationOwner,
41 val id: FileDialogSessionId,
42 val window: WindowOwner?,
43 val kind: Int,
44 native: Ptr<Unit>
45) {
46 var pointer: Ptr<Unit>? = Some(native)
47 var phase = FileDialogState.Pending
48 var commandPending = true
49 var cancellation: FileDialogCancelReason? = None
50 var completion: FileDialogResult<Array<Path>>? = None
52 fun blocks(target: WindowOwner): Boolean =
53 when (window) {
54 case Some(val value) -> value === target
55 case None -> true
56 }
57 fun owns(target: WindowOwner): Boolean =
58 when (window) {
59 case Some(val value) -> value === target || value.isDescendantOf(target)
60 case None -> false
61 }
62 fun cancel(reason: FileDialogCancelReason) {
63 if (phase == FileDialogState.Ending || phase == FileDialogState.Completed)
64 return
65 @Unsafe
66 {
67 if (nativeFileCancel(pointer!!) == 0)
68 return
69 cancellation = Some(reason)
70 phase = FileDialogState.Ending
71 }
72 application.mailbox.wake()
73 }
74 fun start() {
75 try {
76 if (phase == FileDialogState.Ending)
77 return
78 if (application.exiting) {
79 cancel(FileDialogCancelReason.ApplicationExit)
80 return
81 }
82 phase = FileDialogState.Showing
83 @Unsafe
84 {
85 nativeFileStart(pointer!!)
86 }
87 } finally {
88 commandPending = false
89 application.mailbox.wake()
90 }
91 }