@@ -9,7 +9,10 @@ import androidx.compose.foundation.layout.height
9
9
import androidx.compose.foundation.lazy.LazyColumn
10
10
import androidx.compose.foundation.lazy.itemsIndexed
11
11
import androidx.compose.foundation.lazy.rememberLazyListState
12
+ import androidx.compose.foundation.text.selection.LocalTextSelectionColors
13
+ import androidx.compose.foundation.text.selection.TextSelectionColors
12
14
import androidx.compose.runtime.Composable
15
+ import androidx.compose.runtime.CompositionLocalProvider
13
16
import androidx.compose.runtime.DisposableEffect
14
17
import androidx.compose.runtime.LaunchedEffect
15
18
import androidx.compose.runtime.getValue
@@ -19,6 +22,7 @@ import androidx.compose.runtime.rememberUpdatedState
19
22
import androidx.compose.runtime.setValue
20
23
import androidx.compose.ui.Modifier
21
24
import androidx.compose.ui.focus.FocusRequester
25
+ import androidx.compose.ui.graphics.Color
22
26
import androidx.compose.ui.unit.dp
23
27
import jp.kaleidot725.texteditor.controller.rememberTextEditorController
24
28
import jp.kaleidot725.texteditor.state.TextEditorState
@@ -30,6 +34,11 @@ typealias DecorationBoxComposable = @Composable (
30
34
innerTextField: @Composable (modifier: Modifier ) -> Unit
31
35
) -> Unit
32
36
37
+ val customTextSelectionColors = TextSelectionColors (
38
+ handleColor = Color .Transparent ,
39
+ backgroundColor = Color .Transparent ,
40
+ )
41
+
33
42
@Composable
34
43
fun TextEditor (
35
44
textEditorState : TextEditorState ,
@@ -59,91 +68,95 @@ fun TextEditor(
59
68
}
60
69
}
61
70
62
- LazyColumn (
63
- state = lazyColumnState,
64
- modifier = modifier,
65
- contentPadding = contentPaddingValues
71
+ CompositionLocalProvider (
72
+ LocalTextSelectionColors provides customTextSelectionColors,
66
73
) {
67
- itemsIndexed(
68
- items = textEditorState.fields,
69
- key = { _, item -> item.id }
70
- ) { index, textFieldState ->
71
- val focusRequester by remember { mutableStateOf(FocusRequester ()) }
74
+ LazyColumn (
75
+ state = lazyColumnState,
76
+ modifier = modifier,
77
+ contentPadding = contentPaddingValues
78
+ ) {
79
+ itemsIndexed(
80
+ items = textEditorState.fields,
81
+ key = { _, item -> item.id }
82
+ ) { index, textFieldState ->
83
+ val focusRequester by remember { mutableStateOf(FocusRequester ()) }
72
84
73
- DisposableEffect (Unit ) {
74
- focusRequesters[index] = focusRequester
75
- onDispose {
76
- focusRequesters.remove(index)
85
+ DisposableEffect (Unit ) {
86
+ focusRequesters[index] = focusRequester
87
+ onDispose {
88
+ focusRequesters.remove(index)
89
+ }
77
90
}
78
- }
79
91
80
- decorationBox(
81
- index = index,
82
- isSelected = textFieldState.isSelected,
83
- innerTextField = { modifier ->
84
- Box (
85
- modifier = modifier
86
- .clickable(
87
- interactionSource = remember { MutableInteractionSource () },
88
- indication = null
89
- ) {
90
- if (! textEditorState.isMultipleSelectionMode) return @clickable
91
- editableController.selectField(targetIndex = index)
92
- }
93
- ) {
94
- TextField (
95
- textFieldState = textFieldState,
96
- enabled = ! textEditorState.isMultipleSelectionMode,
97
- focusRequester = focusRequester,
98
- onUpdateText = { newText ->
99
- editableController.updateField(
100
- targetIndex = index,
101
- textFieldValue = newText
102
- )
103
- },
104
- onContainNewLine = { newText ->
105
- if (lastScrollEvent != null && lastScrollEvent?.isConsumed != true ) return @TextField
106
- editableController.splitNewLine(
107
- targetIndex = index,
108
- textFieldValue = newText
109
- )
110
- lastScrollEvent = ScrollEvent (index + 1 )
111
- },
112
- onAddNewLine = { newText ->
113
- if (lastScrollEvent != null && lastScrollEvent?.isConsumed != true ) return @TextField
114
- editableController.splitAtCursor(
115
- targetIndex = index,
116
- textFieldValue = newText
117
- )
118
- lastScrollEvent = ScrollEvent (index + 1 )
119
- },
120
- onDeleteNewLine = {
121
- if (lastScrollEvent != null && lastScrollEvent?.isConsumed != true ) return @TextField
122
- editableController.deleteField(targetIndex = index)
123
- if (index != 0 ) lastScrollEvent = ScrollEvent (index - 1 )
124
- },
125
- onFocus = {
126
- editableController.selectField(index)
127
- },
128
- onUpFocus = {
129
- if (lastScrollEvent != null && lastScrollEvent?.isConsumed != true ) return @TextField
130
- editableController.selectPreviousField()
131
- if (index != 0 ) lastScrollEvent = ScrollEvent (index - 1 )
132
- },
133
- onDownFocus = {
134
- if (lastScrollEvent != null && lastScrollEvent?.isConsumed != true ) return @TextField
135
- editableController.selectNextField()
136
- if (index != textEditorState.fields.lastIndex) lastScrollEvent =
137
- ScrollEvent (index + 1 )
138
- },
139
- )
92
+ decorationBox(
93
+ index = index,
94
+ isSelected = textFieldState.isSelected,
95
+ innerTextField = { modifier ->
96
+ Box (
97
+ modifier = modifier
98
+ .clickable(
99
+ interactionSource = remember { MutableInteractionSource () },
100
+ indication = null
101
+ ) {
102
+ if (! textEditorState.isMultipleSelectionMode) return @clickable
103
+ editableController.selectField(targetIndex = index)
104
+ }
105
+ ) {
106
+ TextField (
107
+ textFieldState = textFieldState,
108
+ enabled = ! textEditorState.isMultipleSelectionMode,
109
+ focusRequester = focusRequester,
110
+ onUpdateText = { newText ->
111
+ editableController.updateField(
112
+ targetIndex = index,
113
+ textFieldValue = newText
114
+ )
115
+ },
116
+ onContainNewLine = { newText ->
117
+ if (lastScrollEvent != null && lastScrollEvent?.isConsumed != true ) return @TextField
118
+ editableController.splitNewLine(
119
+ targetIndex = index,
120
+ textFieldValue = newText
121
+ )
122
+ lastScrollEvent = ScrollEvent (index + 1 )
123
+ },
124
+ onAddNewLine = { newText ->
125
+ if (lastScrollEvent != null && lastScrollEvent?.isConsumed != true ) return @TextField
126
+ editableController.splitAtCursor(
127
+ targetIndex = index,
128
+ textFieldValue = newText
129
+ )
130
+ lastScrollEvent = ScrollEvent (index + 1 )
131
+ },
132
+ onDeleteNewLine = {
133
+ if (lastScrollEvent != null && lastScrollEvent?.isConsumed != true ) return @TextField
134
+ editableController.deleteField(targetIndex = index)
135
+ if (index != 0 ) lastScrollEvent = ScrollEvent (index - 1 )
136
+ },
137
+ onFocus = {
138
+ editableController.selectField(index)
139
+ },
140
+ onUpFocus = {
141
+ if (lastScrollEvent != null && lastScrollEvent?.isConsumed != true ) return @TextField
142
+ editableController.selectPreviousField()
143
+ if (index != 0 ) lastScrollEvent = ScrollEvent (index - 1 )
144
+ },
145
+ onDownFocus = {
146
+ if (lastScrollEvent != null && lastScrollEvent?.isConsumed != true ) return @TextField
147
+ editableController.selectNextField()
148
+ if (index != textEditorState.fields.lastIndex) lastScrollEvent =
149
+ ScrollEvent (index + 1 )
150
+ },
151
+ )
152
+ }
140
153
}
141
- }
142
- )
143
- }
154
+ )
155
+ }
144
156
145
- item {
146
- Spacer (modifier = Modifier .height(100 .dp))
157
+ item {
158
+ Spacer (modifier = Modifier .height(100 .dp))
159
+ }
147
160
}
148
161
}
149
162
}
0 commit comments