Scoop API

scoop:scoop.ui.component:0.1.0

platform/desktop/native/chrome/component.scoop

Selected production source · platform/desktop/native/chrome/component.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.window.*
8public class WindowChrome public constructor(
9 private val props: Props<WindowChromeProps>,
10 scope: ComponentScope
11): Component(scope) {
12 private val metrics = state<WindowFrameMetrics?>(None)
13 public override fun onMount() {
14 scope.claimWindowFrame()
15 scope.observeFrameMetrics { value -> metrics.value = value }
16 }
17 public override fun render(): View {
18 val input = props.value
19 return element(
20 WindowChromeInput(input, metrics.value),
21 { current, mount -> WindowChromeLayout(current, scope = mount) },
22 [input.titleBar ?: empty(), input.content]
23 )
24 }
27internal class WindowChromeInput(val value: WindowChromeProps, val metrics: WindowFrameMetrics?)
29private struct ChromeConfiguration(
30 val frame: WindowFrame,
31 val fallback: WindowFrame?,
32 val transparent: Boolean?,
33 val shadow: WindowShadow?
36internal class WindowChromeLayout(
37 private val props: Props<WindowChromeInput>,
38 scope: ComponentScope
39): Element<Unit>(scope) {
40 private var previous: ChromeConfiguration? = None
41 private var shape: WindowRegionHandle? = None
42 private var input: WindowRegionHandle? = None
43 public override fun onMount() {
44 configure()
45 }
46 public override fun update(): UiInvalidation {
47 configure();
48 return UiInvalidation.Layout
49 }
50 private fun configure() {
51 val value = props.value.value
52 val desired =
53 ChromeConfiguration(value.frame, value.fallbackFrame, value.transparent, value.shadow)
54 val window = scope.nativeWindow
55 val frameChanged =
56 when (previous) {
57 case Some(val old) -> old.frame != value.frame || old.fallback != value.fallbackFrame
58 case None -> true
59 }
60 if (frameChanged) {
61 val result = window.setFrame(value.frame)
62 if (result == RequestStatus.Unsupported) {
63 when (value.fallbackFrame) {
64 case Some(val fallback) -> requireChromeRequest(
65 window.setFrame(fallback),
66 "fallback window frame"
67 )
68 case None -> requireChromeRequest(result, "window frame")
69 }
70 }
71 }
72 val transparentChanged =
73 when (previous) {
74 case Some(val old) -> old.transparent != value.transparent
75 case None -> true
76 }
77 if (transparentChanged) {
78 when (value.transparent) {
79 case Some(val transparent) -> requireChromeRequest(
80 window.setTransparent(transparent),
81 "transparent window"
82 );
83 case None -> {}
84 }
85 }
86 val shadowChanged =
87 when (previous) {
88 case Some(val old) -> old.shadow != value.shadow
89 case None -> true
90 }
91 if (shadowChanged) {
92 when (value.shadow) {
93 case Some(val shadow) -> requireChromeRequest(
94 window.setShadow(shadow),
95 "window shadow"
96 );
97 case None -> {}
98 }
99 }
100 previous = Some(desired)
101 shape = updateRegion(shape, value.windowShape, WindowRegionKind.Shape)
102 input = updateRegion(input, value.inputShape, WindowRegionKind.Input)
103 }
104 private fun updateRegion(
105 previous: WindowRegionHandle?,
106 geometry: WindowChromeShape?,
107 kind: WindowRegionKind
108 ): WindowRegionHandle? =
109 when (geometry) {
110 case None -> {
111 previous?.close();
112 None
113 }
114 case Some(_) -> when (previous) {
115 case Some(_) -> previous
116 case None -> Some(scope.createWindowRegion(kind))
117 }
118 }
119 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
120 Unit
121 > = measureWindowChrome(context, constraints, props.value)
122 public override fun scene(context: SceneContext, measured: Unit) {
123 val value = props.value.value
124 val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)
125 when (value.clip) {
126 case Some(val clip) -> withChromePath(clip, bounds) { path -> context.clipPath(path) };
127 case None -> {}
128 }
129 when (value.windowShape) {
130 case Some(val geometry) -> withChromePath(geometry, bounds) {
131 path -> context.place(shape!!, path)
132 };
133 case None -> {}
134 }
135 when (value.inputShape) {
136 case Some(val geometry) -> withChromePath(geometry, bounds) {
137 path -> context.place(input!!, path)
138 };
139 case None -> {}
140 }
141 context.children()
142 }
145internal fun withChromePath(shape: WindowChromeShape, bounds: Rect, action: (Path) -> Unit) {
146 when (shape) {
147 case Path(val value) -> action(value)
148 case Bounds -> path { addRect(bounds) }.use(action)
149 case Rounded(val radius) -> path { addRoundRect(bounds, radius, radius) }.use(action)
150 case Ellipse -> path { addOval(bounds) }.use(action)
151 }