storybook-nitroui-controls 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -6
- package/android/src/main/java/com/margelo/nitro/sbcontrols/ContentMeasure.kt +5 -4
- package/android/src/main/java/com/margelo/nitro/sbcontrols/HybridNativeCodeEditor.kt +144 -0
- package/android/src/main/java/com/margelo/nitro/sbcontrols/HybridNativeDatePicker.kt +119 -45
- package/android/src/main/java/com/margelo/nitro/sbcontrols/HybridNativeSegmentedControl.kt +53 -2
- package/android/src/main/java/com/margelo/nitro/sbcontrols/HybridNativeSlider.kt +6 -0
- package/android/src/main/java/com/margelo/nitro/sbcontrols/HybridNativeSwitch.kt +6 -0
- package/android/src/main/java/com/margelo/nitro/sbcontrols/PickerIcon.kt +78 -0
- package/demo/ControlsDemo.tsx +5 -5
- package/ios/HybridNativeCodeEditor.swift +94 -0
- package/ios/HybridNativeSlider.swift +4 -0
- package/ios/HybridNativeSwitch.swift +5 -0
- package/nitrogen/generated/android/StorybookNitroControls+autolinking.cmake +2 -0
- package/nitrogen/generated/android/StorybookNitroControlsOnLoad.cpp +5 -0
- package/nitrogen/generated/android/c++/JCallback_void_std__string.cpp +56 -0
- package/nitrogen/generated/android/c++/JHybridNativeCodeEditorSpec.cpp +135 -0
- package/nitrogen/generated/android/c++/JHybridNativeSliderSpec.cpp +14 -2
- package/nitrogen/generated/android/c++/JHybridNativeSwitchSpec.cpp +14 -2
- package/nitrogen/generated/android/c++/include/StorybookNitroControls/private/JCallback_void_std__string.hpp +55 -0
- package/nitrogen/generated/android/c++/include/StorybookNitroControls/private/JHybridNativeCodeEditorSpec.hpp +103 -0
- package/nitrogen/generated/android/c++/include/StorybookNitroControls/private/JHybridNativeCodeEditorSpecJavaPart.hpp +27 -0
- package/nitrogen/generated/android/c++/include/StorybookNitroControls/private/JHybridNativeSliderSpec.hpp +6 -2
- package/nitrogen/generated/android/c++/include/StorybookNitroControls/private/JHybridNativeSwitchSpec.hpp +6 -2
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/sbcontrols/Callback_void_std__string.kt +69 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/sbcontrols/HybridNativeCodeEditorSpec.kt +118 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/sbcontrols/HybridNativeSliderSpec.kt +6 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/sbcontrols/HybridNativeSwitchSpec.kt +6 -0
- package/nitrogen/generated/apple/StorybookNitroControls-Swift-Cxx-Bridge.cpp +43 -0
- package/nitrogen/generated/apple/StorybookNitroControlsAppleCxx/include/StorybookNitroControlsAppleCxx/StorybookNitroControlsAppleCxx.h +17 -0
- package/nitrogen/generated/apple/StorybookNitroControlsAutolinking.cpp +2 -0
- package/nitrogen/generated/apple/c++/HybridNativeCodeEditorSpecSwift.cpp +98 -0
- package/nitrogen/generated/apple/c++/HybridNativeCodeEditorSpec_cxx-Swift.cpp +28 -0
- package/nitrogen/generated/apple/c++/HybridNativeSliderSpecSwift.cpp +9 -0
- package/nitrogen/generated/apple/c++/HybridNativeSwitchSpecSwift.cpp +9 -0
- package/nitrogen/generated/apple/c++/include/StorybookNitroControls/private/HybridNativeCodeEditorSpecSwift.hpp +84 -0
- package/nitrogen/generated/apple/c++/include/StorybookNitroControls/private/HybridNativeCodeEditorSpec_cxx-Swift.hpp +23 -0
- package/nitrogen/generated/apple/c++/include/StorybookNitroControls/private/HybridNativeSliderSpecSwift.hpp +2 -0
- package/nitrogen/generated/apple/c++/include/StorybookNitroControls/private/HybridNativeSwitchSpecSwift.hpp +2 -0
- package/nitrogen/generated/apple/c++/include/StorybookNitroControls/private/StorybookNitroControls-Swift-Cxx-Umbrella.hpp +2 -0
- package/nitrogen/generated/apple/swift/Callback_void_swift__String.swift +66 -0
- package/nitrogen/generated/apple/swift/HybridNativeCodeEditorSpec.swift +72 -0
- package/nitrogen/generated/apple/swift/HybridNativeCodeEditorSpec_cxx.swift +208 -0
- package/nitrogen/generated/apple/swift/HybridNativeSliderSpec.swift +1 -0
- package/nitrogen/generated/apple/swift/HybridNativeSliderSpec_cxx.swift +9 -0
- package/nitrogen/generated/apple/swift/HybridNativeSwitchSpec.swift +1 -0
- package/nitrogen/generated/apple/swift/HybridNativeSwitchSpec_cxx.swift +9 -0
- package/nitrogen/generated/apple/swift/StorybookNitroControls+ForeignTypeMetadata.swift +7 -0
- package/nitrogen/generated/hybrid-classes.json +14 -0
- package/nitrogen/generated/shared/javascript/NativeCodeEditor.cjs +19 -0
- package/nitrogen/generated/shared/javascript/NativeCodeEditor.d.cts +9 -0
- package/package.json +15 -4
- package/src/components/CodeEditor.tsx +77 -0
- package/src/components/DatePicker.tsx +4 -2
- package/src/components/Slider.tsx +6 -1
- package/src/components/Switch.tsx +6 -3
- package/src/index.ts +1 -0
- package/src/specs/NativeCodeEditor.nitro.ts +27 -0
- package/src/specs/NativeDatePicker.nitro.ts +2 -2
- package/src/specs/NativeSlider.nitro.ts +2 -0
- package/src/specs/NativeSwitch.nitro.ts +2 -0
- package/types/demo/ControlsDemo.d.ts +3 -0
- package/types/demo/ControlsDemo.d.ts.map +1 -0
- package/types/src/Option.d.ts +6 -0
- package/types/src/Option.d.ts.map +1 -0
- package/types/src/components/ChoiceList.d.ts +45 -0
- package/types/src/components/ChoiceList.d.ts.map +1 -0
- package/types/src/components/CodeEditor.d.ts +37 -0
- package/types/src/components/CodeEditor.d.ts.map +1 -0
- package/types/src/components/ColorWell.d.ts +39 -0
- package/types/src/components/ColorWell.d.ts.map +1 -0
- package/types/src/components/DatePicker.d.ts +33 -0
- package/types/src/components/DatePicker.d.ts.map +1 -0
- package/types/src/components/MultiSelect.d.ts +37 -0
- package/types/src/components/MultiSelect.d.ts.map +1 -0
- package/types/src/components/SegmentedControl.d.ts +29 -0
- package/types/src/components/SegmentedControl.d.ts.map +1 -0
- package/types/src/components/SelectMenu.d.ts +32 -0
- package/types/src/components/SelectMenu.d.ts.map +1 -0
- package/types/src/components/Slider.d.ts +36 -0
- package/types/src/components/Slider.d.ts.map +1 -0
- package/types/src/components/Stepper.d.ts +34 -0
- package/types/src/components/Stepper.d.ts.map +1 -0
- package/types/src/components/Switch.d.ts +26 -0
- package/types/src/components/Switch.d.ts.map +1 -0
- package/types/src/index.d.ts +16 -0
- package/types/src/index.d.ts.map +1 -0
- package/types/src/pickFile.d.ts +16 -0
- package/types/src/pickFile.d.ts.map +1 -0
- package/types/src/specs/DatePickerMode.d.ts +2 -0
- package/types/src/specs/DatePickerMode.d.ts.map +1 -0
- package/types/src/specs/FilePicker.nitro.d.ts +13 -0
- package/types/src/specs/FilePicker.nitro.d.ts.map +1 -0
- package/types/src/specs/MultiSelectPresentation.d.ts +3 -0
- package/types/src/specs/MultiSelectPresentation.d.ts.map +1 -0
- package/types/src/specs/NativeChoiceList.nitro.d.ts +18 -0
- package/types/src/specs/NativeChoiceList.nitro.d.ts.map +1 -0
- package/types/src/specs/NativeCodeEditor.nitro.d.ts +23 -0
- package/types/src/specs/NativeCodeEditor.nitro.d.ts.map +1 -0
- package/types/src/specs/NativeColorWell.nitro.d.ts +17 -0
- package/types/src/specs/NativeColorWell.nitro.d.ts.map +1 -0
- package/types/src/specs/NativeDatePicker.nitro.d.ts +16 -0
- package/types/src/specs/NativeDatePicker.nitro.d.ts.map +1 -0
- package/types/src/specs/NativeMultiSelect.nitro.d.ts +18 -0
- package/types/src/specs/NativeMultiSelect.nitro.d.ts.map +1 -0
- package/types/src/specs/NativeSegmentedControl.nitro.d.ts +12 -0
- package/types/src/specs/NativeSegmentedControl.nitro.d.ts.map +1 -0
- package/types/src/specs/NativeSelectMenu.nitro.d.ts +13 -0
- package/types/src/specs/NativeSelectMenu.nitro.d.ts.map +1 -0
- package/types/src/specs/NativeSlider.nitro.d.ts +19 -0
- package/types/src/specs/NativeSlider.nitro.d.ts.map +1 -0
- package/types/src/specs/NativeStepper.nitro.d.ts +18 -0
- package/types/src/specs/NativeStepper.nitro.d.ts.map +1 -0
- package/types/src/specs/NativeSwitch.nitro.d.ts +13 -0
- package/types/src/specs/NativeSwitch.nitro.d.ts.map +1 -0
- package/types/src/specs/PickFileOptions.d.ts +6 -0
- package/types/src/specs/PickFileOptions.d.ts.map +1 -0
- package/types/src/specs/PickedFile.d.ts +9 -0
- package/types/src/specs/PickedFile.d.ts.map +1 -0
- package/types/src/utils/hexColor.d.ts +5 -0
- package/types/src/utils/hexColor.d.ts.map +1 -0
- package/types/src/utils/optionIndex.d.ts +4 -0
- package/types/src/utils/optionIndex.d.ts.map +1 -0
- package/types/src/utils/useLabels.d.ts +4 -0
- package/types/src/utils/useLabels.d.ts.map +1 -0
- package/types/src/utils/useNativeControl.d.ts +30 -0
- package/types/src/utils/useNativeControl.d.ts.map +1 -0
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# storybook-nitroui-controls
|
|
2
2
|
|
|
3
3
|
Native controls for [NitroUI](https://nitro-ui.com) apps: switch, slider, stepper, segmented control,
|
|
4
|
-
select menu, multi-select, radio and checkbox groups, date picker, color well and a file picker. Each component renders the platform
|
|
4
|
+
select menu, multi-select, radio and checkbox groups, date picker, color well, a monospace code editor and a file picker. Each component renders the platform
|
|
5
5
|
widget (UIKit on iOS, Material 3 on Android) as a Nitro view, and sizes itself.
|
|
6
6
|
|
|
7
7
|
Storybook for NitroUI uses them in its controls panel, but they work in any NitroUI 0.3.0 app.
|
|
@@ -26,15 +26,16 @@ user's value while the owner answers, so an owner that updates `value` asynchron
|
|
|
26
26
|
update after the story re-renders) does not make it jump back first. If the owner does not accept a change
|
|
27
27
|
(`value` still differs 1 s after the last user change), the native widget is set back to `value`, like a
|
|
28
28
|
controlled React DOM input. All values are plain JS values. Every component also accepts the shared NitroUI
|
|
29
|
-
view props (`layout`, `style`, `gestureRecognizers`) and `disabled`.
|
|
29
|
+
view props (`layout`, `style`, `gestureRecognizers`) and `disabled`. `Switch` and `Slider` take an
|
|
30
|
+
`accessibilityLabel` for VoiceOver, TalkBack and UI automation, since nothing else names them.
|
|
30
31
|
|
|
31
32
|
```ts
|
|
32
33
|
type Option = { label: string; value: string }
|
|
33
34
|
|
|
34
|
-
<Switch value={boolean} onValueChange={(v: boolean) => void} disabled? />
|
|
35
|
+
<Switch value={boolean} onValueChange={(v: boolean) => void} disabled? accessibilityLabel?={string} />
|
|
35
36
|
|
|
36
37
|
<Slider value={number} min={number} max={number} step?={number}
|
|
37
|
-
onValueChange={(v: number) => void} onSlidingComplete?={(v: number) => void} />
|
|
38
|
+
onValueChange={(v: number) => void} onSlidingComplete?={(v: number) => void} accessibilityLabel?={string} />
|
|
38
39
|
|
|
39
40
|
<Stepper value={number} min?={number} max?={number} step?={number} onValueChange={(v: number) => void} />
|
|
40
41
|
|
|
@@ -56,6 +57,9 @@ type Option = { label: string; value: string }
|
|
|
56
57
|
<ColorWell value={string /* #rrggbb or #rrggbbaa */} supportsAlpha?={boolean}
|
|
57
58
|
presets?={string[]} onValueChange={(v: string) => void} />
|
|
58
59
|
|
|
60
|
+
<CodeEditor text={string} onTextChange={(v: string) => void} textColor?={number /* 0xRRGGBBAA */}
|
|
61
|
+
fontSize?={number} disabled? readOnly? />
|
|
62
|
+
|
|
59
63
|
pickFile(opts?: { types?: string[]; multiple?: boolean }):
|
|
60
64
|
Promise<{ uri: string; name: string; mimeType?: string; size?: number }[]>
|
|
61
65
|
```
|
|
@@ -69,8 +73,8 @@ function Example() {
|
|
|
69
73
|
const [volume, setVolume] = useState(40)
|
|
70
74
|
return (
|
|
71
75
|
<>
|
|
72
|
-
<Switch value={on} onValueChange={setOn} />
|
|
73
|
-
<Slider value={volume} min={0} max={100} step={5} onValueChange={setVolume} />
|
|
76
|
+
<Switch value={on} onValueChange={setOn} accessibilityLabel="Notifications" />
|
|
77
|
+
<Slider value={volume} min={0} max={100} step={5} onValueChange={setVolume} accessibilityLabel="Volume" />
|
|
74
78
|
</>
|
|
75
79
|
)
|
|
76
80
|
}
|
|
@@ -93,6 +97,10 @@ Details:
|
|
|
93
97
|
- `ColorWell`: accepts `#rgb`, `#rgba`, `#rrggbb` and `#rrggbbaa`. The callback returns `#rrggbb` when
|
|
94
98
|
the picked color is opaque and the current value has no alpha digits, otherwise `#rrggbbaa`. Without
|
|
95
99
|
`supportsAlpha`, picked colors are made opaque. Preset swatches select a color on tap.
|
|
100
|
+
- `CodeEditor`: a multiline editor for JSON and code, with autocorrection, suggestions and smart
|
|
101
|
+
punctuation off. It has no background or border, so draw the frame around it. `text` is applied while
|
|
102
|
+
the editor is not focused; `onTextChange` reports user edits only. `readOnly` shows text to read,
|
|
103
|
+
select and copy, at full opacity and without a keyboard (Cut and Paste are left out of the menu).
|
|
96
104
|
- `pickFile`: resolves with an empty array when the user cancels.
|
|
97
105
|
|
|
98
106
|
## Sizing
|
|
@@ -115,6 +123,7 @@ Sizes:
|
|
|
115
123
|
| Slider | the offered width (200 when unconstrained) | intrinsic |
|
|
116
124
|
| MultiSelect, ColorWell | the offered width; chips and swatches wrap | grows with the rows |
|
|
117
125
|
| RadioGroup, CheckboxGroup | the offered width | one row per option |
|
|
126
|
+
| CodeEditor | the offered width | grows with the text; set `maxHeight` to cap it, and it scrolls inside |
|
|
118
127
|
|
|
119
128
|
Like any NitroUI view, a control in a column with the default `alignItems: 'stretch'` is stretched to the
|
|
120
129
|
column's width. Put it in a row, or set `alignSelf`, to keep the intrinsic width.
|
|
@@ -137,6 +146,7 @@ explicit `layout={{ width, height }}` until this package is updated.
|
|
|
137
146
|
| CheckboxGroup | plain `UIButton` rows with an SF Symbol mark (`square`, `checkmark.square.fill` when checked); iOS has no checkbox | M3 `MaterialCheckBox`es |
|
|
138
147
|
| DatePicker | `UIDatePicker`, compact style (calendar or time wheel in a popover) | tonal buttons with the date and time, opening `MaterialDatePicker` and `MaterialTimePicker` |
|
|
139
148
|
| ColorWell | `UIColorWell` (system picker: grid, spectrum, sliders, hex, eyedropper, opacity) plus preset swatches | our own picker in a `MaterialAlertDialog` (saturation and brightness square, hue and alpha tracks, hex field, presets) plus preset swatches; Android has no system color picker |
|
|
149
|
+
| CodeEditor | `UITextView` in Menlo | `EditText` in the `monospace` typeface |
|
|
140
150
|
| pickFile | `PHPickerViewController` when every type is `image/*` or `video/*`, otherwise `UIDocumentPickerViewController`. Files are copied into the temporary directory and returned as `file://` URLs | Photo Picker for image or video types, otherwise the system document picker (`OpenDocument`). Returns `content://` URIs and takes a persistable read grant when the provider allows it |
|
|
141
151
|
|
|
142
152
|
Other notes:
|
|
@@ -6,14 +6,15 @@ import com.margelo.nitro.renderer.LayoutNode
|
|
|
6
6
|
/**
|
|
7
7
|
* Measures a control's content view for the NitroUI layout engine, in points.
|
|
8
8
|
* `ViewState(view, measure)` is public in NitroUI 0.3.0, so Android controls size themselves.
|
|
9
|
-
* With [fillWidth] the control takes the width it is offered
|
|
9
|
+
* With [fillWidth] the control takes the width it is offered. When unconstrained it takes
|
|
10
|
+
* [defaultWidth], or its own natural width when [defaultWidth] is null.
|
|
10
11
|
*/
|
|
11
|
-
internal fun contentMeasure(content: View, fillWidth: Boolean = false, defaultWidth: Float = 200f): LayoutNode.Measure =
|
|
12
|
+
internal fun contentMeasure(content: View, fillWidth: Boolean = false, defaultWidth: Float? = 200f): LayoutNode.Measure =
|
|
12
13
|
LayoutNode.Measure { width, widthMode, height, heightMode ->
|
|
13
14
|
val density = content.resources.displayMetrics.density
|
|
14
15
|
val widthSpec =
|
|
15
|
-
if (fillWidth) {
|
|
16
|
-
val points = if (widthMode == MODE_UNDEFINED) defaultWidth else width
|
|
16
|
+
if (fillWidth && (widthMode != MODE_UNDEFINED || defaultWidth != null)) {
|
|
17
|
+
val points = if (widthMode == MODE_UNDEFINED) defaultWidth ?: 0f else width
|
|
17
18
|
View.MeasureSpec.makeMeasureSpec(pixels(points, density), View.MeasureSpec.EXACTLY)
|
|
18
19
|
} else {
|
|
19
20
|
measureSpec(width, widthMode, density)
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
package com.margelo.nitro.sbcontrols
|
|
2
|
+
|
|
3
|
+
import android.annotation.SuppressLint
|
|
4
|
+
import android.graphics.Typeface
|
|
5
|
+
import android.text.Editable
|
|
6
|
+
import android.text.InputType
|
|
7
|
+
import android.text.TextWatcher
|
|
8
|
+
import android.util.TypedValue
|
|
9
|
+
import android.view.ActionMode
|
|
10
|
+
import android.view.Menu
|
|
11
|
+
import android.view.MenuItem
|
|
12
|
+
import android.view.Gravity
|
|
13
|
+
import android.view.MotionEvent
|
|
14
|
+
import android.widget.EditText
|
|
15
|
+
import com.margelo.nitro.core.Callback
|
|
16
|
+
import com.margelo.nitro.renderer.AndroidHost
|
|
17
|
+
import com.margelo.nitro.renderer.NativeView
|
|
18
|
+
import com.margelo.nitro.renderer.ViewOperations
|
|
19
|
+
import com.margelo.nitro.renderer.ViewState
|
|
20
|
+
|
|
21
|
+
class HybridNativeCodeEditor
|
|
22
|
+
private constructor(
|
|
23
|
+
private val editor: EditText,
|
|
24
|
+
private val frame: ControlFrame,
|
|
25
|
+
override val nativeState: ViewState,
|
|
26
|
+
) : HybridNativeCodeEditorSpec(), NativeView, ViewOperations by nativeState {
|
|
27
|
+
private var listener: Callback<(text: String) -> Unit>? = null
|
|
28
|
+
private var updating = false
|
|
29
|
+
|
|
30
|
+
init {
|
|
31
|
+
nativeState.bind(this)
|
|
32
|
+
frame.show(editor, fill = true)
|
|
33
|
+
editor.addTextChangedListener(object : TextWatcher {
|
|
34
|
+
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
|
|
35
|
+
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
|
|
36
|
+
override fun afterTextChanged(s: Editable?) {
|
|
37
|
+
nativeState.layoutNode.markDirty()
|
|
38
|
+
if (!updating) listener.emit(s?.toString() ?: "")
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
override var text: String
|
|
44
|
+
get() = editor.text?.toString() ?: ""
|
|
45
|
+
set(value) {
|
|
46
|
+
if (text == value) return
|
|
47
|
+
updating = true
|
|
48
|
+
editor.setText(value)
|
|
49
|
+
updating = false
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
override val isFocused: Boolean
|
|
53
|
+
get() = editor.isFocused
|
|
54
|
+
|
|
55
|
+
override var textColor: Double = 0.0
|
|
56
|
+
set(value) {
|
|
57
|
+
field = value
|
|
58
|
+
editor.setTextColor(Rgba.toArgb(value))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
override var fontSize: Double = 13.0
|
|
62
|
+
set(value) {
|
|
63
|
+
field = value
|
|
64
|
+
editor.setTextSize(TypedValue.COMPLEX_UNIT_SP, value.toFloat())
|
|
65
|
+
nativeState.layoutNode.markDirty()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
override var enabled: Boolean = true
|
|
69
|
+
set(value) {
|
|
70
|
+
field = value
|
|
71
|
+
editor.isEnabled = value
|
|
72
|
+
editor.alpha = if (value) 1f else 0.5f
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
override var readOnly: Boolean = false
|
|
76
|
+
set(value) {
|
|
77
|
+
if (field == value) return
|
|
78
|
+
field = value
|
|
79
|
+
// TYPE_NULL keeps the keyboard away; text stays selectable (long press, Copy). TYPE_NULL makes
|
|
80
|
+
// the view single-line, so multi-line wrapping is restored after it.
|
|
81
|
+
editor.inputType = if (value) InputType.TYPE_NULL else EDITABLE_INPUT
|
|
82
|
+
editor.isSingleLine = false
|
|
83
|
+
editor.setHorizontallyScrolling(false)
|
|
84
|
+
editor.setTextIsSelectable(value)
|
|
85
|
+
editor.showSoftInputOnFocus = !value
|
|
86
|
+
// The selection menu would still offer Cut and Paste on an EditText; only Copy, Share and Select all stay.
|
|
87
|
+
editor.customSelectionActionModeCallback = if (value) ReadOnlySelection else null
|
|
88
|
+
editor.customInsertionActionModeCallback = if (value) ReadOnlySelection else null
|
|
89
|
+
// setTextIsSelectable(false) also clears these.
|
|
90
|
+
if (!value) {
|
|
91
|
+
editor.isFocusableInTouchMode = true
|
|
92
|
+
editor.isFocusable = true
|
|
93
|
+
editor.isClickable = true
|
|
94
|
+
editor.isLongClickable = true
|
|
95
|
+
}
|
|
96
|
+
nativeState.layoutNode.markDirty()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
override fun setOnTextChange(listener: Callback<(text: String) -> Unit>?) {
|
|
100
|
+
this.listener = listener
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private object ReadOnlySelection : ActionMode.Callback {
|
|
104
|
+
override fun onCreateActionMode(mode: ActionMode, menu: Menu) = true
|
|
105
|
+
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
|
|
106
|
+
menu.removeItem(android.R.id.cut)
|
|
107
|
+
menu.removeItem(android.R.id.paste)
|
|
108
|
+
menu.removeItem(android.R.id.pasteAsPlainText)
|
|
109
|
+
return true
|
|
110
|
+
}
|
|
111
|
+
override fun onActionItemClicked(mode: ActionMode, item: MenuItem) = false
|
|
112
|
+
override fun onDestroyActionMode(mode: ActionMode) = Unit
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
companion object : HybridNativeCodeEditorSpec.Companion {
|
|
116
|
+
private const val EDITABLE_INPUT =
|
|
117
|
+
InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE or InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
|
|
118
|
+
|
|
119
|
+
@SuppressLint("ClickableViewAccessibility")
|
|
120
|
+
override fun constructor(): HybridNativeCodeEditor {
|
|
121
|
+
val context = AndroidHost.context
|
|
122
|
+
val editor = EditText(context)
|
|
123
|
+
editor.background = null
|
|
124
|
+
editor.typeface = Typeface.MONOSPACE
|
|
125
|
+
editor.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13f)
|
|
126
|
+
editor.gravity = Gravity.TOP or Gravity.START
|
|
127
|
+
editor.inputType = EDITABLE_INPUT
|
|
128
|
+
editor.setHorizontallyScrolling(false)
|
|
129
|
+
editor.isVerticalScrollBarEnabled = true
|
|
130
|
+
editor.minLines = 1
|
|
131
|
+
editor.setPadding(context.dp(10f), context.dp(8f), context.dp(10f), context.dp(8f))
|
|
132
|
+
// Inside the scrolling addons panel: keep the drag while the editor itself can still scroll.
|
|
133
|
+
editor.setOnTouchListener { view, event ->
|
|
134
|
+
if (event.actionMasked == MotionEvent.ACTION_DOWN || event.actionMasked == MotionEvent.ACTION_MOVE) {
|
|
135
|
+
val scrollable = view.canScrollVertically(1) || view.canScrollVertically(-1)
|
|
136
|
+
view.parent?.requestDisallowInterceptTouchEvent(scrollable)
|
|
137
|
+
}
|
|
138
|
+
false
|
|
139
|
+
}
|
|
140
|
+
val frame = ControlFrame(context)
|
|
141
|
+
return HybridNativeCodeEditor(editor, frame, ViewState(frame, contentMeasure(editor, fillWidth = true)))
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
package com.margelo.nitro.sbcontrols
|
|
2
2
|
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.text.TextUtils
|
|
3
5
|
import android.text.format.DateFormat
|
|
4
|
-
import android.
|
|
6
|
+
import android.view.View
|
|
7
|
+
import com.google.android.material.button.MaterialButton
|
|
5
8
|
import com.google.android.material.datepicker.MaterialDatePicker
|
|
6
9
|
import com.google.android.material.timepicker.MaterialTimePicker
|
|
7
10
|
import com.google.android.material.timepicker.TimeFormat
|
|
@@ -13,30 +16,19 @@ import com.margelo.nitro.renderer.ViewState
|
|
|
13
16
|
import java.util.Calendar
|
|
14
17
|
import java.util.TimeZone
|
|
15
18
|
|
|
16
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* One tonal button showing the formatted value. Date mode opens the M3 date picker, time mode the
|
|
21
|
+
* M3 time picker, and date-and-time mode the date picker followed by the time picker, because
|
|
22
|
+
* Material has no combined date-time picker.
|
|
23
|
+
*/
|
|
17
24
|
class HybridNativeDatePicker
|
|
18
25
|
private constructor(
|
|
19
|
-
private val
|
|
26
|
+
private val button: FittingButton,
|
|
20
27
|
private val frame: ControlFrame,
|
|
21
28
|
override val nativeState: ViewState,
|
|
22
29
|
) : HybridNativeDatePickerSpec(), NativeView, ViewOperations by nativeState {
|
|
23
30
|
private var listener: Callback<(value: Double) -> Unit>? = null
|
|
24
|
-
private
|
|
25
|
-
private val timeButton = tonalButton(control.context, "")
|
|
26
|
-
|
|
27
|
-
init {
|
|
28
|
-
nativeState.bind(this)
|
|
29
|
-
frame.show(control, fill = false)
|
|
30
|
-
control.orientation = LinearLayout.HORIZONTAL
|
|
31
|
-
val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
|
|
32
|
-
control.addView(dateButton, params)
|
|
33
|
-
val timeParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
|
|
34
|
-
timeParams.marginStart = control.context.dp(8f)
|
|
35
|
-
control.addView(timeButton, timeParams)
|
|
36
|
-
dateButton.setOnClickListener { openDatePicker() }
|
|
37
|
-
timeButton.setOnClickListener { openTimePicker() }
|
|
38
|
-
refresh()
|
|
39
|
-
}
|
|
31
|
+
private var iconIsClock: Boolean? = null
|
|
40
32
|
|
|
41
33
|
override var value: Double = 0.0
|
|
42
34
|
set(value) {
|
|
@@ -53,23 +45,55 @@ private constructor(
|
|
|
53
45
|
override var enabled: Boolean = true
|
|
54
46
|
set(value) {
|
|
55
47
|
field = value
|
|
56
|
-
|
|
57
|
-
timeButton.isEnabled = value
|
|
48
|
+
button.isEnabled = value
|
|
58
49
|
}
|
|
59
50
|
|
|
51
|
+
// After the properties above, which refresh() reads.
|
|
52
|
+
init {
|
|
53
|
+
nativeState.bind(this)
|
|
54
|
+
frame.show(button, fill = false)
|
|
55
|
+
button.setOnClickListener { open() }
|
|
56
|
+
refresh()
|
|
57
|
+
}
|
|
58
|
+
|
|
60
59
|
private fun calendar(): Calendar = Calendar.getInstance().apply { timeInMillis = value.toLong() }
|
|
61
60
|
|
|
62
61
|
private fun refresh() {
|
|
63
|
-
val context =
|
|
62
|
+
val context = button.context
|
|
64
63
|
val date = calendar().time
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
64
|
+
val time = DateFormat.getTimeFormat(context).format(date)
|
|
65
|
+
val medium = DateFormat.getMediumDateFormat(context).format(date)
|
|
66
|
+
val short = DateFormat.getDateFormat(context).format(date)
|
|
67
|
+
button.labels =
|
|
68
|
+
when (mode) {
|
|
69
|
+
DatePickerMode.DATE -> listOf(medium, short)
|
|
70
|
+
DatePickerMode.TIME -> listOf(time)
|
|
71
|
+
DatePickerMode.DATETIME -> listOf(joined(medium, time), joined(short, time))
|
|
72
|
+
}
|
|
73
|
+
button.contentDescription = button.labels.first()
|
|
74
|
+
val clock = mode == DatePickerMode.TIME
|
|
75
|
+
if (clock != iconIsClock) {
|
|
76
|
+
iconIsClock = clock
|
|
77
|
+
button.icon = PickerIcon(clock)
|
|
78
|
+
}
|
|
79
|
+
button.requestLayout()
|
|
69
80
|
nativeState.layoutNode.markDirty()
|
|
70
81
|
}
|
|
71
82
|
|
|
72
|
-
|
|
83
|
+
/** "May 12, 2024, 10:00 AM": the comma is how Android's own date-time formats join the two. */
|
|
84
|
+
private fun joined(date: String, time: String) = "$date, $time"
|
|
85
|
+
|
|
86
|
+
private fun open() {
|
|
87
|
+
when (mode) {
|
|
88
|
+
DatePickerMode.DATE -> pickDate { commit(it) }
|
|
89
|
+
DatePickerMode.TIME -> pickTime(calendar()) { commit(it) }
|
|
90
|
+
// Cancelling the time step still keeps the picked date, with the time unchanged.
|
|
91
|
+
DatePickerMode.DATETIME -> pickDate { withDate -> pickTime(withDate, onCancel = { commit(it) }) { commit(it) } }
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Opens the date picker; [onPick] gets the current value moved to the picked day, time unchanged. */
|
|
96
|
+
private fun pickDate(onPick: (Calendar) -> Unit) {
|
|
73
97
|
val local = calendar()
|
|
74
98
|
// MaterialDatePicker selections are UTC midnights.
|
|
75
99
|
val utc = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
|
|
@@ -78,34 +102,36 @@ private constructor(
|
|
|
78
102
|
val picker = MaterialDatePicker.Builder.datePicker().setSelection(utc.timeInMillis).build()
|
|
79
103
|
picker.addOnPositiveButtonClickListener { selection ->
|
|
80
104
|
utc.timeInMillis = selection
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
commit(next.timeInMillis.toDouble())
|
|
105
|
+
local.set(utc.get(Calendar.YEAR), utc.get(Calendar.MONTH), utc.get(Calendar.DAY_OF_MONTH))
|
|
106
|
+
onPick(local)
|
|
84
107
|
}
|
|
85
108
|
picker.show(AndroidHost.activity.supportFragmentManager, "storybook-date-picker")
|
|
86
109
|
}
|
|
87
110
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
val format = if (DateFormat.is24HourFormat(
|
|
111
|
+
/** Opens the time picker seeded with [base]; [onPick] gets [base] at the picked time, [onCancel] gets [base]. */
|
|
112
|
+
private fun pickTime(base: Calendar, onCancel: (Calendar) -> Unit = {}, onPick: (Calendar) -> Unit) {
|
|
113
|
+
val format = if (DateFormat.is24HourFormat(button.context)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H
|
|
91
114
|
val picker = MaterialTimePicker.Builder()
|
|
92
115
|
.setTimeFormat(format)
|
|
93
|
-
.setHour(
|
|
94
|
-
.setMinute(
|
|
116
|
+
.setHour(base.get(Calendar.HOUR_OF_DAY))
|
|
117
|
+
.setMinute(base.get(Calendar.MINUTE))
|
|
95
118
|
.build()
|
|
119
|
+
var picked = false
|
|
96
120
|
picker.addOnPositiveButtonClickListener {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
121
|
+
picked = true
|
|
122
|
+
base.set(Calendar.HOUR_OF_DAY, picker.hour)
|
|
123
|
+
base.set(Calendar.MINUTE, picker.minute)
|
|
124
|
+
onPick(base)
|
|
101
125
|
}
|
|
126
|
+
picker.addOnDismissListener { if (!picked) onCancel(base) }
|
|
102
127
|
picker.show(AndroidHost.activity.supportFragmentManager, "storybook-time-picker")
|
|
103
128
|
}
|
|
104
129
|
|
|
105
|
-
private fun commit(next:
|
|
106
|
-
|
|
107
|
-
value
|
|
108
|
-
|
|
130
|
+
private fun commit(next: Calendar) {
|
|
131
|
+
val millis = next.timeInMillis.toDouble()
|
|
132
|
+
if (millis == value) return
|
|
133
|
+
value = millis
|
|
134
|
+
listener.emit(millis)
|
|
109
135
|
}
|
|
110
136
|
|
|
111
137
|
override fun setOnValueChange(listener: Callback<(value: Double) -> Unit>?) {
|
|
@@ -115,9 +141,57 @@ private constructor(
|
|
|
115
141
|
companion object : HybridNativeDatePickerSpec.Companion {
|
|
116
142
|
override fun constructor(): HybridNativeDatePicker {
|
|
117
143
|
val context = AndroidHost.context
|
|
118
|
-
val
|
|
144
|
+
val button = FittingButton(context)
|
|
119
145
|
val frame = ControlFrame(context)
|
|
120
|
-
return HybridNativeDatePicker(
|
|
146
|
+
return HybridNativeDatePicker(button, frame, ViewState(frame, contentMeasure(button)))
|
|
121
147
|
}
|
|
122
148
|
}
|
|
123
149
|
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* A tonal button with a leading icon that shows the first of [labels] fitting on one line in the
|
|
153
|
+
* width it is offered, dropping the icon before moving on to a shorter label. It never wraps.
|
|
154
|
+
*/
|
|
155
|
+
internal class FittingButton(context: Context) : MaterialButton(context) {
|
|
156
|
+
var labels: List<String> = listOf("")
|
|
157
|
+
|
|
158
|
+
init {
|
|
159
|
+
val template = tonalButton(context, "")
|
|
160
|
+
backgroundTintList = template.backgroundTintList
|
|
161
|
+
setTextColor(template.textColors)
|
|
162
|
+
insetTop = 0
|
|
163
|
+
insetBottom = 0
|
|
164
|
+
iconTint = template.textColors
|
|
165
|
+
iconSize = context.dp(18f)
|
|
166
|
+
iconPadding = context.dp(8f)
|
|
167
|
+
iconGravity = ICON_GRAVITY_TEXT_START
|
|
168
|
+
isSingleLine = true
|
|
169
|
+
ellipsize = TextUtils.TruncateAt.END
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private var iconDrawable = icon
|
|
173
|
+
|
|
174
|
+
override fun setIcon(icon: android.graphics.drawable.Drawable?) {
|
|
175
|
+
iconDrawable = icon
|
|
176
|
+
super.setIcon(icon)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
|
180
|
+
val available =
|
|
181
|
+
if (View.MeasureSpec.getMode(widthMeasureSpec) == View.MeasureSpec.UNSPECIFIED) Int.MAX_VALUE
|
|
182
|
+
else View.MeasureSpec.getSize(widthMeasureSpec)
|
|
183
|
+
// Each label with the icon, then without it, before falling back to the next, shorter label.
|
|
184
|
+
val candidates = labels.flatMap { listOf(it to true, it to false) }
|
|
185
|
+
val (label, showIcon) =
|
|
186
|
+
candidates.firstOrNull { (label, icon) -> paint.measureText(label) + chrome(icon) <= available } ?: candidates.last()
|
|
187
|
+
if (text?.toString() != label) text = label
|
|
188
|
+
if (showIcon != (icon != null)) super.setIcon(if (showIcon) iconDrawable else null)
|
|
189
|
+
// M3 button padding: 24dp on both sides, or 16dp before a leading icon.
|
|
190
|
+
setPaddingRelative(context.dp(if (showIcon) 16f else 24f), paddingTop, context.dp(24f), paddingBottom)
|
|
191
|
+
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Horizontal space besides the label: padding, and the icon with its gap. */
|
|
195
|
+
private fun chrome(withIcon: Boolean): Int =
|
|
196
|
+
if (withIcon) context.dp(16f + 24f) + iconSize + iconPadding else context.dp(24f + 24f)
|
|
197
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
package com.margelo.nitro.sbcontrols
|
|
2
2
|
|
|
3
|
+
import android.content.Context
|
|
3
4
|
import android.view.View
|
|
4
5
|
import android.widget.LinearLayout
|
|
5
6
|
import com.google.android.material.button.MaterialButton
|
|
@@ -63,6 +64,9 @@ private constructor(
|
|
|
63
64
|
button.text = label
|
|
64
65
|
button.isEnabled = enabled
|
|
65
66
|
button.maxLines = 1
|
|
67
|
+
// Material's 88dp/48dp minimum width would override the equal-width segments.
|
|
68
|
+
button.minWidth = 0
|
|
69
|
+
button.minimumWidth = 0
|
|
66
70
|
control.addView(button, LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f))
|
|
67
71
|
buttons.add(button)
|
|
68
72
|
}
|
|
@@ -89,9 +93,56 @@ private constructor(
|
|
|
89
93
|
companion object : HybridNativeSegmentedControlSpec.Companion {
|
|
90
94
|
override fun constructor(): HybridNativeSegmentedControl {
|
|
91
95
|
val context = AndroidHost.context
|
|
92
|
-
val control =
|
|
96
|
+
val control = SegmentedGroup(context)
|
|
93
97
|
val frame = ControlFrame(context)
|
|
94
|
-
|
|
98
|
+
// Fill the width offered so the segments share it; take the natural width when unconstrained.
|
|
99
|
+
return HybridNativeSegmentedControl(control, frame, ViewState(frame, contentMeasure(control, fillWidth = true, defaultWidth = null)))
|
|
95
100
|
}
|
|
96
101
|
}
|
|
97
102
|
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* A toggle group whose equal-width segments fit their labels. Material's outlined buttons pad
|
|
106
|
+
* their text by 24dp a side, which leaves almost no room for text in a narrow segment, so the
|
|
107
|
+
* labels were ellipsized. Before measuring, the padding shrinks (down to [MIN_PADDING_DP]) to fit
|
|
108
|
+
* the widest label; if it still doesn't fit, labels wrap onto a second line instead of truncating.
|
|
109
|
+
*/
|
|
110
|
+
private class SegmentedGroup(context: Context) : MaterialButtonToggleGroup(context) {
|
|
111
|
+
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
|
112
|
+
if (View.MeasureSpec.getMode(widthMeasureSpec) != View.MeasureSpec.UNSPECIFIED) {
|
|
113
|
+
fitLabels(View.MeasureSpec.getSize(widthMeasureSpec))
|
|
114
|
+
} else {
|
|
115
|
+
setButtonPadding(MAX_PADDING_DP, lines = 1)
|
|
116
|
+
}
|
|
117
|
+
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private fun fitLabels(width: Int) {
|
|
121
|
+
val buttons = (0 until childCount).map { getChildAt(it) }.filterIsInstance<MaterialButton>()
|
|
122
|
+
if (buttons.isEmpty()) return
|
|
123
|
+
val density = resources.displayMetrics.density
|
|
124
|
+
val segment = (width - paddingLeft - paddingRight) / buttons.size.toFloat()
|
|
125
|
+
val text = buttons.maxOf { it.paint.measureText(it.text.toString()) }
|
|
126
|
+
val stroke = buttons.maxOf { it.strokeWidth }.toFloat()
|
|
127
|
+
// Room either side of the widest label, less the borders and a pixel of rounding slack.
|
|
128
|
+
val room = (segment - text - 2 * stroke - 2) / 2 / density
|
|
129
|
+
if (room >= MIN_PADDING_DP) setButtonPadding(room.coerceAtMost(MAX_PADDING_DP), lines = 1)
|
|
130
|
+
else setButtonPadding(MIN_PADDING_DP, lines = 2)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private fun setButtonPadding(dp: Float, lines: Int) {
|
|
134
|
+
val pixels = (dp * resources.displayMetrics.density).toInt()
|
|
135
|
+
for (index in 0 until childCount) {
|
|
136
|
+
val button = getChildAt(index) as? MaterialButton ?: continue
|
|
137
|
+
if (button.paddingLeft != pixels || button.paddingRight != pixels) {
|
|
138
|
+
button.setPadding(pixels, button.paddingTop, pixels, button.paddingBottom)
|
|
139
|
+
}
|
|
140
|
+
if (button.maxLines != lines) button.maxLines = lines
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private companion object {
|
|
145
|
+
const val MIN_PADDING_DP = 4f
|
|
146
|
+
const val MAX_PADDING_DP = 16f
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -51,6 +51,12 @@ private constructor(
|
|
|
51
51
|
override var minimumValue: Double = 0.0
|
|
52
52
|
override var maximumValue: Double = 1.0
|
|
53
53
|
override var step: Double = 0.0
|
|
54
|
+
override var accessibilityLabel: String?
|
|
55
|
+
get() = control.contentDescription?.toString()
|
|
56
|
+
set(value) {
|
|
57
|
+
control.contentDescription = value
|
|
58
|
+
}
|
|
59
|
+
|
|
54
60
|
override var enabled: Boolean
|
|
55
61
|
get() = control.isEnabled
|
|
56
62
|
set(value) {
|
|
@@ -39,6 +39,12 @@ private constructor(
|
|
|
39
39
|
control.isEnabled = value
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
override var accessibilityLabel: String?
|
|
43
|
+
get() = control.contentDescription?.toString()
|
|
44
|
+
set(value) {
|
|
45
|
+
control.contentDescription = value
|
|
46
|
+
}
|
|
47
|
+
|
|
42
48
|
override fun setOnValueChange(listener: Callback<(value: Boolean) -> Unit>?) {
|
|
43
49
|
this.listener = listener
|
|
44
50
|
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
package com.margelo.nitro.sbcontrols
|
|
2
|
+
|
|
3
|
+
import android.content.res.ColorStateList
|
|
4
|
+
import android.graphics.Canvas
|
|
5
|
+
import android.graphics.ColorFilter
|
|
6
|
+
import android.graphics.Paint
|
|
7
|
+
import android.graphics.PixelFormat
|
|
8
|
+
import android.graphics.RectF
|
|
9
|
+
import android.graphics.drawable.Drawable
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The calendar or clock mark on the date picker button, drawn in code: Material ships these icons
|
|
13
|
+
* only as private resources, which could be renamed in any release. Drawn on a 24-unit grid like
|
|
14
|
+
* Material icons, and tinted by the button's `iconTint`.
|
|
15
|
+
*/
|
|
16
|
+
internal class PickerIcon(private val clock: Boolean) : Drawable() {
|
|
17
|
+
private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
|
18
|
+
style = Paint.Style.STROKE
|
|
19
|
+
strokeCap = Paint.Cap.ROUND
|
|
20
|
+
strokeJoin = Paint.Join.ROUND
|
|
21
|
+
}
|
|
22
|
+
private var tint: ColorStateList? = null
|
|
23
|
+
private val rect = RectF()
|
|
24
|
+
|
|
25
|
+
override fun draw(canvas: Canvas) {
|
|
26
|
+
val unit = bounds.width() / 24f
|
|
27
|
+
canvas.save()
|
|
28
|
+
canvas.translate(bounds.left.toFloat(), bounds.top.toFloat())
|
|
29
|
+
canvas.scale(unit, unit)
|
|
30
|
+
paint.strokeWidth = 2f
|
|
31
|
+
if (clock) {
|
|
32
|
+
canvas.drawCircle(12f, 12f, 9f, paint)
|
|
33
|
+
canvas.drawLine(12f, 7f, 12f, 12f, paint)
|
|
34
|
+
canvas.drawLine(12f, 12f, 15.5f, 14f, paint)
|
|
35
|
+
} else {
|
|
36
|
+
rect.set(3f, 5f, 21f, 21f)
|
|
37
|
+
canvas.drawRoundRect(rect, 2f, 2f, paint)
|
|
38
|
+
canvas.drawLine(3f, 10f, 21f, 10f, paint)
|
|
39
|
+
canvas.drawLine(8f, 3f, 8f, 6f, paint)
|
|
40
|
+
canvas.drawLine(16f, 3f, 16f, 6f, paint)
|
|
41
|
+
}
|
|
42
|
+
canvas.restore()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
override fun setTintList(tint: ColorStateList?) {
|
|
46
|
+
this.tint = tint
|
|
47
|
+
updateColor()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
override fun isStateful() = tint?.isStateful == true
|
|
51
|
+
|
|
52
|
+
override fun onStateChange(state: IntArray): Boolean = updateColor()
|
|
53
|
+
|
|
54
|
+
private fun updateColor(): Boolean {
|
|
55
|
+
val color = tint?.getColorForState(state, tint!!.defaultColor) ?: return false
|
|
56
|
+
if (color == paint.color) return false
|
|
57
|
+
paint.color = color
|
|
58
|
+
invalidateSelf()
|
|
59
|
+
return true
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
override fun getIntrinsicWidth() = -1
|
|
63
|
+
|
|
64
|
+
override fun getIntrinsicHeight() = -1
|
|
65
|
+
|
|
66
|
+
override fun setAlpha(alpha: Int) {
|
|
67
|
+
paint.alpha = alpha
|
|
68
|
+
invalidateSelf()
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
override fun setColorFilter(colorFilter: ColorFilter?) {
|
|
72
|
+
paint.colorFilter = colorFilter
|
|
73
|
+
invalidateSelf()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
@Deprecated("Deprecated in Java")
|
|
77
|
+
override fun getOpacity() = PixelFormat.TRANSLUCENT
|
|
78
|
+
}
|