Skip to content

Commit 33a0c77

Browse files
authored
Use material3 for Compose views (readium#460)
1 parent 9696c43 commit 33a0c77

File tree

11 files changed

+112
-59
lines changed

11 files changed

+112
-59
lines changed

gradle/libs.versions.toml

-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ timber = "5.0.1"
7171

7272

7373
[libraries]
74-
accompanist-themeadapter-material = { group = "com.google.accompanist", name = "accompanist-themeadapter-material", version.ref = "accompanist" }
7574

7675
androidx-activity-ktx = { group = "androidx.activity", name = "activity-ktx", version.ref = "androidx-activity" }
7776
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidx-appcompat" }

test-app/build.gradle.kts

-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ dependencies {
8181
// Only required if you want to support PDF files using PDFium.
8282
implementation(project(":readium:adapters:pdfium"))
8383

84-
implementation(libs.accompanist.themeadapter.material)
85-
8684
implementation(libs.androidx.activity.ktx)
8785
implementation(libs.androidx.appcompat)
8886
implementation(libs.androidx.browser)

test-app/src/main/java/org/readium/r2/testapp/reader/preferences/UserPreferences.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
package org.readium.r2.testapp.reader.preferences
1010

1111
import androidx.compose.foundation.layout.*
12-
import androidx.compose.material.*
12+
import androidx.compose.material3.*
1313
import androidx.compose.runtime.Composable
1414
import androidx.compose.runtime.collectAsState
1515
import androidx.compose.runtime.getValue
@@ -63,7 +63,8 @@ private fun <P : Configurable.Preferences<P>, E : PreferencesEditor<P>> UserPref
6363
Text(
6464
text = title,
6565
textAlign = TextAlign.Center,
66-
style = MaterialTheme.typography.h6,
66+
style = MaterialTheme.typography.headlineSmall,
67+
color = MaterialTheme.colorScheme.onSurface,
6768
modifier = Modifier
6869
.fillMaxWidth()
6970
)
@@ -611,15 +612,14 @@ private fun PresetsMenuButton(
611612

612613
for (preset in presets) {
613614
DropdownMenuItem(
615+
text = { Text(preset.title) },
614616
onClick = {
615617
dismiss()
616618
clear()
617619
preset.apply()
618620
commit()
619621
}
620-
) {
621-
Text(preset.title)
622-
}
622+
)
623623
}
624624
}
625625
}

test-app/src/main/java/org/readium/r2/testapp/reader/tts/TtsControls.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import androidx.compose.foundation.layout.Arrangement
1010
import androidx.compose.foundation.layout.Row
1111
import androidx.compose.foundation.layout.Spacer
1212
import androidx.compose.foundation.layout.size
13-
import androidx.compose.material.Card
14-
import androidx.compose.material.Icon
15-
import androidx.compose.material.IconButton
1613
import androidx.compose.material.icons.Icons
1714
import androidx.compose.material.icons.filled.*
15+
import androidx.compose.material3.Card
16+
import androidx.compose.material3.Icon
17+
import androidx.compose.material3.IconButton
1818
import androidx.compose.runtime.Composable
1919
import androidx.compose.runtime.getValue
2020
import androidx.compose.ui.Alignment

test-app/src/main/java/org/readium/r2/testapp/shared/views/Group.kt

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66

77
package org.readium.r2.testapp.shared.views
88

9-
import androidx.compose.material.ContentAlpha
10-
import androidx.compose.material.LocalContentAlpha
119
import androidx.compose.runtime.Composable
12-
import androidx.compose.runtime.CompositionLocalProvider
10+
import org.readium.r2.testapp.utils.compose.Emphasis
11+
import org.readium.r2.testapp.utils.compose.EmphasisProvider
12+
import org.readium.r2.testapp.utils.compose.LocalContentEmphasis
1313

1414
/**
1515
* Sets the emphasis (alpha) of a group of [Composable] views.
1616
*/
1717
@Composable
1818
fun Group(lowEmphasis: Boolean = false, enabled: Boolean = true, content: @Composable () -> Unit) {
19-
val alpha = when {
20-
!enabled -> ContentAlpha.disabled
21-
lowEmphasis -> ContentAlpha.medium
22-
else -> ContentAlpha.high
19+
val emphasis = when {
20+
!enabled -> Emphasis.Disabled
21+
else -> Emphasis.Medium
22+
}
23+
EmphasisProvider(LocalContentEmphasis provides emphasis) {
24+
content()
2325
}
24-
25-
CompositionLocalProvider(LocalContentAlpha provides alpha, content = content)
2626
}

test-app/src/main/java/org/readium/r2/testapp/shared/views/Preferences.kt

+15-20
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import androidx.compose.foundation.layout.Arrangement
1313
import androidx.compose.foundation.layout.Column
1414
import androidx.compose.foundation.layout.Row
1515
import androidx.compose.foundation.layout.widthIn
16-
import androidx.compose.material.*
1716
import androidx.compose.material.icons.Icons
17+
import androidx.compose.material.icons.automirrored.filled.Backspace
1818
import androidx.compose.material.icons.filled.Add
19-
import androidx.compose.material.icons.filled.Backspace
2019
import androidx.compose.material.icons.filled.Palette
2120
import androidx.compose.material.icons.filled.Remove
21+
import androidx.compose.material3.*
2222
import androidx.compose.runtime.*
2323
import androidx.compose.ui.Alignment
2424
import androidx.compose.ui.Modifier
@@ -90,7 +90,7 @@ private fun <T> ButtonGroupItem(
9090
) { option ->
9191
Text(
9292
text = formatValue(option),
93-
style = MaterialTheme.typography.caption
93+
style = MaterialTheme.typography.labelMedium
9494
)
9595
}
9696
}
@@ -139,19 +139,18 @@ private fun <T> MenuItem(
139139
text = {
140140
Text(
141141
text = formatValue(value),
142-
style = MaterialTheme.typography.caption
142+
style = MaterialTheme.typography.labelMedium
143143
)
144144
}
145145
) { dismiss ->
146146
for (aValue in values) {
147147
DropdownMenuItem(
148+
text = { Text(formatValue(aValue)) },
148149
onClick = {
149150
dismiss()
150151
onValueChanged(aValue)
151152
}
152-
) {
153-
Text(formatValue(aValue))
154-
}
153+
)
155154
}
156155
}
157156
}
@@ -308,7 +307,7 @@ private fun ColorItem(
308307

309308
OutlinedButton(
310309
onClick = { isPicking = true },
311-
colors = ButtonDefaults.buttonColors(backgroundColor = color)
310+
colors = ButtonDefaults.buttonColors(containerColor = color)
312311
) {
313312
if (noValueSelected) {
314313
Icon(
@@ -367,7 +366,6 @@ fun LanguageItem(
367366
)
368367
}
369368

370-
@OptIn(ExperimentalMaterialApi::class)
371369
@Composable
372370
private fun Item(
373371
title: String,
@@ -383,19 +381,18 @@ private fun Item(
383381
} else {
384382
Modifier
385383
},
386-
text = {
387-
val alpha = if (isActive) 1.0f else ContentAlpha.disabled
388-
CompositionLocalProvider(LocalContentAlpha provides alpha) {
384+
headlineContent = {
385+
Group(enabled = isActive) {
389386
Text(title)
390387
}
391388
},
392-
trailing = {
389+
trailingContent = {
393390
Row {
394391
content()
395392

396393
IconButton(onClick = onClear ?: {}, enabled = onClear != null) {
397394
Icon(
398-
Icons.Default.Backspace,
395+
Icons.AutoMirrored.Filled.Backspace,
399396
contentDescription = "Clear"
400397
)
401398
}
@@ -427,7 +424,6 @@ fun <T> SelectorListItem(
427424
* A Material [ListItem] displaying a dropdown menu to select a value. The current value is
428425
* displayed on the right.
429426
*/
430-
@OptIn(ExperimentalMaterialApi::class)
431427
@Composable
432428
private fun <T> SelectorListItem(
433429
title: String,
@@ -445,12 +441,12 @@ private fun <T> SelectorListItem(
445441
.clickable(enabled = enabled) {
446442
isExpanded = true
447443
},
448-
text = {
444+
headlineContent = {
449445
Group(enabled = enabled) {
450446
Text(title)
451447
}
452448
},
453-
trailing = {
449+
trailingContent = {
454450
Group(enabled = enabled) {
455451
Text(formatValue(selection))
456452
}
@@ -461,13 +457,12 @@ private fun <T> SelectorListItem(
461457
) {
462458
for (value in values) {
463459
DropdownMenuItem(
460+
text = { Text(formatValue(value)) },
464461
onClick = {
465462
onSelected(value)
466463
dismiss()
467464
}
468-
) {
469-
Text(formatValue(value))
470-
}
465+
)
471466
}
472467
}
473468
}

test-app/src/main/java/org/readium/r2/testapp/utils/compose/AppTheme.kt

+21-4
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,33 @@
66

77
package org.readium.r2.testapp.utils.compose
88

9+
import android.os.Build
10+
import androidx.compose.foundation.isSystemInDarkTheme
11+
import androidx.compose.material3.MaterialTheme
12+
import androidx.compose.material3.darkColorScheme
13+
import androidx.compose.material3.dynamicDarkColorScheme
14+
import androidx.compose.material3.dynamicLightColorScheme
15+
import androidx.compose.material3.lightColorScheme
916
import androidx.compose.runtime.Composable
10-
import com.google.accompanist.themeadapter.material.MdcTheme
17+
import androidx.compose.ui.platform.LocalContext
1118

1219
/**
1320
* Setup the Compose app-wide theme.
1421
*/
1522
@Composable
16-
fun AppTheme(content: @Composable () -> Unit) {
17-
MdcTheme(
18-
setDefaultFontFamily = true,
23+
fun AppTheme(useDarkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit) {
24+
val colors = when {
25+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
26+
when {
27+
useDarkTheme -> dynamicDarkColorScheme(LocalContext.current)
28+
else -> dynamicLightColorScheme(LocalContext.current)
29+
}
30+
}
31+
useDarkTheme -> lightColorScheme()
32+
else -> darkColorScheme()
33+
}
34+
MaterialTheme(
35+
colorScheme = colors,
1936
content = content
2037
)
2138
}

test-app/src/main/java/org/readium/r2/testapp/utils/compose/ComposeBottomSheetDialogFragment.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import android.view.LayoutInflater
1111
import android.view.View
1212
import android.view.ViewGroup
1313
import android.widget.FrameLayout
14+
import androidx.compose.material3.Surface
1415
import androidx.compose.runtime.Composable
1516
import androidx.compose.ui.platform.ComposeView
1617
import androidx.core.widget.NestedScrollView
@@ -32,7 +33,9 @@ abstract class ComposeBottomSheetDialogFragment(
3233
val composeView = ComposeView(context).apply {
3334
setContent {
3435
AppTheme {
35-
this@ComposeBottomSheetDialogFragment.Content()
36+
Surface {
37+
this@ComposeBottomSheetDialogFragment.Content()
38+
}
3639
}
3740
}
3841
}

test-app/src/main/java/org/readium/r2/testapp/utils/compose/DropdownMenuButton.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ package org.readium.r2.testapp.utils.compose
88

99
import androidx.compose.foundation.layout.ColumnScope
1010
import androidx.compose.foundation.layout.RowScope
11-
import androidx.compose.material.DropdownMenu
12-
import androidx.compose.material.OutlinedButton
11+
import androidx.compose.material3.DropdownMenu
12+
import androidx.compose.material3.OutlinedButton
1313
import androidx.compose.runtime.*
1414

1515
@Composable
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.readium.r2.testapp.utils.compose
2+
3+
import androidx.compose.material3.LocalContentColor
4+
import androidx.compose.material3.MaterialTheme
5+
import androidx.compose.material3.ProvideTextStyle
6+
import androidx.compose.runtime.Composable
7+
import androidx.compose.runtime.CompositionLocalProvider
8+
import androidx.compose.runtime.ProvidedValue
9+
import androidx.compose.runtime.compositionLocalOf
10+
import androidx.compose.ui.text.TextStyle
11+
import androidx.compose.ui.text.font.FontWeight
12+
13+
val LocalContentEmphasis = compositionLocalOf { Emphasis.Medium }
14+
15+
enum class Emphasis {
16+
Disabled,
17+
Medium,
18+
High
19+
}
20+
21+
@Composable
22+
fun EmphasisProvider(emphasis: ProvidedValue<Emphasis>, content: @Composable () -> Unit) {
23+
val contentColor = when (emphasis.value) {
24+
Emphasis.Disabled -> MaterialTheme.colorScheme.onSurface.copy(alpha = 0.38f)
25+
Emphasis.Medium -> MaterialTheme.colorScheme.onSurfaceVariant
26+
Emphasis.High -> MaterialTheme.colorScheme.onSurface
27+
}
28+
val fontWeight = when (emphasis.value) {
29+
Emphasis.High -> FontWeight.Bold
30+
else -> FontWeight.Normal
31+
}
32+
33+
CompositionLocalProvider(
34+
LocalContentColor provides contentColor,
35+
content = {
36+
ProvideTextStyle(value = TextStyle(fontWeight = fontWeight)) {
37+
content()
38+
}
39+
}
40+
)
41+
}

test-app/src/main/java/org/readium/r2/testapp/utils/compose/ToggleButton.kt

+12-12
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ package org.readium.r2.testapp.utils.compose
88

99
import androidx.compose.foundation.layout.Row
1010
import androidx.compose.foundation.layout.RowScope
11-
import androidx.compose.material.ButtonDefaults
12-
import androidx.compose.material.MaterialTheme
13-
import androidx.compose.material.OutlinedButton
14-
import androidx.compose.material.Text
11+
import androidx.compose.material3.ButtonDefaults
12+
import androidx.compose.material3.MaterialTheme
13+
import androidx.compose.material3.OutlinedButton
14+
import androidx.compose.material3.Text
1515
import androidx.compose.runtime.Composable
1616
import androidx.compose.ui.graphics.compositeOver
1717
import androidx.compose.ui.tooling.preview.Preview
@@ -55,22 +55,22 @@ fun ToggleButton(
5555
enabled = enabled,
5656
content = content,
5757
colors = ButtonDefaults.outlinedButtonColors(
58-
contentColor = MaterialTheme.colors.onBackground,
59-
backgroundColor = when {
58+
contentColor = MaterialTheme.colorScheme.onBackground,
59+
containerColor = when {
6060
selected ->
61-
MaterialTheme.colors.onBackground
61+
MaterialTheme.colorScheme.onBackground
6262
.copy(alpha = 0.15f)
63-
.compositeOver(MaterialTheme.colors.background)
63+
.compositeOver(MaterialTheme.colorScheme.background)
6464
active ->
65-
MaterialTheme.colors.onBackground
65+
MaterialTheme.colorScheme.onBackground
6666
.copy(alpha = 0.05f)
67-
.compositeOver(MaterialTheme.colors.background)
68-
else -> MaterialTheme.colors.surface
67+
.compositeOver(MaterialTheme.colorScheme.background)
68+
else -> MaterialTheme.colorScheme.surface
6969
}
7070
),
7171
elevation =
7272
if (selected) {
73-
ButtonDefaults.elevation(defaultElevation = 2.dp)
73+
ButtonDefaults.buttonElevation(defaultElevation = 2.dp)
7474
} else {
7575
null
7676
}

0 commit comments

Comments
 (0)