Scoop API

scoop:scoop.ui.component.desktop:0.1.0

src/window.scoop

Selected production source · src/window.scoop

1package scoop.ui.component.desktop
3import scoop.desktop.*
4import scoop.ui.*
5import scoop.window.*
7public class NativeDesktopWindowBuilder internal constructor() {
8 public var icon: DesktopImage? = None
9 public var overlayIcon: DesktopImage? = None
10 public var overlayDescription: String = ""
11 public var progress: DesktopProgress = DesktopProgress.None
12 public var miniwindowCaption: String? = None
13 public var thumbnailTooltip: String? = None
16internal class NativeDesktopWindowProps(
17 val controller: NativeDesktopController,
18 val window: Window,
19 val icon: DesktopImage?,
20 val overlay: DesktopImage?,
21 val overlayDescription: String,
22 val progress: DesktopProgress,
23 val miniwindowCaption: String?,
24 val thumbnailTooltip: String?
27public fun nativeDesktopWindow(
28 controller: NativeDesktopController,
29 window: Window,
30 key: ViewKey,
31 configure: NativeDesktopWindowBuilder.() -> Unit = {}
32): ApplicationItem {
33 val builder = NativeDesktopWindowBuilder()
34 builder.configure()
35 return applicationElement(
36 NativeDesktopWindowProps(
37 controller,
38 window,
39 builder.icon,
40 builder.overlayIcon,
41 builder.overlayDescription,
42 builder.progress,
43 builder.miniwindowCaption,
44 builder.thumbnailTooltip
45 ),
46 { props, scope -> NativeDesktopWindowElement(props, scope) },
47 key
48 )
51internal class NativeDesktopWindowElement(
52 private val props: Props<NativeDesktopWindowProps>,
53 scope: ApplicationScope
54): ApplicationElement(scope) {
55 private var binding: DesktopWindow? = None
56 private var previous: NativeDesktopWindowProps? = None
57 public override fun update() {
58 val next = props.value
59 when (previous) {
60 case Some(val old) -> if (
61 !(old.controller === next.controller) || !(old.window === next.window)
62 ) {
63 binding?.close()
64 binding = None
65 previous = None
66 }
67 case None -> {}
68 }
69 if (
70 next.controller.isClosed ||
71 next.window.state ==
72 WindowState.Closing ||
73 next.window.state ==
74 WindowState.Destroyed
75 )
76 return
77 val target =
78 when (binding) {
79 case Some(val value) -> value
80 case None -> {
81 val value =
82 scope.resource { next.controller.application.attachWindow(next.window) }
83 binding = Some(value)
84 value
85 }
86 }
87 if (target.isClosed)
88 return
89 target.setIcon(next.icon)
90 target.setOverlayIcon(next.overlay, next.overlayDescription)
91 when (previous) {
92 case Some(val old) -> {
93 if (!sameDesktopProgress(old.progress, next.progress))
94 target.setProgress(next.progress)
95 if (old.miniwindowCaption != next.miniwindowCaption)
96 target.setMiniwindowCaption(next.miniwindowCaption)
97 if (old.thumbnailTooltip != next.thumbnailTooltip)
98 target.setThumbnailTooltip(next.thumbnailTooltip)
99 }
100 case None -> {
101 target.setProgress(next.progress)
102 target.setMiniwindowCaption(next.miniwindowCaption)
103 target.setThumbnailTooltip(next.thumbnailTooltip)
104 }
105 }
106 previous = Some(next)
107 }
108 public override fun onUnmount() {
109 binding?.close()
110 binding = None
111 previous = None
112 }
115private fun sameDesktopProgress(left: DesktopProgress, right: DesktopProgress): Boolean =
116 when ((left, right)) {
117 case(None, None) -> true
118 case(Indeterminate, Indeterminate) -> true
119 case(Normal(val a, val b), Normal(val c, val d)) -> a == c && b == d
120 case(Paused(val a, val b), Paused(val c, val d)) -> a == c && b == d
121 case(Error(val a, val b), Error(val c, val d)) -> a == c && b == d
122 else -> false
123 }