Scoop API

scoop:scoop.ui.component:0.1.0

src/content/progress.scoop

Selected production source · src/content/progress.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.math.floor
6import scoop.surface.*
7import scoop.time.Duration
8import scoop.ui.*
9import scoop.ui.component.column as progressColumn
10import scoop.ui.style.*
11import scoop.window.accessibility.*
13public 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 }
39internal 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 -> None
45 }
48public class Progress public constructor(
49 private val props: Props<ProgressProps>,
50 scope: ComponentScope
51): 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.value
60 val theme = themes.read(input.theme)
61 val palette = theme.tokens.palette
62 val density = theme.tokens.typography.body.fontSize / 14.0
63 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.fonts
78 )
79 )
80 else
81 None
82 }
83 val labelCaption =
84 when (input.label) {
85 case Some(val value) -> Some(panelCaption(value, labelStyle, input.fonts))
86 case None -> None
87 }
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.period
101 ),
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.0
114 ),
115 0L
116 )
117 )
118 this.add(panelSlot(track, 1L))
119 this.spacing = 12.0 * density
120 this.alignment = CrossAlignment.Stretch
121 }
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) * position
131 )
132 )
133 case None -> None
134 }
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 -> None
141 }
142 }
143 return semanticContent(
144 content,
145 SemanticRole.ProgressBar,
146 facts,
147 relations = input.semantics.relations,
148 reference = input.semantics.reference
149 )
150 }
153internal 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 }
159public class ProgressBuilder internal constructor() {
160 public var minimum: Double = 0.0
161 public var maximum: Double = 100.0
162 public var label: PanelContent? = None
163 public var valueLabel: PanelContent? = None
164 public var showValue: Boolean = false
165 public var thickness: Double? = None
166 public var indicatorColor: Color? = None
167 public var style: StyleOverride = styleOverride()
168 public var transition: StyleValue<AnimationSpec?> = StyleValue.Inherit
169 public var period: Duration = Duration.seconds(2L)
170 public var fonts: FontCollection? = None
171 public var theme: ThemeSource? = None
172 public var semantics: ComponentSemantics = ComponentSemantics()
173 public var key: ViewKey = ViewKey.Position
174 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 }
200public fun progress(value: Double?, configure: ProgressBuilder.() -> Unit = {}): View =
201 makeProgress(value, configure)
203public fun progress(value: Double, configure: ProgressBuilder.() -> Unit = {}): View =
204 makeProgress(Some(value), configure)
206internal 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.semantics
225 ),
226 { current, mount -> Progress(current, scope = mount) },
227 builder.key
228 )