Scoop API

scoop:scoop.ui.component:0.1.0

src/otp/style.scoop

Selected production source · src/otp/style.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.style.*
7public class InputOtpStyle internal constructor(
8 public val cellSize: Size? = None,
9 public val gap: Double = 8.0,
10 public val surface: StyleOverride = styleOverride(),
11 public val text: SpanStyle? = None,
12 public val focusOutline: OutlineStyle? = None,
13 public val validation: OutlineStyle? = None,
14 public val selectionColor: Color? = None,
15 public val inactiveSelectionColor: Color? = None,
16 public val caretColor: Color? = None,
17 public val markedColor: Color? = None,
18 public val caretWidth: Double? = None,
19 /** Uses the theme pair by default; a local selection background keeps original glyph colors. */
20 public val selectionTextColor: Color? = None,
21 public val inactiveSelectionTextColor: Color? = None
22) {
23 init {
24 nonnegative(gap, "invalid OTP gap")
25 when (caretWidth) {
26 case Some(val value) -> requireComponent(
27 value.isFinite() && value > 0.0,
28 "invalid OTP caret width"
29 )
30 case None -> {}
31 }
32 when (cellSize) {
33 case Some(val size) -> requireComponent(
34 size.width.isFinite() &&
35 size.height.isFinite() &&
36 size.width >
37 0.0 &&
38 size.height >
39 0.0,
40 "invalid OTP cell size"
41 )
42 case None -> {}
43 }
44 }
45 public fun copy(configure: InputOtpStyleBuilder.() -> Unit = {}): InputOtpStyle {
46 val builder = InputOtpStyleBuilder(this)
47 builder.configure()
48 return builder.snapshot()
49 }
52public fun inputOtpStyle(configure: InputOtpStyleBuilder.() -> Unit = {}): InputOtpStyle =
53 InputOtpStyle().copy(configure)
55public class InputOtpStyleBuilder internal constructor(initial: InputOtpStyle) {
56 public var cellSize: Size? = initial.cellSize
57 public var gap: Double = initial.gap
58 public var surface: StyleOverride = initial.surface
59 public var text: SpanStyle? = initial.text
60 public var focusOutline: OutlineStyle? = initial.focusOutline
61 public var validation: OutlineStyle? = initial.validation
62 public var selectionColor: Color? = initial.selectionColor
63 public var inactiveSelectionColor: Color? = initial.inactiveSelectionColor
64 public var selectionTextColor: Color? = initial.selectionTextColor
65 public var inactiveSelectionTextColor: Color? = initial.inactiveSelectionTextColor
66 public var caretColor: Color? = initial.caretColor
67 public var markedColor: Color? = initial.markedColor
68 public var caretWidth: Double? = initial.caretWidth
69 public fun surface(configure: StyleOverrideBuilder.() -> Unit) {
70 surface = surface.copy(configure)
71 }
72 public fun text(configure: SpanStyleBuilder.() -> Unit) {
73 text = Some(configureOptionalSpanStyle(text, configure))
74 }
75 internal fun snapshot(): InputOtpStyle =
76 InputOtpStyle(
77 cellSize,
78 gap,
79 surface,
80 text,
81 focusOutline,
82 validation,
83 selectionColor,
84 inactiveSelectionColor,
85 caretColor,
86 markedColor,
87 caretWidth,
88 selectionTextColor,
89 inactiveSelectionTextColor
90 )
93internal fun otpOptions(
94 theme: Theme,
95 style: InputOtpStyle,
96 visual: InteractionVisual
97): SurfaceOptions {
98 val options =
99 surfaceOptions {
100 this.focus = style.focusOutline;
101 this.validation = style.validation
102 }
103 val color = controlBorder(theme, options, visual, theme.tokens.palette.input)
104 val base =
105 styleOverride {
106 this.background(
107 Some(
108 Paint(
109 if (theme.preferences.highContrast || theme.preferences.reduceTransparency)
110 theme.tokens.palette.background.background
111 else
112 if (theme.tokens.mode == ThemeMode.Dark)
113 theme.tokens.palette.input.withAlpha(
114 theme.tokens.palette.input.alpha * 0.3
115 )
116 else
117 Color.Transparent
118 )
119 )
120 )
121 this.border(Some(Border.all(1.0, color)))
122 this.corners(Corners.all(theme.tokens.corners.medium))
123 this.boxShadows([])
124 }
125 return options.copy {
126 this.style = base.then(style.surface)
127 this.padding = Some(Insets.all(0.0))
128 this.minimumSize = Some(Size(0.0, 0.0))
129 }