storybook-nitroui-controls 0.2.0 → 0.2.2
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 +9 -1
- package/android/src/main/java/com/margelo/nitro/sbcontrols/ContentMeasure.kt +5 -4
- package/android/src/main/java/com/margelo/nitro/sbcontrols/HybridNativeCodeEditor.kt +103 -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/PickerIcon.kt +78 -0
- package/ios/HybridNativeCodeEditor.swift +86 -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 +125 -0
- 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 +101 -0
- package/nitrogen/generated/android/c++/include/StorybookNitroControls/private/JHybridNativeCodeEditorSpecJavaPart.hpp +27 -0
- 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 +112 -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 +89 -0
- package/nitrogen/generated/apple/c++/HybridNativeCodeEditorSpec_cxx-Swift.cpp +28 -0
- package/nitrogen/generated/apple/c++/include/StorybookNitroControls/private/HybridNativeCodeEditorSpecSwift.hpp +82 -0
- package/nitrogen/generated/apple/c++/include/StorybookNitroControls/private/HybridNativeCodeEditorSpec_cxx-Swift.hpp +23 -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 +71 -0
- package/nitrogen/generated/apple/swift/HybridNativeCodeEditorSpec_cxx.swift +199 -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 +1 -1
- package/src/components/CodeEditor.tsx +70 -0
- package/src/components/DatePicker.tsx +4 -2
- package/src/index.ts +1 -0
- package/src/specs/NativeCodeEditor.nitro.ts +25 -0
- package/src/specs/NativeDatePicker.nitro.ts +2 -2
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.
|
|
@@ -56,6 +56,9 @@ type Option = { label: string; value: string }
|
|
|
56
56
|
<ColorWell value={string /* #rrggbb or #rrggbbaa */} supportsAlpha?={boolean}
|
|
57
57
|
presets?={string[]} onValueChange={(v: string) => void} />
|
|
58
58
|
|
|
59
|
+
<CodeEditor text={string} onTextChange={(v: string) => void} textColor?={number /* 0xRRGGBBAA */}
|
|
60
|
+
fontSize?={number} disabled? />
|
|
61
|
+
|
|
59
62
|
pickFile(opts?: { types?: string[]; multiple?: boolean }):
|
|
60
63
|
Promise<{ uri: string; name: string; mimeType?: string; size?: number }[]>
|
|
61
64
|
```
|
|
@@ -93,6 +96,9 @@ Details:
|
|
|
93
96
|
- `ColorWell`: accepts `#rgb`, `#rgba`, `#rrggbb` and `#rrggbbaa`. The callback returns `#rrggbb` when
|
|
94
97
|
the picked color is opaque and the current value has no alpha digits, otherwise `#rrggbbaa`. Without
|
|
95
98
|
`supportsAlpha`, picked colors are made opaque. Preset swatches select a color on tap.
|
|
99
|
+
- `CodeEditor`: a multiline editor for JSON and code, with autocorrection, suggestions and smart
|
|
100
|
+
punctuation off. It has no background or border, so draw the frame around it. `text` is applied while
|
|
101
|
+
the editor is not focused; `onTextChange` reports user edits only.
|
|
96
102
|
- `pickFile`: resolves with an empty array when the user cancels.
|
|
97
103
|
|
|
98
104
|
## Sizing
|
|
@@ -115,6 +121,7 @@ Sizes:
|
|
|
115
121
|
| Slider | the offered width (200 when unconstrained) | intrinsic |
|
|
116
122
|
| MultiSelect, ColorWell | the offered width; chips and swatches wrap | grows with the rows |
|
|
117
123
|
| RadioGroup, CheckboxGroup | the offered width | one row per option |
|
|
124
|
+
| CodeEditor | the offered width | grows with the text; set `maxHeight` to cap it, and it scrolls inside |
|
|
118
125
|
|
|
119
126
|
Like any NitroUI view, a control in a column with the default `alignItems: 'stretch'` is stretched to the
|
|
120
127
|
column's width. Put it in a row, or set `alignSelf`, to keep the intrinsic width.
|
|
@@ -137,6 +144,7 @@ explicit `layout={{ width, height }}` until this package is updated.
|
|
|
137
144
|
| CheckboxGroup | plain `UIButton` rows with an SF Symbol mark (`square`, `checkmark.square.fill` when checked); iOS has no checkbox | M3 `MaterialCheckBox`es |
|
|
138
145
|
| DatePicker | `UIDatePicker`, compact style (calendar or time wheel in a popover) | tonal buttons with the date and time, opening `MaterialDatePicker` and `MaterialTimePicker` |
|
|
139
146
|
| 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 |
|
|
147
|
+
| CodeEditor | `UITextView` in Menlo | `EditText` in the `monospace` typeface |
|
|
140
148
|
| 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
149
|
|
|
142
150
|
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,103 @@
|
|
|
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.Gravity
|
|
10
|
+
import android.view.MotionEvent
|
|
11
|
+
import android.widget.EditText
|
|
12
|
+
import com.margelo.nitro.core.Callback
|
|
13
|
+
import com.margelo.nitro.renderer.AndroidHost
|
|
14
|
+
import com.margelo.nitro.renderer.NativeView
|
|
15
|
+
import com.margelo.nitro.renderer.ViewOperations
|
|
16
|
+
import com.margelo.nitro.renderer.ViewState
|
|
17
|
+
|
|
18
|
+
class HybridNativeCodeEditor
|
|
19
|
+
private constructor(
|
|
20
|
+
private val editor: EditText,
|
|
21
|
+
private val frame: ControlFrame,
|
|
22
|
+
override val nativeState: ViewState,
|
|
23
|
+
) : HybridNativeCodeEditorSpec(), NativeView, ViewOperations by nativeState {
|
|
24
|
+
private var listener: Callback<(text: String) -> Unit>? = null
|
|
25
|
+
private var updating = false
|
|
26
|
+
|
|
27
|
+
init {
|
|
28
|
+
nativeState.bind(this)
|
|
29
|
+
frame.show(editor, fill = true)
|
|
30
|
+
editor.addTextChangedListener(object : TextWatcher {
|
|
31
|
+
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
|
|
32
|
+
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
|
|
33
|
+
override fun afterTextChanged(s: Editable?) {
|
|
34
|
+
nativeState.layoutNode.markDirty()
|
|
35
|
+
if (!updating) listener.emit(s?.toString() ?: "")
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
override var text: String
|
|
41
|
+
get() = editor.text?.toString() ?: ""
|
|
42
|
+
set(value) {
|
|
43
|
+
if (text == value) return
|
|
44
|
+
updating = true
|
|
45
|
+
editor.setText(value)
|
|
46
|
+
updating = false
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
override val isFocused: Boolean
|
|
50
|
+
get() = editor.isFocused
|
|
51
|
+
|
|
52
|
+
override var textColor: Double = 0.0
|
|
53
|
+
set(value) {
|
|
54
|
+
field = value
|
|
55
|
+
editor.setTextColor(Rgba.toArgb(value))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
override var fontSize: Double = 13.0
|
|
59
|
+
set(value) {
|
|
60
|
+
field = value
|
|
61
|
+
editor.setTextSize(TypedValue.COMPLEX_UNIT_SP, value.toFloat())
|
|
62
|
+
nativeState.layoutNode.markDirty()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
override var enabled: Boolean = true
|
|
66
|
+
set(value) {
|
|
67
|
+
field = value
|
|
68
|
+
editor.isEnabled = value
|
|
69
|
+
editor.alpha = if (value) 1f else 0.5f
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
override fun setOnTextChange(listener: Callback<(text: String) -> Unit>?) {
|
|
73
|
+
this.listener = listener
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
companion object : HybridNativeCodeEditorSpec.Companion {
|
|
77
|
+
@SuppressLint("ClickableViewAccessibility")
|
|
78
|
+
override fun constructor(): HybridNativeCodeEditor {
|
|
79
|
+
val context = AndroidHost.context
|
|
80
|
+
val editor = EditText(context)
|
|
81
|
+
editor.background = null
|
|
82
|
+
editor.typeface = Typeface.MONOSPACE
|
|
83
|
+
editor.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13f)
|
|
84
|
+
editor.gravity = Gravity.TOP or Gravity.START
|
|
85
|
+
editor.inputType =
|
|
86
|
+
InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE or InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
|
|
87
|
+
editor.setHorizontallyScrolling(false)
|
|
88
|
+
editor.isVerticalScrollBarEnabled = true
|
|
89
|
+
editor.minLines = 1
|
|
90
|
+
editor.setPadding(context.dp(10f), context.dp(8f), context.dp(10f), context.dp(8f))
|
|
91
|
+
// Inside the scrolling addons panel: keep the drag while the editor itself can still scroll.
|
|
92
|
+
editor.setOnTouchListener { view, event ->
|
|
93
|
+
if (event.actionMasked == MotionEvent.ACTION_DOWN || event.actionMasked == MotionEvent.ACTION_MOVE) {
|
|
94
|
+
val scrollable = view.canScrollVertically(1) || view.canScrollVertically(-1)
|
|
95
|
+
view.parent?.requestDisallowInterceptTouchEvent(scrollable)
|
|
96
|
+
}
|
|
97
|
+
false
|
|
98
|
+
}
|
|
99
|
+
val frame = ControlFrame(context)
|
|
100
|
+
return HybridNativeCodeEditor(editor, frame, ViewState(frame, contentMeasure(editor, fillWidth = true)))
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
internal import NitroModules
|
|
2
|
+
internal import NitroUIRenderer
|
|
3
|
+
internal import UIKit
|
|
4
|
+
|
|
5
|
+
final class HybridNativeCodeEditor: HybridNativeCodeEditorSpec {
|
|
6
|
+
typealias AllowedChildrenType = Never
|
|
7
|
+
|
|
8
|
+
let view = UITextView()
|
|
9
|
+
let layoutNode = LayoutNode()
|
|
10
|
+
var parentReference: (any HybridViewSpec)?
|
|
11
|
+
var gestureRecognizers: [any HybridGestureRecognizerSpec] = []
|
|
12
|
+
|
|
13
|
+
private var measurer: LayoutMeasurer?
|
|
14
|
+
private var observer: NSObjectProtocol?
|
|
15
|
+
private var listener: (any Callback<(String), Void>)?
|
|
16
|
+
|
|
17
|
+
override init() throws {
|
|
18
|
+
try super.init()
|
|
19
|
+
view.backgroundColor = .clear
|
|
20
|
+
view.textContainerInset = UIEdgeInsets(top: 8, left: 10, bottom: 8, right: 10)
|
|
21
|
+
view.textContainer.lineFragmentPadding = 0
|
|
22
|
+
view.autocorrectionType = .no
|
|
23
|
+
view.autocapitalizationType = .none
|
|
24
|
+
view.spellCheckingType = .no
|
|
25
|
+
view.smartQuotesType = .no
|
|
26
|
+
view.smartDashesType = .no
|
|
27
|
+
view.smartInsertDeleteType = .no
|
|
28
|
+
view.keyboardType = .asciiCapable
|
|
29
|
+
view.alwaysBounceVertical = false
|
|
30
|
+
applyFont()
|
|
31
|
+
measurer = LayoutMeasurer(node: layoutNode.ref) { [unowned view] fitting in
|
|
32
|
+
let width = fitting.width.isFinite && fitting.width < .greatestFiniteMagnitude ? fitting.width : 240
|
|
33
|
+
let height = view.sizeThatFits(CGSize(width: width, height: .greatestFiniteMagnitude)).height
|
|
34
|
+
return CGSize(width: width, height: height)
|
|
35
|
+
}
|
|
36
|
+
observer = NotificationCenter.default.addObserver(
|
|
37
|
+
forName: UITextView.textDidChangeNotification, object: view, queue: .main
|
|
38
|
+
) { [weak self] _ in
|
|
39
|
+
guard let self else { return }
|
|
40
|
+
self.measurer?.invalidate()
|
|
41
|
+
self.listener?.emit(self.view.text ?? "")
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
deinit {
|
|
46
|
+
if let observer { NotificationCenter.default.removeObserver(observer) }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
var text: String {
|
|
50
|
+
get { view.text ?? "" }
|
|
51
|
+
set {
|
|
52
|
+
guard view.text != newValue else { return }
|
|
53
|
+
view.text = newValue
|
|
54
|
+
measurer?.invalidate()
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
var isFocused: Bool { view.isFirstResponder }
|
|
59
|
+
|
|
60
|
+
var textColor: Double = 0x000000ff {
|
|
61
|
+
didSet { view.textColor = UIColor(packedRGBA: textColor) }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
var fontSize: Double = 13 {
|
|
65
|
+
didSet {
|
|
66
|
+
applyFont()
|
|
67
|
+
measurer?.invalidate()
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
var enabled: Bool = true {
|
|
72
|
+
didSet {
|
|
73
|
+
view.isEditable = enabled
|
|
74
|
+
view.alpha = enabled ? 1 : 0.5
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private func applyFont() {
|
|
79
|
+
let size = CGFloat(fontSize)
|
|
80
|
+
view.font = UIFont(name: "Menlo", size: size) ?? .monospacedSystemFont(ofSize: size, weight: .regular)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
func setOnTextChange(listener: (any Callback<(String), Void>)?) throws {
|
|
84
|
+
self.listener = listener
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -15,10 +15,12 @@ target_sources(StorybookNitroControls PRIVATE
|
|
|
15
15
|
"${CMAKE_CURRENT_LIST_DIR}/StorybookNitroControlsOnLoad.cpp"
|
|
16
16
|
"${CMAKE_CURRENT_LIST_DIR}/../android/c++/JCallback_void_bool.cpp"
|
|
17
17
|
"${CMAKE_CURRENT_LIST_DIR}/../android/c++/JCallback_void_double.cpp"
|
|
18
|
+
"${CMAKE_CURRENT_LIST_DIR}/../android/c++/JCallback_void_std__string.cpp"
|
|
18
19
|
"${CMAKE_CURRENT_LIST_DIR}/../android/c++/JCallback_void_std__vector_bool_.cpp"
|
|
19
20
|
"${CMAKE_CURRENT_LIST_DIR}/../android/c++/JDatePickerMode.cpp"
|
|
20
21
|
"${CMAKE_CURRENT_LIST_DIR}/../android/c++/JHybridFilePickerSpec.cpp"
|
|
21
22
|
"${CMAKE_CURRENT_LIST_DIR}/../android/c++/JHybridNativeChoiceListSpec.cpp"
|
|
23
|
+
"${CMAKE_CURRENT_LIST_DIR}/../android/c++/JHybridNativeCodeEditorSpec.cpp"
|
|
22
24
|
"${CMAKE_CURRENT_LIST_DIR}/../android/c++/JHybridNativeColorWellSpec.cpp"
|
|
23
25
|
"${CMAKE_CURRENT_LIST_DIR}/../android/c++/JHybridNativeDatePickerSpec.cpp"
|
|
24
26
|
"${CMAKE_CURRENT_LIST_DIR}/../android/c++/JHybridNativeMultiSelectSpec.cpp"
|
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
#include <StorybookNitroControls/private/JHybridFilePickerSpec.hpp>
|
|
11
11
|
#include <StorybookNitroControls/private/JHybridNativeChoiceListSpec.hpp>
|
|
12
12
|
#include <StorybookNitroControls/private/JCallback_void_std__vector_bool_.hpp>
|
|
13
|
+
#include <StorybookNitroControls/private/JHybridNativeCodeEditorSpec.hpp>
|
|
14
|
+
#include <StorybookNitroControls/private/JCallback_void_std__string.hpp>
|
|
13
15
|
#include <StorybookNitroControls/private/JHybridNativeColorWellSpec.hpp>
|
|
14
16
|
#include <StorybookNitroControls/private/JCallback_void_double.hpp>
|
|
15
17
|
#include <StorybookNitroControls/private/JHybridNativeDatePickerSpec.hpp>
|
|
@@ -26,6 +28,8 @@ namespace margelo::nitro::sbcontrols {
|
|
|
26
28
|
margelo::nitro::sbcontrols::JHybridFilePickerSpec::CxxPart::registerNatives();
|
|
27
29
|
margelo::nitro::sbcontrols::JHybridNativeChoiceListSpec::CxxPart::registerNatives();
|
|
28
30
|
margelo::nitro::sbcontrols::JCallback_void_std__vector_bool_::registerNatives();
|
|
31
|
+
margelo::nitro::sbcontrols::JHybridNativeCodeEditorSpec::CxxPart::registerNatives();
|
|
32
|
+
margelo::nitro::sbcontrols::JCallback_void_std__string::registerNatives();
|
|
29
33
|
margelo::nitro::sbcontrols::JHybridNativeColorWellSpec::CxxPart::registerNatives();
|
|
30
34
|
margelo::nitro::sbcontrols::JCallback_void_double::registerNatives();
|
|
31
35
|
margelo::nitro::sbcontrols::JHybridNativeDatePickerSpec::CxxPart::registerNatives();
|
|
@@ -39,6 +43,7 @@ namespace margelo::nitro::sbcontrols {
|
|
|
39
43
|
|
|
40
44
|
HybridClassRegistry::registerHybridClass("sbcontrols.FilePicker", JHybridFilePickerSpec::Class::get());
|
|
41
45
|
HybridClassRegistry::registerHybridClass("sbcontrols.NativeChoiceList", JHybridNativeChoiceListSpec::Class::get());
|
|
46
|
+
HybridClassRegistry::registerHybridClass("sbcontrols.NativeCodeEditor", JHybridNativeCodeEditorSpec::Class::get());
|
|
42
47
|
HybridClassRegistry::registerHybridClass("sbcontrols.NativeColorWell", JHybridNativeColorWellSpec::Class::get());
|
|
43
48
|
HybridClassRegistry::registerHybridClass("sbcontrols.NativeDatePicker", JHybridNativeDatePickerSpec::Class::get());
|
|
44
49
|
HybridClassRegistry::registerHybridClass("sbcontrols.NativeMultiSelect", JHybridNativeMultiSelectSpec::Class::get());
|