Browse Source

Update widgets gallery example to 0.3.0-build154

pull/398/head
Alexey Tsvetkov 4 years ago committed by Alexey Tsvetkov
parent
commit
1c0fb3a1e5
  1. 7
      examples/widgetsgallery/android/build.gradle.kts
  2. 2
      examples/widgetsgallery/android/src/main/java/org/jetbrains/compose/demo/widgets/MainActivity.kt
  3. 4
      examples/widgetsgallery/build.gradle.kts
  4. 6
      examples/widgetsgallery/common/src/androidMain/kotlin/org/jetbrains/compose/demo/widgets/platform/Resources.kt
  5. 35
      examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/MainView.kt
  6. 29
      examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/WidgetView.kt
  7. 2
      examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/screens/AppBars.kt
  8. 2
      examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/screens/Chips.kt
  9. 2
      examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/screens/Toggles.kt
  10. 3
      examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/screens/UICards.kt
  11. 13
      examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/utils/ResizablePanel.kt
  12. 18
      examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/utils/VerticalSplittable.kt
  13. 6
      examples/widgetsgallery/common/src/desktopMain/kotlin/org/jetbrains/compose/demo/widgets/platform/Mouse.kt
  14. 2
      examples/widgetsgallery/desktop/src/jvmMain/kotlin/org/jetbrains/compose/demo/widgets/main.kt

7
examples/widgetsgallery/android/build.gradle.kts

@ -22,4 +22,11 @@ android {
dependencies { dependencies {
implementation(project(":common")) implementation(project(":common"))
implementation("androidx.activity:activity-compose:1.3.0-alpha02") {
exclude(group = "androidx.compose.animation")
exclude(group = "androidx.compose.foundation")
exclude(group = "androidx.compose.material")
exclude(group = "androidx.compose.runtime")
exclude(group = "androidx.compose.ui")
}
} }

2
examples/widgetsgallery/android/src/main/java/org/jetbrains/compose/demo/widgets/MainActivity.kt

@ -1,8 +1,8 @@
package org.jetbrains.compose.demo.widgets package org.jetbrains.compose.demo.widgets
import android.os.Bundle import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.compose.ui.platform.setContent
import org.jetbrains.compose.demo.widgets.ui.MainView import org.jetbrains.compose.demo.widgets.ui.MainView
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {

4
examples/widgetsgallery/build.gradle.kts

@ -9,10 +9,10 @@ buildscript {
dependencies { dependencies {
// __LATEST_COMPOSE_RELEASE_VERSION__ // __LATEST_COMPOSE_RELEASE_VERSION__
classpath("org.jetbrains.compose:compose-gradle-plugin:0.3.0-build146") classpath("org.jetbrains.compose:compose-gradle-plugin:0.3.0-build154")
classpath("com.android.tools.build:gradle:4.0.1") classpath("com.android.tools.build:gradle:4.0.1")
// __KOTLIN_COMPOSE_VERSION__ // __KOTLIN_COMPOSE_VERSION__
classpath(kotlin("gradle-plugin", version = "1.4.21-2")) classpath(kotlin("gradle-plugin", version = "1.4.30"))
} }
} }

6
examples/widgetsgallery/common/src/androidMain/kotlin/org/jetbrains/compose/demo/widgets/platform/Resources.kt

@ -3,18 +3,20 @@ package org.jetbrains.compose.demo.widgets.platform
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.res.vectorResource
import org.jetbrains.compose.demo.widgets.R import org.jetbrains.compose.demo.widgets.R
@Composable @Composable
actual fun imageResource(res: String): ImageBitmap { actual fun imageResource(res: String): ImageBitmap {
val id = drawableId(res) val id = drawableId(res)
return androidx.compose.ui.res.imageResource(id) return ImageBitmap.imageResource(id)
} }
@Composable @Composable
actual fun vectorResource(res: String): ImageVector { actual fun vectorResource(res: String): ImageVector {
val id = drawableId(res) val id = drawableId(res)
return androidx.compose.ui.res.vectorResource(id) return ImageVector.vectorResource(id)
} }
// TODO: improve resource loading // TODO: improve resource loading

35
examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/MainView.kt

@ -1,21 +1,22 @@
package org.jetbrains.compose.demo.widgets.ui package org.jetbrains.compose.demo.widgets.ui
import androidx.compose.animation.animate
import androidx.compose.animation.core.Spring import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.SpringSpec import androidx.compose.animation.core.SpringSpec
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumnFor import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.AmbientContentColor import androidx.compose.foundation.text.selection.DisableSelection
import androidx.compose.material.LocalContentColor
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clipToBounds import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.platform.AmbientDensity import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.selection.DisableSelection
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit import androidx.compose.ui.unit.TextUnit
@ -46,10 +47,10 @@ fun WidgetsPanel() {
val animatedSize = if (panelState.splitter.isResizing) { val animatedSize = if (panelState.splitter.isResizing) {
if (panelState.isExpanded) panelState.expandedSize else panelState.collapsedSize if (panelState.isExpanded) panelState.expandedSize else panelState.collapsedSize
} else { } else {
animate( animateDpAsState(
if (panelState.isExpanded) panelState.expandedSize else panelState.collapsedSize, if (panelState.isExpanded) panelState.expandedSize else panelState.collapsedSize,
SpringSpec(stiffness = Spring.StiffnessLow) SpringSpec(stiffness = Spring.StiffnessLow)
) ).value
} }
VerticalSplittable( VerticalSplittable(
@ -82,24 +83,26 @@ fun WidgetsPanel() {
@Composable @Composable
private fun WidgetsListView(widgetsTypeState: MutableState<WidgetsType>) { private fun WidgetsListView(widgetsTypeState: MutableState<WidgetsType>) {
Box { Box {
with(AmbientDensity.current) { with(LocalDensity.current) {
val scrollState = rememberLazyListState() val scrollState = rememberLazyListState()
val fontSize = 14.sp val fontSize = 14.sp
val lineHeight = fontSize.toDp() * 1.5f val lineHeight = fontSize.toDp() * 1.5f
val items = WidgetsType.sortedValues val sortedItems = WidgetsType.sortedValues
LazyColumnFor( LazyColumn(
items,
modifier = Modifier.fillMaxSize().withoutWidthConstraints(), modifier = Modifier.fillMaxSize().withoutWidthConstraints(),
state = scrollState, state = scrollState
itemContent = { WidgetsListItemViewImpl(it, widgetsTypeState, fontSize, lineHeight) } ) {
) items(sortedItems) {
WidgetsListItemViewImpl(it, widgetsTypeState, fontSize, lineHeight)
}
}
VerticalScrollbar( VerticalScrollbar(
Modifier.align(Alignment.CenterEnd), Modifier.align(Alignment.CenterEnd),
scrollState, scrollState,
items.size, sortedItems.size,
lineHeight lineHeight
) )
} }
@ -124,7 +127,7 @@ private fun WidgetsListItemViewImpl(
.padding(start = 16.dp) .padding(start = 16.dp)
) { ) {
var inFocus by remember { mutableStateOf(false) } var inFocus by remember { mutableStateOf(false) }
val textColor = AmbientContentColor.current.let { val textColor = LocalContentColor.current.let {
when { when {
isCurrent -> it isCurrent -> it
inFocus -> it.copy(alpha = 0.6f) inFocus -> it.copy(alpha = 0.6f)

29
examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/WidgetView.kt

@ -1,7 +1,8 @@
package org.jetbrains.compose.demo.widgets.ui package org.jetbrains.compose.demo.widgets.ui
import androidx.compose.foundation.ScrollableColumn
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState import androidx.compose.runtime.MutableState
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
@ -12,20 +13,18 @@ fun WidgetsView(
widgetsTypeState: MutableState<WidgetsType>, widgetsTypeState: MutableState<WidgetsType>,
modifier: Modifier modifier: Modifier
) { ) {
ScrollableColumn(modifier = modifier) { Column(modifier = modifier.verticalScroll(state = rememberScrollState())) {
Column { @Suppress("UNUSED_VARIABLE")
@Suppress("UNUSED_VARIABLE") val exhaustive = when (widgetsTypeState.value) {
val exhaustive = when (widgetsTypeState.value) { WidgetsType.APP_BARS -> AppBars()
WidgetsType.APP_BARS -> AppBars() WidgetsType.BUTTONS -> Buttons()
WidgetsType.BUTTONS -> Buttons() WidgetsType.CHIPS -> Chips()
WidgetsType.CHIPS -> Chips() WidgetsType.LOADERS -> Loaders()
WidgetsType.LOADERS -> Loaders() WidgetsType.SNACK_BARS -> SnackBars()
WidgetsType.SNACK_BARS -> SnackBars() WidgetsType.TEXT_VIEWS -> TextViews()
WidgetsType.TEXT_VIEWS -> TextViews() WidgetsType.TEXT_INPUTS -> TextInputs()
WidgetsType.TEXT_INPUTS -> TextInputs() WidgetsType.TOGGLES -> Toggles()
WidgetsType.TOGGLES -> Toggles() WidgetsType.UI_CARDS -> UICards()
WidgetsType.UI_CARDS -> UICards()
}
} }
} }
} }

2
examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/screens/AppBars.kt

@ -81,7 +81,7 @@ private fun TopAppBarsDemo() {
imageResource(Res.drawable.p6), imageResource(Res.drawable.p6),
contentDescription = "", contentDescription = "",
modifier = Modifier.padding(vertical = 4.dp, horizontal = 8.dp) modifier = Modifier.padding(vertical = 4.dp, horizontal = 8.dp)
.preferredSize(32.dp).clip(CircleShape) .requiredSize(32.dp).clip(CircleShape)
) )
}, },
actions = { actions = {

2
examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/screens/Chips.kt

@ -87,7 +87,7 @@ private fun CustomImageChip(
Image( Image(
imageResource(imageId), imageResource(imageId),
contentDescription = null, contentDescription = null,
modifier = Modifier.padding(8.dp).preferredSize(20.dp).clip(CircleShape) modifier = Modifier.padding(8.dp).requiredSize(20.dp).clip(CircleShape)
) )
Text( Text(
text = text, text = text,

2
examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/screens/Toggles.kt

@ -67,7 +67,7 @@ fun Toggles() {
Slider(value = sliderState2, modifier = Modifier.fillMaxWidth().padding(8.dp), Slider(value = sliderState2, modifier = Modifier.fillMaxWidth().padding(8.dp),
valueRange = 0f..100f, valueRange = 0f..100f,
steps = 5, steps = 5,
thumbColor = MaterialTheme.colors.secondary, colors = SliderDefaults.colors(thumbColor = MaterialTheme.colors.secondary),
onValueChange = { newValue -> onValueChange = { newValue ->
sliderState2 = newValue sliderState2 = newValue
} }

3
examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/screens/UICards.kt

@ -15,6 +15,7 @@ import org.jetbrains.compose.demo.widgets.platform.Res
import org.jetbrains.compose.demo.widgets.platform.imageResource import org.jetbrains.compose.demo.widgets.platform.imageResource
import org.jetbrains.compose.demo.widgets.theme.typography import org.jetbrains.compose.demo.widgets.theme.typography
@OptIn(ExperimentalMaterialApi::class)
@Composable @Composable
fun UICards() { fun UICards() {
val item = remember { DemoDataProvider.item } val item = remember { DemoDataProvider.item }
@ -54,7 +55,7 @@ fun UICards() {
Image( Image(
imageResource(Res.drawable.p3), imageResource(Res.drawable.p3),
contentDescription = null, contentDescription = null,
modifier = Modifier.preferredSize(60.dp) modifier = Modifier.requiredSize(60.dp)
) )
Text(text = item.title, modifier = Modifier.padding(16.dp)) Text(text = item.title, modifier = Modifier.padding(16.dp))
} }

13
examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/utils/ResizablePanel.kt

@ -1,13 +1,11 @@
package org.jetbrains.compose.demo.widgets.ui.utils package org.jetbrains.compose.demo.widgets.ui.utils
import androidx.compose.animation.animate import androidx.compose.animation.core.*
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.SpringSpec
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.material.AmbientContentColor
import androidx.compose.material.Icon import androidx.compose.material.Icon
import androidx.compose.material.LocalContentColor
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack import androidx.compose.material.icons.filled.ArrowBack
@ -38,7 +36,10 @@ fun ResizablePanel(
title: String, title: String,
content: @Composable () -> Unit, content: @Composable () -> Unit,
) { ) {
val alpha = animate(if (state.isExpanded) 1f else 0f, SpringSpec(stiffness = Spring.StiffnessLow),) val alpha = animateFloatAsState(
if (state.isExpanded) 1f else 0f,
SpringSpec(stiffness = Spring.StiffnessLow),
).value
Box(modifier) { Box(modifier) {
Column { Column {
@ -56,7 +57,7 @@ fun ResizablePanel(
Icon( Icon(
if (state.isExpanded) Icons.Default.ArrowBack else Icons.Default.ArrowForward, if (state.isExpanded) Icons.Default.ArrowBack else Icons.Default.ArrowForward,
contentDescription = if (state.isExpanded) "Collapse" else "Expand", contentDescription = if (state.isExpanded) "Collapse" else "Expand",
tint = AmbientContentColor.current, tint = LocalContentColor.current,
modifier = Modifier modifier = Modifier
.size(24.dp) .size(24.dp)
.padding(end = 8.dp) .padding(end = 8.dp)

18
examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/utils/VerticalSplittable.kt

@ -2,6 +2,8 @@ package org.jetbrains.compose.demo.widgets.ui.utils
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.draggable import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.width import androidx.compose.foundation.layout.width
@ -10,9 +12,9 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.gesture.scrollorientationlocking.Orientation
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.Layout import androidx.compose.ui.layout.Layout
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Constraints import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@ -32,7 +34,7 @@ fun VerticalSplittable(
) = Layout({ ) = Layout({
children() children()
VerticalSplitter(splitterState, onResize) VerticalSplitter(splitterState, onResize)
}, modifier, measureBlock = { measurables, constraints -> }, modifier, measurePolicy = { measurables, constraints ->
require(measurables.size == 3) require(measurables.size == 3)
val firstPlaceable = measurables[0].measure(constraints.copy(minWidth = 0)) val firstPlaceable = measurables[0].measure(constraints.copy(minWidth = 0))
@ -61,6 +63,7 @@ fun VerticalSplitter(
onResize: (delta: Dp) -> Unit, onResize: (delta: Dp) -> Unit,
color: Color = Color.DarkGray color: Color = Color.DarkGray
) = Box { ) = Box {
val density = LocalDensity.current
Box( Box(
Modifier Modifier
.width(8.dp) .width(8.dp)
@ -68,13 +71,16 @@ fun VerticalSplitter(
.run { .run {
if (splitterState.isResizeEnabled) { if (splitterState.isResizeEnabled) {
this.draggable( this.draggable(
Orientation.Horizontal, state = rememberDraggableState {
with(density) {
onResize(it.toDp())
}
},
orientation = Orientation.Horizontal,
startDragImmediately = true, startDragImmediately = true,
onDragStarted = { splitterState.isResizing = true }, onDragStarted = { splitterState.isResizing = true },
onDragStopped = { splitterState.isResizing = false } onDragStopped = { splitterState.isResizing = false }
) { ).cursorForHorizontalResize()
onResize(it.toDp())
}.cursorForHorizontalResize()
} else { } else {
this this
} }

6
examples/widgetsgallery/common/src/desktopMain/kotlin/org/jetbrains/compose/demo/widgets/platform/Mouse.kt

@ -1,6 +1,6 @@
package org.jetbrains.compose.demo.widgets.platform package org.jetbrains.compose.demo.widgets.platform
import androidx.compose.desktop.AppWindowAmbient import androidx.compose.desktop.LocalAppWindow
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
@ -21,9 +21,9 @@ actual fun Modifier.cursorForHorizontalResize(): Modifier = composed {
var isHover by remember { mutableStateOf(false) } var isHover by remember { mutableStateOf(false) }
if (isHover) { if (isHover) {
AppWindowAmbient.current!!.window.cursor = Cursor(Cursor.E_RESIZE_CURSOR) LocalAppWindow.current.window.cursor = Cursor(Cursor.E_RESIZE_CURSOR)
} else { } else {
AppWindowAmbient.current!!.window.cursor = Cursor.getDefaultCursor() LocalAppWindow.current.window.cursor = Cursor.getDefaultCursor()
} }
pointerMoveFilter( pointerMoveFilter(

2
examples/widgetsgallery/desktop/src/jvmMain/kotlin/org/jetbrains/compose/demo/widgets/main.kt

@ -1,14 +1,12 @@
package org.jetbrains.compose.demo.widgets package org.jetbrains.compose.demo.widgets
import androidx.compose.desktop.Window import androidx.compose.desktop.Window
import androidx.compose.foundation.layout.ExperimentalLayout
import androidx.compose.ui.unit.IntSize import androidx.compose.ui.unit.IntSize
import org.jetbrains.compose.demo.widgets.ui.MainView import org.jetbrains.compose.demo.widgets.ui.MainView
import java.awt.Dimension import java.awt.Dimension
import java.awt.Toolkit import java.awt.Toolkit
import javax.swing.SwingUtilities.invokeLater import javax.swing.SwingUtilities.invokeLater
@OptIn(ExperimentalLayout::class)
fun main() { fun main() {
invokeLater { invokeLater {
Window( Window(

Loading…
Cancel
Save