scoop:scoop.ui.component:0.1.0
src/content/progress.scoop
Selected production source · src/content/progress.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.math.floor6
import scoop.surface.*7
import scoop.time.Duration8
import scoop.ui.*9
import scoop.ui.component.column as progressColumn10
import scoop.ui.style.*11
import scoop.window.accessibility.*13
public class ProgressProps public constructor(14
public val value: Double?,15
public val minimum: Double = 0.0,16
public val maximum: Double = 100.0,17
public val label: PanelContent? = None,18
public val valueLabel: PanelContent? = None,19
public val showValue: Boolean = false,20
public val thickness: Double? = None,21
public val indicatorColor: Color? = None,22
public val style: StyleOverride = styleOverride(),23
public val transition: StyleValue<AnimationSpec?> = StyleValue.Inherit,24
public val period: Duration = Duration.seconds(2L),25
public val fonts: FontCollection? = None,26
public val theme: ThemeSource? = None,27
public val semantics: ComponentSemantics = ComponentSemantics()28
) {29
public val position: Double? = progressPosition(value, minimum, maximum)30
init {31
when (thickness) {32
case Some(val value) -> nonnegative(value, "invalid progress thickness");33
case None -> {}34
}35
requireComponent(period > Duration.ZERO, "progress period must be positive")36
}37
}39
internal fun progressPosition(value: Double?, minimum: Double, maximum: Double): Double? {40
val range = RangeSpec(minimum, maximum)41
requireComponent(maximum > minimum, "progress maximum must exceed minimum")42
return when (value) {43
case Some(val current) -> Some(range.positionOf(current));44
case None -> None45
}46
}48
public class Progress public constructor(49
private val props: Props<ProgressProps>,50
scope: ComponentScope51
): Component(scope) {52
private val themes = ThemeAccess(scope)53
private val labels = ActionTextStyle()54
private val values = ActionTextStyle()55
public override fun onMount() {56
themes.mount(props.value.theme)57
}58
public override fun render(): View {59
val input = props.value60
val theme = themes.read(input.theme)61
val palette = theme.tokens.palette62
val density = theme.tokens.typography.body.fontSize / 14.063
val labelStyle =64
labels.resolve(theme.tokens.typography.action, palette.background.foreground, None)65
val valueStyle =66
values.resolve(theme.tokens.typography.body, palette.muted.foreground, None)67
val valueCaption =68
when (input.valueLabel) {69
case Some(val value) -> Some(panelCaption(value, valueStyle, input.fonts))70
case None -> if (input.showValue)71
Some(72
panelCaption(73
PanelContent.Label(74
LabelProps(LabelText.Plain(progressCaption(input.position), None))75
),76
valueStyle,77
input.fonts78
)79
)80
else81
None82
}83
val labelCaption =84
when (input.label) {85
case Some(val value) -> Some(panelCaption(value, labelStyle, input.fonts))86
case None -> None87
}88
val track =89
element(90
ProgressInkProps(91
input.position,92
input.thickness ?: 4.0 * density,93
input.indicatorColor ?: palette.primary.background,94
palette.muted.background,95
input.style,96
theme.tokens.motion.resolve(97
input.transition.resolve(theme.tokens.motion.selection)98
),99
theme.tokens.motion.preference == MotionPreference.Full,100
input.period101
),102
{ current, mount -> ProgressInk(current, scope = mount) }103
)104
val content =105
progressColumn {106
if (panelPresent(labelCaption) || panelPresent(valueCaption))107
this.add(108
panelSlot(109
panelAside(110
labelCaption,111
valueCaption,112
8.0 * density,113
theme.tokens.typography.body.fontSize * 4.0114
),115
0L116
)117
)118
this.add(panelSlot(track, 1L))119
this.spacing = 12.0 * density120
this.alignment = CrossAlignment.Stretch121
}122
val facts =123
input.semantics.properties(124
range =125
when (input.position) {126
case Some(val position) -> Some(127
SemanticValueRange(128
input.minimum,129
input.maximum,130
input.minimum + (input.maximum - input.minimum) * position131
)132
)133
case None -> None134
}135
).copy {136
if (name == None && input.semantics.relations.labelledBy.size == 0L)137
name =138
when (input.label) {139
case Some(val caption) -> candidateCaption(caption)140
case None -> None141
}142
}143
return semanticContent(144
content,145
SemanticRole.ProgressBar,146
facts,147
relations = input.semantics.relations,148
reference = input.semantics.reference149
)150
}151
}153
internal fun progressCaption(position: Double?): String =154
when (position) {155
case Some(val value) -> f"${floor(value * 100.0 + 0.5).toLong()}%"156
case None -> "…"157
}159
public class ProgressBuilder internal constructor() {160
public var minimum: Double = 0.0161
public var maximum: Double = 100.0162
public var label: PanelContent? = None163
public var valueLabel: PanelContent? = None164
public var showValue: Boolean = false165
public var thickness: Double? = None166
public var indicatorColor: Color? = None167
public var style: StyleOverride = styleOverride()168
public var transition: StyleValue<AnimationSpec?> = StyleValue.Inherit169
public var period: Duration = Duration.seconds(2L)170
public var fonts: FontCollection? = None171
public var theme: ThemeSource? = None172
public var semantics: ComponentSemantics = ComponentSemantics()173
public var key: ViewKey = ViewKey.Position174
public fun label(text: String): Unit {175
label = Some(PanelContent.Label(LabelProps(LabelText.Plain(text, None))))176
}177
public fun label(text: StyledText): Unit {178
label = Some(PanelContent.Label(LabelProps(LabelText.Styled(text))))179
}180
public fun label(content: View): Unit {181
label = Some(PanelContent.Custom(content))182
}183
public fun valueLabel(text: String): Unit {184
valueLabel = Some(PanelContent.Label(LabelProps(LabelText.Plain(text, None))))185
}186
public fun valueLabel(text: StyledText): Unit {187
valueLabel = Some(PanelContent.Label(LabelProps(LabelText.Styled(text))))188
}189
public fun valueLabel(content: View): Unit {190
valueLabel = Some(PanelContent.Custom(content))191
}192
public fun style(configure: StyleOverrideBuilder.() -> Unit): Unit {193
style = style.copy(configure)194
}195
public fun transition(value: AnimationSpec?): Unit {196
transition = StyleValue.Set(value)197
}198
}200
public fun progress(value: Double?, configure: ProgressBuilder.() -> Unit = {}): View =201
makeProgress(value, configure)203
public fun progress(value: Double, configure: ProgressBuilder.() -> Unit = {}): View =204
makeProgress(Some(value), configure)206
internal fun makeProgress(value: Double?, configure: ProgressBuilder.() -> Unit): View {207
val builder = ProgressBuilder()208
builder.configure()209
return component(210
ProgressProps(211
value,212
builder.minimum,213
builder.maximum,214
builder.label,215
builder.valueLabel,216
builder.showValue,217
builder.thickness,218
builder.indicatorColor,219
builder.style,220
builder.transition,221
builder.period,222
builder.fonts,223
builder.theme,224
builder.semantics225
),226
{ current, mount -> Progress(current, scope = mount) },227
builder.key228
)229
}