Scoop API

scoop:scoop.ui.component:0.1.0

platform/desktop/native/chrome/controls.scoop

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

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.*
8import scoop.window.accessibility.*
10public class WindowControlsProps public constructor(
11 public val buttons: WindowButtons = WindowButtons(),
12 public val size: ButtonSize = ButtonSize.IconSm,
13 public val style: ButtonStyle = buttonStyle(),
14 public val theme: ThemeSource? = None,
15 public val semantics: (WindowSystemButton, Boolean) -> ComponentSemantics =
16 ::windowControlSemantics
19public class WindowControlsBuilder internal constructor() {
20 public var buttons: WindowButtons = WindowButtons()
21 public var size: ButtonSize = ButtonSize.IconSm
22 public var style: ButtonStyle = buttonStyle()
23 public var theme: ThemeSource? = None
24 public var key: ViewKey = ViewKey.Position
25 public var semantics: (WindowSystemButton, Boolean) -> ComponentSemantics =
26 ::windowControlSemantics
29public fun windowControls(configure: WindowControlsBuilder.() -> Unit = {}): View {
30 val builder = WindowControlsBuilder()
31 builder.configure()
32 return component(
33 WindowControlsProps(
34 builder.buttons,
35 builder.size,
36 builder.style,
37 builder.theme,
38 builder.semantics
39 ),
40 { props, scope -> WindowControls(props, scope) },
41 builder.key
42 )
45public class WindowControls public constructor(
46 private val props: Props<WindowControlsProps>,
47 scope: ComponentScope
48): Component(scope) {
49 public override fun render(): View {
50 val input = props.value
51 return row {
52 for (
53 button in [
54 WindowSystemButton.Minimize,
55 WindowSystemButton.Maximize,
56 WindowSystemButton.Close
57 ]
58 ) {
59 val visible =
60 when (button) {
61 case Minimize -> input.buttons.minimize
62 case Maximize -> input.buttons.maximize
63 case Close -> input.buttons.close
64 }
65 if (visible)
66 add(
67 component(
68 WindowControlInput(button, input),
69 { current, mount -> WindowControl(current, mount) },
70 ViewKey.Number(
71 when (button) {
72 case Minimize -> 0L;
73 case Maximize -> 1L;
74 case Close -> 2L
75 }
76 )
77 )
78 )
79 }
80 }
81 }
84internal class WindowControlInput(
85 val button: WindowSystemButton,
86 val appearance: WindowControlsProps
89private class WindowControl(
90 private val props: Props<WindowControlInput>,
91 scope: ComponentScope
92): Component(scope) {
93 private val themes = ThemeAccess(scope)
94 private val nativeInteraction = state(InteractionState())
95 private val maximized = state(false)
96 private val click: () -> Unit = { activate() }
97 private val control = PressControl(scope, PressOptions(), FocusOptions(), click)
98 private val semantics = createComponentSemantics(scope, SemanticRole.Button)
99 init {
100 semantics?.bindFocus(Some(control.focus))
101 scope.routeSemanticsChildren(semantics)
102 }
103 public override fun onMount() {
104 themes.mount(props.value.appearance.theme)
105 maximized.value = scope.nativeWindow.isMaximized ?: false
106 scope.observeWindow(scope.nativeWindow) {
107 event -> when (event) {
108 case FrameButtonChanged(val button, val hovered, val pressed) -> if (
109 button == props.value.button
110 ) {
111 val next = InteractionState(hovered, pressed)
112 if (nativeInteraction.value != next)
113 nativeInteraction.value = next
114 }
115 case GeometryChanged(_) -> {
116 val next = scope.nativeWindow.isMaximized ?: false
117 if (maximized.value != next)
118 maximized.value = next
119 }
120 else -> {}
121 }
122 }
123 }
124 private fun activate() {
125 val window = scope.nativeWindow
126 when (props.value.button) {
127 case Close -> {
128 window.requestClose()
129 }
130 case Minimize -> requireChromeRequest(window.requestMinimized(true), "minimize window")
131 case Maximize -> requireChromeRequest(
132 window.requestMaximized(!(window.isMaximized ?: false)),
133 "maximize window"
134 )
135 }
136 }
137 public override fun render(): View {
138 val input = props.value
139 val appearance = input.appearance
140 val theme = themes.read(appearance.theme)
141 val keyboard = control.press.interaction
142 val native = nativeInteraction.value
143 val interaction =
144 InteractionState(
145 keyboard.hovered || native.hovered,
146 keyboard.pressed || native.pressed,
147 keyboard.focused,
148 keyboard.focusVisible
149 )
150 val visual = SurfaceVisual(appearance.style.kind, interaction.asVisual(true, false))
151 val shape = ActionShape.Button(appearance.size)
152 val button =
153 element(
154 ActionInkProps(
155 theme,
156 shape,
157 ButtonVariant.Ghost,
158 appearance.style,
159 visual,
160 ActionBehavior.Press(control, click, FocusOptions()),
161 semantics = semantics,
162 semanticOptions = appearance.semantics(input.button, maximized.value)
163 ),
164 { current, mount -> ActionInk(current, mount) },
165 [
166 windowControlGlyph(
167 input.button,
168 maximized.value,
169 actionIconSize(shape),
170 actionTextColor(theme, shape, ButtonVariant.Ghost, visual)
171 )
172 ]
173 )
174 return element(
175 input.button,
176 { current, mount -> WindowControlHit(current, mount) },
177 [button]
178 )
179 }
182private class WindowControlHit(
183 private val props: Props<WindowSystemButton>,
184 scope: ComponentScope
185): Element<Unit>(scope) {
186 private var region: WindowRegionHandle? = None
187 public override fun onMount() {
188 if (scope.application.desktopCapabilities().frame.systemButtonHitRegions)
189 region =
190 Some(
191 scope.createWindowRegion(
192 WindowRegionKind.Hit(WindowHitRole.SystemButton(props.value))
193 )
194 )
195 }
196 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
197 Unit
198 > {
199 val child = context.measureChild(0L, constraints)
200 context.placeChild(0L, Point(0.0, 0.0))
201 return LayoutResult(child, Unit)
202 }
203 public override fun scene(context: SceneContext, measured: Unit) {
204 when (region) {
205 case Some(val value) -> context.place(
206 value,
207 Rect(0.0, 0.0, context.size.width, context.size.height)
208 )
209 case None -> {}
210 }
211 context.children()
212 }
215/** Default accessible names for the current native window operation. */
216public fun windowControlSemantics(
217 button: WindowSystemButton,
218 maximized: Boolean
219): ComponentSemantics =
220 ComponentSemantics(
221 name =
222 Some(
223 when (button) {
224 case Minimize -> "Minimize window"
225 case Maximize -> if (maximized)
226 "Restore window"
227 else
228 "Maximize window"
229 case Close -> "Close window"
230 }
231 )
232 )