Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to custom selected textStyle #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fun HoursNumberPicker(
onValueChange: (Hours) -> Unit,
dividersColor: Color = MaterialTheme.colors.primary,
textStyle: TextStyle = LocalTextStyle.current,
selectedTextStyle: TextStyle = LocalTextStyle.current,
) {
when (value) {
is FullHours ->
Expand All @@ -60,6 +61,7 @@ fun HoursNumberPicker(
onValueChange = onValueChange,
dividersColor = dividersColor,
textStyle = textStyle,
selectedTextStyle = selectedTextStyle,
)
is AMPMHours ->
AMPMHoursNumberPicker(
Expand All @@ -73,6 +75,7 @@ fun HoursNumberPicker(
onValueChange = onValueChange,
dividersColor = dividersColor,
textStyle = textStyle,
selectedTextStyle = selectedTextStyle,
)
}
}
Expand All @@ -89,6 +92,7 @@ fun FullHoursNumberPicker(
onValueChange: (Hours) -> Unit,
dividersColor: Color = MaterialTheme.colors.primary,
textStyle: TextStyle = LocalTextStyle.current,
selectedTextStyle: TextStyle = LocalTextStyle.current,
) {
Row(
modifier = modifier,
Expand All @@ -105,6 +109,7 @@ fun FullHoursNumberPicker(
},
dividersColor = dividersColor,
textStyle = textStyle,
selectedTextStyle = selectedTextStyle,
range = hoursRange
)

Expand All @@ -121,6 +126,7 @@ fun FullHoursNumberPicker(
},
dividersColor = dividersColor,
textStyle = textStyle,
selectedTextStyle = selectedTextStyle,
range = minutesRange
)

Expand All @@ -140,6 +146,7 @@ fun AMPMHoursNumberPicker(
onValueChange: (Hours) -> Unit,
dividersColor: Color = MaterialTheme.colors.primary,
textStyle: TextStyle = LocalTextStyle.current,
selectedTextStyle: TextStyle = LocalTextStyle.current,
) {
Row(
modifier = modifier,
Expand All @@ -156,6 +163,7 @@ fun AMPMHoursNumberPicker(
},
dividersColor = dividersColor,
textStyle = textStyle,
selectedTextStyle = selectedTextStyle,
range = hoursRange
)

Expand All @@ -172,6 +180,7 @@ fun AMPMHoursNumberPicker(
},
dividersColor = dividersColor,
textStyle = textStyle,
selectedTextStyle = selectedTextStyle,
range = minutesRange
)

Expand Down Expand Up @@ -200,7 +209,8 @@ fun AMPMHoursNumberPicker(
},
dividersColor = dividersColor,
textStyle = textStyle,
selectedTextStyle = selectedTextStyle,
range = (0..1)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ package com.chargemap.compose.numberpicker

import androidx.compose.animation.core.*
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.gestures.*
import androidx.compose.foundation.layout.*
import androidx.compose.material.LocalTextStyle
import androidx.compose.material.MaterialTheme
import androidx.compose.material.ProvideTextStyle
import androidx.compose.material.Text
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -46,6 +40,7 @@ fun <T> ListItemPicker(
dividersColor: Color = MaterialTheme.colors.primary,
list: List<T>,
textStyle: TextStyle = LocalTextStyle.current,
selectedTextStyle: TextStyle = LocalTextStyle.current,
) {
val minimumAlpha = 0.3f
val verticalMargin = 8.dp
Expand Down Expand Up @@ -117,32 +112,33 @@ fun <T> ListItemPicker(
.offset { IntOffset(x = 0, y = coercedAnimatedOffset.roundToInt()) }
) {
val baseLabelModifier = Modifier.align(Alignment.Center)
ProvideTextStyle(textStyle) {
if (indexOfElement > 0)
Label(
text = label(list.elementAt(indexOfElement - 1)),
modifier = baseLabelModifier
.offset(y = -halfNumbersColumnHeight)
.alpha(maxOf(minimumAlpha, coercedAnimatedOffset / halfNumbersColumnHeightPx))
)
if (indexOfElement > 0)
Label(
text = label(list.elementAt(indexOfElement)),
text = label(list.elementAt(indexOfElement - 1)),
textStyle = textStyle,
modifier = baseLabelModifier
.alpha(
(maxOf(
minimumAlpha,
1 - abs(coercedAnimatedOffset) / halfNumbersColumnHeightPx
))
)
.offset(y = -halfNumbersColumnHeight)
.alpha(maxOf(minimumAlpha, coercedAnimatedOffset / halfNumbersColumnHeightPx))
)
if (indexOfElement < list.count() - 1)
Label(
text = label(list.elementAt(indexOfElement + 1)),
modifier = baseLabelModifier
.offset(y = halfNumbersColumnHeight)
.alpha(maxOf(minimumAlpha, -coercedAnimatedOffset / halfNumbersColumnHeightPx))
Label(
text = label(list.elementAt(indexOfElement)),
textStyle = selectedTextStyle,
modifier = baseLabelModifier
.alpha(
(maxOf(
minimumAlpha,
1 - abs(coercedAnimatedOffset) / halfNumbersColumnHeightPx
))
)
}
)
if (indexOfElement < list.count() - 1)
Label(
text = label(list.elementAt(indexOfElement + 1)),
textStyle = textStyle,
modifier = baseLabelModifier
.offset(y = halfNumbersColumnHeight)
.alpha(maxOf(minimumAlpha, -coercedAnimatedOffset / halfNumbersColumnHeightPx))
)
}
Box(
modifier
Expand Down Expand Up @@ -188,14 +184,19 @@ fun <T> ListItemPicker(
}

@Composable
private fun Label(text: String, modifier: Modifier) {
private fun Label(
text: String,
textStyle: TextStyle,
modifier: Modifier
) {
Text(
modifier = modifier.pointerInput(Unit) {
detectTapGestures(onLongPress = {
// FIXME: Empty to disable text selection
})
},
text = text,
style = textStyle,
textAlign = TextAlign.Center,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fun NumberPicker(
dividersColor: Color = MaterialTheme.colors.primary,
range: Iterable<Int>,
textStyle: TextStyle = LocalTextStyle.current,
selectedTextStyle: TextStyle = LocalTextStyle.current,
) {
ListItemPicker(
modifier = modifier,
Expand All @@ -26,6 +27,7 @@ fun NumberPicker(
onValueChange = onValueChange,
dividersColor = dividersColor,
list = range.toList(),
textStyle = textStyle
textStyle = textStyle,
selectedTextStyle = selectedTextStyle
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package com.chargemap.android.sample
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.chargemap.compose.numberpicker.*
Expand Down Expand Up @@ -57,7 +56,9 @@ private fun NumberPicker() {
range = 0..10,
onValueChange = {
state = it
}
},
textStyle = TextStyle(),
selectedTextStyle = TextStyle(fontWeight = FontWeight.Bold)
)
}

Expand All @@ -80,7 +81,9 @@ private fun HoursNumberPicker1() {
textAlign = TextAlign.Center,
text = ":"
)
}
},
textStyle = TextStyle(),
selectedTextStyle = TextStyle(fontWeight = FontWeight.Bold)
)
}

Expand Down Expand Up @@ -182,7 +185,9 @@ private fun DoublesPicker() {
label = { it.toString() },
value = state,
onValueChange = { state = it },
list = possibleValues
list = possibleValues,
textStyle = TextStyle(),
selectedTextStyle = TextStyle(fontWeight = FontWeight.Bold)
)
}

Expand All @@ -208,4 +213,4 @@ private fun IntRangePicker() {
onValueChange = { value = it },
list = possibleValues
)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand All @@ -23,7 +21,8 @@ private fun NumberPickerPreview() {
onValueChange = {
state = it
},
textStyle = TextStyle(Color.White)
textStyle = TextStyle(Color.White),
selectedTextStyle = TextStyle(Color.White, fontWeight = FontWeight.Bold)
)
}

Expand All @@ -48,7 +47,8 @@ private fun HoursNumberPicker1Preview() {
text = ":"
)
},
textStyle = TextStyle(Color.White)
textStyle = TextStyle(Color.White),
selectedTextStyle = TextStyle(Color.White, fontWeight = FontWeight.Bold)
)
}

Expand Down Expand Up @@ -159,7 +159,8 @@ private fun DoublesPickerPreview() {
value = state,
onValueChange = { state = it },
list = possibleValues,
textStyle = TextStyle(Color.White)
textStyle = TextStyle(Color.White),
selectedTextStyle = TextStyle(Color.White, fontWeight = FontWeight.Bold)
)
}

Expand Down Expand Up @@ -188,4 +189,4 @@ private fun IntRangePickerPreview() {
list = possibleValues,
textStyle = TextStyle(Color.White)
)
}
}