Skip to content

Commit b76eac2

Browse files
committed
Add decorationBox
1 parent 35fe072 commit b76eac2

File tree

3 files changed

+81
-79
lines changed

3 files changed

+81
-79
lines changed

app/src/main/java/jp/kaleidot725/sample/MainActivity.kt

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ import android.os.Bundle
44
import androidx.activity.ComponentActivity
55
import androidx.activity.compose.setContent
66
import androidx.compose.foundation.background
7-
import androidx.compose.foundation.layout.Box
87
import androidx.compose.foundation.layout.Row
98
import androidx.compose.foundation.layout.Spacer
109
import androidx.compose.foundation.layout.fillMaxSize
10+
import androidx.compose.foundation.layout.fillMaxWidth
1111
import androidx.compose.foundation.layout.width
12+
import androidx.compose.material.Scaffold
1213
import androidx.compose.material.Text
14+
import androidx.compose.material.TopAppBar
15+
import androidx.compose.runtime.Composable
1316
import androidx.compose.runtime.getValue
14-
import androidx.compose.runtime.setValue
17+
import androidx.compose.ui.Alignment
1518
import androidx.compose.ui.Modifier
1619
import androidx.compose.ui.graphics.Color
1720
import androidx.compose.ui.unit.dp
@@ -27,61 +30,51 @@ class MainActivity : ComponentActivity() {
2730
val textEditorState by rememberTextEditorState(lines = DemoText.lines())
2831
TextEditor(
2932
textEditorState = textEditorState,
30-
onUpdatedState = { },
33+
onUpdatedState = { /** Save Action */ },
3134
modifier = Modifier.fillMaxSize()
32-
)
35+
) { index, isSelected, innerTextField ->
36+
val backgroundColor = if (isSelected) Color.Red else Color.White
37+
Row(modifier = Modifier.background(backgroundColor)) {
38+
Text(text = (index + 1).toString().padStart(3, '0'))
39+
Spacer(modifier = Modifier.width(4.dp))
40+
innerTextField(modifier = Modifier.fillMaxWidth())
41+
}
42+
}
3343
}
3444
}
3545
}
3646
}
3747

3848
private val DemoText = """
39-
アリスは川辺でおねえさんのよこにすわって、
40-
なんにもすることがないのでとても退屈(たいくつ)しはじめていました。
41-
一、二回はおねえさんの読んでいる本をのぞいてみたけれど、そこには絵も会話もないのです。
42-
「絵や会話のない本なんて、なんの役にもたたないじゃないの」とアリスは思いました。
43-
そこでアリスは、頭のなかで、ひなぎくのくさりをつくったら楽しいだろうけれど、
44-
起きあがってひなぎくをつむのもめんどくさいし、どうしようかと考えていました
45-
(といっても、昼間で暑いし、とってもねむくて頭もまわらなかったので、これもたいへんだったのですが)。
46-
そこへいきなり、ピンクの目をした白うさぎが近くを走ってきたのです。
47-
それだけなら、そんなにめずらしいことでもありませんでした。さらにアリスとしては、
48-
そのうさぎが「どうしよう! どうしよう! ちこくしちゃうぞ!」とつぶやくのを聞いたときも、
49-
それがそんなにへんてこだとは思いませんでした(あとから考えてみたら、これも不思議に思うべきだったのですけれど、
50-
でもこのときには、それがごく自然なことに思えたのです)。
51-
アリスは川辺でおねえさんのよこにすわって、
52-
なんにもすることがないのでとても退屈(たいくつ)しはじめていました。
53-
一、二回はおねえさんの読んでいる本をのぞいてみたけれど、そこには絵も会話もないのです。
54-
「絵や会話のない本なんて、なんの役にもたたないじゃないの」とアリスは思いました。
55-
そこでアリスは、頭のなかで、ひなぎくのくさりをつくったら楽しいだろうけれど、
56-
起きあがってひなぎくをつむのもめんどくさいし、どうしようかと考えていました
57-
(といっても、昼間で暑いし、とってもねむくて頭もまわらなかったので、これもたいへんだったのですが)。
58-
そこへいきなり、ピンクの目をした白うさぎが近くを走ってきたのです。
59-
それだけなら、そんなにめずらしいことでもありませんでした。さらにアリスとしては、
60-
そのうさぎが「どうしよう! どうしよう! ちこくしちゃうぞ!」とつぶやくのを聞いたときも、
61-
それがそんなにへんてこだとは思いませんでした(あとから考えてみたら、これも不思議に思うべきだったのですけれど、
62-
でもこのときには、それがごく自然なことに思えたのです)。
63-
アリスは川辺でおねえさんのよこにすわって、
64-
なんにもすることがないのでとても退屈(たいくつ)しはじめていました。
65-
一、二回はおねえさんの読んでいる本をのぞいてみたけれど、そこには絵も会話もないのです。
66-
「絵や会話のない本なんて、なんの役にもたたないじゃないの」とアリスは思いました。
67-
そこでアリスは、頭のなかで、ひなぎくのくさりをつくったら楽しいだろうけれど、
68-
起きあがってひなぎくをつむのもめんどくさいし、どうしようかと考えていました
69-
(といっても、昼間で暑いし、とってもねむくて頭もまわらなかったので、これもたいへんだったのですが)。
70-
そこへいきなり、ピンクの目をした白うさぎが近くを走ってきたのです。
71-
それだけなら、そんなにめずらしいことでもありませんでした。さらにアリスとしては、
72-
そのうさぎが「どうしよう! どうしよう! ちこくしちゃうぞ!」とつぶやくのを聞いたときも、
73-
それがそんなにへんてこだとは思いませんでした(あとから考えてみたら、これも不思議に思うべきだったのですけれど、
74-
でもこのときには、それがごく自然なことに思えたのです)。
75-
アリスは川辺でおねえさんのよこにすわって、
76-
なんにもすることがないのでとても退屈(たいくつ)しはじめていました。
77-
一、二回はおねえさんの読んでいる本をのぞいてみたけれど、そこには絵も会話もないのです。
78-
「絵や会話のない本なんて、なんの役にもたたないじゃないの」とアリスは思いました。
79-
そこでアリスは、頭のなかで、ひなぎくのくさりをつくったら楽しいだろうけれど、
80-
起きあがってひなぎくをつむのもめんどくさいし、どうしようかと考えていました
81-
(といっても、昼間で暑いし、とってもねむくて頭もまわらなかったので、これもたいへんだったのですが)。
82-
そこへいきなり、ピンクの目をした白うさぎが近くを走ってきたのです。
83-
それだけなら、そんなにめずらしいことでもありませんでした。さらにアリスとしては、
84-
そのうさぎが「どうしよう! どうしよう! ちこくしちゃうぞ!」とつぶやくのを聞いたときも、
85-
それがそんなにへんてこだとは思いませんでした(あとから考えてみたら、これも不思議に思うべきだったのですけれど、
86-
でもこのときには、それがごく自然なことに思えたのです)。
49+
Android Inc. was founded in Palo Alto, California,
50+
in October 2003 by Andy Rubin, Rich Miner, Nick Sears,
51+
and Chris White.[20][21] Rubin described the Android project as having "tremendous potential in
52+
developing smarter mobile devices that are more aware of its owner's location and preferences".[21]
53+
The early intentions of the company were to develop an advanced operating system for digital cameras,
54+
and this was the basis of its pitch to investors in April 2004.[22]
55+
The company then decided that the market for cameras was not large enough for its goals,
56+
and five months later it had diverted its efforts and was pitching Android as a handset operating system that would rival Symbian and Microsoft Windows Mobile.[22][23]
57+
Rubin had difficulty attracting investors early on, and Android was facing eviction from its office space.
58+
Steve Perlman, a close friend of Rubin, brought him ${'$'}10,000 in cash in an envelope,
59+
and shortly thereafter wired an undisclosed amount as seed funding.
60+
Perlman refused a stake in the company, and has stated "I did it because I believed in the thing, and I wanted to help Andy."[24][25]
61+
62+
In 2005, Rubin tried to negotiate deals with Samsung[26] and HTC.[27] Shortly afterwards,
63+
Google acquired the company in July of that year for at least ${'$'}50 million;[21][28]
64+
this was Google's "best deal ever" according to Google's then-vice president of corporate development,
65+
David Lawee, in 2010.[26] Android's key employees, including Rubin, Miner, Sears, and White,
66+
joined Google as part of the acquisition.[21] Not much was known about the secretive Android Inc. at the time,
67+
with the company having provided few details other than that it was making software for mobile phones.[21]
68+
At Google, the team led by Rubin developed a mobile device platform powered by the Linux kernel.
69+
Google marketed the platform to handset makers and carriers on the promise of providing a flexible, upgradeable system.[29]
70+
Google had "lined up a series of hardware components and software partners and signaled to carriers that it was open to various degrees of cooperation".[attribution needed][30]
71+
72+
Speculation about Google's intention to enter the mobile communications market continued to build through December 2006.
73+
[31] An early prototype had a close resemblance to a BlackBerry phone, with no touchscreen and a physical QWERTY keyboard,
74+
but the arrival of 2007's Apple iPhone meant that Android "had to go back to the drawing board".[32][33]
75+
Google later changed its Android specification documents to state that "Touchscreens will be supported",
76+
although "the Product was designed with the presence of discrete physical buttons as an assumption,
77+
therefore a touchscreen cannot completely replace physical buttons".[34] By 2008,
78+
both Nokia and BlackBerry announced touch-based smartphones to rival the iPhone 3G, and Android's focus eventually switched to just touchscreens.
79+
The first commercially available smartphone running Android was the HTC Dream, also known as T-Mobile G1, announced on September 23, 2008.[35][36]
8780
""".trimIndent()

texteditor/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ afterEvaluate {
6565
from components.release
6666
groupId = 'com.github.kaleidot725'
6767
artifactId = 'text-editor-compose'
68-
version = '0.1.0-beta03'
68+
version = '0.1.0'
6969
}
7070
}
7171
}

texteditor/src/main/java/jp/kaleidot725/texteditor/view/TextEditor.kt

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ import androidx.compose.ui.platform.LocalFocusManager
1414
import jp.kaleidot725.texteditor.state.EditableTextEditorState
1515
import jp.kaleidot725.texteditor.state.TextEditorState
1616

17+
typealias DecorationBoxComposable = @Composable (
18+
index: Int,
19+
isSelected: Boolean,
20+
innerTextField: @Composable (modifier: Modifier) -> Unit
21+
) -> Unit
22+
1723
@Composable
1824
fun TextEditor(
1925
textEditorState: TextEditorState,
2026
onUpdatedState: () -> Unit,
21-
modifier: Modifier = Modifier
27+
modifier: Modifier = Modifier,
28+
decorationBox: DecorationBoxComposable = { _, _, innerTextField -> innerTextField(Modifier) },
2229
) {
2330
LazyColumn(modifier = modifier) {
2431
itemsIndexed(
@@ -33,32 +40,34 @@ fun TextEditor(
3340
if (textFieldState.isSelected) focusRequester.requestFocus()
3441
}
3542

36-
TextField(
37-
textFieldValue = textFieldState.value,
38-
onUpdateText = { newText ->
39-
textEditorState.toEditable()
40-
.updateField(targetIndex = index, textFieldValue = newText)
41-
onUpdatedState()
42-
},
43-
onAddNewLine = { newText ->
44-
textEditorState.toEditable()
45-
.splitField(targetIndex = index, textFieldValue = newText)
46-
onUpdatedState()
47-
},
48-
onDeleteNewLine = {
49-
textEditorState.toEditable().deleteField(targetIndex = index)
50-
onUpdatedState()
43+
decorationBox(index, textFieldState.isSelected) {
44+
TextField(
45+
textFieldValue = textFieldState.value,
46+
onUpdateText = { newText ->
47+
textEditorState.toEditable()
48+
.updateField(targetIndex = index, textFieldValue = newText)
49+
onUpdatedState()
50+
},
51+
onAddNewLine = { newText ->
52+
textEditorState.toEditable()
53+
.splitField(targetIndex = index, textFieldValue = newText)
54+
onUpdatedState()
55+
},
56+
onDeleteNewLine = {
57+
textEditorState.toEditable().deleteField(targetIndex = index)
58+
onUpdatedState()
5159

52-
// workaround: prevent to hide ime when editor delete newline
53-
focusManager.moveFocus(FocusDirection.Up)
54-
},
55-
focusRequester = focusRequester,
56-
onFocus = {
57-
if (textEditorState.selectedIndices.contains(index)) return@TextField
58-
textEditorState.toEditable().selectField(targetIndex = index)
59-
onUpdatedState()
60-
}
61-
)
60+
// workaround: prevent to hide ime when editor delete newline
61+
focusManager.moveFocus(FocusDirection.Up)
62+
},
63+
focusRequester = focusRequester,
64+
onFocus = {
65+
if (textEditorState.selectedIndices.contains(index)) return@TextField
66+
textEditorState.toEditable().selectField(targetIndex = index)
67+
onUpdatedState()
68+
}
69+
)
70+
}
6271
}
6372
}
6473
}

0 commit comments

Comments
 (0)