scoop:scoop.ui.component:0.1.0
platform/desktop/native/chrome/controls.scoop
Selected production source · platform/desktop/native/chrome/controls.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.window.*8
import scoop.window.accessibility.*10
public 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
::windowControlSemantics17
)19
public class WindowControlsBuilder internal constructor() {20
public var buttons: WindowButtons = WindowButtons()21
public var size: ButtonSize = ButtonSize.IconSm22
public var style: ButtonStyle = buttonStyle()23
public var theme: ThemeSource? = None24
public var key: ViewKey = ViewKey.Position25
public var semantics: (WindowSystemButton, Boolean) -> ComponentSemantics =26
::windowControlSemantics27
}29
public 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.semantics39
),40
{ props, scope -> WindowControls(props, scope) },41
builder.key42
)43
}45
public class WindowControls public constructor(46
private val props: Props<WindowControlsProps>,47
scope: ComponentScope48
): Component(scope) {49
public override fun render(): View {50
val input = props.value51
return row {52
for (53
button in [54
WindowSystemButton.Minimize,55
WindowSystemButton.Maximize,56
WindowSystemButton.Close57
]58
) {59
val visible =60
when (button) {61
case Minimize -> input.buttons.minimize62
case Maximize -> input.buttons.maximize63
case Close -> input.buttons.close64
}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 -> 2L75
}76
)77
)78
)79
}80
}81
}82
}84
internal class WindowControlInput(85
val button: WindowSystemButton,86
val appearance: WindowControlsProps87
)89
private class WindowControl(90
private val props: Props<WindowControlInput>,91
scope: ComponentScope92
): 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 ?: false106
scope.observeWindow(scope.nativeWindow) {107
event -> when (event) {108
case FrameButtonChanged(val button, val hovered, val pressed) -> if (109
button == props.value.button110
) {111
val next = InteractionState(hovered, pressed)112
if (nativeInteraction.value != next)113
nativeInteraction.value = next114
}115
case GeometryChanged(_) -> {116
val next = scope.nativeWindow.isMaximized ?: false117
if (maximized.value != next)118
maximized.value = next119
}120
else -> {}121
}122
}123
}124
private fun activate() {125
val window = scope.nativeWindow126
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.value139
val appearance = input.appearance140
val theme = themes.read(appearance.theme)141
val keyboard = control.press.interaction142
val native = nativeInteraction.value143
val interaction =144
InteractionState(145
keyboard.hovered || native.hovered,146
keyboard.pressed || native.pressed,147
keyboard.focused,148
keyboard.focusVisible149
)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
}180
}182
private class WindowControlHit(183
private val props: Props<WindowSystemButton>,184
scope: ComponentScope185
): Element<Unit>(scope) {186
private var region: WindowRegionHandle? = None187
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
Unit198
> {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
}213
}215
/** Default accessible names for the current native window operation. */216
public fun windowControlSemantics(217
button: WindowSystemButton,218
maximized: Boolean219
): ComponentSemantics =220
ComponentSemantics(221
name =222
Some(223
when (button) {224
case Minimize -> "Minimize window"225
case Maximize -> if (maximized)226
"Restore window"227
else228
"Maximize window"229
case Close -> "Close window"230
}231
)232
)